@extends('backend.master')

@section('maincontent')
@section('title')
    {{ env('APP_NAME') }}- Profit Loss
@endsection

<style>
    div#roleinfo_length {
        color: red;
    }

    div#roleinfo_filter {
        color: red;
    }

    div#roleinfo_info {
        color: red;
    }
    #collupshead{
        width: 100%;
        display: flex;
        justify-content: space-between;
    }
    #taka{
        font-size: 25px;
        padding-left: 14px;
        color: black;
    }
</style>
{{-- summernote --}}
<link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-bs4.min.css" rel="stylesheet">
@php
    $orders=\App\Models\Order::whereBetween('orders.created_at', [$start . ' 00:00:00', $end . ' 23:59:59'])->where('courier_charge','>',0)->get();
    $subtotal=$orders->sum('subTotal');
    $paymentamount=$orders->sum('paymentAmount');
    $deliverycharge=$orders->sum('deliveryCharge');
    $couriercharge=$orders->sum('courier_charge');
    $sales=(($subtotal+$paymentamount)-$deliverycharge);
@endphp
<div class="px-4 pt-4 container-fluid">
    <div class="row">
        <div class="col-sm-12 col-md-12 col-xl-12">
            <div class="p-4 rounded bg-secondary h-100">
                <div class="card">
                    <div class="card-header">
                        <div class="text-center">
                            <h3 class="mb-0 text-center">Accounts</h3>
                            <p>From: {{$start}} To: {{$end}}</p>
                        </div>
                    </div>
                    <div class="card-body">
                        <form name="form" action="{{url('admin/profit-loss')}}">
                            <div class="row mb-2">  
                                <div class="form-group col-md-2 p-2">
                                    <label for="inputCity" class="col-form-label">Start Date</label>
                                    <input type="text" name="startDate" class="form-control datepicker" id="startDate"
                                        value="<?php echo $start; ?>" placeholder="Select Date">
                                </div>
                                <div class="form-group col-md-2 p-2">
                                    <label for="inputCity" class="col-form-label">End Date</label>
                                    <input type="text" name="endDate" class="form-control datepicker" id="endDate"
                                        value="<?php echo $end; ?>" placeholder="Select Date">
                                </div>

                                <div class="form-group col-md-2 pt-2" style="margin-top: 35px;">
                                    <button class="btn btn-info" type="submit"><i class="fas fa-search"></i>
                                        Search</button>
                                </div>   
                            </div>
                        </form>
                        <br>
                        <br>
                        <br>
                        <table class="table">
                          <thead>
                            <tr>
                              <th scope="col">#Date</th>
                              <th scope="col">Title</th>
                              <th scope="col">Amount</th> 
                            </tr>
                          </thead>
                          <tbody>
                            <tr>
                              <td>{{$start}} - {{$end}}</td>
                              <td>Total Order</td>
                              <td>{{$orders->count()}}</td> 
                            </tr>
                            <tr>
                              <td>{{$start}} - {{$end}}</td>
                              <td>Total Sale Amount</td>
                              <td>{{$subtotal+$paymentamount}}</td> 
                            </tr>
                            <tr>
                              <td>{{$start}} - {{$end}}</td>
                              <td>Delivery Charge</td>
                              <td>{{$deliverycharge}}</td> 
                            </tr>
                            <tr>
                              <td>{{$start}} - {{$end}}</td>
                              <td>Courier Charge</td>
                              <td>{{$couriercharge}}</td> 
                            </tr>
                            <tr>
                              <td>{{$start}} - {{$end}}</td>
                              <td>Courier Profit</td>
                              <td>{{$deliverycharge-$couriercharge}}</td> 
                            </tr>
                            <tr>
                              <td>{{$start}} - {{$end}}</td>
                              <td>Product Sale Price</td>
                              <td>{{($subtotal+$paymentamount)-$deliverycharge}}</td> 
                            </tr>
                            
                            <tr>
                              <td>{{$start}} - {{$end}}</td>
                              <td>Product Price</td>
                              <td>
                                    @php
                                        $productprice=0;
                                        foreach($orders as $order){ 
                                            $orderproducts=App\Models\Orderproduct::where('order_id',$order->id)->get();
                                            foreach($orderproducts as $orderproduct){
                                                $product=App\Models\Product::where('id',$orderproduct->product_id)->first();
                                                $wp=App\Models\Size::where('product_id',$product->id)->first()->Wholesale;
                                                $amount=$wp*$orderproduct->quantity;
                                                $productprice+=$amount;
                                            }
                                        }
                                    @endphp
                                    {{$productprice}}
                              </td> 
                              
                                <tr>
                                  <td>{{$start}} - {{$end}}</td>
                                  <td>Product Sale Profit</td>
                                  <td>{{$sales-$productprice}}</td> 
                                </tr>
                                <tr>
                                  <td>{{$start}} - {{$end}}</td>
                                  <td>Total Profit</td>
                                  <td>{{($sales-$productprice)+($deliverycharge-$couriercharge)}}</td> 
                                </tr>
                            </tr>
                          </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <input type="hidden" name="_token" value="{{ csrf_token() }}" />
    </div>
</div>
 

    <script>
        $(document).ready(function(){
            $(".datepicker").flatpickr();
        });
    </script>

@endsection
