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

    #myprint { display: none; }
#print{  	


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


}

#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;
					}
					 $fromdate=date("Y-m-d 00:00:00");
					  $mydate=date("Y-m-d H:i:s");

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);
	 
			    $cashiersales=$this->usermodel->populatuserpermissionsdataarray('cashiersales','all',$userid);
			    $cashierreprint=$this->usermodel->populatuserpermissionsdataarray('sales','reprint',$userid);
 $wiewinvoices=$this->usermodel->populatuserpermissionsdataarray('incomeexpenses','wiewinvoices',$userid);
	      $viewbills=$this->usermodel->populatuserpermissionsdataarray('incomeexpenses','viewbills',$userid);
  $viewdebtors=$this->usermodel->populatuserpermissionsdataarray('incomeexpenses','viewdebtors',$userid);
  $viewcreditors=$this->usermodel->populatuserpermissionsdataarray('incomeexpenses','viewcreditors',$userid);
    $viewexpense=$this->usermodel->populatuserpermissionsdataarray('incomeexpenses','viewexpense',$userid);

  $addpeople=$this->usermodel->populatuserpermissionsdataarray('members','add',$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 All invoices</span>
					 
						  <?php

 if( $viewexpense==true){
 ?>
 						 <a href="<?php echo base_url();?>user/viewexpenses" class="btn btn-default"> View Expense</a>

 
<?php	
}
  ?>
 <?php

 if( $viewbills==true){
 ?>
 						 <a href="<?php echo base_url();?>user/paybill" class="btn btn-default"> Pay bills</a>

 
<?php	
}
  ?>
	  <?php

 if( $wiewinvoices==true){
 ?>
 	  	  				 	  	  				 <a href="<?php echo base_url();?>user/allinvoices" class="btn btn-default"> All Invoices</a>
	  	  				 <a href="<?php echo base_url();?>user/incoicepayments" class="btn btn-default"> Receive Payments</a>

 
<?php	
}
  ?>
	  <?php

 if( $viewdebtors==true){
 ?>
 	  	  				 <a href="<?php echo base_url();?>user/allcreditors" class="btn btn-default"> All Debtors</a>

 
<?php	
}
  ?>
	  <?php

 if( $viewcreditors==true){
 ?>
 						 	  	  				 <a href="<?php echo base_url();?>user/viewallcreditors" class="btn btn-default"> All Creditors</a>

 
<?php	
}
  ?>
	  <?php

 if( $addpeople==true){
 ?>
                         <a data-toggle='modal' data-target='#addsupplierModal' class="btn btn-default">Add Campany </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('sortable_table')" 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">
														<center>	<b>	<?php  if(isset($title)) {echo $title; }   ?></b>	</center>

							<div class="panel-body">
	
									
				<table border="1"  width="100%">			

             <thead>
										 <tr>
										 <th> Rct No</th>
									<th> Ref No</th>
                                        <th>  Mode</th>
										   

                                              <th> Table</th> 
														<th> Total</th> 
                                              <th> Bal</th>   
											  <th> Cash</th>  
											 <th> Mpesa</th>
												<th> Card</th>  
											 <th> Voucher</th> 
												<th> Credit</th> 											
											  <th> Points</th>                       
											  <th>Cheque</th> 
												<th> Code</th>											  
												<th> Tax</th> 											
											  <th> Due date</th>                       
											  <th>Customer</th>                            

											 
											 

									       <th>Date sold</th>
										 
									
																	<th> Counter</th> 

										    <th> Solde by</th>  
											
  <th>Payment</th>    

                 </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 pending payments..</div>';
    echo "</td>";
   echo "</tr>";
    }else {
?>
    
             <?php 
  $total=0; 
    $totalcash=0; 
		  $totaltax=0; 
$totalmpesa=0;		  
$cashmpesa=0;

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




  	foreach ($salesreport as $c): 


             ?>



             <tr>
            <td><?php echo $c->id ; ?></td>      
            <?php 
$id=$c->id;
 $total=$total+$c->total;

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

	 $view="user/viewsales";

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

              
  <td>
  
  
  
  
  <?php
  		if($type=="Invoice"){
											
 if( $cashierreprint==true){
										 ?>
				 <a  id="non-printable"  href="<?php echo base_url();?>user/printinvoice?invoice=<?php   if(isset($myinvoice)) {echo $myinvoice; } ?>&view=<?php   {echo  $view;}  ?>"> <?php echo $c->invoice ; ?>-Print   </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"  href="<?php echo base_url();?>user/printreceipts?invoice=<?php   if(isset($myinvoice)) {echo $myinvoice; } ?>&view=<?php   {echo  $view;}  ?>"> <?php echo $c->invoice ; ?>-Print   </a>
						 <span id="myprint"><?php echo $c->invoice ; ?></span>

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

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

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

	  
	  </td>
 <td>
 <a  id="non-printable"  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 $c->total ; ?></span>
 
 </td>
    <td><?php echo $c->balance ; ?></td>
   <td><?php echo $c->cash ; ?></td>
      <td><?php echo $c->mpesa ; ?></td>
			     <td><?php echo $c->card ; ?></td>
											   <td><?php echo $c->voucher ; ?></td>
												  <td><?php echo $c->credit ; ?></td>
												   <td><?php echo $c->points ; ?></td>
											   <td><?php echo $c->cheque ; ?></td>
											    <td><?php echo $c->cashier ; ?></td>

	 
	    <td><?php
	
		$totalcash=$totalcash+$c->cash;
		$totalmpesa=$totalmpesa+$c->mpesa;
		
		$totalcredit=$totalcredit+$c->credit;		  
$totalcheque=$totalcheque+$c->cheque;	
$totalvoucher=$totalvoucher+$c->voucher;			  
$totalpoints=$totalpoints+$c->points;	
$totalcard=$totalcard+$c->card;	
		
		
		
		
		$mytax=$this->usermodel->populateall('sales_order','invoice', $myinvoice);
				$cashmpesa=	 $totalmpesa+$totalcash+$totalcredit+$totalcheque+$totalvoucher+$totalpoints+$totalcard;
	
$taxrate=0;

if(is_array($mytax)) {

   foreach($mytax as $myrow){
    $myrow->tax;
$taxrate=$taxrate+$myrow->tax;

}
}
$totaltax= $totaltax+$taxrate;
			echo$taxrate;
		
		?></td>
	  <td><?php echo $c->due_date ; ?></td>


<td><?php  $customer=$c->customer ;

$mycustomer=$this->usermodel->populateallarray('people','id', $customer);
			echo$myname=$mycustomer[0]['firstname'];

 ?></td>

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

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



if(is_array($branch)) {

   foreach($branch as $branchrow){

   echo $branchrow->name;
 
 
}} else {
    echo 'General.';
}
	
?>
												 
	 </td>
 

 <td><?php  $userids= $c->userid ;

$myuser=$this->usermodel->geteuser($userids);
   echo $myname=$myuser[0]['firstname'];

  ?></td>  
  
  
			<td><?php 
 $invoiceid=$c->invoice;
	
	  ?>
	  	  	  	 <a  id="more" class="btn btn-warning"  href="<?php echo base_url();?>user/viewbalance?invoice=<?php   {echo  $invoiceid;}  ?>">View Payments  </a>
	  	  	  	 <a  id="more" class=" btn-success"  href="<?php echo base_url();?>user/viewmyexpense?pono=<?php   {echo  $c->mytable;}  ?>&finaltotal=<?php   {echo  $c->total;;}  ?>">Expenses  </a>

  </td>
   
         
             </tr>

	<?php endforeach;} ?>

								<tr border="1" background="green">
									<td colspan="6"><center><b>Expected  Amount:
		<?php if(isset($total)) {echo formatMoney($total, true);  }?><b></center></td>

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

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

			<td>  <b><?php if(isset($totalvoucher)) {echo formatMoney($totalvoucher, true);  }?><b></td>
	<td>  <b><?php if(isset($totalcredit)) {echo formatMoney($totalcredit, true);  }?><b></td>
				<td>  <b><?php if(isset($totalpoints)) {echo formatMoney($totalpoints, true);  }?><b></td>
			<td>  <b><?php if(isset($totalcheque)) {echo formatMoney($totalcheque, true);  }?><b></td>

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

		
			<td colspan="6">  <b>Total for all payments : <?php if(isset($cashmpesa)) {echo formatMoney($cashmpesa, true);  }?><b></td>
	<td id="non-printable"></td>
</tr>
	
	
	
              <?php }?>

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

			<div id="userModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Sales Report per user</h4>
      </div>
      <div class="modal-body">
      


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



				echo $this->session->flashdata('msg'); 
		
                 echo form_open('user/viewgroupreceiptreportbyuser' , array('class' => 'form-horizontal form-groups-bordered validate', 'enctype' => 'multipart/form-data'));
				 if(validation_errors()):?>
<div class='alert alert-danger text-center'><?php echo validation_errors(); ?></div>
<?php endif;?>

		  			  		<input type="hidden" name="type" value="all" class="form-control"  placeholder="Enter type">

  <div class="form-group">
    <label for="inputbranchname" class="col-sm-2 control-label">Counter</label>
    <div class="col-sm-10">
	<select class="form-control" name="branchname"  id="branchname" required>
	
	
	<option value=""  >---Select Branch/Counter Name--</option>
<?php
									 if( $viewallaction==true){
										 ?>
					<option value="all"  >All branches/Counters</option>
									<?php
									 }
							
	?>
	
	<?php 
					 
					 		$maincategory = $this->usermodel->populateall('branch','companyid',$companyid);


							


if(is_array($maincategory)) {

   foreach($maincategory as $maincategoryrow){

   //echo $courserow->coursename;
    ?>
					<option selected="selected" value="<?=  $maincategoryrow->id;   ?>"><?=  $maincategoryrow->name; ?></option>



	<?php

}} else {
    echo 'No main counter available.';
}
	
?>
</select>
		

		
	</div>
  </div>
	
	
				  		<input type="hidden" name="view" value="viewgroupreceiptreport" class="form-control" required placeholder="Enter sales type">

     <div class="form-group">
    <label for="inputuser" class="col-sm-2 control-label">User?</label>
    <div class="col-sm-10">
	<select class="form-control" name="user" required>
	
	<option value=""  >---Select user---</option>

						<?php
		
		 if($cashiersales==true){
			$allusers = $this->usermodel->populateall('people', 'companyid',$companyid);
	

 }else{
			$allusers = $this->usermodel->populateall('people', 'id',$userid);

 
  
 }
  ?>

	
	
	
	<?php 
				
				
	

	
if(is_array($allusers)) {

   foreach($allusers as $userrow){

   //echo $courserow->coursename;
    ?>
					<option value="<?=  $userrow->id;   ?>"><?=  $userrow->email ."(". $userrow->firstname ."- ".$userrow->surname.")"; ?></option>



	<?php

}} else {
    echo 'No user available.';
}
	
?>
</select>
		
						<span class="text-danger"><?php echo form_error('people'); ?>

	</div>
  </div>
  
  
  
  
  
  
  
  
  
	
	
	
	
	
  <div class="form-group">
    <label for="inputfromdate" class="col-sm-2 control-label">From Date </label>
    <div class="col-sm-10">
	
		  		<input type="text" name="fromdate" value="<?php echo set_value('fromdate',$fromdate); ?>" class="form-control" required placeholder="Enter start date">
		 
				
		<span class="text-danger"> <?php echo form_error('fromdate'); ?>
		
		</span>
	</div>
  </div>
  
  	<div class="form-group">
    <label for="inputtodate" class="col-sm-2 control-label">	To Date </label>
    <div class="col-sm-10">
	
		  		<input type="text" name="todate" value="<?php echo set_value('todate',$mydate); ?>" class="form-control" required placeholder="Enter End date">
		 
				
		<span class="text-danger"> <?php echo form_error('todate'); ?>
		
		</span>
	</div>
  </div>
  
    
     <div class="form-group">
	 
	 
	 
    <label for="inputdisplay" class="col-sm-2 control-label">Dispaly type?</label>
    <div class="col-sm-10">
	<select class="form-control" name="display" required>
	<option  value="all"  >---Do you want to print user z-report--</option>
	<option value="yes" >Yes</option>
<option selected="selected" value="no">No i want to just view sales</option>

	
</select>
		
						<span class="text-danger"><?php echo form_error('display'); ?>

	</div>
  </div>
   

	 <div class="form-group">
    <label for="inputviewmaincategory" class="col-sm-2 control-label">Department Category?</label>
    <div class="col-sm-10">
	<select class="form-control" name="maincategory"  id="maincategory" required>
	
	<option selected="selected" value="all"  >---All/Any Category--</option>

	
	<?php 
					 
					 		$maincategory = $this->usermodel->populateall('maincategory','companyid',$companyid);


							


if(is_array($maincategory)) {

   foreach($maincategory as $maincategoryrow){

   //echo $courserow->coursename;
    ?>
					<option value="<?=  $maincategoryrow->id;   ?>"><?=  $maincategoryrow->name; ?></option>



	<?php

}} else {
    echo 'No main category available.';
}
	
?>
</select>
		
						<span class="text-danger"><?php echo form_error('maincategory'); ?>

	</div>
  </div>

  

	
	 <div class="form-group">

    <div class="col-sm-offset-2 col-sm-10">
     
	         <button type="submit"  value="event" name="btn_addevent" class="btn btn-primary"> View Sales  </button>

    </div>
  </div>
  					<?php echo form_close();?>
			</div>							   
				
									
							
								
			<div class="modal-footer">

    

				</div>

		</div>
	    </div>
    </div>
			<div id="deleteModel" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Delete</h4>
      </div>
      <div class="modal-body">
        <h3>Are you sure you want to delete this item? </h3>
      </div>
      <div class="modal-footer">
	  	

     
		<?PHP
				$session_data = $this->session->userdata('logged_in');
 echo $this->session->flashdata('deletemsg'); 

		
                 echo form_open('user/deletebyid' , array('class' => 'form-horizontal ', 'enctype' => 'multipart/form-data'));
				 if(validation_errors()):?>
<div class='alert alert-danger text-center'><?php echo validation_errors(); ?></div>
<?php endif;?>
 <input type="hidden" id="table"name="table" class="table" placeholder="table" value="sales">

 <input type="hidden" id="view"name="view" class="view" placeholder="view" value="user/startsales">
 <input type="hidden" id="itemid1"name="itemid" class="itemid" placeholder="itemid" value=""><br>
 <button type="button" class="btn btn-success" data-dismiss="modal">No</button>
 		  	         <button type="submit" name="btn_delete" value="deletebyid" class="btn btn-danger"> Delete  </button>

	<?php echo form_close();?>
      </div>
    </div>

  </div>

  </div>		
	
	
	
	
	
	
	
	
	
	

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

 
</div>


















