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

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

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

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

		    // document.body.innerHTML = originalContents;

	}
}
	</script>

         <script type="text/javascript">
  
		
		 $(document).on("click", "#managebtndelete", function()
        {
          var itemid = $(this).data('id');
		  $("#itemid1").val(itemid); 
		  
		  //
		   
        });
		
		
		
    </script>   
  <style type="text/css">
  
  .invoice-box table tr.heading td {
        background: #eee;
        border-bottom: 1px solid #ddd;
        font-weight: bold;
		height:auto;
		
		
    }
  
  

    #myprint { display: none; }
#print{  	


    background-repeat: no-repeat;
	font-size;20px;
  
    width:100%; 


}
#maintable tr { border-bottom:1px dashed #494949; padding:0px;}

#sale{  	


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


}
    @media print
    {
    	#non-printable { display: none; }
    	#printableArea { display: block; font-size:16; width:100%; }
		    	#myprint { display: block; font-size:16; width:100%; }

    }
    </style>
 
 <div class="row">
  
   	

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

  				<?PHP
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;
					}
echo $this->session->flashdata('deletemsg'); 
	 $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'];	
	    $addproductaction=$this->usermodel->populatuserpermissionsdataarray('reports','add',$userid);

	 $updateaction=$this->usermodel->populatuserpermissionsdataarray('reports','update',$userid);
  $updatebuyingpriceaction=$this->usermodel->populatuserpermissionsdataarray('reports','update',$userid);
  $deleteaction=$this->usermodel->populatuserpermissionsdataarray('reports','delete',$userid);
	         		$configuration = $this->usermodel->populateallarray('smsconfig','id',$companyid);
					$etims=$configuration[0]["hscode"];
					
					

    $mpesaaction=$this->usermodel->populatuserpermissionsdataarray('sales','mpesa',$userid);
	      $chequeaction=$this->usermodel->populatuserpermissionsdataarray('sales','cheque',$userid);
 $voucheraction=$this->usermodel->populatuserpermissionsdataarray('sales','voucher',$userid);
	      $creditaction=$this->usermodel->populatuserpermissionsdataarray('sales','credit',$userid);
		   $pointsaction=$this->usermodel->populatuserpermissionsdataarray('sales','points',$userid);
	      $cardaction=$this->usermodel->populatuserpermissionsdataarray('sales','card',$userid);
	      $orderaction=$this->usermodel->populatuserpermissionsdataarray('sales','order',$userid);
		  		    $cashaction=$this->usermodel->populatuserpermissionsdataarray('sales','cash',$userid);
		  	      $creditnoteeaction=$this->usermodel->populatuserpermissionsdataarray('sales','creditnote',$userid);

			    $cashierreprint=$this->usermodel->populatuserpermissionsdataarray('sales','reprint',$userid);




				echo $this->session->flashdata('msg'); 
								?>	
		<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 salesreport</span>
				
						
						
								<?php	
		  $data['category'] = $session_data['category'];
	 $category = $data['category'];
	 	 if($category=="reseller"){
			 ?>
						                        <a  id="non-printable"  class="btn btn-primary" href="<?php echo base_url();?>user/payments">back  </a>

<?php
		 }else{
			?> 
			 
			                        <a  id="non-printable"  class="btn btn-primary" href="<?php echo base_url();?>user/startsales">back  </a>
 
			<?php 
		 }
		 ?>
						
						
						
						
						
						
						
						
				 		 	<div class="pull-right"  >
<form name="print" method="post" action="">
 <input type="submit" id="print"  accesskey="p" class="btn btn-primary"  name="print"onclick="printDiv('printableArea')" value="print receipt!" />
</form></div>
            	</div>
            </div>
			<div class="panel-body">

			
	<div class="widget-content nopadding table_holder table-responsive">				

					<div class="row">
						<div class="col-md-12">
							
							<div class="panel-body">
	<div id="printableArea" style="width:100%" class="invoice-box">
									
  <table cellpadding="0" cellspacing="0" border="0" id="example3" style=" width:100%;"  >

             <thead>
			
			   <tr  border="0" style="border-color:white">
			 <th colspan="3"> 
					
