
<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>
            </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);
	 		

  	   $dontshowprofit=$this->usermodel->populatuserpermissionsdataarray('reports','dontshowprofit',$userid);
  	   $dontshowbp=$this->usermodel->populatuserpermissionsdataarray('reports','dontshowbp',$userid);

  


			echo $this->session->flashdata('deletemsg'); 

				echo $this->session->flashdata('msg'); 
								?>	
					<div class="row">
						<div class="col-md-12">
							 		 	<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 class="panel-body">
	
									
				<table style="width:100%" class="tablesorter table table-bordered  table-hover" id="sortable_table">			

   
		
		
			 <th colspan="12"><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>
			
			 
					
         <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>
				 		 			<?php
						if( $dontshowbp !=true){
										 ?>
											
							<th>B.Price</th>
				 		 	  											  
									<?php
			 
	  
									 }
									?>
				 
				 		 	     <th>S.Price</th>
								 
								 	 			<?php
						if( $dontshowbp !=true){
										 ?>
											
						
				 		 	     <th>TT 	B.price</th>											  
									<?php
			 
	  
									 }
									?>
								 
				 		 	     <th>TT S.price</th>
						
    
	  			<?php
		 if( $dontshowprofit!=true){
										 ?>
	

													<th> Profit </th>
													<th>Profit:% </th>
			
	
													  		<?php
			

			}
	  ?>
			
							


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

  $myqty=0;
    $ttbuying=0;
	    $ourprofit=0;
    $ttselling=0;
	$ttprofit=0;
   $myqtypurchased=0;
  	foreach ($viewincomingstock as $c): 

		 

             ?>



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

if($c->total < 0){
	$TT= -1*($c->total);
}else{
		$TT=($c->total);

}

if($c->itemstatus=="received"){
	
	$myqtypurchased=(double)$myqtypurchased+(double)($TT);
	
}




if($c->itemstatus=="returned"){
	$ttbuying=$ttbuying+-($TT*(double)$c->buyingprice);
	$ttselling=$ttselling+-($TT*(double)$c->sellingprice);
	$ttprofit=$ttprofit +($ttselling-(double)$ttbuying);
	$myqty=$myqty+-($TT);
		$ttprofitpercent=$myqty+-((double)$TT);
		$myprofit=($ttselling-(double)$ttbuying);
		
				if($ttbuying==0){
																$ttprofitpercent=(($myprofit/1)*100); 

							}else{
																$ttprofitpercent=(($myprofit)/($ttbuying)*100); 

							}

		
		
		

}else{
		
}
if($c->itemstatus=="sold"){
	$ttbuying=$ttbuying+($TT*(double)$c->buyingprice);
	$ttselling=$ttselling+($TT*(double)$c->sellingprice);
	$totalselling=($TT*(double)$c->sellingprice);
	$totalbuying=($TT*(double)$c->buyingprice);
	$ourprofit=$totalselling-$totalbuying;
$myqty=$myqty+(double)$TT;
	$ttprofit=(double)$ttprofit +(double) $ourprofit;
	
	
	
	$ttprofitpercent=$myqty+(double)($TT);
		$myprofit=($ttselling-(double)$ttbuying);
		
				if($ttbuying==0){
																$ttprofitpercent=(($myprofit/1)*100); 

							}else{
																$ttprofitpercent=(($myprofit)/($ttbuying)*100); 

							}

		
	
	

	}

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

		?>
	
			   <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
		}
		//echo $c->invoice_number ;
	}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>
					<?php
						if( $dontshowbp !=true){
							 ?>
 <td><?php echo $c->buyingprice ; ?></td>


							<?php
						}
							 ?>
			    <td><?php echo $c->sellingprice ; ?></td>
					<?php
						if( $dontshowbp !=true){
										 ?>
											
				
							    <td><?php echo (double)$c->buyingprice*(-1*(double)$c->total)  ; ?></td>										  
									<?php
			 
	  
									 }
									?>
				
							    <td><?php echo (double)$c->sellingprice*(-1*(double)$c->total)  ; ?></td>
			 		
					
	  			<?php
		 if( $dontshowprofit!=true){
										 ?>
	
      <td ><?php echo $ourprofit;
	  
	  
	  ?></td> 
	    <td ><?php echo sprintf('%0.2f', $ttprofitpercent)." %:"; ?></td> 
	  
	  	<?php
		}
		?>
				
	  
	  

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

					 $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 Sold</b></td>

									<td colspan="4"><b><?php if(isset($myqty)) {echo $myqty;   }?></b> </td>
									<td><b><?php if(isset($ttbuying)) {echo $ttbuying;   }?></b> </td>
									<td><b><?php if(isset($ttselling)) {echo $ttselling;   }?></b> </td>
									<td><b><?php if(isset($ttprofit)) {echo $ttprofit;   }?></b> </td>

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

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

	<td colspan="5"></td>								
</tr>
	TOTAL Items Quantity Sold =<?PHP if(isset($myqty)) {echo formatMoney($myqty);} ?></BR>
      	TOTAL Items Quantity Purchased =<?PHP if(isset($myqtypurchased)) {echo formatMoney($myqtypurchased);} ?></BR>
	TOTAL Buying Price =<?PHP if(isset($ttbuying)) {echo formatMoney($ttbuying);} ?></BR>
      	TOTAL selling price =<?PHP if(isset($ttselling)) {echo formatMoney($ttselling);} ?></BR>
      	TOTAL Profit =<?PHP if(isset($ttprofit)) {echo formatMoney($ttprofit);} ?></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>


















