 <?php $this->load->view("partial/header"); ?>
 
 <?php if($this->session->userdata['reprint_perission']=="sales_person_user"){


 	///////////////////////////////
 	$has_permission=array();

 	$has_permission['BNK']="BNK";
 	$has_permission['SDB']="SDB";
 	$has_permission['ADB']="ADB";
 	$has_permission['KLB']="KLB";
 	$has_permission['TAL']="TAL";
 	$has_permission['UTA']="UTA";
 	$has_permission['MYN']="MYN";
 	$has_permission['SYL']="SYL";
 	$has_permission['RAJ']="RAJ";
 	$has_permission['RAN']="RAN";
 	$has_permission['CHK']="CHK";
 	$has_permission['DNJ']="DNJ";

 	$limit_time=REPRINT_TIME_LIMIT;///////////////////2 Mins all invoice


 	if(trim($has_permission[LOCATION_CODE])){
 		$query = $this->db->query("SELECT sale_id,sale_time,receipt_no,payment_type
			        FROM  ospos_sales_reprint
				    WHERE receipt_no='".mysql_real_escape_string($receipt_no)."'
				    ORDER BY sale_time DESC");
 	}else{
 		$query = $this->db->query("SELECT sale_id,sale_time,receipt_no,payment_type
			        FROM  ospos_sales
				    WHERE receipt_no='".mysql_real_escape_string($receipt_no)."'
				    ORDER BY sale_time DESC");
 	}

 	$last_sale_id= $query->row()->sale_id;
 	$last_sale_time= $query->row()->sale_time;
 	$last_receipt_no= $query->row()->receipt_no;

 	$reprint_password_session_end=date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s"))-REPRINT_TIME_LIMIT);

 	$error_found=0;
 	if(intval($last_sale_id)){
 		if($last_sale_time<$reprint_password_session_end){
 			$error_found=1;
 		}
 		if(date("Y-m-d",strtotime($last_sale_time))!=date("Y-m-d")){
 			$error_found=1;
 		}
 	}else{
 		$error_found=1;
 	}
 }
 if($error_found==0){
 	$current_directory=getcwd();
 	$file_name_with_date="reprint_success_activity.txt";
 	$filename = $current_directory.'\pdf_report\report_file_runtime\\'.$file_name_with_date;
 	$text=$receipt_no." # ".date("Y-m-d H:i:s")." # ".$this->session->userdata['reprint_perission']."\n";

 	if (!file_exists($filename)) {
 		$fhandle = fopen($filename,"w");
 		fwrite($fhandle,$text);
 		fclose($fhandle);
 	}else{
 		if ($fd = fopen($filename, 'rb')) {
 			$data = fread($fd, filesize($filename));
 			fclose($fd);
 			$text_final=$data.$text;
 			$fhandle = fopen($filename,"w");
 			fwrite($fhandle,$text_final);
 			fclose($fhandle);
 		}
 	}

 	$linecount = 0;
 	$handleFile = fopen($filename, "r");

 	$lines_details=array();
 	while(!feof($handleFile)){
 		$line = fgets($handleFile);
 		if(trim($line)){
 			$linecount++;
 			$lines_details[]=$line;
 		}
 	}
 	fclose($handleFile);
 	$last10_attemp = array_slice($lines_details, -10, 10, true);

 ?>
 <div id="page_title" style="margin-bottom:8px;">Reprint Invoice # &nbsp;<?php echo $receipt_no; ?></div>
     <table style='border-collapse: collapse; border: 2px solid #318ee1;width: 97%;margin: 12px;box-shadow: 0px 8px 8px #000;'>    
                <tr>
                    <td  style='text-align:center;font-weight:bold;padding-top:78px;padding-bottom:10px; font-size:20px;'>Invoice # &nbsp;<font style="color:#FF0000;"><?php echo $receipt_no; ?> </font>has been re-printed successfully.</td>
                </tr>
                <tr>
                    <td  style='text-align:center;font-weight:bold;padding-top:10px;padding-bottom:10px; font-size:20px;'><?php echo anchor('reports/invoices', "Back to the list", null); ?></td>
                </tr>
                <tr>
                    <td  style='text-align:center;font-weight:bold;padding-top:10px;padding-bottom:100px; font-size:20px;'><?php echo anchor('sales', "Back to the sale", null); ?></td>
                </tr>
       </table>         
       <table style='border-collapse: collapse; border: 2px solid #318ee1;width: 97%;margin: 12px;box-shadow: 0px 8px 8px #000;'>    
                <tr>
                    <td  style='text-align:center;font-weight:bold;padding-top:10px;padding-bottom:10px; font-size:30px;'>
                    <font style="color:#FF0000;">LAST 10 REPRINT HISTORY</font>
                    </td>
                </tr>
                
                <?php 
                krsort($last10_attemp);
                foreach ($last10_attemp as $key => $value) {
                	$temp=explode("#",$value);
                	$text_show="Invoice No:".trim($temp[0])." AT ".date("d/m/Y h:i:s A",strtotime(trim($temp[1])));

                	?>
                	
                <tr>
                    <td  style='text-align:center;font-weight:bold;padding-top:5px;padding-bottom:5px; font-size:12px;'>
                    <font style="color:#000000;"><?php echo $text_show;?></font>
                    </td>
                </tr>
                <?php }?>
                <tr>
                    <td  style='text-align:center;font-weight:bold;padding-top:5px;padding-bottom:5px; font-size:12px;'>
                    &nbsp;
                    </td>
                </tr>
       </table>  
       
       <div style="height:10px;">&nbsp;</div>
       
       
<?php }else{


	$current_directory=getcwd();
	$file_name_with_date="reprint_illegal_activity.txt";
	$filename = $current_directory.'\pdf_report\report_file_runtime\\'.$file_name_with_date;
	$text=$receipt_no." # ".date("Y-m-d H:i:s")." # ".$this->session->userdata['reprint_perission']."\n";

	if (!file_exists($filename)) {
		$fhandle = fopen($filename,"w");
		fwrite($fhandle,$text);
		fclose($fhandle);
	}else{
		if ($fd = fopen($filename, 'rb')) {
			$data = fread($fd, filesize($filename));
			fclose($fd);
			$text_final=$data.$text;
			$fhandle = fopen($filename,"w");
			fwrite($fhandle,$text_final);
			fclose($fhandle);
		}
	}

	$linecount = 0;
	$handleFile = fopen($filename, "r");

	$lines_details=array();
	while(!feof($handleFile)){
		$line = fgets($handleFile);
		if(trim($line)){
			$linecount++;
			$lines_details[]=$line;
		}
	}
	fclose($handleFile);
	$last10_attemp = array_slice($lines_details, -10, 10, true);
	?>

	<div id="page_title" style="margin-bottom:8px;">Reprint Invoice # &nbsp;<?php echo $receipt_no; ?></div>
     <table style='border-collapse: collapse; border: 2px solid #318ee1;width: 97%;margin: 12px;box-shadow: 0px 8px 8px #000;'>    
                <tr>
                    <td  style='text-align:center;font-weight:bold;padding-top:78px;padding-bottom:10px; font-size:30px;'>
                    <font style="color:#FF0000;">Direct Access Denied.</font>
                    </td>
                </tr>
                <tr>
                    <td  style='text-align:center;font-weight:bold;padding-bottom:10px; font-size:20px;'>
                    <font style="color:#FF0000;">It's a illegal activity. Your activity has been recorded. Management can take action, so be careful in future.</font>
                    </td>
                </tr>
                <tr>
                    <td  style='text-align:center;font-weight:bold;padding-bottom:10px; font-size:20px;'>
                    <font style="color:#FF0000;">Your <?php echo $linecount;?> illegal activity has been recorded.</font>
                    </td>
                </tr>
                
                <tr>
                    <td  style='text-align:center;font-weight:bold;padding-top:10px;padding-bottom:10px; font-size:20px;'><?php echo anchor('reports/invoices', "Back to the list", null); ?></td>
                </tr>
                <tr>
                    <td  style='text-align:center;font-weight:bold;padding-top:10px;padding-bottom:100px; font-size:20px;'><?php echo anchor('sales', "Back to the sale", null); ?></td>
                </tr>
       </table>
       <table style='border-collapse: collapse; border: 2px solid #318ee1;width: 97%;margin: 12px;box-shadow: 0px 8px 8px #000;'>    
                <tr>
                    <td  style='text-align:center;font-weight:bold;padding-top:10px;padding-bottom:10px; font-size:30px;'>
                    <font style="color:#FF0000;">LAST 10 ILLEGAL ATTEMPTS HISTORY</font>
                    </td>
                </tr>
                
                <?php 
                krsort($last10_attemp);
                foreach ($last10_attemp as $key => $value) {
                	$temp=explode("#",$value);
                	$text_show="Invoice No:".trim($temp[0])." AT ".date("d/m/Y h:i:s A",strtotime(trim($temp[1])));

                	?>
                	
                <tr>
                    <td  style='text-align:center;font-weight:bold;padding-top:5px;padding-bottom:5px; font-size:12px;'>
                    <font style="color:#FF0000;"><?php echo $text_show;?></font>
                    </td>
                </tr>
                <?php }?>
                <tr>
                    <td  style='text-align:center;font-weight:bold;padding-top:5px;padding-bottom:5px; font-size:12px;'>
                    &nbsp;
                    </td>
                </tr>
       </table>  
       <div style="height:10px;">&nbsp;</div>  
       <?php $this->load->view("partial/footer"); ?>     
<?php exit();}?>
<div style="height:10px;">&nbsp;</div>
<?php $this->load->view("partial/footer"); ?>