<?php
$img1='uploads/';
						$profile = $this->usermodel->populateallarray('smsconfig','id',$companyid);

									$photo=$profile[0]['logo'];
									$img=$profile[0]['logo'];
										$receiptlogo=$profile[0]['receiptlogo'];

	
	if($receiptlogo=="yesbranch") {
					 $allbranches= $this->usermodel->populateallarray('branch','id',$branchid);
					$branchlogo=$allbranches[0]['logo']; 
											
				
					$url = base_url().$img1.$branchlogo;

			}else{
									$url = base_url().$img1.$img;


			}

	
if($photo!=""){

	
	
	
	
	
	
	
	
	
	
	
	?>
	
 <div>

<img class="img-circle" src="<?php echo $url ?>" height="150" width="200" style="border-radius:1px;margin:0px  ">		 

</div>
<?php
 
 }
 ?>

 


			 </th>
			 
			 	 <th colspan="10">
			 
			 
		
			 <?php  if(isset($configuration[0]['businessname'])) {echo   $configuration[0]['businessname']; }
			   $TIMEOFREPORT=date("d/m/Y H:i:s a");
			   
			   
		 	  $vat1=$configuration[0]['view'];	
		  		 	  $vat2=$configuration[0]['display1'];	

			 ?>
</BR>
			 <B><?PHP if(isset($reporttype)) {echo   $reporttype; } ?>
			 FROM  <?php  if(isset($finalfromdate)) {echo   date("d/m/Y H:i:s", strtotime($finalfromdate)); }?>  TO  <?php  if(isset($finaltodate)) {echo     date("d/m/Y H:i:s", strtotime($finaltodate));; }?>   </B><br>
			 Report was run on <?php echo  date("d/m/Y H:i:s a") ; ?>
									<br>Name----------------------Date--------------------Signature----------------</span>

			</th>
	
		
			 </tr>
       	<TR >
					<td colspan="14">
					<hr style="border-top: 2px solid gray;margin:0px;padding:0px">

					</td>
					</TR>
							
						
							<tr  class="heading">
									 <th> #</th>
		 									 <th> Date</th>
											  <th>Customer Name</th>                            
	 <th> Sales Order</th>
									 <th> Invoice No</th>
										 <th> Sales Person</th>
                                        <th>  Mode</th>
										   

														<th> Invoice Amount</th> 
              <th> Status</th> 



												<th>Payment Code</th>											  

											
												<th>Cost Value</th>	
											 
												<th>Profit/Loss</th>	

										 
									<th> Branch</th> 

		 											  
		                          <th id="non-printable"> More</th> 


                 </tr>
				  	<TR >
					<td colspan="14">
					<hr style="border-top: 2px solid gray;margin:0px;padding:0px">

					</td>
					</TR>
             </thead>
