<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$addpmtr = "?outletid=".$this->input->get("outletid")."&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-6">
            <h3>Outlet Stock Summary</h3>
          </div>
          <div class="col-md-6 text-right">
		  <?php
		  if(decodeValue($this->input->get("outletid"))>0)
		  {
			 $outletid =  decodeValue($this->input->get("outletid"));
		  ?>
			<a href="account/<?php echo $mgmtname;?>/exportcsv/?<?php echo $_SERVER['QUERY_STRING'];?>" class="btn btn-info">Export CSV</a>
			<?php
		  }
			  ?>
          </div>
        </div>
        
        <div class="container_fluid mt-2 px-3">
		<form name="frmsearch" method="get" action="account/<?php echo $mgmtname;?>">
         <table class="webforms sttbl bg-light my-0 table-responsive-sm">
          <tr>
           <td><input type="text" name="search" class="form-control" size="20" value="<?php echo $this->input->get("search");?>" placeholder="search name,barcode "></td>
          
          
          <td>
		<select name="outletid" id="outletid" class="js-example-basic-single">
		  <option value="">Select Outlet</option>
						<?php
						if(is_array($outletlist) && count(array_filter($outletlist))>0)
						{

							foreach($outletlist as $outletrow)
							{
								$sel_outlet="";
								if(decodeValue($this->input->get("outletid"))==$outletrow->fld_id)
								{
									$sel_outlet = 'selected="selected"';
								}				
									?>
											<option value="<?php echo encodeValue($outletrow->fld_id);?>" <?php echo $sel_outlet;?>><?php echo $outletrow->fld_name;?></option>
									<?php
								
							}
						}
						?> 
		  </select>
		  </td>
           <td><input type="text" name="fromdate" id="fromdate" value="<?php echo $fromdate;?>"   placeholder="From Date" class="datepicker form-control" /></td>
           <td><input type="text" name="todate" id="todate"  value="<?php echo $todate;?>" placeholder="To Date" class="datepicker form-control"/></td>
           <td>
           	<button class="btn btn-dark">Go</button>
           </td>
          </tr>
