<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>
  $(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);
                            }  
                        },  
                    });  
                },  
                     
            });  
        });  
		








	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/viewopeningstock";
		alert(" printing...........");
    }else {

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

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


}
#alltitle{ font-size: 1em; }
	#mytitle { font-size: 1.5em; }
#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 OPENNING/ CLOSING STOCK REPORT</span>
			
                  
		  <a  id="more"  class="btn btn-default" href="<?php echo base_url();?>user/viewopeningstock">Back </a>
 	


				 		 	<div class="pull-right"  >

<form name="print" method="post" action="">
 <input type="submit" id="print"  accesskey="p" class="btn btn-default"  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('msg'); 
								?>	
					<div class="row">
						<div class="col-md-12">
							
							<div class="panel-body">
	
		<div class="col-md-12">
        <?php 
		
        $attr = array("class" => "form-horizontal", "role" => "form", "id" => "search", "name" => "search");
        echo form_open("user/searchallclosingstock", $attr);?>
            <div class="form-group">
			
                <div class="col-md-3">
                    <input class="form-control" id="searchterm" 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" id="startdate" value="<?php echo set_value('startdate',$startdate); ?>" class="form-control" required placeholder="startdate" >

					</div>
				
											
										<input type="hidden" name="branchid" id="myinvoice" value="<?php echo set_value('branchid',$branchid); ?>" class="form-control" required placeholder="branchid" >
										<input type="hidden" name="department" id="department" value="<?php echo set_value('department',$department); ?>" class="form-control" required placeholder="department" >

				
				
					 
					 
				
				
				

				
                <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/enterallclosingstock?startdate='.$startdate.'&department='.$department.'&branchid='.$branchid.'&action='.$action.'&method='.$method; ?>" 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>
			<center>
						<h3>Update closing stock in bulk.:
						Opening date:<?php echo $startdate; ?>
						</h3>		
							
			</center>
		
								
						
         <tr>
     <th>Code</th>
     <th>Item Name</th>
	
	     <th>Category</th>

	 <th>Open</th>
		 

<th>Issued </th>

	 <th>Total</th>

<th>Cl.Stock </th>
<th>Sale </th>
<th>Price </th>
<th>Total Amount </th>

<th>Branch</th>

<th>Staff</th>

<th>Update Stock</th>
<th>Status</th>


			  </tr>
             </thead>
