<link href="<?php echo base_url(); ?>assets/autocomplete/jquery-ui.css" rel="stylesheet">
    <link href="<?php echo base_url(); ?>assets/autocomplete/ui.theme.css" rel="stylesheet">
    <link href="<?php echo base_url(); ?>assets/autocomplete/autocomplete.css" rel="stylesheet">
	


         <script type="text/javascript">
   $(this).ready( function() {  
            $("#searchterm").autocomplete({  
 
                minLength: 1,  
                source:   
                function(req, add){  
                    $.ajax({  
                        url: "<?php echo base_url(); ?>Systemusers/productlookup",  
                        dataType: 'json',  
                        type: 'POST',  
                        data: req,  
                        success:      
                        function(data){  
                            if(data.response =="true"){  
                                add(data.message);  
                                console.log(data);
                            }  
                        },  
                    });  
                },  
                     
            });  
        }); 
		 $(document).on("click", "#managebtndelete", function()
        {
          var itemid = $(this).data('id');
		  $("#itemid1").val(itemid); 
		  
		  
		   
        });
		
		
		
    </script>   
	<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/viewtocktaking";
		alert(" printing...........");
    }else {

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

		    // document.body.innerHTML = originalContents;

	}
}

 
	</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%; }
				#myprint { display: block; font-size:16; width:100%; }

    }
    </style>
   	

   	<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 STOCK TAKING REPORT</span>
                        <a  id="more"  class="btn btn-primary" href="<?php echo base_url();?>user/viewtocktaking">Back to stock taking  List </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;
					}
 $session_data = $this->session->userdata('logged_in');

   		
$mydate=date('Y-m-d');
$mytime=date('H:i:s');
 	 	$data['id'] = $session_data['id'];
        
		 $userid = $data['id'];
$data['companyid'] = $session_data['companyid'];
	 $companyid = $data['companyid'];
		 $data['usertype'] = $session_data['usertype'];
	 $usertype = $data['usertype'];	
	
	
	 				echo $this->session->flashdata('deletemsg'); 
	 				echo $this->session->flashdata('postedtime'); 

				echo $this->session->flashdata('msg'); 
								?>	
					<div class="row">
						<div class="col-md-12">
							
							<div class="panel-body">
	<div class="col-md-12" id="non-printable">
        <?php 
		
        $attr = array("class" => "form-horizontal", "role" => "form", "id" => "search", "name" => "search");
        echo form_open("user/searchallstocktaking", $attr);?>
            <div class="form-group">
			
                <div class="col-md-3">
                    <input class="form-control" id="searchterm" autofocus name="searchterm" placeholder="Search for product by name, code,description.." type="text" value="<?php echo set_value('searchterm'); ?>" />
                </div>
				
				
				    <div class="col-md-2">
		  		<input type="text" name="startdate" value="<?php echo set_value('startdate',$mydate); ?>" class="form-control" required placeholder="Enter from date?">

					</div>
				
				
				
				  <div class="col-md-2">
		  		<input type="text" name="closedate" value="<?php echo set_value('closedate',$mydate); ?>" class="form-control" required placeholder="Enter to date?">

					</div>
					  <div class="col-md-2">
<select class="form-control" name="branchid"  id="branchid" required>
	
	
	<option value=""  >---Select Branch/Counter to Open--</option>

									
					<option value="all"  >All branches/Counters</option>
								

	
	
	<?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 class="col-md-3">
                    <input id="btn_search" name="btn_search" type="submit" class="btn btn-success" value="Search" />
                    <a href="<?php echo base_url(). "user/viewtocktaking"; ?>" class="btn btn-primary">Refresh</a>
                </div>
				 
            </div>
        <?php echo form_close(); ?>

        </div>	
									
									
				<table class="tablesorter table table-bordered  table-hover" id="sortable_table">			

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

										<?php $bussinessname=$configuration[0]['businessname']; if(isset($bussinessname)) {echo   $bussinessname; }
										
									
										
										?>
												<span id="mytitle"><b> STOCK TAKEN ON <?PHP echo $startdate ?></b>		
								
								<br>Name---------------------------Date--------------------------Signature---------------------</span>

								</center></td>
								
								
							</tr>
         <tr>
     <th>Code</th>
     <th>Product Name</th>
	
	     <th>Category</th>

	 <th>Available </th>
	 	  <th>B.Price</th>

	  <th>S.Price</th>
		   <th>Actual qty</th> 
		   <th>Diff qty</th> 
		  <th>TT Exp</th> 
 		  <th>TT Actual</th> 
 		  <th>Diff amt</th> 

	
