@extends('layouts.backend')

@section('extraCss')
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/fixedheader/3.1.2/css/fixedHeader.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/fixedcolumns/3.3.3/css/fixedColumns.dataTables.min.css">
<style type="text/css">
table.dataTable {    
     margin-top: 0px !important; 
     margin-bottom: 0px !important;
    }

table.dataTable tr.odd { background-color: rgb(240, 241, 243);  }
table.dataTable tr.even{ background-color: white;  }
</style>
@endsection

@php 
       $spscount = 0;
       $spcCustomer = \DB::table('oc_customer_ref')->select('customer_id')->whereIn('warehouse_id', $pos_id)->get();    

       foreach ($spcCustomer as $key => $value) { 
          $spsCustomersCollection[] = $value->customer_id;
        }
        if(!empty($spsCustomersCollection)){
       $spscount = \DB::table('oc_plan_to_customer')->where('current',1)
       ->where('status',1)->whereBetween('start_date', [$startdate, $enddate])->whereIn('customer_id', $spsCustomersCollection)->count();
      }
        
@endphp

@section('mainContentArea')
<div class="card card-default">
    <div class="card-header">
        <h4>Daily Sales Summary Report  </h4>
    </div>     

    <div class="alert alert-default " style="overflow-x:auto;">
            <form action="{{ route('pos-daily-sales-summary-new-report2') }}" id="productreport">
                <h4>Filter  </h4>
                <div style= "padding:15px;">
                        <label><input type="checkbox" value="range" name="filterby" checked> Date Range </label>
                        <label><input type="checkbox" value="month" name="filterby"> Month </label>                                             
                </div>                

                <table class="table filterPosSo">
                     <tr>  
                         <a class="btn btn-md btn-primary" style="float:right; margin-bottom:10px;" href="{{ ($warehouseName) ? route('salesSummaryNewExport2',[implode(",",$pos_id),$startdate,$enddate,$filterby] ) : '#' }}" id="eexcel">Export Excel</a> 
                        </tr>
                    <tr>
                        <td width="15%">Warehouse</td>
                        <td width="15%">Store</td>
                        <td width="10%" id="sdate">Start Date</td>
                        <td width="10%" id="edate">End Date</td>                    
                        <td colspan="2">ACTION</td>
                    </tr>
                    <tr>  
                         <td>
                            <select name="pos_id[]" id="warehouse" class="input-md"  multiple="multiple" >
                                
                                @foreach($ALL_WAREHOUSES as $warehouse)
                                <option value="{{ $warehouse->id }}" {{ ( !empty(request("pos_id")) && in_array($warehouse->id, request("pos_id")) ? "selected" : "") }}>{{ $warehouse->warehouse_code . ' ('.$warehouse->name.')' }}</option>
                                @endforeach
                            </select>
                        </td>

                        <td>
                            <select name="pos_id[]" id="Pos" class="input-md"  multiple="multiple">                                
                                @foreach($ALL_POS as $warehouse)
                                <option value="{{ $warehouse->id }}" {{ ( !empty(request("pos_id")) && in_array($warehouse->id, request("pos_id")) ? "selected" : "") }}>{{ $warehouse->warehouse_code . ' ('.$warehouse->name.')' }}</option>
                                @endforeach
                            </select>
                        </td>                      
                        <td>
                            <input type="text" name="startdate" id="startdate" readonly class="form-control datepicker" placeholder="Enter Start date" value="<?php echo empty(request('startdate')) ? date("Y-m-d") : request('startdate') ?>" >
                             @if ($errors->has('startdate'))
                             <span class="text-danger">{{ $errors->first('startdate') }}</span>
                             @endif
                        </td>                         

                        <td>
                           <input type="text" name="enddate" id="enddate" readonly class="form-control datepicker" value="<?php echo empty(request('enddate')) ? date("Y-m-d") : request('enddate') ?>" placeholder="Enter End date" >
                            @if ($errors->has('enddate'))
                             <span class="text-danger">{{ $errors->first('enddate') }}</span>
                            @endif
                        </td>                        
                       
                        <td><button class="btn btn-block btn-success" id="submit" ><i class="fa fas fa-search"></i></button></td>
                        <td><button class="btn btn-block btn-warning" type="button" onclick="window.location.href='{{ route('pos-daily-sales-summary-new-report2') }}';"><i class="fa fas fa-refresh"></i></button></td>
                    </tr>
                </table>
            </form>
    </div> 

    <div class="d-flex justify-content-center">
       <div class="spinner-border text-primary " role="status" id="loader_div">
        <span class="sr-only">Loading...</span>
      </div>
     </div> 
           
        <div class="card-body table-responsive" >
           @if(!empty($warehouseName))  <h5> Total SPS : <?php echo $spscount; ?> </h5> @endif
            <table class="table cell-border " id="salesSumarryTable">
                <thead>
                   
                    <tr>
                        <th>Date</th>
                        @if($filterby != 'month')
                        <th>Day</th>
                        @endif
                        <th> Store Location</th>
                        <th>Foot fall</th>
                        <th>Repeat</th>
                        <th>New</th>
                        
                        <th>Total NOB</th>
                        <th>QTY</th>
                        <th>UPT</th>
                        <th>ABV</th>
                        <th>ASP</th>
                        <th>Target</th> 
                        <th> New SPS </th>   
                        <th> Old SPS </th>                  
                        <th>MRP</th>
                        <th>SPS Discount</th>
                        <th>Clearence Discount</th>
                        <th>Other Offer Discount</th>
                        <th>Gift Vouchers</th>
                        <th>Reward Points</th>
                        <th>Realised sale</th> 
                        <th>ACH%</th>  
                        <th>CONV%</th> 
                        <th>0 - 1499</th>   
                        <th>1500 - 2499</th>  
                        <th>2500 - 4999</th>  
                        <th>5000 - 7499</th>   
                        <th>7500 - 9999</th>  
                        <th>10000 & above</th> 
                        <th>Upper</th>
                        <th>Suit Sets</th>
                        <th>Bottom</th>
                        <th>Home</th>
                        <th>Accessory</th>
                        <th>Jewellery</th>
                    </tr>
                </thead>
                @if(!empty($orders))
                <tbody>
                    @php    
                    $gtotalCash = $gtotalQty = $gtotalbill = $gtotalmrp = $gtotalspss = $gtotalOffer = $gtotalvocher = $gotherOffer = $gclearence =
                    $gtotalreward = $gtotal = $gtotalbill0 = $gtotalbill15 = $gtotalbill25 = $gtotalbill5 = $gtotalbill7 = $gtotalbill10 = $gtotalSuits = $gtotalBottom = $gtotalacceess = 
                    $gtotaljewelery = $averaguabv = $gtotalSPSNew = $gtotalSPSOld = $averagasp = $averagupt = $gtotalSPS = $abv = $asp = 0.00; 
                    $totalCount = count($orders);
                    
                    @endphp
                    @foreach($orders as $key => $order)  
                      @php 
                             
                      $totalTotal = $otherOffer = $clearence = 0; 
                      $totalexchangeqty = $order['exchangeqty'];
                      $exchangeamount = $order['exchangeamount'];

                      $order['totalfinalamt'] = $order['totalfinalamt'] - $exchangeamount;
                      $order['totalQty'] = $order['totalQty'] - $totalexchangeqty; 

                      $order['totalMrp'] = $order['totalMrp'] - $exchangeamount; 

                      $gtotalmrp += $order['totalMrp'];

                      $totalTotal += $order['totalfinalamt'];
                      $gtotalQty += $order['totalQty'];

                      $gtotalSPS += $order['spscount'];  
                      $gtotalSPSOld += $order['oldspscount'];                     

                      $gtotalbill += $order['totalBill'];                     

                      $dismrp = ($order['totalMrp'] * 10/100);

                       $otherOffer = $order['otherOffer'];

                       $gotherOffer += $otherOffer;

                      $clearence = $order['clearence'];
                      $gclearence += $clearence;                     
                    

                      $averagupt += $order['upt'];
                       
                      if($order['totalBill'] > 0)
                      {
                          $abv= $totalTotal/  $order['totalBill']; 
                      }
                      
                      $averaguabv += $abv;  
                      
                      if($order['totalQty'] > 0)
                      {
                          $asp = $totalTotal/  $order['totalQty']; 
                      }
                      
                      $averagasp += $asp;

                      
                      $gtotalspss += $order['totalSpsDiscount']; 
                     
                      $gtotalvocher += $order['voucher'];  
                      $gtotalreward += $order['totalreward']; 
                      $gtotal += $totalTotal; 

                      $gtotalbill5 += $order['bill5']; 
                      $gtotalbill7 += $order['bill7']; 
                      $gtotalbill10 += $order['bill10']; 

                      $gtotalbill0 += $order['bill0']; 
                      $gtotalbill15 += $order['bill15']; 
                      $gtotalbill25 += $order['bill25']; 

                      $gtotalSuits += $order['Suits']; 
                      $gtotalBottom += $order['Bottom']; 
                      $gtotalacceess += $order['acceess']; 
                      $gtotaljewelery += $order['jewelery']; 

                      @endphp         
                            <tr>
                                <td >{{$order['date']}}</td>
                               @if($filterby != 'month') <td >{{date('l', strtotime($order['date']))}}</td> @endif
                                <td >{{$order['store']}}</td>                                
                                <td >0</td>
                                <td >0</td>
                                <td >0</td>                                
                                <td >{{$order['totalBill']}}</td>
                                <td >{{$order['totalQty']}}</td>                                
                                <td >{{number_format($order['upt'],2)}}</td>
                                <td>{{ number_format((float)$abv, 2, '.', '')  }}</td>
                                <td>{{ number_format((float)$asp, 2, '.', '')  }}</td> 
                                
                                <td >0</td> 
                                <td >{{$order['spscount']}}</td>
                                <td >{{$order['oldspscount']}}</td>
                               
                                <td >{{$order['totalMrp']}}</td>
                                <td >{{$order['totalSpsDiscount']}}</td> 
                                <td >{{$clearence}}</td> 
                                <td>{{$otherOffer}}</td>
                                <td>{{$order['voucher']}}</td>
                                <td>{{$order['totalreward']}}</td>
                                <td >{{$totalTotal}}</td> 
                                <td >0</td> 
                                <td >0</td> 
                                <td >{{$order['bill0']}}</td> 
                                <td >{{$order['bill15']}}</td> 
                                <td >{{$order['bill25']}}</td> 
                                <td >{{$order['bill5']}}</td> 
                                <td >{{$order['bill7']}}</td> 
                                <td >{{$order['bill10']}}</td> 
                                <td >0</td> 
                                <td >{{!empty($order['Suits']) ? $order['Suits'] : 0 }}</td> 
                                <td >{{ !empty($order['Bottom']) ? $order['Bottom'] : 0 }}</td> 
                                <td >0</td> 
                                <td >{{!empty($order['acceess']) ? $order['acceess'] :0 }}</td>    
                                <td >{{ !empty($order['jewelery']) ? $order['jewelery'] : 0 }}</td>      
                            </tr>
                    @endforeach
                </tbody> 
                <tfoot>
                            <tr>
                                <td></td>
                                @if($filterby != 'month')  <td></td> @endif
                                <td></td>
                                <td>0</td>
                                <td>0</td>
                                <td>0</td>
                                <td> {{$gtotalbill}}</td>
                                <td> {{$gtotalQty}} </td>
                                
                                @php   $aupt=$averagupt/$totalCount; 
                                       $aabv=$averaguabv/$totalCount; 
                                       $aasp=$averagasp/$totalCount; 
                                @endphp

                                <td>{{ round ($aupt) }}</td>
                                <td>{{ round ($aabv) }}</td>
                                <td>{{ round ($aasp) }}</td>
                                <td>0</td>
                                <td> {{round($gtotalSPS)}}</td>
                                <td> {{round($gtotalSPSOld)}}</td>                                

                                <td> {{round($gtotalmrp)}} </td>
                                <td> {{round($gtotalspss)}} </td>
                                <td> {{round($gclearence)}} </td>
                                <td> {{round($gotherOffer)}}</td>
                                <td> {{round($gtotalvocher)}} </td>
                                <td> {{round($gtotalreward)}} </td>
                                <td> {{round($gtotal)}} </td>
                                <td>0</td>
                                <td>0</td>
                                <td> {{round($gtotalbill0) }} </td>
                                <td> {{round($gtotalbill15) }} </td>
                                <td> {{round($gtotalbill25) }} </td>
                                <td> {{round($gtotalbill5) }} </td>
                                <td> {{round($gtotalbill7) }} </td>
                                <td> {{round($gtotalbill10) }} </td>
                                <td>0</td>
                                <td>{{ round($gtotalSuits)}}</td>
                                <td>{{ round($gtotalBottom)}}</td>
                                <td>0</td>
                                <td>{{round($gtotalacceess)}}</td>
                                <td>{{round($gtotaljewelery)}}</td>
                                
                            </tr>  
                            <tr>
                        <th>Date</th>
                        @if($filterby != 'month')
                        <th>Day</th>
                        @endif
                        <th> Store Location</th>
                        <th>Foot fall</th>
                        <th>Repeat</th>
                        <th>New</th>
                        
                        <th>Total NOB</th>
                        <th>QTY</th>
                        <th>UPT</th>
                        <th>ABV</th>
                        <th>ASP</th>
                        <th>Target</th> 
                        <th> New SPS </th>   
                        <th> Old SPS </th>                  
                        <th>MRP</th>
                        <th>SPS Discount</th>
                        <th>Clearence Discount</th>
                        <th>Other Offer Discount</th>
                        <th>Gift Vouchers</th>
                        <th>Reward Points</th>
                        <th>Realised sale</th> 
                        <th>ACH%</th>  
                        <th>CONV%</th> 
                        <th>0 - 1499</th>   
                        <th>1500 - 2499</th>  
                        <th>2500 - 4999</th>  
                        <th>5000 - 7499</th>   
                        <th>7500 - 9999</th>  
                        <th>10000 & above</th> 
                        <th>Upper</th>
                        <th>Suit Sets</th>
                        <th>Bottom</th>
                        <th>Home</th>
                        <th>Accessory</th>
                        <th>Jewellery</th>
                    </tr>                 
                </tfoot>
               
                @endif
            </table>
        
        </div>
  
