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

    #printable { 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%; }
    }
    </style>
 
 <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 salesreport</span>
                        <a  id="more"  class="btn btn-primary" href="<?php echo base_url();?>user/mysales">back  </a>
				 		 	<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" >				
				<?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;
					}
					  
	function MyformatMoney($number, $fractional=false) {
						if ($fractional) {
							$number = sprintf('%.2f', $number);
						}
						while (true) {
							$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1$1', $number);
							if ($replaced != $number) {
								$number = $replaced;
							} else {
								break;
							}
						}
						return $number;
					}
						
 
 $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);
	 				echo $this->session->flashdata('deletemsg'); 

				echo $this->session->flashdata('msg'); 
								?>	
					<div class="row">
						<div class="col-md-12">
							
							<div class="panel-body">
	
									
				<table class="tablesorter table table-bordered  table-hover" id="sortable_table">			

             <thead>
			  <tr>
			 <th colspan="19"><center>
			 <?php  if(isset($configuration[0]['businessname'])) {echo   $configuration[0]['businessname']; }
			   $TIMEOFREPORT=date("d/m/Y H:i:s a");
			 ?>

			 <B><?PHP if(isset($reporttype)) {echo   $reporttype; } ?><BR> 
			 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");?>
									<br>Name----------------------Date--------------------Signature----------------</span>

			</th>
	
			 </center>
			 </tr>
         <tr>
									<th> Code</th>
									
                                    
                                             

											 <th> Sale Type</th>
									<th> Before</th>
												 
   <th> Qty</th>	
   
   		<th> After</th>
   

   
                                              <th> B. price</th>   
											  <th> S. price</th>   
													<th>			W. Price </th>

                                                                   
											  <th>Dsc</th>                            
												<th>TT. B </th>
												<th>TT. S </th>
												<th>TT. WH </th>

												<th> Profit </th>
																								<th>Profit:% </th>

													   
													<th> 	Tax</th>
										<th> 	Tax2</th>
													
													 <th> Type</th>  
									       <th>Date sold</th>
										 
									
												<th> Branch</th> 
													<th> Customer</th> 
										    <th> Added by</th>  



                 </tr>
             </thead>
<?php



 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; 
  		  $totaltax=0; 
		    		  $totaltax2=0; 

  		  $totalbb=0; 
  		  $totalsprice=0; 					
		  
		  		 		  $itemname="" ;

						  $totalwhprice=0; 					


    $totalqty=0; 
							$totalprofit=0; 
														$ttprofitpercent=0; 

  	foreach ($salesreport as $c): 

	
	
	 $mytax2=(double)$c->mytax2;
  $totaltax2= (double)$totaltax2+ (double)$mytax2;
	
	
	
		 $bprice=$c->bprice;
		 $sprices=0;
									$sprice=$c->sprice;
									$qty=$c->qty;
$salestype=$c->salestype;


							$profit=((double)$sprice-(double)$bprice)*$c->myqty;
							$myprofit=((double)$sprice-(double)$bprice)*$c->qty;
							
							if($bprice==0){
																$ttprofitpercent=(($myprofit/1)*100*$c->qty); 

							}else{
																$ttprofitpercent=(($myprofit)/($bprice*$c->qty)*100); 

							}

							 $totalprofit=(double)$totalprofit+(double)$profit;


             ?>



             <tr>
                
            <?php 
			$myinvoice=$c->invoice;
				  	 $recipt = $this->usermodel->populateallarray('sales','invoice',$myinvoice);
		 	  $receiptno=$recipt[0]['id'];

$id=$c->id;
 $total=(double)$total+(double)$c->myamount;
 $salestype =$c->salestype ;
 $totalqty= (double)$totalqty+(double)$c->myqty;
$mycode="";
$tax=$c->mytax;
 $totaltax= (double)$totaltax+ (double)$tax;
$mycode=$c->code;
$myproducts=$this->usermodel->geteproductbyid($mycode);
		  		 		  $itemname=$myproducts[0]['name']."-".$myproducts[0]['description'] ;  

?>

              

  
  																		  <td>
  
  		   <a  id="non-printable" class=" btn-default" target="_blank"  href="<?php echo base_url();?>user/searchallmyinvoices?searchterm=<?php   if(isset($c->invoice )) {echo $c->invoice ; } ?>&view=userdata/allinvoices"> 		
<?php 
if($c->serial!==""){
	echo $c->code." S/N:".$c->serial  ;
}else{
	echo $c->code  ;
}
	echo "<br> RCT- ". $receiptno;

  ?>
  </a>
  
   <span id="myprint">