<?php



 if(!is_array($salesreport)) {
				    echo '<div class="alert alert-danger text-center">No data found</div>';

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

 {


   echo "<tr  style='margin-bottom:0px;margin-top:0px;' '>";
      echo "<td colspan='1'>";
    
       echo '<div class="alert alert-warning text-center">There no records found..</div>';
    echo "</td>";
   echo "</tr>";
    }else {
?>
    
             <?php 
  $total=0; 
  $mytotal=0; 
    $totalcash=0; 
		  $totaltax=0; 
$totalmpesa=0;		  
$cashmpesa=0;

$totalcredit=0;		  
$totalcheque=0;
$totalvoucher=0;		  
$totalpoints=0;
$totalcard=0;		  
$totalcreditnote=0;

$i=0;


  	foreach ($salesreport as $c): 
$i++;

 $myinvoice=$c->invoice ; 
              
$id=$c->id;
 $total=$alltotals[0]["total"];


 $view="";
$salestype= $c->salestype;
$type=$c->type;
if ($salestype=="wholesale"){
		
	  $view="user/viewwholesales";
}else if ($salestype=="retail"){

	 $view="user/viewsales";

}else{
	 $view="user/viewsales";
	
}
 
?>



             <tr>
     <td><?php  echo $i; ;  ?></td>
			      <td><?php 
 
 
 
 
  if($group=="no"){
	///  echo $c->dateposted ;
	  echo  date("d/m/Y", strtotime($c->dateposted));
  }else{
	  // echo $c->mydateposted;
	  echo  date("d/m/Y", strtotime($c->mydateposted));
  }
    

 
 
 
	  ?></td>	
	  
	  
<td>
 
  <?php  $mycustomer= $c->customer ;
  
    if(($mycustomer!="") && ($mycustomer!="directly")){

$myuser=$this->usermodel->getpeople($mycustomer);
    $myname=$myuser[0]['company'];
	    $phone=$myuser[0]['phone'];

     $firstname=$myuser[0]['firstname'];
   if($myname!=""){
	   echo $myname."-".$phone;
	   
   }else{
	   
	   echo $firstname."-".$phone; 
	   
   }
	}else{
		echo $name= $c->name;
		
	}
	//echo " ".$c->description ;
	
	 
    if(($mycustomer!="") && ($mycustomer!="directly")){
if($myuser[0]['idnumber']!=""){
									echo "</br> ID/PIN ".$myname=$myuser[0]['idnumber'];

				}
	}
  ?>
 
 
 </td>
     <td><?php  echo ($c->invoice) ;  ?></td>

	  

	  
          <td>
		   <a  id="non-printable" target="_blank"   href="<?php echo base_url();?>user/searchinvoice?code=<?php   if(isset($myinvoice)) {echo $myinvoice; } ?>&view=<?php   {echo  $view;}  ?>"> <?php  if($c->receiptno!=""){  echo $c->receiptno; }else{ echo $c->id;  } ?>   </a>



<?php
  		if(($type=="Invoice") || ($type=="Creditnote")){
											
 if( $cashierreprint==true){
										 ?>
				 <a  id="non-printable" target="_blank"  href="<?php echo base_url();?>user/printinvoice?invoice=<?php   if(isset($myinvoice)) {echo $myinvoice; } ?>&view=<?php   {echo  $view;}  ?>">    </a>
						 <span id="myprint"><?php echo $c->invoice ; ?></span>

									<?php
									 }else{
										 
										 if(isset($myinvoice)) {echo $myinvoice; } 
										 
										 
									 }			
									 ?>
												
		

								<?php
								}else{
									 if( $cashierreprint==true){
									?>
				
				 <a  id="non-printable" target="_blank"  href="<?php echo base_url();?>user/printreceipts?invoice=<?php   if(isset($myinvoice)) {echo $myinvoice; } ?>&view=<?php   {echo  $view;}  ?>">    </a>
						 <span id="myprint"><?php echo $c->invoice ; ?></span>

									<?php
									 }else{
										 
										 if(isset($myinvoice)) {echo $myinvoice; } 
										 
										 
									 }			
									 ?>
								
								
								
								
								
								
								
								
								
								
								<?php
								}
								?>
		  		
		  </td>          


	  
 <td><?php 
if($group =="no"){
	
 $userids= $c->userid ;

$myuser=$this->usermodel->geteuser($userids);
   echo $myname=$myuser[0]['firstname'];
	
}else{
	
	echo "Everyone";


}
  ?></td>  
              
            
  <td><?php echo $c->type ;


  if ($etims =="EnableEtims"){
									
 if($c->rcptsign !=""){
	 
	 if($c->salesdump !=""){
		 
	 if(($c->salesdump !="") &&($c->etimsstatus =="1")){

	echo " -Y"; 
	 }else {
		 echo " -P";

	 }		
	 
	 }else{
		 echo " -Y";  
	 }

}else{
	echo " -N";
	
}
  }

  ?></td>


 <td>
 <a  id="non-printable" target="_blank"   href="<?php echo base_url();?>user/searchinvoice?code=<?php   if(isset($myinvoice)) {echo $myinvoice; } ?>&view=<?php   {echo  $view;}  ?>"> <?php echo $c->total ; ?>   </a>
 
 <span id="myprint"><?php  echo formatMoney($c->total, true) ; ?></span>
 
 </td>
   <td><?php 
	 
	
	 if($group=="no"){
	 
	 echo $c->paymentstatus ; 
	 
	 
	 					
	
	 if($c->discountedtotal != $c->total){
		   echo "<br>";
	
		 ?>
		 <span style="color:red">
		<?php echo "Changed- ";
		     echo formatMoney($c->discountedtotal, true) ;
		    ?>
		   </span>
		<?php 
		
	
	 }
	
	

	 }




	?>
	 
 
 
 
 
 
	    <?php
	
		$totalcash=$alltotals[0]["cash"];
		$totalmpesa=$alltotals[0]["mpesa"];
		
		$totalcredit=$alltotals[0]["credit"];		  
$totalcheque=$alltotals[0]["cheque"];	
$totalvoucher=$alltotals[0]["voucher"];			  
$totalpoints=$alltotals[0]["points"];	
$totalcard=$alltotals[0]["card"];	
		$totalcreditnote=$alltotals[0]["creditnote"];;
		
		
		//$mytax=$this->usermodel->getVat($myinvoice,$companyid);
						$cashmpesa=	 $totalmpesa+$totalcash+$totalcheque+$totalcard;

				//$cashmpesa=	 $totalmpesa+$totalcash+$totalcredit+$totalcheque+$totalvoucher+$totalpoints+$totalcard;
	$mytotal=$totalmpesa+$totalcash+$totalcheque+$totalpoints+$totalcard;
$taxrate=0;


$totaltax= 0;
		
		
		?></td>

      <td><?php  echo ($c->cashier) ;  ?></td>




  
        <td><?php  
$profit=0;
	  		  $getbprice=0;

		$mybprice= $this->usermodel->getbuyingpricebyinvoice($companyid,$c->invoice);

 $getbprice=$mybprice[0]["ttbprice"];
echo formatMoney($getbprice, true); 


$profit=$c->total- $getbprice;


		?></td>

	 
	      <td><?php  echo ($profit) ;  ?></td>

	 
	 
	 
	
      <td>
												 
	<?php 
					// $branch="";
					 $mybranch=$c->branch;

					 $allbranch= $this->usermodel->populateall('branch','id',$mybranch);



if(is_array($allbranch)) {

   foreach($allbranch as $branchrow){

   echo $branchrow->name;
 
 
}} else {
    echo 'General.';
}
	
?>
												 
	 </td>
 
	 
	 
   <td  id="non-printable"> 

 
 
					<div class="btn-group">
                                    <button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
                                        More <span class="caret"></span>
                                    </button>
									
									
									
                                    <ul class="dropdown-menu dropdown-default pull-right" role="menu">
                                        
                                        <!-- teacher EDITING LINK -->
                                      <?php
										 if( $deleteaction==true){
											
											?>
										<!--
											  <li> 
										 <a class="btn btn-danger managebtn" id="managebtndelete"		 data-id='<?php echo  $c->id;?>'    data-toggle='modal' data-target='#deleteModel<?php  $c->id; ?>'>	 


                                                <i class="glyphicon glyphicon-pencil"></i>
																Cancel Sale
                                                </a>
                                                        </li>
														
											-->			
									
									<?php
										}
										?>
                                        
                                        <li class="divider"></li>
                                        <li> 
				<a  id="more" class=" btn-success" target="_blank"  href="<?php echo base_url();?>user/searchJE?searchterm=<?php   {echo  $myinvoice;}  ?> ">
								 
																							 

                                                <i class="glyphicon glyphicon-pencil"></i>
															View	GL Transactions
                                                </a>
																		
										</li>
                                           <li class="divider"></li>
                                        <li> 
				<a  id="more" class=" btn-primary" target="_blank"  href="<?php echo base_url();?>user/Reconcilesale?searchterm=<?php   {echo  $myinvoice;}  ?>&type=<?php   {echo  $type;}  ?>&branch=<?php   {echo  $c->branch;}  ?>">
								 
																							 

                                                <i class="glyphicon glyphicon-pencil"></i>
															Reconcile GL Transactions
                                                </a>
																		
										</li>
                                    </ul>
											
                                </div>
</td> 
         
             </tr>

	<?php endforeach;} ?>


<tfoot>
	<TR >
					<td colspan="14">
					<hr style="border-top: 2px solid gray;margin:0px;padding:0px">

					</td>
					</TR>
							
								<tr border="1" background="green">
									<td colspan="7"><b>Grand Tototal:</b></td>

									<td ><b>
		<?php if(isset($total)) {echo formatMoney($total, true);  }?><b></td>
		
		

</tr>


	<TR >
					<td colspan="14">
					<hr style="border-top: 2px solid gray;margin:0px;padding:0px">

					</td>
					</TR>
	</tfoot>
	
	
              <?php }?>

        </table>
			 <div class="col-md-12 text-center">    <?php echo $links; ?>
					   		<span style="color:red; font-size:20px"><?php  echo $total_rows. "   Sales. " ;?>    </span>

        
        </div>
		
		<br></br>
		
		
		
				
			</div>
		</div>
  
  </div>
			
			
			
		
		
  

 
</div>


