</div>
@endsection

@section('extraJs')
<script src="https://cdn.datatables.net/fixedheader/3.1.2/js/dataTables.fixedHeader.min.js" type="text/javascript"></script>
<script src="https://cdn.datatables.net/fixedcolumns/3.3.3/js/dataTables.fixedColumns.min.js" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function() {
  var table = $('#salesSumarryTable').DataTable( {
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],                  
          dom: 'Bfrtip',
          buttons: ['colvis'],         
          colReorder: true, 
          paging: false , 
          scrollX:        true,
          scrollY: "800px",
          scrollCollapse: true,
          
          fixedColumns:   {
            leftColumns: 3,
           
           } 
    } ); 
 
} );

   jQuery("#loader_div").hide();
   $("#submit").click(function () {

   if (($('#warehouse').val() == '') && ($('#Pos').val() == '')) {

            alert("Please Select Warehouse / Store");
           
            $("#productreport").attr("disabled", "disabled");

            return false;
        }
    
    var startDate = document.getElementById("startdate").value;
    var endDate = document.getElementById("enddate").value;

    if ((Date.parse(startDate) > Date.parse(endDate))) {

        alert("End date should be greater than Start date");
       
        $("#productreport").attr("disabled", "disabled");

        return false;
    }else{
        jQuery("#loader_div").show();
    }
    
});

$("input[type='checkbox']").on('click', function() {
    $("input[type='checkbox']").not(this).prop('checked', false);
});

</script>
@endsection