<?php 
if($c->serial!==""){
	echo $c->code." S/N:".$c->serial  ;
}else{
	echo $c->code  ;
}
	echo "<br> RCT- ". $receiptno;

  ?>
						 </span>
  
  </td>
			
		
  
  
  
  
  
  
  
  
  </td>
  


    <td><?php echo $c->salestype ; ?></td>
	
	 <td><?php echo $c->stockbefore ; ?></td>
	
		  <td><?php echo $c->myqty ; ?></td>
		      <td><?php echo $c->stockafter ; ?></td>
   <td><?php echo $c->bprice ; ?></td>
	  <td><?php 
	  if($salestype=="retail"){
		 echo $sprices= $c->sprice ; 
	  }else{
		  		 echo $sprices= 0 ; 

	  }
	  
	  
	  
	  ?></td>
	  	  <td><?php 
		  
		    if($salestype=="wholesale"){
		 echo $sprices= $c->sprice ; 
	  }else{
		  		 echo $sprices= 0 ; 

	  }
		  
		  //echo $wholesaleprice=$myproducts[0]['wholesaleprice'] ;
		  ?></td>


<td><?php echo $c->discount ; ?></td>
  <td><?php 
  $TTB=$c->bprice* (double)$c->myqty ;
  echo $TTB ; 
  $totalbb=(double)$totalbb+(double)$TTB;
  
  ?></td>

  <td><?php  
  if($salestype=="retail"){
		 echo $ttprice=$c->myamount ;
	  }else{
		  		 echo $ttprice=0 ; 

	  }
			$totalsprice=(double)$totalsprice+(double)$ttprice;


  ?></td>
    <td><?php  
		  if($salestype=="wholesale"){
		 echo $ttwh= $c->myamount ;
	  }else{
		  		 echo $ttwh=0 ; 

	  }

			$totalwhprice=(double)$totalwhprice+(double)$ttwh;

	
	
	?></td>

  
      <td ><?php echo $profit;
	  
	  
	  ?></td> 
	    <td ><?php echo sprintf('%0.2f', $ttprofitpercent)." %:";
	  
	  
	  ?></td> 
           
			       <td><?php
								   echo formatMoney($c->mytax,true);  

				   ?></td>
				   			       <td><?php 
															   if($c->mytax2 !=0){
	

	 echo formatMoney($c->mytax2,true);

}
	  
								   
								   								   if($c->address !=""){
	
	echo "  ". $c->address ;

}
							   								   if($c->serial !=""){
	
	echo" ".$c->serial ;

}
								   
								   
								   ?></td>

   
				    <td><?php
					// echo $c->status ;
					?></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 
 
  $allsales = $this->usermodel->getsalesbyinvoice($c->invoice);
						 $mycusomer=$allsales[0]['customer'];
						  $ourname=$allsales[0]['name'];
if($mycusomer !=""){
$ssmyuser=$this->usermodel->geteuser($mycusomer);
   echo $ssmyname=$ssmyuser[0]['firstname'];
}else{
	


echo $ourname=$allsales[0]['name'];


}
  ?></td>  
  
 
	 
	 
	 
	 
	 
	 
 <td>
 
 
 
 <?php 
if($user=="all"){
	echo "Everyone";
}else{
	


 $userids= $c->userid ;

$myuser=$this->usermodel->geteuser($userids);
   echo $myname=$myuser[0]['firstname'];
}
  ?></td>  
  
 
         
             </tr>

	<?php endforeach; ?>

								<tr border="1" background="green">
									<td colspan="3"><b>Total Amount</b></td>
		<td colspan="2"><b><?php if(isset($totalqty)) {echo $totalqty;   }?> </b></td>

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

				<td><b><?php if(isset($totalsprice)) {echo formatMoney($totalsprice, true);  }?> </b></td>
												<td><b><?php if(isset($totalwhprice)) {echo formatMoney($totalwhprice, true);  }?>  </b></td>
	
									<td colspan="4"></td>
									<td><b><?php if(isset($totalprofit)) {echo formatMoney($totalprofit, true);  }?> </b> </td>

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

									
	<td colspan="7"></td>								
</tr>
		PRODUCT NAME=<?PHP if(isset($itemname)) {echo   $itemname; } ?></BR>

	TOTAL ITEMS SOLD=<?PHP if(isset($totalqty)) {echo   formatMoney($totalqty); } ?></BR>
			TOTAL SALES MADE=<?PHP if(isset($totalsprice)) { echo formatMoney($totalsprice);} ?></BR>
		TOTAL PROFITS SOLD=<?PHP if(isset($totalprofit)) {echo formatMoney($totalprofit);} ?></BR>
              <?php }?>

   
		
		
		
		
		
		
		
		

             <thead>
		
			   <tr>
								
								<td colspan="16" ><center  >
								
						
									

					
												<span id="mytitle"></br><b> Stock movement report between   <?PHP echo $finalfromdate ?> and <?PHP echo $finaltodate ?></b>		
								
								<br>Name----------------------Date--------------------Signature----------------</span>

								</center></td>
								
								
							</tr>
					
         <tr>
       <th>Invoice /P.O No</th>
	        <th>Code</th>

     <th colspan="5">Name</th>
	 		   		 	     <th>Before</th>

	       <th>Added / Returned</th>

		 	     <th>After</th>
				 		 	     <th>S.Price</th>
				 		 	     <th>TT S.price</th>
								 
								  <th>B.Price</th>
				 		 	     <th>TT 	B.price</th>


   <th>Type</th>
	     <th>From</th>
		 		  <th>To</th>
		  <th>Branch</th>
		 <th colspan="2">Added Date</th>
		 	 <th>Supplier</th>
