<?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-5">
            <h3>Manage Stock Status</h3>
          </div>
          <div class="col-md-7 text-right">
			<!--<a href="outlet/<?php echo $mgmtname;?>/exportcsv/?<?php echo $_SERVER['QUERY_STRING'];?>" class="btn btn-info">Export CSV</a>//-->
			<!--<a href="outlet/<?php echo $mgmtname;?>/exportpdf/?<?php echo $_SERVER['QUERY_STRING'];?>" class="btn btn-info">Export PDF</a>
			<a href="outlet/<?php echo $mgmtname;?>/exportpdftypewise/?<?php echo $_SERVER['QUERY_STRING'];?>" class="btn btn-info">Export Type Wise PDF</a>//-->
          </div>
        </div>
        
        <div class="container_fluid mt-2 px-3">
		<form name="frmsearch" method="get" action="outlet/<?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 name,barcode "></td>
          
		<td width="15%">
		  <select name="type" id="type" class="js-example-basic-single">
		  <option value="">All Type</option>
						<?php
							
							foreach(arrProductType() as $ptkey => $ptval)
							{
								$sel_pt ="";
								if($this->input->get("type")==$ptkey)
								{
									$sel_pt = 'selected="selected"';
								}				
									?>
											<option value="<?php echo $ptkey;?>" <?php echo $sel_pt;?>><?php echo $ptval;?></option>
									<?php
								
							}
						
						?> 
		  </select>
		  </td>
          <td class="pr-0" width="20%">
		<select name="catid" id="catid" class="js-example-basic-single" >
		  <option value="">All Category</option>
						<?php
						if(is_array($catlist) && count(array_filter($catlist))>0)
						{

							foreach($catlist as $mcatrow)
							{
								$sel_mcat ="";
								if(decodeValue($this->input->get("catid"))==$mcatrow->fld_id)
								{
									$sel_mcat = 'selected="selected"';
								}				
									?>
											<option value="<?php echo encodeValue($mcatrow->fld_id);?>" <?php echo $sel_mcat;?>><?php echo $mcatrow->fld_name;?></option>
									<?php
								
							}
						}
						?> 
		  </select>
		  </td>

         <td class="pr-0" width="20%">
		<select name="brandid" id="brandid" class="js-example-basic-single" >
		  <option value="">All Brand</option>
						<?php
						if(is_array($brandlist) && count(array_filter($brandlist))>0)
						{
							foreach($brandlist as $brandrow)
							{
								$sel_brand ="";
								if(decodeValue($this->input->get("brandid"))==$brandrow->fld_id)
								{
									$sel_brand = 'selected="selected"';
								}				
									?>
											<option value="<?php echo encodeValue($brandrow->fld_id);?>" <?php echo $sel_brand;?>><?php echo $brandrow->fld_name;?></option>
									<?php
								
							}
						}
						?> 
		  </select>
		  </td>      
           <td class="pr-0" width="10%"><input type="text" name="fromdate" id="fromdate" value="<?php echo $fromdate;?>"   placeholder="From Date" class="datepicker"  style="width:100px;"/></td>
           <td class="pr-0" width="10%"><input type="text" name="todate" id="todate"  value="<?php echo $todate;?>" placeholder="To Date" class="datepicker" style="width:100px;"/></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 style="vertical-align:top;">
                    	<th width="5%" style="vertical-align:top;">SRNo.</th>                    	
                    	<th width="15%" style="vertical-align:top;">Item</th>
						<th width="9%" style="vertical-align:top;">Opening<br>Stock</th>
                    	<th width="9%" style="vertical-align:top;" class="text-center">Stock<br> IN</th>
                    	<th width="9%" style="vertical-align:top;" class="text-center">Stock<br> Transferred</th>
                    	<th width="9%"  style="vertical-align:top;"class="text-center">Sold<br> Qty</th>
                    	<th width="8%"  style="vertical-align:top;" class="text-center">Mrp</th>
                    	<th width="9%" style="vertical-align:top;" class="text-center">Mrp<br> Value</th>
                    	<th width="9%" style="vertical-align:top;" class="text-center">Avg.<br> Selling<br> Price</th>
                    	<th width="9%" style="vertical-align:top;" class="text-center">Sold<br> Value</th>
                        <th width="9%"style="vertical-align:top;" class="text-right">Stock<br> Left</th>
                    </tr>
                    </thead>
                    <tbody>
					<?php
					//predata($results);
					if(is_array($results) && count(array_filter($results))>0)
					{
						$rowindex = 0;
						$instock = 0;
						$tot_previousstock =0;
						$tot_incoming =0;
						$tot_stocktransfer =0;
						$tot_soldqty =0;
						$tot_mrp =0;
						$tot_mrpvalue =0;
						$tot_avgprice =0;
						$tot_soldvalue =0;
						$tot_instock =0;
						foreach($results as $row)
						{
							$rowindex++;
							$minstockvalue = minstockStatus($row['minstockvalue'],$row['instock']);
							$soldqty = $row['soldqty'];
							$mrp = $row['mrp'];
							$mrpvalue = ($soldqty*$mrp);
							$avgprice = 0;
							$soldvalue = 0;
							if($row['totprice']>0 && $row['soldqty']>0)
							{
								$avgprice = ($row['totprice']/$soldqty);
								$soldvalue = ($soldqty*$avgprice);
							}
							
							

					?>
                    <tr>
                    	<td class="text-center"><?php echo $rowindex;?>.</td>
                    	<td><a href="outlet/orders/product/<?php echo encodeValue($row['id']).''. $addpmtr;?>"><?php echo $row['name'];?></a></td>
						<td  class="text-center"><?php echo round($row['previousstock'],2);?></td>
						<td  class="text-center"><?php echo round($row['incoming'],2);?></td>
                    	<td  class="text-center"><?php echo round($row['stocktransfer'],2);?></td>             
                    	<td  class="text-center">
						<?php
						if($soldqty>0)
						{
						?>
						<a href="outlet/orders/product/<?php echo encodeValue($row['id']).''. $addpmtr;?>"><?php echo round($soldqty,2);?></a>
						<?php
						}
						else
							{
							echo $soldqty;
							}
							?>
						</td>             
                    	<td  class="text-center"><?php echo round($mrp,2);?></td>             
                    	<td  class="text-center"><?php echo round($mrpvalue,2);?></td>            
                    	<td  class="text-center"><?php echo round($avgprice,2);?> </td>            
                    	<td  class="text-center"><?php echo round($soldvalue,2);?></td>            
                    	              
                      
						<td class="text-right "> <span class="btn btn-<?php echo arrOSColor()[$minstockvalue];?> "><strong><?php echo $row['instock'];?></strong></span> </td>
                    </tr>
                    <?php
							$tot_previousstock +=$row['previousstock'];
							$tot_incoming +=$row['incoming'];
							$tot_stocktransfer +=$row['stocktransfer'];
							$tot_soldqty +=$soldqty;
							$tot_mrp +=$mrp;
							$tot_mrpvalue +=$mrpvalue;
							$tot_avgprice +=$avgprice;
							$tot_soldvalue +=$soldvalue;
							$tot_instock +=$row['instock'];
						}
						?>
                    <!--<tr>
                    	
                    	<th colspan="2"><strong>Total</strong></th>
						<th  class="text-center"><?php echo round($tot_previousstock,2);?></th>
						<th  class="text-center"><?php echo round($tot_incoming,2);?></th>
						<th  class="text-center"><?php echo round($tot_stocktransfer,2);?></th>
						<th  class="text-center"><?php echo round($tot_soldqty,2);?></th>
						<th  class="text-center"><?php echo round($tot_mrp,2);?></th>
						<th  class="text-center"><?php echo round($tot_mrpvalue,2);?></th>
						<th  class="text-center"><?php echo round($tot_avgprice,2);?></th>
						<th  class="text-center"><?php echo round($tot_soldvalue,2);?></th>
						<th class="text-right "> <strong><?php echo $tot_instock;?></strong></th>
                    </tr>//-->
						<?php
							
					}
					else
					{
						
                    ?>
                    <tr><td colspan="11" style="text-align:center">Record not found!</td></tr>
                    <?php
						
					}
                    ?>
                    
                    
                    </tbody>
                </table>
                 
	         <div class="clearfix"></div>
          
        </div>
        <div id="paginate" class="row">
			<?php echo $this->ajax_pagination->create_links(); ?>
        </div>
            
        </div>

