<style>
    .nopadding{
        padding:0;
    }
    .box_head{
        font-size:20px;
    }
    
    .table>thead>tr>th, .table>tbody>tr>th, .table>tfoot>tr>th, .table>thead>tr>td, .table>tbody>tr>td, .table>tfoot>tr>td {
        display: table-cell !important;
        text-align: center !important;
    }
    @media print{
        .table-bordered>thead>tr>th, .table-bordered>tbody>tr>th, .table-bordered>tfoot>tr>th, .table-bordered>thead>tr>td, .table-bordered>tbody>tr>td, .table-bordered>tfoot>tr>td {
            border-right: 1px solid #ddd !important;
            -webkit-print-color-adjust: exact !important;
        }
        .table>thead>tr>th, .table>tbody>tr>th, .table>tfoot>tr>th, .table>thead>tr>td, .table>tbody>tr>td, .table>tfoot>tr>td {
            display: table-cell !important;
            text-align: center !important;
        }
    }
</style>
<link href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css" rel="stylesheet"/>

<!--<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>-->
<script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
    $('#example').DataTable( {
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        "footerCallback": function ( row, data, start, end, display ) {
            var api = this.api(), data;
    
            // Remove the formatting to get integer data for summation
            var intVal = function ( i ) {
                return typeof i === 'string' ?
                    i.replace(/[\$,]/g, '')*1 :
                    typeof i === 'number' ?
                        i : 0;
            };
 
            // Total over all pages
            total = api
                .column( 4 )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
            totale = api
                .column( 5 )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
            totales = api
                .column( 6 )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
 
 
            // Total over this page
            pageTotal = api
                .column( 4, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
 
            pageTotale = api
                .column( 5, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
             pageTotales = api
                .column( 6, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
            
 
            // Update footer
            $( api.column(4 ).footer() ).html(
                'SAR'+pageTotal +''
            );
             // Update footer
            $( api.column(5 ).footer() ).html(
                ' '+pageTotale +''
            );
           $( api.column(6 ).footer() ).html(
                'SAR'+pageTotales +''
            );
        }
    } );
} );
</script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#form').hide();
        $('.toggle_down').click(function () {
            $("#form").slideDown();
            return false;
        });
        $('.toggle_up').click(function () {
            $("#form").slideUp();
            return false;
        });

    });
