<?PHP
//header('Refresh: 5'); 
 $session_data = $this->session->userdata('logged_in');

   
 	$data['id'] = $session_data['id'];
        
		 $userid = $data['id'];

 $data['companyid'] = $session_data['companyid'];
	 $companyid = $data['companyid'];
		 $data['usertype'] = $session_data['usertype'];
	 $usertype = $data['usertype'];	
	 $data['branchid'] = $session_data['branchid'];
	 $branchid = $data['branchid'];		
													  $logoutaction=$this->usermodel->populatuserpermissionsdataarray('sales','Logout',$userid);
 $smsconfig = $this->usermodel->populateallarray('smsconfig','id',$companyid);
		 	 	
   $receipt=$smsconfig[0]['receipt'];	
 
  
  
  
  
  
								?>	
					  		<input type="hidden" name="printing" id="printing"  value="<?php echo set_value('printing',$logoutaction); ?>" class="form-control"  placeholder="Enter printing">



	<script>
	var util = { };

document.addEventListener('keydown', function(e){

    var key = util.key[e.which];
    if( key ){
        e.preventDefault();
    }

   if( key === 'F5' ){        
  return  printDiv('printableArea');
    }
  if( key === 'F6' ){        
  window.location.href = "../user/viewsales";
    }

	 if( key === 'enter' ){        
return true;
    }
	
   
	
	
})


  
util.key = { 
  9: "tab",
  //13: "enter",
  16: "shift",
  12: "alt",
  27: "esc",
  33: "rePag",
  34: "avPag",
  35: "end",
  36: "home",
  37: "left",
  38: "up",
  39: "right",
  40: "down",
  112: "F1",
  113: "F2",
  114: "F3",
  115: "F4",
  116: "F5",
  117: "F6",
  112: "F7",
  119: "F8",
  120: "F9",
  121: "F10",
  122: "F11",
  123: "F12"
}
 

	</script>
<script>
	function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;
  var printing = document.getElementById('printing');

     document.body.innerHTML = printContents.fontsize(16);
     window.print();

	 if( window.print()) {
		 if(printing=true){
			 
			  window.location.href = "../user/logout";

		 }else{
			         window.location.href = "../user/viewsales";
 		alert(" printing...........");

		 }
    }else {

	
		      return      window.location.href = "../user/viewsales";

		    // document.body.innerHTML = originalContents;

	}
}
	</script>
  
	
  <style type="text/css">
  


    #printable { display: none; }
#print{  	


    background-repeat: no-repeat;
	font-size;20px;

    width:100%; height:50px;



}
.doted{
    border: dotted 3px #000000;
}
table { border-collapse: collapse; }
td { padding: 5px; }
tr { border-bottom:1px dashed #494949; }


table{
    border-collapse:collapse;/*This property collapses borders together*/
}

#sale{  	
background-image:url(images/add.png) ;

    background-repeat: no-repeat;
  
    width:100px; height:50px;


}
#alltitle{ font-size: 1em; }
	#mytitle { font-size: 1.5em; }
#extratitle { font-size: 2em; }
    @media print
    {
		
    	#non-printable { display: none; }
    	#printableArea { display: block; font-size:18; width:100%; }
		  margin:0; 
		#receipt{
	font-style: normal;font-size: 2em;
	
	padding:0px;
	
}
    }
    </style>
<?PHP

 echo $this->session->flashdata('imagemsg1'); 
		 
		    
$session_data = $this->session->userdata('logged_in');


$data['surname'] = $session_data['surname'];
$data['id'] = $session_data['id'];

$surname=$data['surname'];
   $userid=$data['id'];

   $myuser=$this->usermodel->geteuser($userid);
   $firstname=$myuser[0]['firstname'];
    $postedtime = date('Y-m-d');
		$currenttime = date('H:i:sa');
 		$dateposted =  ($postedtime)." ".$currenttime ;	