<th>Added by</th>


                 </tr>
             </thead>
<?php

 if(!is_array($viewincomingstock)) 

 {


   echo "<tr  style='margin-bottom:0px;margin-top:0px;' '>";
      echo "<td colspan='12'>";
    
       echo '<div class="alert alert-warning text-center">There no records found..</div>';
    echo "</td>";
   echo "</tr>";
    }else {
?>
    
             <?php 
  $myqty=0;
  	foreach ($viewincomingstock as $c): 

		 

             ?>



             <tr>
                
            <?php 
$id=$c->id;


if($c->itemstatus == "returned"){
	
	$myqty=(double)$myqty+-(double)($c->total);
}else{
$myqty=(double)$myqty+(double)$c->total;	
}


?>
    <td><?php if($c->invoice_number !=""){
		?>
	
			   <a  id="non-printable" class=" btn-default" target="_blank"  href="<?php echo base_url();?>user/searchallmysalesorder?searchterm=<?php   if(isset($c->invoice_number )) {echo $c->invoice_number ; } ?>&view=userdata/allinvoices"> 		

  <?php echo $c->invoice_number ; ?>
    </a>
     <span id="myprint">
<?php echo $c->invoice_number ; ?>
						 </span>
  

		
		<?php
	}else{
			echo"Internal";
		
	}
	; ?></td>
	
	
	
	
	
	
	
	
	
	
	    <td><?php echo $c->item_id ; ?></td>

                 <td colspan="5"><?php 
	$itemid=$c->item_id;
	
	$mycategory = $this->usermodel->populateall('products','code', $itemid);
	


if(is_array($mycategory)) {

   foreach($mycategory as $mycategoryrow){
		echo $mycategoryrow->name." (" .$mycategoryrow->description.")";


}} else {
    echo 'No Product .';
}
	
?>
</td>
		    <td><?php echo $c->item_id1 ; ?></td>

 
    <td><?php
if($c->itemstatus == "returned"){
	
	echo "-". $c->total;
}else{
echo $c->total;	
}




	//echo $c->total ;


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

			    <td><?php echo $c->sellingprice ; ?></td>
				
							    <td><?php echo(double) $c->sellingprice*(double)$c->total  ; ?></td>

 <td><?php echo $c->buyingprice ; ?></td>
				
							    <td><?php echo (double)$c->buyingprice*(double)$c->total  ; ?></td>
							    <td><?php echo $c->itemstatus;  ; ?></td>

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

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



if(is_array($branchfrom)) {

   foreach($branchfrom as $branchfromrow){

   echo $branchfromrow->name;
 
 
}} else {
    echo $stockfrom;
}
	
?>
												 
	 </td>											 
			
		 <td>
												 
	<?php 
					// $branch="";
					 $type=$c->type;

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



if(is_array($branchtype)) {

   foreach($branchtype as $branchtyperow){

   echo $branchtyperow->name;
 
 
}} else {
    echo $type;
}
	
?>
												 
	 </td>											 
			
	
	
	
	


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

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



if(is_array($branch)) {

   foreach($branch as $branchrow){

   echo $branchrow->name;
 
 
}} else {
    echo 'General.';
}
	
?>
												 
	 </td>
	
	
	
	
	
	
	 <td colspan="2"><?php echo $c->date ; ?></td>
	 
		 
		 
		  <td><?php 
		  
		  
		  
		  
		  
		  $allstocks = $this->usermodel->getstockbyinvoice($c->invoice_number);
						 $mysupplier=$allstocks[0]['supplier'];
if($mysupplier !=""){
$supliers=$this->usermodel->geteuser($mysupplier);

if($supliers[0]['company']!=""){
	   echo $supliers[0]['company'];
}else{
   echo $supliers[0]['firstname'];	
}

   
   
   
   
   
}else{
	
 echo "Internal ";	




}
		  
		  
		  

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

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

  ?></td>
	 	 
             </tr>

	<?php endforeach;} ?>

            
 								<tr colspan="3" border="1" background="green">
									<td colspan="8"><b>Total Quantity</b></td>

									<td><b><?php if(isset($myqty)) {echo $myqty;   }?></b> </td>
</td>
	<td colspan="5"></td>								
</tr>
	TOTAL Items Quantity =<?PHP if(isset($myqty)) {echo formatMoney($myqty);} ?></BR>
        </table>
				
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	</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="view" value="sales_order">

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


