<th>Branch</th>
<th>Start Date</th>
<th>Close Date </th>
<th>Staff</th>

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

			  </tr>
             </thead>
<?php
 
 



 if(!is_array($viewproducts)) {
				    echo '<div class="alert alert-danger text-center">There was no products found in the system</div>';

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

 {


   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 
  
  $totalsales=0;
   $ttexp=0;
$ttactual=0;
$difference=0;
$maindifference=0;
$totaldifference1=0;
$totaldifference2=0;

  	foreach ($viewproducts as $c): 

	


             ?>

							 
										<?php
	
                 echo form_open('user/updatestocktaking' , 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="view" value="user/viewstocktakingreporbranch?startdate=<?php if(isset($startdate)) {echo $startdate; }; ?>&branchid=<?php if(isset($branchid)) {echo $branchid; }; ?>&department=<?php if(isset($department)) {echo $department; }; ?>" class="form-control" required placeholder="Enter view">
							<input type="hidden" name="myid" value="<?php echo set_value('myid',$c->id); ?>" class="form-control" required placeholder="Enter id">

							<input type="hidden" name="branchid" value="<?php echo set_value('branchid',$c->branchid); ?>" class="form-control" required placeholder="Enter branchid">
							<input type="hidden" name="open" value="<?php echo set_value('open',$c->open); ?>" class="form-control" required placeholder="Enter qty available">
							<input type="hidden" name="code" value="<?php echo set_value('code',$c->code); ?>" class="form-control" required placeholder="Enter code">
							<input type="hidden" name="startdate" value="<?php if(isset($startdate)) {echo $startdate; }; ?>" class="form-control" required placeholder="">
							<input type="hidden" name="department" value="<?php if(isset($department)) {echo $department; }; ?>" class="form-control" required placeholder="">

             <tr>
                
            <?php 
$id=$c->id;
$myaty="0";
$myaty=$c->open ;
$totalsales=(double)$totalsales+(double)$c->balance;

?>

              
  <td><?php echo $c->code ; ?></td>
    <td><?php 
	 $mycode=$c->code;
	 	$buyingprice="";

	  $userid=$c->userid;
   if($mycode!=""){

      $productname = $this->usermodel->geteproductbyid($mycode);
  
     echo$name=$productname[0]['name']."  ". $description=$productname[0]['description'];
	  		$buyingprice=$productname[0]['buyingprice'];

	
   }else{
	    echo "No Product";  
   }
 $c->branchid;
 if($c->description !=""){
 echo "-".$c->description;
 }

	?>
	</td>

    <td><?php 
	
	
	if($mycode!=""){
	  
      $productname = $this->usermodel->geteproductbyid($mycode);
  
     $category=$productname[0]['category'];

	

	
	
	$mycategory = $this->usermodel->populatecategorybyowner($category);
	


if(is_array($mycategory)) {

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


}} else {
    echo 'No Category .';
}
	   }else{
	    echo "No Product";  
   }
  

$myuser=$this->usermodel->geteuser($userid);
   //echo $myname=$myuser[0]['firstname'];
 if($c->description !=""){
 echo "-".$c->description;
 }
?>
</td>
	
  <td><?php echo $c->open ; ?></td>
  <td>
		 <input type="text" name="buyingprice" id="non-printable" value="<?php echo set_value('buyingprice',$buyingprice); ?>" class="form-control1" style="width:95%" required placeholder="buying price" >
							<span id="myprint"><?php echo $buyingprice ; ?></span>

</td>
  <td>
		  					<input type="text" name="sellingprice" id="non-printable" value="<?php echo set_value('sellingprice',$c->sellingprice); ?>" class="form-control1" style="width:95%" required placeholder=" selling price" >
							<span id="myprint"><?php echo $c->sellingprice ; ?></span>

		  </td> 
		   <td>
		  					<input type="text" name="added" id="non-printable" value="<?php echo set_value('added',$c->added); ?>" class="form-control1" style="width:95%" required placeholder="sold adjusted qty" >

							<span id="myprint"><?php echo $c->added ; ?></span>
		  </td> 
		  
		  
		  
      <td><?php echo $c->balance; ?></td>  
        <td><?php  $ttexp=(double)$c->sellingprice*(double)$c->open; 
		
		echo formatMoney($ttexp,true);
		?></td>  
        <td><?php  $ttactual=(double)$c->added*(double)$c->sellingprice;
echo formatMoney($ttactual,true);
		?></td>  

         <td><?php  $difference=(double)$ttactual-(double)$ttexp;

		echo formatMoney($difference,true);
$totaldifference1=$totaldifference1+($ttexp);
$totaldifference2=$totaldifference2+$ttactual;
$maindifference=($maindifference+($difference));


		 ?></td>  

												 
			   <td><?php
			  if($c->branchid==0){
				  echo "Main Store";
				  
			  }else{
				  
			 
								$mybranch = $this->usermodel->populateallarray('branch','id',$c->branchid);
echo$branchname=$mybranch[0]['name'];
 }
		?></td>  									 
				  <td><?php echo $c->startdate; ?></td> 											 
									 
						<td><?php echo $c->closedate; ?></td> 	

											   
			      <td><?php 
				  $myid="";
				  $updatedby=$c->updatedby;
				  if($updatedby==0){
					    $myid=$userid;
					  
				  }else{
					  $myid=$updatedby;

					  
				  }
				
		 $myuser=$this->usermodel->geteuser($myid);
			echo $myname=$myuser[0]['firstname'];
				  ?></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 -->
                                      
											<li> 
	
	 	 <a  id="more" target="_blank" class="btn btn-default" href="<?php echo base_url();?>user/viewcountedserials?randomitemidentifier=<?php   {echo  $c->code ;}  ?>&branch=<?php   {echo  $c->branchid ;}  ?>&startdate=<?php   {echo  $c->startdate ;}  ?>">

                                        <i class="glyphicon glyphicon-pencil"></i>
															View counted serials Numbers
                                                </a>
												
                                                        </li> 
										
                                        <li class="divider"></li>
                                        
												<li> 
				<a  id="more" target="_blank"class="btn btn-default" href="<?php echo base_url();?>user/viewmissingserials?branch=<?php   {echo  $c->branchid;}  ?>&mycode=<?php   {echo  $c->code;}  ?>&startdate=<?php   {echo  $c->startdate ;}  ?>"> 


                                                <i class="glyphicon glyphicon-pencil"></i>
															View Missing Serial Numbers
                                                </a>
                                                        </li> 
                                        <li class="divider"></li>

										
																							
                                    </ul>
										
                                </div>
 </td>
 <?php echo form_close();?>	
		 
		 	
			</tr>

	<?php endforeach;} ?>

              <?php }?>
			  
		

			  <tr border="1" background="green">
									<td colspan="2"><b>Total</b></td>
									<td colspan="3"></td>
		<td  colspan="2" style="background:#999999 ; width:100px" ><b>QTY Difference:  </td><td> <?php if(isset($totalsales)) {echo $totalsales;   }?></b></td>
		<td style="background:#999999 ; width:100px" ><b>TT EXP : <?php if(isset($totaldifference1)) {echo formatMoney($totaldifference1,true);   }?></b></td>
		<td   style="background:#999999 ; width:100px" ><b>TT Actual: <?php if(isset($totaldifference2)) { echo formatMoney($totaldifference2,true); }?></b></td>
		<td   style="background:#999999 ; width:100px" ><b>Difference: <?php if(isset($maindifference)) { echo formatMoney($maindifference,true); }?></b></td>

							
</tr>

        </table>
			 	
	</div>

		
	
	
	
	
	
	
	
	
	

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

 
</div>


















