<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$addpmtr = "?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>Average Sale Report</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="account/<?php echo $mgmtname;?>">
         <table class="webforms sttbl bg-light my-0 table-responsive-sm">
          <tr>
           <td class="pr-0" width="20%"><input type="text" name="search" size="20" value="<?php echo $this->input->get("search");?>" placeholder="search by name"></td>
           <td class="pr-0" width="20%"><input type="text" name="fromdate" id="fromdate" value="<?php echo $this->input->get("fromdate");?>"   placeholder="From Date" class="datepicker" /></td>
           <td class="pr-0" width="20%"><input type="text" name="todate" id="todate"  value="<?php echo $this->input->get("todate");?>" placeholder="To Date" class="datepicker"/></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
			}
			if($this->input->get("fromdate")!="" && $this->input->get("todate")!="")
			{
         ?>
         <div class="text-center my-4">
			 <strong>From <?php echo $this->input->get("fromdate");?> To <?php echo $this->input->get("todate");?></strong>
         </div>
         <?php
			}
         ?>
         <table class="table mt-2 table-responsive-sm">
		 
                <thead>
                	<tr>
                    	<th width="5%">Serial</th>                    	
                    	<th width="20%">Item</th>
                    	<?php
                    	$arroutlets = array();
                    	$arrwarehouses = array();
                    	if($outletslist)
                    	{
							if(count(array_filter($outletslist))>0)
							{
								foreach($outletslist as $row)
								{
									$arroutlets[] = $row->fld_id;
									echo '<th class="text-right">' . $row->fld_name . ' (Qty.)</th>';
								}
							}
						}
                    	if($warehouselist)
                    	{
							if(count(array_filter($warehouselist))>0)
							{
								foreach($warehouselist as $row)
								{
									$arrwarehouses[] = $row->fld_id;
									echo '<th class="text-right">' . $row->fld_name . ' (Qty.)</th>';
								}
							}
						}
                    	?>
                    	<th class="text-right">Total</th>
                    </tr>
                    </thead>
                    <tbody>
					<?php
					if(is_array($results["products"]) && count(array_filter($results["products"]))>0)
					{
						$rowindex = 0;
						foreach($results["products"] as $row)
						{
							$itemqtytotal = 0;
							$rowindex++;
							$productid = $row["fld_id"];
					?>
                    <tr>
                    	<td class="text-center"><?php echo $rowindex;?>.</td>
                    	<td><?php echo $row["fld_name"];?></td>
                    	<?php
                    	if(count(array_filter($arroutlets))>0)
                    	{
							for($ix=0;$ix<count(array_filter($arroutlets));$ix++)
							{
								$qty = 0;
								$outletid = $arroutlets[$ix];
								if($results["outlets"][$outletid][$productid])
								{
									$qty = $results["outlets"][$outletid][$productid]["qty"];
								}
								$itemqtytotal += $qty;
								echo '<td class="text-right">' . $qty . '</td>';
							}
						}
                    	if(count(array_filter($arrwarehouses))>0)
                    	{
							for($ix=0;$ix<count(array_filter($arrwarehouses));$ix++)
							{
								$qty = 0;
								$warehouseid = $arrwarehouses[$ix];
								if($results["warehouses"][$warehouseid][$productid])
								{
									$qty = $results["warehouses"][$warehouseid][$productid]["qty"];
								}
								$itemqtytotal += $qty;
								echo '<td class="text-right">' . $qty . '</td>';
							}
						}
                    	?>
						<td class="text-right "><strong><?php echo $itemqtytotal;?></strong></td>
                    </tr>
                    <?php
						}
							
					}
					else
					{
                    ?>
                    <tr><td colspan="3" style="text-align:center">Record not found!</td></tr>
                    <?php
					}
                    ?>
                    
                    
                    </tbody>
                </table>
                 
	         <div class="clearfix"></div>
          
        </div>
  
        </div>