function formatMoney($number, $fractional=false) {
						if ($fractional) {
							$number = sprintf('%.2f', $number);
						}
						while (true) {
							$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
							if ($replaced != $number) {
								$number = $replaced;
							} else {
								break;
							}
						}
						return $number;
					}	

 $displaycode=$this->usermodel->populatuserpermissionsdataarray('config','productcode',$userid);
  $displaydiscount=$this->usermodel->populatuserpermissionsdataarray('sales','discount',$userid);
$profile = $this->usermodel->populateallarray('smsconfig','id',$companyid);

									$photo=$profile[0]['logo'];
									$img=$profile[0]['logo'];
											$receiptlogo=$profile[0]['receiptlogo'];
																	 $countbranch=$this->usermodel->countbranches($companyid);

					
?>
 
 <div class="row">

 

   	<div class="col-md-12">
	
  
		<div class="panel panel-default" data-collapsed="0">
        	<div class="panel-heading" id="non-printable">
            	<div class="panel-title" id="redcolor" >
            		<i class="entypo-plus-circled"></i>
						 <span class="pull-right"> View Sales</span>
						 
							<div class="print-hide"  >
<form name="print" method="post" action="">
 
 <div class="row">

 

   	<div class="col-md-6">

	
  			  <a class="btn btn-primary" id="next-btn" href="<?php echo base_url(); ?>user/viewsales">Add New Sales </a>
</div>
   	<div class="col-md-6">
<input type="submit" id="print"  accesskey="p" class="btn btn-primary"  name="print"onclick="printDiv('printableArea')" value="print receipt!" />
</form></div></div>
							</div> 
						 
						 

				
            	</div>
            </div>
				<div class="table-responsive">
			<div id="printableArea" style="width:100%;margin:0" >
<table    width="100%" >
						<thead>
						
												<?php
if($receiptlogo=="yes"){
?>
								<tr>
								

					<td COLSPAN="6">
					<center>


			<?php
$img1='uploads/';
	$url = base_url().$img1.$img;
	
if($photo!=""){
	
	?>
	
 <div>
<img class="img-circle" src="<?php echo $url ?>" height="150" width="100%" style="border-radius:4px;margin:0px  ">		 

</div>
<?php
 
 }
 ?>
		
						</center>	
						</td>
					
				
				</TR>
					<?php
}

						?>
						
						
						
						
						
						
						
						
						
						<tr>
								
		<td colspan="6"><center><b>
       	<center>		<span id="extratitle"> HOLD ORDER	</span>		</center>			

								</b></center></td>
								
								
							</tr>
							<tr>
								
								<td colspan="6"><center>
								
									<?php 

										 if( $countbranch > 1){
										 $mybranches= $this->usermodel->populateallarray('branch','id',$branchid);
										$branchdescription=$mybranches[0]['description']; if(isset($branchdescription)) {
											
											
											  echo strtoupper($branchdescription); }


										}else{
									$bussinessname=$configuration[0]['businessname']; if(isset($bussinessname)) {  echo $bussinessname; }
 
											 
										 }
										?>

								</center></td>
								
								
							</tr>
									<tr>
								
								<td ><b id="extratitle"> ORDER NO   </td>
								
								<td colspan="2" id="myalltitle" >	<h4 style="padding:0; margin:0" ><b id="extratitle">
																<?php 
																
															 $refno = $this->usermodel->populateallarray('sales','invoice',$invoiceid);

														echo $refno[0]['id']; 
																
																
																?><b></h4>
																	
																	 
															

								
								</td>
								
								
						
								
								<td ><b>REF NO:(<?php  echo$viewreceipt[0]['type'];?>)<b></td>
								
								<td colspan="2" id="mytitle">	<h4 style="padding:0; margin:0"><b>
																<?php $invoiceid; if(isset($invoiceid)) {  echo $invoiceid; }?><b></h4>
																	
																	 
															

								
								</td>
								
								
								
							</tr>
							<tr>
								
									<td colspan="4" id="mytitle"><h4 style="padding:0; margin:0"><b> TIME POSTED: <?php  $myypostedtime= $viewreceipt[0]['dateposted']; echo date("d/m/Y", strtotime($myypostedtime));    echo "  AT :" ;?> <?php echo date("H:i:a", strtotime($myypostedtime)) ; ?>
									<b>  <span class="pull-right"> 
													 <?php $mytable=$viewreceipt[0]['mytable']; if(($mytable!="")) {echo "Table No:".$mytable; }?>
													 </span></h4></td>
													 
													 		<td colspan="2" id="mytitle"><h4 style="padding:0; margin:0"><b> TIME PRINTED :  <?php echo $currenttime;?>
									</h4></td>
								
							</tr>
						
							
							
							<?php $customer=$viewreceipt[0]['customer'];
							$customername=$viewreceipt[0]['name'];
						 if(($customer!=0)&&($customer!="")){
							 	 ?><tr><td colspan="6"  id="mytitle"> CUSTOMER:
							<?php
							$mysupplier=$this->usermodel->populateallarray('people','id', $customer);
						$myname=$mysupplier[0]['company'].''.$mysupplier[0]['firstname'];
							echo strtoupper($myname);
						?>
						</td></tr><?php
						 }else  if(($customername!=0)||($customername!="")){
							 	 ?><tr><td colspan="6"  id="mytitle"> CUSTOMER:
							<?php
						
						
						echo strtoupper($customername);
						?>
						</td></tr><?php
							 
						 }	
							?>
							<?php 
							 $description=$viewreceipt[0]['description'];


							if($description!="") {
								?>
								<tr><td colspan="6"><center><h2>
								<?php
								
								echo " ".  strtoupper($description); 
								?>
								</h2></center></td></tr>
									<?php
								}?>
										
							
								

							
							
								
						
							
							
							<tr>
							
