<?php $this->load->view("partial/header"); ?>
<script type="text/javascript">
$(document).ready(function()
{
	init_table_sorting();
	enable_select_all();
	enable_row_selection();
	enable_search('<?php echo site_url("$controller_name/suggest")?>','<?php echo $this->lang->line("common_confirm_search")?>');
	enable_email('<?php echo site_url("$controller_name/mailto")?>');
	enable_delete('<?php echo $this->lang->line($controller_name."_confirm_delete")?>','<?php echo $this->lang->line($controller_name."_none_selected")?>');
});

function init_table_sorting()
{
	//Only init if there is more than one row
	if($('.tablesorter tbody tr').length >1)
	{
		$("#sortable_table").tablesorter(
		{
			sortList: [[1,0]],
			headers:
			{
				0: { sorter: false},
				6: { sorter: false}
			}

		});
	}
}

function post_person_form_submit(response)
{
	if(!response.success)
	{
		set_feedback(response.message,'error_message',true);
	}
	else
	{
		//This is an update, just update one row
		if(jQuery.inArray(response.person_id,get_visible_checkbox_ids()) != -1)
		{
			update_row(response.person_id,'<?php echo site_url("$controller_name/get_row")?>');
			set_feedback(response.message,'success_message',false);

		}
		else //refresh entire table
		{
			do_search(true,function()
			{
				//highlight new row
				hightlight_row(response.person_id);
				set_feedback(response.message,'success_message',false);
			});
		}
	}
}
</script>
<div id="page_title" style="margin-bottom:8px;"><?php echo $this->lang->line('common_list_of').' Invoices ( Date : '.date("d/m/Y").")"; ?></div>
<?php echo $this->pagination->create_links();?>
<?php

$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";


$show_permission_reprint=0;
$error_message_reprint="Your Session has been expaired OR Access denied.";
if($this->session->userdata['reprint_perission']=="reprint_password_ok"){
	$reprint_password_session_start=$this->session->userdata['reprint_password_session_start'];
	$reprint_password_session_end=date("Y-m-d H:i:s",strtotime($reprint_password_session_start)+(60*3));

	if($reprint_password_session_end<date("Y-m-d H:i:s")){
		$this->session->sess_destroy();
		redirect('login');
	}else{
		$show_permission_reprint=1;
		$error_message_reprint="";
		/////////////////////////LAST INVOICE TIME///////////////////////////////////////
		if(trim($has_permission[LOCATION_CODE])){
			$query = $this->db->query("SELECT sale_id,sale_time,receipt_no,payment_type
			        FROM  ospos_sales_reprint
				    WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".mysql_real_escape_string(date("Y-m-d"))."'
				    ORDER BY sale_time DESC");
		}else{
			$query = $this->db->query("SELECT sale_id,sale_time,receipt_no,payment_type
			        FROM  ospos_sales
				    WHERE DATE_FORMAT(sale_time,'%Y-%m-%d')='".mysql_real_escape_string(date("Y-m-d"))."'
				    ORDER BY sale_time DESC");
		}

		$data_reprint_list=array();
		$data_count=0;
		foreach ($query->result() as $row) {
			$data_reprint_list[]=$row;
			$data_count++;
		}
	}
}elseif($this->session->userdata['reprint_perission']=="sales_person_user"){
	$url1=$_SERVER['REQUEST_URI'];

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

	/////////////////////////LAST INVOICE TIME///////////////////////////////////////

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

	$data_reprint_list=array();
	$data_count=0;
	foreach ($query->result() as $row) {
		$reprint_password_session_end=date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s"))-$limit_time);
		if($row->sale_time>=$reprint_password_session_end){
			$data_reprint_list[]=$row;
		}
		$data_count++;
	}
	$show_permission_reprint=1;
	$error_message_reprint="";
	$refresh_time=10;
	
	?>
	<script>

	function refreshpage(interval, countdownel, totalel){
		var countdownel = document.getElementById(countdownel)
		var totalel = document.getElementById(totalel)
		var timeleft = interval+1
		var countdowntimer

		function countdown(){
			timeleft--
			countdownel.innerHTML = timeleft
			if (timeleft == 0){
				clearTimeout(countdowntimer)
				//window.location.reload()
				window.location.href="<?php echo $url1;?>";
			}
			countdowntimer = setTimeout(function(){
				countdown()
			}, 1000)
		}

		countdown()
	}
	window.onload = function(){
		refreshpage(<?php echo $refresh_time;?>, "countdown") // refreshpage(duration_in_seconds, id_of_element_to_show_result)
	}

</script>
<div  style="font-size:20px; text-align:right;text-shadow: 2px 2px 4px #FF182D;height: 20px;padding-top:10px;text-align:center;">
 List of invoices will be refresh with in <b style="color:#FF0000;font-size:18px;font-weight:bold;" id="countdown"></b> seconds
</div>	
<?php	
}
?>

<?php if($show_permission_reprint==1){?>
<table class="tablesorter">
<thead>
	<tr>
	<th>Serial</th>
	<th>Invoice Time</th>
	<th>Receipt No</th>
	<th>Payment Types</th>
	<th>Action</th>
	</tr>
</thead>
<tbody>

<?php
if(count($data_count)){
	$serial=1;
	if(count($data_reprint_list)){
		foreach ($data_reprint_list as $key => $value) {?>
			<tr>
			<td width="5%"><?php echo $serial;?></td>
			<td width="20%"><?php echo date("d/m/Y h:i:s A",strtotime($value->sale_time));?></td>
			<td width="20%"><?php echo $value->receipt_no;?></td>
			<td  width="42%"><?php echo $value->payment_type;?></td>
			<td width="13%"><?php echo anchor('sales/receipt_reprint/'.$value->receipt_no, "Re-print", null);?></tr>
		<?php 
		$serial++;
		}
	}else{
		$error_message_reprint="You will have maximum ".($limit_time/60)." minutes for reprint after last invoice.";
		$table_data_rows="<tr><td colspan='5' style='text-align:center;font-size:15px;color:#FF0000;font-weight:bold;'><div class='warning_message' style='padding:7px;'>".$error_message_reprint."</div></tr></tr>";
		echo $table_data_rows;
	}
}else{
	$table_data_rows="<tr><td colspan='5'><div class='warning_message' style='padding:7px;'>There are no invoice to display</div></tr></tr>";
	echo $table_data_rows;
} ?>

</tbody>
</table>
</br>
</br>

<?php }else{?>
</br>
<div id="table_holder" style="text-align:center;font-size:15px;color:#FF0000;font-weight:bold;">
<?php echo $error_message_reprint; ?>
</div>
<div id="feedback_bar"></div>
<?php }?>
<?php $this->load->view("partial/footer"); ?>