<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$addpmtr = "?warehouseid=".$this->input->get("warehouseid")."=&fromdate=".$fromdate."&todate=".$todate;
$previousstockdate = date('d-M-Y', strtotime('-1 day', strtotime($fromdate)));
?>
      <div class="main my-0">
        <div class="row mt-3 mx-0">
          <div class="col-md-9">
            <h3>Item Ledger</h3>
          </div>
          <div class="col-md-3 text-right">
			
          </div>
        </div>
        
        <div class="container_fluid mt-2 px-3">
		<form name="frmsearch" method="get" action="outlet/<?php echo $mgmtname;?>">
		<input type="hidden" name="outletid" value="<?php echo encodeValue($this->outletid);?>">
         <table class="webforms sttbl bg-light my-0 table-responsive-sm">
          <tr>
          <td class="pr-0" width="20%">
		<select name="productid" id="productid" class="js-example-basic-single">
		  <option value="">Select Item</option>
			<?php
			if(is_array($rsproducts) && count(array_filter($rsproducts))>0)
			{

				foreach($rsproducts as $row)
				{
					$selelement = "";
					if(decodeValue($this->input->get("productid"))==$row->fld_id)
					{
						$selelement = ' selected="selected"';
					}				
						?>
								<option value="<?php echo encodeValue($row->fld_id);?>" <?php echo $selelement;?>><?php echo $row->fld_name;?></option>
						<?php
					
				}
			}
			?> 
		  </select>
		  </td>

           <td class="pr-0" width="8%"><input type="text" name="fromdate" id="fromdate" value="<?php echo $this->input->get("fromdate");?>"   placeholder="From Date" class="datepicker" style="width:150px;" /></td>
           <td class="pr-0" width="8%"><input type="text" name="todate" id="todate"  value="<?php echo $this->input->get("todate");?>" placeholder="To Date" class="datepicker" style="width:150px;" /></td>
		   <td>
           	<button class="btn btn-dark pt-0 pb-1 px-2">Go</button>
           </td>
		   </tr>

         </table>
       </form>
         <?php
			if($this->session->flashdata("message")!="" && $this->session->flashdata("alerttype")!="")
			{
         ?>
         <div class="text-center my-4">
			 <span class="alert <?php echo $this->session->flashdata("alerttype");?>"><?php echo $this->session->flashdata("message");?></span>
         </div>
         <?php
			}
         ?>
         
         <table class="table mt-2 table-responsive-sm">
		 
                <thead>
                	<tr>
                    	<th width="5%">Serial</th>                    	
                    	<th width="10%">Date</th>
                    	<th width="10%">Bill/Challan No.</th>
                    	<th width="15%">Particulars</th>
                    	<th width="10%" class="text-right">Received Qty.</th>
                    	<th width="10%" class="text-right">Issued Qty.</th>
                    	<th width="10%" class="text-right">Balance</th>
                    </tr>
                    </thead>
                    <tbody>
					<?php
					if(is_array($results) && count(array_filter($results))>0)
					{
						$rowindex = 0;
						$instock = 0;
						$totalreceived = 0;
						$totalissued = 0;
						$totaldeleted = 0;
						$balanceqty = 0;
						$previousqty = 0;
						$previoustitle = "";
						foreach($results as $row)
						{
							$rowindex++;
							$deletedtext = "";
							if($row["delstatus"]==1)
							{
								$deletedtext = '<span class="text-danger"> (Deleted)</span>';
								$totaldeleted += $row['qty'];
							}
							$totalreceived += $row['receivedqty'];
							$totalissued += $row['deliveredqty'];
							$balanceqty = $row['balance'];
							if($row["type"]=="stockin" && $row["qty"]==0 && $row["balance"]>0) 
							{
								$previousqty = $row['balance'];
								$previoustitle = $row['particular'];
							}

					?>
                    <tr>
                    	<td class="text-center"><?php echo $rowindex;?>.</td>
                    	<td><?php echo $row['refdate'];?></td>
                    	<td><?php echo $row['refno'];?></td>
                    	<td><?php echo $row['particular'] . $deletedtext;?></td>
                    	<td class="text-right"><?php echo $row['receivedqty']>0 ? $row['receivedqty'] : "-";?></td>
                    	<td class="text-right"><?php echo $row['deliveredqty']>0 ? $row['deliveredqty'] : "-";?></td>
                    	<td class="text-right"><?php echo $row['balance'];?></td>
                    </tr>
                    <?php
						}
					?>
                    <tr style="background-color:#dee2e6;">
                    	<td colspan="4"></td>
                    	<td class="text-right"><strong><?php echo number_format($totalreceived);?></strong></td>
                    	<td class="text-right"><strong><?php echo number_format($totalissued);?></strong></td>
                    	<td class="text-right"><strong><?php echo number_format($balanceqty);?></strong></td>
                    </tr>                    
                    <tr style="background-color:#dee2e6;">
                    	<td class="text-right" colspan="6"><strong>Total Received:</strong></td>
                    	<td class="text-right"><strong><?php echo number_format($totalreceived);?></strong></td>
                    </tr>                    
                    <tr style="background-color:#dee2e6;">
                    	<td class="text-right" colspan="6"><strong>Total Delivered:</strong></td>
                    	<td class="text-right"><strong><?php echo number_format($totalissued);?></strong></td>
                    </tr>
                    <!--<tr style="background-color:#dee2e6;">
                    	<td colspan="6" class="text-right text-danger"><strong>Deleted:</strong></td>
                    	<td class="text-right text-danger"><strong><?php echo number_format($totaldeleted);?></strong></td>
                    </tr>
                    <tr style="background-color:#dee2e6;">
                    	<td class="text-right" colspan="6"><strong>Net Delivered:</strong></td>
                    	<td class="text-right"><strong><?php echo number_format($totalissued - $totaldeleted);?></strong></td>
                    </tr>//-->
                    <?php
						if($previousqty>0 && $previoustitle!="")
						{
                    ?>
                    <tr style="background-color:#dee2e6;">
                    	<td class="text-right" colspan="6"><strong><?php echo $previoustitle;?>:</strong></td>
                    	<td class="text-right"><strong><?php echo number_format($previousqty);?></strong></td>
                    </tr>
                    <?php
						}
                    ?>
                    <tr style="background-color:#dee2e6;">
                    	<td class="text-right" colspan="6"><strong>Stock In Hand:</strong></td>
                    	<td class="text-right"><strong><?php echo number_format($balanceqty);?></strong></td>
                    </tr>
					<?php
					}
					else
					{
						$message2show = "Select item from the list above!";
						if($this->input->get("productid")!="") $message2show = "Data not found for this item!";
                    ?>
                    <tr><td colspan="9" style="text-align:center"><?php echo $message2show;?></td></tr>
                    <?php
					}
                    ?>
                    
                    </tbody>
                </table>
                 
	         <div class="clearfix"></div>
          
        </div>
        <div id="paginate" class="row">
			<?php echo $this->ajax_pagination->create_links(); ?>
        </div>
            
        </div>