<?php 
if($displaycode==true){
	 ?>
<td id="mytitle"><b>Code</b></td>
<td id="mytitle"><b>Item Name</b></td>
	 	<?php
		}else{?>
	<td colspan="2" id="mytitle"><b>Item Name</b></td>
 <?php
 }
 ?>
 <td id="mytitle"><b>Qty</b></td>
 <?php 
if($displaydiscount==true){
	 ?>
								<td id="mytitle"><b>Dsc</b></td>
 <?php
 }
 ?>					
								<td id="mytitle"><b>Price</b></td>
								<td id="mytitle"><b>Total</b></td>
							
							</tr>
						</thead>
						<?php
					
 if(!is_array($viewsales)) {
	 

			 }else{
 if(!is_array($viewsales)) 

 {


   echo "<tr  style='margin-bottom:0px;margin-top:0px;' '>";
      echo "<td colspan='5'>";
    
    echo "</td>";
   echo "</tr>";
    }else {
 $totalcost=0;
  $totalvatcost=0;
  $totalexemptedcost=0;
   $totaltax=0;
      $total=0;
   $totalqty=0;
  	foreach ($viewsales as $c): 
 $total=$c->qty *$c->sprice;
		$totalcost= $totalcost+$total;
 $totalqty=$totalqty+$c->qty;
		 

             ?>
				                    <tbody>
									
										 
												
											<tr>
											
											<?php 
if($displaycode==true){
	 ?>
									<td><b><?php echo $c->code ; ?></b></td>

													    <td id="text"> <b><?php ; 
	 $receiptdescription=$configuration[0]['receiptdescription'];
   $customer=$c->code;
   if($customer!=""){
	  
      $productname = $this->usermodel->geteproductbyid($customer);
	  $finalmessage="";
       $message=strtoupper($c->address);
	   
	   if($message==""){
		   $finalmessage="";
	   }else{
		   		   $finalmessage=" -".$message;

	   }
		   

     echo$name=strtoupper($productname[0]['name'])." ".$finalmessage;


	 if($receiptdescription=="yes"){ echo"-".$description=strtoupper($productname[0]['description'])." ".$finalmessage;;}

	
   }else{
	    echo "No Product";  
   }
  
  
  ?></b></td>
	 	<?php
		}else{?>
	
	
													    <td  colspan="2" id="mytitle"><b><?php ; 
	 $receiptdescription=$configuration[0]['receiptdescription'];
   $customer=$c->code;
   if($customer!=""){
	   $message=strtoupper($c->address);
	   
	   if($message==""){
		   $finalmessage="";
	   }else{
		   		   $finalmessage=" -".$message;

	   }
		   


      $productname = $this->usermodel->geteproductbyid($customer);
  
     echo$name=strtoupper($productname[0]['name'])." ".$finalmessage;; 
	 
	 if($receiptdescription=="yes"){ echo"-".$description=strtoupper($productname[0]['description'])." ".$finalmessage;;}

	
   }else{
	    echo "No Product";  
   }
  
  
  ?></b></td>
 <?php
 }
 ?>
										<td id="mytitle"><b><?php echo $c->qty ; ?></b></td>
										
										 <?php 
if($displaydiscount==true){
	 ?>
	<td><?php echo $c->discount ; ?></td>
 <?php
 }
 ?>	
										
										
										 	 <td id="mytitle" ><b><?php echo $c->sprice ; ?></b></td>
											 	 <td id="mytitle" ><b><?php echo $c->amount ; 
												 //$totalcost=$totalcost+$c->amount ;
												 $totaltax=$totaltax+$c->tax ;
												 $mytaxrate=$c->tax ;
												 
												 if(($mytaxrate!=0)||($mytaxrate!="")){
													 
													 $totalvatcost=$totalvatcost+$c->amount;
												 }
												  if(($mytaxrate==0)||($mytaxrate=="")){
													 
													 $totalexemptedcost=$totalexemptedcost+$c->amount;
												 }

												 ?></b></td>
												 
											
											</tr>
										<?php endforeach;} ?>

              <?php }?>	
			  
								<tr>
								<td><b>TT QTY:</b></td>
								<td colspan="3" id="mytitle" >
								<B>
	<?php 
	
								if(isset($totalqty)) {echo   formatMoney($totalqty, true); }
								
								?>
								</B>
								</td>
							
								
								
								<td colspan="2" id="mytitle"><b>TOTAL: 
								<?php //$total=$viewreceipt[0]['total']; 
								if(isset($totalcost)) {echo   formatMoney($totalcost, true); }
								 $finalmaindiscount=$viewreceipt[0]['maindiscount'];
								 
								?> </b>
								
								
								
								</td>
							
							</tr>
							
							
							
							
							
							
							
																	<tr>	<td colspan="6"><H3>SERVED BY:
																	<?php 
																	$myuserid=$viewreceipt[0]['userid']; $myuser=$this->usermodel->populateallarray('people','id', $myuserid);
								$myfirstname=$myuser[0]['firstname'];
							echo strtoupper($myfirstname);?>
																	
																	
																	<?php 
															 $countbranch=$this->usermodel->countbranches($companyid);
															 if( $countbranch>1){
																 
																 
															


																		// $mytype=$c->type;

													 $branches= $this->usermodel->populateall('branch','id',$branchid);



								if(is_array($branches)) {

								   foreach($branches as $branches){

								   echo "<b>(".strtoupper($branches->name).")</b>";
								 
								 
								}
								
								} else {
									
							echo '<b>( No  Branch )</b>';

								}
			 }
								
																										
											
																?></H3> </td></tr>

							

							
				
									</tbody>
									
									
					</table>
		
					
					
					
					
				</div>
			</div>
			</div>
		</div>
  
  </div>
 </div>
 </div> 
			
			
		
		


	<script type="text/javascript">


$(window).load(function()
{
	 if( window.print()) {
         window.location.href = "../user/viewsales";
 }else{
	  //       window.location.href = "../user/viewsales";
 
 }
});
</script>	

















