<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Trail Balance | {{  $business_name->name }}</title>
    <style>
        .table,
        .tcell,
        .thead {
            border: 1px solid black;
            text-align: center;
        }

        .table {
            width: 100%;
            border-collapse: collapse;
        }

    </style>
</head>

<body>
<table style="width: 100%;border-collapse: collapse; margin-bottom: 10px">
    <tr>
        <td style="width:33.33%; text-align: center">
            <h1>{{  $business_name->name }}</h1>
        </td>

    </tr>
</table>

<table style="width: 100%;border-collapse: collapse; margin-bottom: 10px">
    <tr>
        <td style="width:33.33%; text-align: left">
        </td>
        <td style="width:33.33%; text-align: center">
            <h4> Trail Balance Reports</h4>
        </td>
        <td style="width:33.33%; text-align: right">
            <span style="font-weight: bold">From Date:</span> <span
                style="text-decoration: underline">{{ $data['form_date']}}</span><br>
            <span style="font-weight: bold">To Date:</span> <span
                style="text-decoration: underline">{{ $data['to_date'] }}</span>
        </td>
    </tr>
</table>
<table class="table">
    @php
        $count =1;
          $total_debit= 0;
          $total_credit= 0;
    @endphp
    <tr>
        <th class="thead">Sr. No.</th>
        <th class="thead">Account Name</th>
        <th class="thead">Debit</th>
        <th class="thead">Credit</th>
    </tr>
    @php
        $cash_debit = $data['cash']->sum('debit');
        $cash_credit = $data['cash']->sum('credit');
        $total_cash = $cash_debit -$cash_credit;
        if($total_cash>0){
            $total_debit +=  abs($total_cash);
        }else{
            $total_credit += abs($total_cash);
        }
    @endphp
    <tr>
        <td class="thead">{{ $count}}</td>
        <td class="thead">Cash A/c</td>
        <td class="thead">
            {{$total_cash>0? number_format( $total_cash,0):0}}
        </td>
        <td class="thead">
            {{$total_cash<0? number_format(abs($total_cash),0):0}}
        </td>
    </tr>

    @forelse($data['banks'] as $key=> $detail)
        @php
            $cash_debit = $detail->ledgers->sum('debit');
            $cash_credit = $detail->ledgers->sum('credit');
            $total_cash = $cash_debit -$cash_credit;
            if($total_cash>0){
                $total_debit +=  abs($total_cash);
            }else{
                $total_credit += abs($total_cash);
            }
                    if($total_cash==0){continue;}

            $count += 1;
        @endphp
        <tr>

            <td class="thead">

                {{ $count }}</td>
            <td class="thead">{{ $detail->name }} A/c</td>
            <td class="thead">
                {{$total_cash>0? number_format( $total_cash,0):0}}
            </td>
            <td class="thead">
                {{$total_cash<0? number_format(abs($total_cash),0):0}}
            </td>
        </tr>
    @empty
    @endforelse
    @forelse($data['workshops'] as $key=> $detail)
        @php
            $workshop_debit = $detail->workshop_payables->sum('debit');
            $workshop_credit = $detail->workshop_payables->sum('credit');
            $total_workshop =$workshop_credit -  $workshop_debit;
            $total_credit = $total_credit + $total_workshop;
                    if($total_workshop==0){continue;}
                      $count += 1;
        @endphp
        <tr>
            <td class="thead">
                {{ $count }}</td>
            <td class="thead">{{ $detail->shop_name }} A/c</td>

            <td class="thead">0</td>
            <td class="thead">
                {{ number_format($total_workshop,0) }}
            </td>
        </tr>
    @empty
    @endforelse

    @forelse($data['brokers'] as $key=> $detail)
        @php
             $broker_debit = $detail->receivables->sum('debit');
            $broker_credit = $detail->receivables->sum('credit');
            $total_broker =$broker_credit -  $broker_debit;
            if($total_broker>0){
                $total_credit +=abs($total_broker);
            }else{
            $total_debit += abs($total_broker);
            }
                    if($total_broker==0){continue;}
            $count += 1;

        @endphp
        <tr>
            <td class="thead">
                {{ $count}}</td>
            <td class="thead">{{ $detail->name }} A/c</td>

            <td class="thead">

                {{$total_broker<0? number_format(abs($total_broker),0):0 }}
            </td>
            <td class="thead">
                {{$total_broker>0? number_format(abs($total_broker),0):0 }}

            </td>
        </tr>
    @empty
    @endforelse
    @forelse($data['drivers'] as $key=> $detail)
        @php
            $broker_debit = $detail->ledgers->sum('debit');
            $broker_credit = $detail->ledgers->sum('credit');
            $total_broker =$broker_credit -  $broker_debit;
              if($total_broker>0){
                        $total_credit += abs($total_broker);
                    }else{
                    $total_debit =$total_debit+ abs($total_broker);
                    }
                      if($total_broker==0){continue;}
            $count += 1;
        @endphp
        <tr>
            <td class="thead">
                {{ $count}}</td>
            <td class="thead">{{ $detail->name }} A/c</td>

            <td class="thead">

                {{$total_broker<0? number_format(abs($total_broker),0):0 }}
            </td>
            <td class="thead">

                {{$total_broker>0? number_format(abs($total_broker),0):0 }}
            </td>
        </tr>
    @empty
    @endforelse

    @forelse($data['pumps'] as $key=> $detail)
        @php
            $pump_debit = $detail->pump_payables->sum('debit');
              $pump_credit = $detail->pump_payables->sum('credit');
              $total_pump =$pump_credit -$pump_debit;
             if($total_pump>0){
              $total_credit += abs($total_pump);
             }else{
                 $total_debit+=abs($total_pump);
             }
                     if($total_pump==0){continue;}
                        $count += 1;
        @endphp
        <tr>
            <td class="thead">
                {{ $count}}</td>
            <td class="thead">{{ $detail->name }} A/c</td>
            <td class="thead">
                {{$total_pump<0? number_format(abs($total_pump),0):0 }}

            </td>
            <td class="thead">
                {{$total_pump>0? number_format(abs($total_pump),0):0 }}

            </td>

        </tr>
    @empty
    @endforelse
    @forelse($data['vehicles'] as $key=> $detail)
        @php
            $vehicle_profit_debit = $detail->profit_ledgers->sum('debit');
            $vehicle_profit_credit = $detail->profit_ledgers->sum('credit');
            $total_vehicle_profit =$vehicle_profit_credit-$vehicle_profit_debit;
            $vehicle_advance_debit = $detail->ledgers->sum('debit');
            $vehicle_advance_credit = $detail->ledgers->sum('credit');
            $total_vehicle_advance =$vehicle_advance_debit-$vehicle_advance_credit ;
            if($total_vehicle_profit>0){
                $total_credit += abs($total_vehicle_profit);
            }else{
                $total_debit +=abs($total_vehicle_profit);
            }
            if($total_vehicle_advance>0){
                $total_debit +=abs($total_vehicle_advance);
            }else{
                $total_credit += abs($total_vehicle_advance);
            }
            if($total_vehicle_advance==0 && $total_vehicle_profit==0){continue;}

                      $count += 1;
        @endphp

        <tr>
            <td class="thead">
                {{ $count}}</td>
            <td class="thead">Advance {{ $detail->vehicle_no }} A/c</td>
            <td class="thead">
                {{$total_vehicle_advance>0 ? number_format(abs($total_vehicle_advance),0):0 }}

            </td>
            <td class="thead">

                {{$total_vehicle_advance<0 ? number_format(abs($total_vehicle_advance),0):0 }}
            </td>

        </tr>
        @php
                   $count += 1;
        @endphp
        <tr>
            <td class="thead">
                {{ $count}}</td>
            <td class="thead">Profit {{ $detail->vehicle_no }} A/c</td>
            <td class="thead">
                {{$total_vehicle_profit<0 ? number_format(abs($total_vehicle_profit),0):0 }}

            </td>
            <td class="thead">

                {{$total_vehicle_profit>0 ? number_format(abs($total_vehicle_profit),0):0 }}
            </td>

        </tr>
    @empty
    @endforelse
    <tr>
        <td colspan="2" style="text-align: right; margin-right: 10px;"><u><i>Total:</i></u></td>
        <th class="thead">{{number_format($total_debit,0)}}</th>
        <th class="thead">{{number_format($total_credit,0)}}</th>
    </tr>
</table>
</body>

</html>