</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">
		 <form name="frmlisting" id="frmlisting" action="<?php echo base_url()."account/".$mgmtname;?>" method="post" onsubmit=" return checkboxvalidate();">
                <thead>
                	<tr>
                    	<th width="30%"></th>                    	
                    	<th width="15%" colspan="4" class="text-center">Opening</th>
                    	<th width="15%" colspan="4" class="text-center">Received</th>
                    	<th width="15%" colspan="4" class="text-center">Sold</th>
                    	<th width="15%" colspan="4" class="text-center">Balance</th>
                    	<th width="10%"></th>
                    </tr>
                	<tr>
                    	<th>Item Name</th>                    	
                    	<th>180 ML</th>
                    	<th>375 ML</th>
                    	<th>750 ML</th>
                    	<th>OTHER</th>
                    	<th>180 ML</th>
                    	<th>375 ML</th>
                    	<th>750 ML</th>
                    	<th>OTHER</th>
                    	<th>180 ML</th>
                    	<th>375 ML</th>
                    	<th>750 ML</th>
                    	<th>OTHER</th>
                    	<th>180 ML</th>
                    	<th>375 ML</th>
                    	<th>750 ML</th>
                    	<th>OTHER</th>
                    	<th class="text-right">Sale</th>
                    </tr>
                    </thead>
                    <tbody>
					<?php
					if(is_array($results))
					{
						$grandtotalopeningqts = 0;
						$grandtotalopeningpts = 0;
						$grandtotalopeningnip = 0;
						$grandtotalreceivedqts = 0;
						$grandtotalreceivedpts = 0;
						$grandtotalreceivednip = 0;
						$grandtotalsoldqts = 0;
						$grandtotalsoldpts = 0;
						$grandtotalsoldnip = 0;
						$grandbalanceqts = 0;
						$grandbalancepts = 0;
						$grandbalancenip = 0;
						$grandtotal = 0;
						foreach($results as $mainkey=>$mainrow)
						{
					?>
					<tr>
						<td colspan="18"><strong><?php echo $mainkey;?></strong></td>
					</tr>
					<?php
							$rowindex = 0;
							$instock = 0;
							$grouptotalopeningqts = 0;
							$grouptotalopeningpts = 0;
							$grouptotalopeningnip = 0;
							$grouptotalopeningother = 0;
							$grouptotalreceivedqts = 0;
							$grouptotalreceivedpts = 0;
							$grouptotalreceivednip = 0;
							$grouptotalreceivedother = 0;
							$grouptotalsoldqts = 0;
							$grouptotalsoldpts = 0;
							$grouptotalsoldnip = 0;
							$grouptotalsoldother = 0;
							$groupbalanceqts = 0;
							$groupbalancepts = 0;
							$groupbalancenip = 0;
							$groupbalanceother = 0;
							$grouptotal = 0;
							foreach($mainrow as $vkey=>$row)
							{
								$rowindex++;
								$minstockvalue = minstockStatus($row['minstockvalue'],$row['instock']);
								
								$balanceqts = $row["opening"]["180 ML"] + $row["received"]["180 ML"] - $row["sold"]["180 ML"];
								$balancepts = $row["opening"]["375 ML"] + $row["received"]["375 ML"] - $row["sold"]["375 ML"];
								$balancenip = $row["opening"]["750 ML"] + $row["received"]["750 ML"] - $row["sold"]["750 ML"];
								$balanceother = $row["opening"]["OTHER"] + $row["received"]["OTHER"] - $row["sold"]["OTHER"];
								
								$saleamount = round((($row["sold"]["180 ML"] * $row["price"]["180 ML"]) + ($row["sold"]["375 ML"] * $row["price"]["375 ML"]) + ($row["sold"]["750 ML"] * $row["price"]["750 ML"]) + ($row["sold"]["OTHER"] * $row["price"]["OTHER"])), 2);
								$grouptotal += $saleamount;
								
								$grouptotalopeningqts += $row["opening"]["180 ML"];
								$grouptotalopeningpts += $row["opening"]["375 ML"];
								$grouptotalopeningnip += $row["opening"]["750 ML"];
								$grouptotalopeningother += $row["opening"]["OTHER"];
								$grouptotalreceivedqts += $row["received"]["180 ML"];
								$grouptotalreceivedpts += $row["received"]["375 ML"];
								$grouptotalreceivednip += $row["received"]["750 ML"];
								$grouptotalreceivedother += $row["received"]["OTHER"];
								$grouptotalsoldqts += $row["sold"]["180 ML"];
								$grouptotalsoldpts += $row["sold"]["375 ML"];
								$grouptotalsoldnip += $row["sold"]["750 ML"];
								$grouptotalsoldother += $row["sold"]["OTHER"];
								$groupbalanceqts += $balanceqts;
								$groupbalancepts += $balancepts;
								$groupbalancenip += $balancenip;
								$groupbalanceother += $balanceother;
					?>
                    <tr>
                    	<td><?php echo $vkey;?></td>
                    	<td><?php echo $row["opening"]["180 ML"];?></td>
                    	<td><?php echo $row["opening"]["375 ML"];?></td>
                    	<td><?php echo $row["opening"]["750 ML"];?></td>
                    	<td><?php echo $row["opening"]["OTHER"];?></td>
                    	<td><?php echo $row["received"]["180 ML"];?></td>
                    	<td><?php echo $row["received"]["375 ML"];?></td>
                    	<td><?php echo $row["received"]["750 ML"];?></td>
                    	<td><?php echo $row["received"]["OTHER"];?></td>
                    	<td><?php echo $row["sold"]["180 ML"];?></td>
                    	<td><?php echo $row["sold"]["375 ML"];?></td>
                    	<td><?php echo $row["sold"]["750 ML"];?></td>
                    	<td><?php echo $row["sold"]["OTHER"];?></td>
                    	<td><?php echo $balanceqts;?></td>
                    	<td><?php echo $balancepts;?></td>
                    	<td><?php echo $balancenip;?></td>
                    	<td><?php echo $balanceother;?></td>
                    	<td class="text-right"><?php echo $saleamount;?></td>
                    </tr>
                    <?php
							}
					?>
                    <tr>
                    	<td>Group Totals</td>
                    	<td><?php echo $grouptotalopeningqts;?></td>
                    	<td><?php echo $grouptotalopeningpts;?></td>
                    	<td><?php echo $grouptotalopeningnip;?></td>
                    	<td><?php echo $grouptotalopeningother;?></td>
                    	<td><?php echo $grouptotalreceivedqts;?></td>
                    	<td><?php echo $grouptotalreceivedpts;?></td>
                    	<td><?php echo $grouptotalreceivednip;?></td>
                    	<td><?php echo $grouptotalreceivedother;?></td>
                    	<td><?php echo $grouptotalsoldqts;?></td>
                    	<td><?php echo $grouptotalsoldpts;?></td>
                    	<td><?php echo $grouptotalsoldnip;?></td>
                    	<td><?php echo $grouptotalsoldother;?></td>
                    	<td><?php echo $groupbalanceqts;?></td>
                    	<td><?php echo $groupbalancepts;?></td>
                    	<td><?php echo $groupbalancenip;?></td>
                    	<td><?php echo $groupbalanceother;?></td>
                    	<td class="text-right"><?php echo $grouptotal;?></td>
                    </tr>
					<?php
							$grandtotalopeningqts += $grouptotalopeningqts;
							$grandtotalopeningpts += $grouptotalopeningpts;
							$grandtotalopeningnip += $grouptotalopeningnip;
							$grandtotalopeningother += $grouptotalopeningother;
							$grandtotalreceivedqts += $grouptotalreceivedqts;
							$grandtotalreceivedpts += $grouptotalreceivedpts;
							$grandtotalreceivednip += $grouptotalreceivednip;
							$grandtotalreceivedother += $grouptotalreceivedother;
							$grandtotalsoldqts += $grouptotalsoldqts;
							$grandtotalsoldpts += $grouptotalsoldpts;
							$grandtotalsoldnip += $grouptotalsoldnip;
							$grandtotalsoldother += $grouptotalsoldother;
							$grandbalanceqts += $groupbalanceqts;
							$grandbalancepts += $groupbalancepts;
							$grandbalancenip += $groupbalancenip;
							$grandbalanceother += $groupbalanceother;
							$grandtotal += $grouptotal;					
						}
					?>
                    <tr>
                    	<td>Grand Totals</td>
                    	<td><?php echo $grandtotalopeningqts;?></td>
                    	<td><?php echo $grandtotalopeningpts;?></td>
                    	<td><?php echo $grandtotalopeningnip;?></td>
                    	<td><?php echo $grandtotalopeningother;?></td>
                    	<td><?php echo $grandtotalreceivedqts;?></td>
                    	<td><?php echo $grandtotalreceivedpts;?></td>
                    	<td><?php echo $grandtotalreceivednip;?></td>
                    	<td><?php echo $grandtotalreceivedother;?></td>
                    	<td><?php echo $grandtotalsoldqts;?></td>
                    	<td><?php echo $grandtotalsoldpts;?></td>
                    	<td><?php echo $grandtotalsoldnip;?></td>
                    	<td><?php echo $grandtotalsoldother;?></td>
                    	<td><?php echo $grandbalanceqts;?></td>
                    	<td><?php echo $grandbalancepts;?></td>
                    	<td><?php echo $grandbalancenip;?></td>
                    	<td><?php echo $grandbalanceother;?></td>
                    	<td class="text-right"><?php echo round($grandtotal);?></td>
                    </tr>
					<?php
							
					}
					else
					{
						if($this->input->get("outletid")=="")
						{
						?>
						<tr><td colspan="18" style="text-align:center">Please select outlet for stock summary</td></tr>
						<?php

						}
						else
						{
                    ?>
                    <tr><td colspan="18" style="text-align:center">Record not found!</td></tr>
                    <?php
						}
					}
                    ?>
                    
                    
                    </tbody>
                </table>
                 
	         <div class="clearfix"></div>
          
        </div>

            
        </div>