</script>
<div class="box">
    <div class="box-header">
        <h2 class="blue"><i class="fa-fw fa fa-money"></i><?= lang('stock_counts'); ?></h2>
        
        <div class="box-icon">
            <ul class="btn-tasks">
                <li class="dropdown">
                    <a href="#" class="toggle_up tip" title="<?= lang('hide_form') ?>">
                        <i class="icon fa fa-toggle-up"></i>
                    </a>
                </li>
                <li class="dropdown">
                    <a href="#" class="toggle_down tip" title="<?= lang('show_form') ?>">
                        <i class="icon fa fa-toggle-down"></i>
                    </a>
                </li>
            </ul>
        </div>
    </div><?php //print_r($product);die();?>
    
    <div class="box-content">
        <div class="row">
            <div class="col-lg-12">
                <div class="pull-right">
                    <button type="button" class="no-print btn btn-primary" onclick="window.print();">Print</button>
                </div>
            </div>
            <div class="col-lg-12 nopadding box_head">
                <span class="col-md-4 text-left"><b>Route: </b></span>
                <?php 
                if($product){
                foreach($product as $row){
                
                $warehouse_name = $this->db->get_where('warehouses',array('id'=>$row->warehouse_id))->row()->name;
                
                }?>
                <span class="col-md-4 text-center"><b>Warehouse:<?= $warehouse_name?></b></span>
                <?php } 
                else {
                    ?>
                     <span class="col-md-4 text-center"><b>Warehouse:</b></span>
                    <?php 
                }?>
                <span class="col-md-4 text-right"><b>Date:<?php if ($this->input->post('start_date')) {
                echo "From " . $this->input->post('start_date') . " to " . $this->input->post('end_date');
            }?> </b></span>
            </div>
            <div class="col-lg-6 upper_portion">
              
            </div>
            <div class="col-lg-6 upper_portion">
            
               
            </div>
            <div class="col-lg-12">
                <div id="form">

                    <?php echo admin_form_open("reports/daily_sales_product"); ?>
                    <div class="row">
                       
                       
                        <div class="col-sm-4">
                            <div class="form-group">
                                <label class="control-label" for="warehouse"><?= lang("warehouse"); ?></label>
                                <?php
                                $wh[""] = lang('select').' '.lang('warehouse');
                                foreach ($warehouses as $warehouse) {
                                    $wh[$warehouse->id] = $warehouse->name;
                                }
                                echo form_dropdown('warehouse', $wh, (isset($_POST['warehouse']) ? $_POST['warehouse'] : ""), 'class="form-control" id="warehouse" data-placeholder="' . $this->lang->line("select") . " " . $this->lang->line("warehouse") . '"');
                                ?>
                            </div>
                        </div>
                        
                      <div class="col-sm-4">
                            <div class="form-group">
                                <?= lang("start_date", "start_date"); ?>
                                <?php echo form_input('start_date', (isset($_POST['start_date']) ? $_POST['start_date'] : ""), 'class="form-control datetime" id="start_date"'); ?>
                            </div>
                        </div>
                        <div class="col-sm-4">
                            <div class="form-group">
                                <?= lang("end_date", "end_date"); ?>
                                <?php echo form_input('end_date', (isset($_POST['end_date']) ? $_POST['end_date'] : ""), 'class="form-control datetime" id="end_date"'); ?>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <div
                            class="controls"> <?php echo form_submit('submit_report', $this->lang->line("submit"), 'class="btn btn-primary"'); ?> </div>
                    </div>
                    <?php echo form_close(); ?>

                </div>
                <?php if (!empty($warehouse_product)){?>
                <div class="table-responsive text-center">
                  <table id="example1" class="table table-bordered display" style="width:100%">
                       <thead>
                           <th>#</th>
                           <th style="display: tebel-cell !important;">Item</th>
                           <th>Op Stock</th>
                           <th>SR</th>
                           <th>Bal Stock</th>
                           <th>SR</th>
                           <th>Sale</th>
                           <th>Purchase</th>
                           <th>Total</th>
                       </thead>
                       <tbody>
                          <?php   $grand=$op_sum =$unit_price=$grand_tot=$price=$op_stck_tot_sum=$op_stck_tot_sums=$pur_stock_qty=$grand_total=$bal_sum_tot=$ba_sum_ts=$opr_sum=$opr_sum_r=  $op_stock_qty=$cur_stock_qty=	$sale_stock_qty=0;
                         //print_r($daily_product);die();
                       
                          if($daily_product)
					    {
					        $countS =1;
					       // $date=date("Y-m-d");
							$count =sizeof($brands);
							for($i=0;$i< $count;$i++)
							{
							    
							     $open_stock_sum=$bal_stock_sum= $sale_qty_sum=$summ =$bal_stock_sumst= $sale_sum = $bal_stock_sum1s= $bal_sum=$sale_qtyss=$sal_tot_gran=$op_stck_tot_sum=$bal_stock_sums=$bal_stock_sum1=0;
							    $op_bal=$cur_bal=$la_sum=0;
							     foreach($daily_product as $stock){
							       
							          
						
									if(($stock->brand) == ($brands[$i]) && ($stock->brand) != 0)
									{
									        
									  $product_id = $stock->product_id;
									   $this->db->select('SUM(c.quantity) as sale_quanty');
            $this->db->from('sales as s');
            $this->db->join('costing as c', ' c.sale_id = s.id', 'left');
            $this->db->where('s.warehouse_id' , $warehouse_product);
            $this->db->where('c.product_id' , $product_id);
            $this->db->where('s.date BETWEEN "'. $sdate. '" and "'.$date.'"');
            $query = $this->db->get();
             if ($query->num_rows() > 0){
            $sal = $query->row();
            $sale_qt = $sal->sale_quanty;
            //print_r($sal->sale_quanty);
             }
             else{
              $sale_qt = 0;   
             }
									 $open_stock_sum +=  $this->sma->formatQuantity($stock->previous_stock + $stock->purchase_quantity) ;
									 if(($stock->current_stock)<= 0){
									      $bal_stock_sums = 0 ;
									 }
									 
									  if(($stock->current_stock) > 0){//print_r($stock->current_stock );
									  $bal_s = $stock->current_stock - $stock->purchase_quantity;
									  if($bal_s == 0){
									  $bal_stock_sum1s =0 ;    
									  }
									  if($bal_s > 0){
									 $bal_stock_sum1 += $this->sma->formatQuantity($bal_s);
									  }
									 }
									 //$bal_stock_sum = $bal_stock_sum1+$bal_stock_sums + $bal_stock_sumst + $bal_stock_sum1s ;
								     $sale_qty_sum += $this->sma->formatQuantity($sale_qt);
								      $unit_price =  $this->sma->formatQuantity($stock->unit_price);
								     
                          ?>
                         <?php 
            $op_bal = $stock->previous_stock + $stock->purchase_quantity;
			$cur_bal = $op_bal - $sale_qt;
		/*	if(	$cur_bal_p < 0){
			    $cur_bal = $cur_bal + $stock->purchase_quantity;
			}
			else{
			    $cur_bal = $op_bal - $stock->sale_quantity;
			}*/
			$pur_bal =$stock->purchase_quantity;
			$bal_stock_sum +=$cur_bal;
				$op_stock_qty += $op_bal;
							                $cur_stock_qty+= $cur_bal;
							                $sale_stock_qty+= $sale_qt; 
							                $pur_stock_qty+= $stock->purchase_quantity; 
                      
                           ?>
                           <tr>
                            <td><?=$countS++;?></td>
                               <td><?=$stock->product_name;?></td>
                              
                               <td><?=$this->sma->formatQuantity($op_bal);?></td>
                               <td></td>
                           <td><?=$this->sma->formatQuantity($cur_bal);?></td>
                              
                               <td></td>
                               <?php if($sale_qt){?>
                               <td><?=$this->sma->formatQuantity($sale_qt);?></td>
                               <?php }
                               else {
                                   ?>
                                     <td>0.00</td>
                                   <?php 
                               }
                               ?>
                              <td><?=$this->sma->formatQuantity($stock->purchase_quantity);?></td>
                               <td><?=$this->sma->formatQuantity($cur_bal);?></td>
                           </tr>
                          <?php 
                          $op_sum = $open_stock_sum * $unit_price;
                          $la_sum = $bal_stock_sum * $unit_price;
                          $sale_sum = $sale_qty_sum *$unit_price;
							$op_stck_tot_sum+= $op_sum;
							
						
									}   
								
							     }//print_r($op_stock_qty);die();
							     if(($stock->brand) != 0){
                      ?>
                 
                           <tr style="border: 2px solid #ccc;">
                               <td></td>
                               <td></td>
                               <td><?=$this->sma->formatQuantity($open_stock_sum);?> * <?=$this->sma->formatQuantity($unit_price);?></td>
                               
                               <td><?=$op_sum;?></td>
                               <td><?=$this->sma->formatQuantity($bal_stock_sum);?> * <?=$this->sma->formatQuantity($unit_price);?></td>
                               <td><?=$la_sum;?></td>
                               <td></td>
                               <td></td>
                               <td></td>
                           </tr>
                        
                           <?php  
							   $grand+= $sale_sum ;
							   $bal_sum_tot +=$la_sum;
							   $opr_sum+=$op_sum;
							     }
							}
							
				
                           }          
                          else
	{
		
		$countn =1;$op_stock_qtyS=0;
		$counting =sizeof($brands);
		for($s=0;$s< $counting;$s++)
		{
		    $open_stock_sum_r=$op_sum_r=$la_sum_r=$sale_sum_r=$grand_r=$bal_sum_tot_r=$op_stck_tot_sum_r=0;
			foreach($product as $stocks)
			{
				if(($stocks->brand) == ($brands[$s])&& ($stocks->brand) != 0)
				{
						$open_stock_sum_r+=  $this->sma->formatQuantity($stocks->qty) ;
					  $unit_price =  $this->sma->formatQuantity($stocks->price);
					  $op_stock_qtyS+=$stocks->qty;
					?>
					<tr>
						<td><?=$countn++;?></td>
						<td><?=$stocks->name;?></td>
						<td><?=$this->sma->formatQuantity($stocks->qty);?></td>
						<td></td>
						<td>0.00</td>
						<td></td>
						<td>0.00</td>
						<td>0.00</td>
						<td>0.00</td>
					</tr>
					<?php 
				
							$op_sum_r = $open_stock_sum_r * $unit_price;
							$la_sum_r = 0.00 * $unit_price;
							$sale_sum_r = 0.00 *$unit_price;
							$op_stck_tot_sum_r+= $op_sum_r;
							//$stock_open+=$stocks->qty;
								
							
				}   
			}	
                    ?>
                    
                        <tr style="border: 2px solid #ccc;">
                               <td></td>
                               <td></td>
                               <td><?=$this->sma->formatQuantity($open_stock_sum_r);?> * <?=$this->sma->formatQuantity($unit_price);?></td>
                               
                               <td><?=$this->sma->formatQuantity($op_sum_r);?></td>
                               <td>0.00 * <?=$this->sma->formatQuantity($unit_price);?></td>
                               <td><?=$this->sma->formatQuantity($la_sum_r);?></td>
                               <td></td>
                               <td></td>
                               <td></td>
                           </tr>
                        
                           <?php  
							   $grand_r+= $sale_sum_r;
							   $bal_sum_tot_r +=$la_sum_r;
							  $opr_sum_r+=$op_sum_r;
		}
							
					
    }  
     
                        if($daily_product)
					    {
					       // $date=date("Y-m-d");
						  $op_stock_qty_c=$cur_stock_qty_c=$sale_stock_qty_c=$op_bal_c=$cur_bal_c=$pur_bal_c=$bal_stock_sum_c=$pur_stock_qty_c=0;
							      $open_stock_sums=$bal_stock_sums= $sale_qty_sums=$summs = $sale_sums =  $bal_sums=$grand_tots=$grnd_totl=$sal_tot_grans=$grand_total=$op_stck_tot_sums=0;
							     
							      foreach($daily_product as $rows)
							      {
							        
							         
								       
									if(($rows->brand)==0)
									{
									     $product_ids = $rows->product_id;
									       $this->db->select('SUM(c.quantity) as sale_quanty');
            $this->db->from('sales as s');
            $this->db->join('costing as c', ' c.sale_id = s.id', 'left');
            $this->db->where('s.warehouse_id' , $warehouse_product);
            $this->db->where('c.product_id' , $product_ids);
            $this->db->where('s.date BETWEEN "'. $sdate. '" and "'.$date.'"');
            $query = $this->db->get();
             if ($query->num_rows() > 0){
            $sals = $query->row();
            $sale_qts = $sals->sale_quanty;
           // print_r($sal->sale_quanty);
             }
             else{
              $sale_qts = 0;   
             }
									    
								      
							 $op_bal_c = $rows->previous_stock + $rows->purchase_quantity;
			$cur_bal_c =  $op_bal_c - $sale_qts;
			$pur_bal_c =$rows->purchase_quantity;
			$bal_stock_sum_c +=$cur_bal_c;	  
			
				$op_stock_qty_c+= $op_bal_c;//print_r($op_stock_qty_c);
							$cur_stock_qty_c+= $cur_bal_c;
							$sale_stock_qty_c+=$sale_qts;
							 $pur_stock_qty_c+= $rows->purchase_quantity; 
                          ?>
                        
                          <tr>
                            <td><?=$countS++;?></td>
                               <td><?=$rows->product_name;?></td>
                              
                               <td><?=$this->sma->formatQuantity($rows->previous_stock + $rows->purchase_quantity);?></td>
                               <td></td>
                           
                         <?php $bala = $rows->current_stock - $rows->purchase_quantity;?>
                               <td><?=$this->sma->formatQuantity($cur_bal_c);?></td>
                              
                               <td></td>
                               <?php if($sale_qts){?>
                               <td><?=$this->sma->formatQuantity($sale_qts);?></td>
                               <?php }
                               else {
                                   ?>
                                     <td>0.00</td>
                                   <?php 
                               }
                               ?>
                              <td><?=$this->sma->formatQuantity($rows->purchase_quantity);?></td>
                               <td><?=$this->sma->formatQuantity($cur_bal_c);?></td>
                           </tr>
                          
                      <?php 
                      
                      $previous_stock_sum = $this->sma->formatQuantity($rows->previous_stock + $rows->purchase_quantity) * $this->sma->formatQuantity($rows->unit_price);
                      $current_stock_sum = $this->sma->formatQuantity($cur_bal_c) * $this->sma->formatQuantity($rows->unit_price);
                      $sale_quantity_sum = $this->sma->formatQuantity($sale_qts) * $this->sma->formatQuantity($rows->unit_price);
                        
                      ?>
                    
                           <tr style="border: 2px solid #ccc;">
                               <td></td>
                               <td></td>
                               <td><?=$this->sma->formatQuantity($op_bal_c);?> * <?=$this->sma->formatQuantity($rows->unit_price);?></td>
                               
                               <td><?php echo $previous_stock_sum;?></td>
                               <td><?=$this->sma->formatQuantity($cur_bal_c);?> * <?=$this->sma->formatQuantity($rows->unit_price);?></td>
                               <td><?php echo $current_stock_sum;?></td>
                               <td></td>
                               <td></td>
                               <td></td>
                           </tr>
                         <?php
                           $grand_total+= $sale_quantity_sum ;
                           $op_stck_tot_sums +=  $previous_stock_sum;
                           $ba_sum_ts+= $current_stock_sum;
                         }
                         }?>
                           <?php  	
                           
					    }   
					     //print_r($grand_tots);
                          // $grand_tot = $grand_total + $grand; 
						else
{
	$ba_sum_ts_r=$previous_stock_sum_r=$current_stock_sum_r=$sale_quantity_sum_r=$grand_total_r=$op_stock_qtys_c=$op_stck_tot_sums_r=0;
	foreach($product as $rowis)
	{
		if(empty($rowis->brand))
		{
		      $op_stock_qtys_c+=$rowis->qty;
			?>
            <tr>
                <td><?=$countn++;?></td>
                <td><?=$rowis->name;?></td>
                <td><?=$this->sma->formatQuantity($rowis->qty);?></td>
                <td></td>
                <td>0.00</td>
                <td></td>
                <td>0.00</td>
                <td>0.00</td>
                <td>0.00</td>
            </tr>
			
			 <?php 
                      
                      $previous_stock_sum_r = $this->sma->formatQuantity($rowis->qty) * $this->sma->formatQuantity($rowis->price);
                      $current_stock_sum_r = 0.00 * $this->sma->formatQuantity($rowis->price);
                      $sale_quantity_sum_r = 0.00 * $this->sma->formatQuantity($rowis->price);
                        $previous_stock_sum_r
						//	$cur_stock_qty_c+=$cur_bal;
						//	$sale_stock_qty_c+=$stock->sale_quantity;
                      ?>
			
            <tr style="border: 2px solid #ccc;">
                <td></td>
                <td></td>
                <td><?=$this->sma->formatQuantity($rowis->qty);?> * <?=$this->sma->formatQuantity($rowis->price);?></td>
                <td>0.00</td>
                <td>0.00</td>
                <td>0.00</td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
                <?php
                        $grand_total_r+= $sale_quantity_sum_r ;
                        $op_stck_tot_sums_r+=  $previous_stock_sum_r;
                        $ba_sum_ts_r+= $current_stock_sum_r;
        }
    }
}	
	?>			
                 
               
                <tr>
                   
                        <td colspan="2">
                        <b>Total  Amount</b>
                    </td>
                     <?php if($opr_sum && $op_stck_tot_sums){
                     
                     ?>
                    <td><b><?=$this->sma->formatQuantity($op_stock_qty_c + $op_stock_qty);?></b></td>
                   
                    <td><b><?=$this->sma->formatQuantity($opr_sum + $op_stck_tot_sums);?></b></td>
                    <td><b><?=$this->sma->formatQuantity($cur_stock_qty_c + $cur_stock_qty);?></b></td>
                    <td><b><?=$this->sma->formatQuantity($bal_sum_tot + $ba_sum_ts);?></b></td>
                    <td><b><?php echo 	$this->sma->formatQuantity($grand + $grand_total);?>(</b><b><?=$this->sma->formatQuantity($sale_stock_qty_c + $sale_stock_qty);?>)</b></td>
                     <td>
                     <b><?=$this->sma->formatQuantity($pur_stock_qty_c + $pur_stock_qty);?></b>
                 </td>
                 <td><b><?=$this->sma->formatQuantity($cur_stock_qty_c + $cur_stock_qty);?></b>
                    </td>
                    <?php }//print_r($op_stck_tot_sum_r);
                     if($opr_sum_r && $op_stck_tot_sums_r){
                        ?>
                    <td><b><?=$this->sma->formatQuantity($op_stock_qtys_c + $op_stock_qtyS);?></b></td>    
                    <td><b><?=$this->sma->formatQuantity($opr_sum_r +  $op_stck_tot_sums_r);?></b></td>
                   <td><b>0.00</b></td>
                   <td></td>
                   <td><b>0.00</b></td>
                    <td><b>0.00</b></td>
                     <td><b>0.00</b></td>
                    <?php 
                    }
                    ?>
                    
                   
                
                </tr>
              
                       </tbody>
                    </table>
                  
                </div>
                <?php } 
                else {
                    ?>
                    <div class="table-responsive text-center">
                   <table id="example1" class="table table-bordered display" style="width:100%">
                       <thead>
                           <td>#</td>
                           <th style="display: tebel-cell !important;">Item</th>
                           <th>Op Stock</th>
                           <th>SR</th>
                           <th>Bal Stock</th>
                           <th>SR</th>
                           <th>Sale</th>
                           <th>Purchase</th>
                           <th>Total</th>
                       </thead>
                       <tbody>
                         
                           <tr>
                               <td colspan="8">NO WAREHOUSE WERE SELECTED</td>
                           </tr>
                          
                           </tbody>
                           </table>
                           </div>
                    <?php 
                }?>
            </div>
        </div>
    </div>
</div>