<?php
 
 




 if(!is_array($viewproducts)) 

 {


   echo "<tr  style='margin-bottom:0px;margin-top:0px;' '>";
      echo "<td colspan='9'>";
    
       echo '<div class="alert alert-warning text-center">There no records found..</div>';
    echo "</td>";
   echo "</tr>";
    }else {
?>
    
		 
										<?php
					
		
                 echo form_open('user/bulkclosingstock' , 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;?>
             <?php 
  
  $totalsales=0;
     $ttsold=0;

  	foreach ($viewproducts as $c){ 

		


             ?>


					
             <tr>
                
            <?php 
		$id="";
if(isset($c->stockid)) {
$id=$c->stockid;

 }else{
	 $id=$c->id;

	 
 }	
			
			
//$id=$c->stockid;

$myaty="0";
$myaty=$c->open ;

?>

              
  <td><?php echo $c->code ; ?></td>
    <td><?php 
	 $mycode=$c->code;
	  $userid=$c->userid;
   if($mycode!=""){
	  
      $productname = $this->usermodel->geteproductbyid($mycode);
  
     echo$name=$productname[0]['name']."  ". $description=$productname[0]['description'];

	
   }else{
	    echo "No Product";  
   }
  
	?>
	</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'];

?>
</td>
	
  <td><?php echo $c->open ; ?></td>
        
  <td>
  
  
  
   <?php 
				$closedate=$c->closedate;
					if((!$closedate=="") || (!$closedate==null)){
						
						  echo $c->added;
					}else{
						
								
											
		?>

	 		

	 

  
  
  
  <?php
 
  if($method=="bulk"){
	  
	  
  ?>
<input type="text" name="added[]" id="added" value="<?php echo set_value('balance',$c->added); ?>" class="form-control" required placeholder="balance" >

	  <?php
  }else{
	  ?>
	  <input type="hidden" name="added[]" id="added" value="<?php echo set_value('balance',$c->added); ?>" class="form-control" required placeholder="balance" >

	  	  <?php
	  echo $c->added;
  }
 
	
	}
	 ?>
    
											
											</td> 
  <td><?php echo $clstock=($c->open+ $c->added) ; ?></td>

			  <td>
			  							<input type="hidden" name="startdate" id="startdate" value="<?php echo set_value('startdate',$startdate); ?>" class="form-control" required placeholder="startdate" >
											<input type="hidden" readonly name="myid[]" id="myid" value="<?php echo set_value('myid',$id); ?>" class="form-control" required placeholder="stockid" >
											<input type="hidden" name="mybranch" id="mybranch" value="<?php echo set_value('mybranch',$c->branchid); ?>" class="form-control" required placeholder="branchid" >

										<input type="hidden" name="branchid" id="myinvoice" value="<?php echo set_value('branchid',$branchid); ?>" class="form-control" required placeholder="branchid" >
										<input type="hidden" name="department" id="department" value="<?php echo set_value('department',$department); ?>" class="form-control" required placeholder="department" >
											<input type="hidden" name="code[]" id="code" value="<?php echo set_value('code',$c->code); ?>" class="form-control" required placeholder="code" >
										<input type="hidden" name="method" id="method" value="<?php echo set_value('method',$method); ?>" class="form-control" required placeholder="department" >


									 <?php 
				$closedate=$c->closedate;
					if((!$closedate=="") || (!$closedate==null)){
						
						echo $c->balance;
					}else{
						
								
											
		?>

	 																<input type="text" name="balance[]" id="balance" value="<?php echo set_value('balance',$c->balance); ?>" class="form-control" required placeholder="balance" >


	 
	 <?php
	}
	 ?>		
											
											
											
											
											
											
											
											</td> 
 <td><?php echo $sold=(($c->open+ $c->added)- $c->balance) ;?></td>  
  <td><?php echo $c->price; ?></td>  
  <td><?php echo $total=$sold*$c->price;
$totalsales=$totalsales+$total;
$ttsold=$ttsold+$sold;


  ?></td>  
												 
			  <td><?php
								$mybranch = $this->usermodel->populateallarray('branch','id',$c->branchid);
echo$branchname=$mybranch[0]['name'];

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

					  
				  }
				
		 $myuser=$this->usermodel->geteuser($myid);
			echo $myname=$myuser[0]['firstname'];
				  ?></td>    
			
			<td>
			<?php echo $action; ?>
		<input type="hidden"  readonly name="action" id="action" value="<?php echo set_value('action',$action); ?>" class="form-control" required placeholder="action" >

			</td>
			
	 <td>
	 
	 <?php 
				$closedate=$c->closedate;
					if((!$closedate=="") || (!$closedate==null)){
						?>
						<span style="color: red"> Closed</span>
						<?php
						
					}else{
						
								
											
		?>

	 						<span style="color: green"> Opened</span>

	 
	 <?php
	}
	

	 ?>
	 </td>
		 	
		 
			</tr>
	
<?php } ?>
			  
	
   	<tr>
			<td colspan="3"> </td>


				<td>			

								 <button type="submit"  value="mybulk" name="btn_bulkclosingstock" class="btn btn-primary"> <span class="glyphicon glyphicon-pencil"></span> Bulk closing stock </a>   </button>
	
								</td>
								
								<td>	


	 <?php 
				$closedate=$c->closedate;
					if((!$closedate=="") || (!$closedate==null)){
					
if($usertype=="admin"){
	
	
	?>
			
			
			<?php
				}
					}
						
								
											
		?>



								</td>
																		<td  style="background:#999999 ; width:100px" ><b><?php if(isset($ttsold)) {echo $ttsold;   }?></b></td>

								<td colspan="2"> 
								
								
								</td>
										<td  colspan="2" style="background:#999999 ; width:100px" ><b><?php if(isset($totalsales)) {echo formatMoney($totalsales, true);  }?></b></td>

							
							</tr>
							<?php echo form_close();?>	
							<?php } ?>
   
			  
		

	

        </table>
			 
	</div>

		
	
	

	

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

 
</div>


















