<html>
<head>
    <style type="text/css" media="all">

       
        #design {
            font-family: Serif;
            border-collapse: collapse;
            width: 100%;
        }

        #design td,
        #design th,
        #design thead, 
        #design tbody {
            border: 1px solid #000;
            font-size: 12px;
        }


        * {
            margin: 2px;
            padding: 2px;
        }
    </style>
</head>
<body>
        
    <div class="card-body">
            
        <div class="card-body">            

            <table class="table table-striped table-bordered " id="design">
        <thead>

          <tr>
                <th>From</th>
                <th colspan="10" >{{ !empty($startdate) ? $startdate : ''}}</th>
                <th>To</th>
                <th colspan="16" >{{ !empty($enddate) ? $enddate : ''}}</th>                            
        </tr>
        <tr>
              <th  colspan='28'  style="border:1px solid #121;background-color:#909090;color:#000; text-align:center; font-size:15px;">DSR Product WISE</th>
        </tr>

        <tr>
        <th>S.No.</th>
        <th>store</th>
        <th>BILL</th>
        <th>Date</th>
        <th>HSN</th>

        <th>Section</th>
        <th>Sub Section</th>
        <th>Color</th>
        <th>Size</th>
        <th>Qty</th>

        <th>Style</th>

        <th>Name</th>
        <th>Telephone </th>
        <th>SPS </th>
        <th>SPS On </th>
        <th>MRP</th>
        <th>SPS<br>MRP</th>
        <th>Discount</th>
        <th>Offer</th>
        <th>Rate</th>
        <th>Base<br>Value</th>
        <th>CGST (%)</th>
        <th>CGST<br>Amt</th>
        <th>SGST (%)</th>
        <th>SGST<br>Amt</th> 
        <th>Tax (%)</th>
        <th>Tax<br>Amt</th>

        <th> Total (₹)</th>                        
        </tr>
        </thead>

        <tbody>
        @php  
        $sno = 1; $Plan_price = $telephone = $customer_id = $plan_id = $customerInfo = $cusName = '';

        @endphp
        @foreach($orders as $key => $row)                       

        @foreach($row->orderProducts as $products)
        @php

        $voucherVal =  $mrp = 0; 
        $isSpsOrder = $row->isSPSOrder();
        if(!empty($products->warehouseProd)){ 

        $mrpProduct = \DB::table('oc_warehouse_product')->select('mrp')->where('barcode',$products->barcode)->first();                                          
        $mrp = $mrpProduct->mrp;                                    
        }
        $Plan_price = $telephone = $customer_id = $plan_id = $customerInfo = $cusName = '';
        if($isSpsOrder == 1) {

        $planinfo = $row->getManipulatedMrpForSpsOrder;
        $customerInfo = $planinfo->customerInfo;
        $cusName =  $customerInfo->firstname.' '.$customerInfo->lastname;
        $telephone =  $customerInfo->telephone;
        $customer_id =  $planinfo->customer_id; 
        $plan_id = $planinfo->plan_id; 
        $Plan_price  =  $planinfo->Plan->price;
        $plan_date =  $planinfo->start_date; 

        if(!empty($planinfo))
        {
        $defaultDiscount = $planinfo->discount;
        $bdayDiscount = $planinfo->bdaydiscount;
        $annDiscount = $planinfo->anndiscount;
        $aptDiscount = 5;

        $bapplied = false;
        $birthres = $products->orderByBirthday;

        if($birthres)
        {
        $bapplied = true;
        }

        $annApplied = false;
        $annverres = $products->orderByAnn;

        if($annverres)
        {
        $annApplied = true;
        }

        $aptApplied = false;
        $appoinres = $products->orderByAppointment;

        if($appoinres)
        {
        $aptApplied = true;
        }

        $vouchRes = $products->orderByVoucher;

        if($vouchRes)
        {
        $voucherVal = $vRow['value'];
        $totProds =  $row->orderProducts->count();
        if($voucherVal > 0) {
          $voucherVal = $voucherVal / $totProds;
        }
        }

        $mrpp = $mrp <= 0 ? 0 : $mrp - ( $products->offer  + $products->discount );

        if($mrpp == 0) {
        $smrp =  $mrpp;
        } else {

        $tempMrp = $mrpp - $voucherVal;


        $tempMrp = $tempMrp - (($defaultDiscount/100) * $tempMrp);

        if($bapplied == true) {
          
          $tempMrp = $tempMrp - (($bdayDiscount/100) * $tempMrp);
        }

        if($annApplied == true) {
           
          $tempMrp = $tempMrp - (($annDiscount/100) * $tempMrp);
        }

        if($aptApplied == true) {
         
          $tempMrp = $tempMrp - (($aptDiscount/100) * $tempMrp);
        }                                            

        $smrp = number_format($tempMrp,2,'.','');
        }

        }else {
        $tax = $products->tax;
        $mrpp = $mrp <= 0 ? 0 : $mrp; 
        $calMrp1 = ($tax * 100)/5;
        $calMrp2 = ($tax * 100)/12;
        $mixMrp1 = abs($mrpp - $calMrp1);
        $mixMrp2 = abs($mrpp - $calMrp2);

        if($mixMrp1 < $mixMrp2) {
        $smrp =  abs($calMrp1 + $tax);
        } else {
        $smrp = abs($calMrp2 + $tax);
        }
        } 
        }else{
        $smrp = 0;
        }

        if($isSpsOrder == 1) {
        $gst = $smrp <= 999 ? 5 : 12;
        }else{ 
        $gst = $mrp <= 999 ? 5 : 12;
        }

        if($isSpsOrder){
        if($smrp > 0){ 
        $rate = ($smrp/($gst + 100)) * 100;
        $baseval = ($smrp/($gst + 100)) * 100;
        $taxamt = $smrp - $rate; //$row['tax'];
        }else{                                
        $rate = $baseval = $taxamt = 0;
        }

        }else{ 

        if($mrp <= 0){ 
        $rate = $baseval = $taxamt = 0;
        }else{ 
        $rate = ($mrp/($gst + 100)) * 100;
        $baseval = ($mrp/($gst + 100)) * 100;
        $taxamt = $products->tax;
        }                                                                                          
        }

        if($taxamt <= 0) {
        $total =number_format(0,2);
        }else{ 
        if($isSpsOrder == 1){ 
        $total =  number_format(($smrp * $products->quantity),3);
        }else{ 
          $mrpdis = $mrp - ( $products->offer  + $products->discount ) ;
          $total = number_format(($mrpdis * $products->quantity),3);
        }
        } 
        @endphp

        <tr>
        <td>{{ $sno }}</td>
        <td>{{ $row->warehouse->name }}</td>
        <td>{{ $row['pos_order_suffix'] }}</td>
        <td>{{ \Helper::humanDate($row['date_added']) }}</td>
        <td>{{ !empty($products->fabric) ? $products->fabric : 'N/A' }}  </td>

        <td>{{ !empty($products->warehouseProd) ? $products->warehouseProd->section : 'N/A' }}   </td>
        <td>{{ !empty($products->warehouseProd) ? $products->warehouseProd->subsection : 'N/A' }} </td>

        <td>{{ $products->color}}  </td> 

        <td>{{ $products->size}}  </td>                           
        <td>{{ $products->quantity}} </td>                            

        <td>{{ !empty($products->warehouseProd) ? $products->warehouseProd->article : 'N/A' }}  </td>

        <td>{{ !empty($cusName) ? $cusName : $row->customerInfo->firstname.' '.$row->customerInfo->lastname }} </td> 
        <td>{{ !empty($telephone) ? $telephone : $row->customerInfo->telephone }} </td> 
        <td>{{ !empty($Plan_price) ? $Plan_price : 0  }} </td>

        <td> @if(!empty($Plan_price))                 
                 {{ ( date('Y-m-d',strtotime($row['date_added'])) == $plan_date ) ? 'New' : 'Old' }}
             @else
             {{ 'none' }}
             @endif    
        </td>

        <td>{{ !empty($products->price) ? $mrp : 'N/A' }} </td>                           
        <td>{{ !empty($isSpsOrder) ? number_format($smrp,3) : '0.00' }}   </td>                            
        <td>{{ $products->discount}}  </td>
        <td>{{ $products->offer}}  </td>
        <td>{{ number_format($rate,3)}}   </td>                           
        <td>{{ number_format($baseval,3)}} </td> 
        <td>   
        @if($taxamt <= 0) {{ 0 }} 
        @else {{number_format(($gst/2),2)}}
        @endif 
        </td>                           
        <td> 
        @if($taxamt <= 0) {{ 0 }} 
        @else {{number_format(($taxamt/2),2)}} 
        @endif 
        </td>                            
        <td>
        @if($taxamt <= 0) {{ 0 }} 
        @else {{number_format(($gst/2),2)}} 
        @endif 
        </td>
        <td> 
        @if($taxamt <= 0) {{ 0 }}
        @else {{number_format(($taxamt/2),2)}} 
        @endif 
        </td>                           
        <td>{{$gst}}</td>
        <td>{{number_format($taxamt,3)}} </td>

        <td> 
        {{$total}}
        </td>                            
        </tr>
        <?php $sno++ ?>
        @endforeach
        @endforeach
        </tbody> 
    </table>

        </div>
    </div> 
</body>
</html>
