<!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>
        body {
            font-family: 'Noto Nastaliq Urdu', 'Jameel Noori Nastaleeq', 'Arial', sans-serif;
            direction: rtl;
        }
        .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_bank_debit = 0;
        $total_bank_credit = 0;
        $total_workshop_debit = 0;
        $total_workshop_credit = 0;
        $total_broker_debit = 0;
        $total_broker_credit = 0;
        $total_others_debit = 0;
        $total_others_credit = 0;
        $total_driver_debit = 0;
        $total_driver_credit = 0;
        $total_pump_debit = 0;
        $total_pump_credit = 0;
        $total_vehicle_advance_debit = 0;
        $total_vehicle_advance_credit = 0;
        $total_vehicle_profit_debit = 0;
        $total_vehicle_profit_credit = 0;
    @endphp
    <tr>
        <th class="thead">سیریل نمبر</th>
        <th class="thead">اکاونٹ نام</th>
        <th class="thead">بنام</th>
        <th class="thead">جمع</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>
    @php
            $cash_debit = $data['opening_balance']->where('balance_type','debit')->sum('amount');

            $cash_credit =  $data['opening_balance']->where('balance_type','credit')->sum('amount') ;
            $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){$count +=1;}
    @endphp
    @if($total_cash!=0)
         <tr>
               <td class="thead">{{ $count}}</td>
               <td class="thead">Opening Balance A/c</td>
               <td class="thead">
                   {{$total_cash < 0 ? number_format( abs($total_cash),0):0}}
               </td>
               <td class="thead">
                   {{$total_cash > 0 ? number_format(abs($total_cash),0):0}}
               </td>
           </tr>
    @endif
    <tr>
        <th class="thead" colspan="4">Category:بنک</th>
    </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);
                $total_bank_debit +=  abs($total_cash);
            }else{
                $total_credit += abs($total_cash);
                $total_bank_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
    <tr>
        <td colspan="2" style="text-align: right; margin-right: 10px;"><u><i>Sub Total:</i></u></td>
        <th class="thead">{{number_format($total_bank_debit,0)}}</th>
        <th class="thead">{{number_format($total_bank_credit,0)}}</th>
    </tr>

    <tr>
        <th class="thead" colspan="4">Category: پارٹی اکاونٹ</th>
    </tr>
    @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);
               $total_broker_credit +=abs($total_broker);
           }else{
           $total_debit += abs($total_broker);
           $total_broker_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
    <tr>
        <td colspan="2" style="text-align: right; margin-right: 10px;"><u><i>Sub Total:</i></u></td>
        <th class="thead">{{number_format($total_broker_debit,0)}}</th>
        <th class="thead">{{number_format($total_broker_credit,0)}}</th>
    </tr>
    <!-- other account ledger -->

    @foreach ($data['catogories'] as $key => $category)
    @if ($category->otherAccount->isNotEmpty())

        <tr>
            <th class="thead" colspan="4">Category: <span style="margin-left:10px;">{{ $category->name }}</span></th>
        </tr>

        @php
            $total_others_credit = 0;
            $total_others_debit = 0;
            $count = 0;
        @endphp

        @forelse ($category->otherAccount as $detail)
            @php
                $other_account_debit = $detail->otherAccountLedger->sum('debit');
                $other_account_credit = $detail->otherAccountLedger->sum('credit');
                $total_others = $other_account_credit - $other_account_debit;

                // Update totals correctly
                if ($total_others > 0) {
                    $total_credit += abs($total_others);
                    $total_others_credit += abs($total_others); // Add to others credit
                } else {
                    $total_debit += abs($total_others);
                    $total_others_debit += abs($total_others); // Add to others debit
                }

                // Skip rows with zero balance
                if ($total_others == 0) {
                    continue;
                }

                $count += 1;
            @endphp

            <tr>
                <td class="thead">{{ $count }}</td>
                <td class="thead">{{ $detail->name }} A/c</td>
                <td class="thead">
                    {{ $total_others < 0 ? number_format(abs($total_others), 0) : 0 }}
                </td>
                <td class="thead">
                    {{ $total_others > 0 ? number_format(abs($total_others), 0) : 0 }}
                </td>
            </tr>
        @empty
            <!-- Handle empty state if needed -->
        @endforelse

        <tr>
            <td colspan="2" style="text-align: right; margin-right: 10px;"><u><i>Sub Total:</i></u></td>
            <th class="thead">{{ number_format($total_others_debit, 0) }}</th>
            <th class="thead">{{ number_format($total_others_credit, 0) }}</th>
        </tr>

    @endif
@endforeach

   <!-- end other account -->



    {{-- <tr>
        <th class="thead" colspan="4">Category: Driver</th>
    </tr>
    @forelse($data['drivers'] as $key=> $detail)
        @php
            // $broker_debit = $detail->ledgers->sum('debit');
            // $broker_credit = $detail->ledgers->sum('credit');
            $broker_debit = 0;
            $broker_credit = 0;

            $total_broker =$broker_credit -  $broker_debit;
              if($total_broker>0){
                        $total_credit += abs($total_broker);
                        $total_driver_credit += abs($total_broker);
                    }else{
                    $total_debit =$total_debit+ abs($total_broker);
                    $total_driver_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
    <tr>
        <td colspan="2" style="text-align: right; margin-right: 10px;"><u><i>Sub Total:</i></u></td>
        <th class="thead">{{number_format($total_driver_debit,0)}}</th>
        <th class="thead">{{number_format($total_driver_credit,0)}}</th>
    </tr> --}}
    {{-- <tr>
        <th class="thead" colspan="4">Category: Pump</th>
    </tr>
    @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);
              $total_pump_credit += abs($total_pump);
             }else{
                 $total_debit+=abs($total_pump);
                 $total_pump_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
    <tr>
        <td colspan="2" style="text-align: right; margin-right: 10px;"><u><i>Sub Total:</i></u></td>
        <th class="thead">{{number_format($total_pump_debit,0)}}</th>
        <th class="thead">{{number_format($total_pump_credit,0)}}</th>
    </tr> --}}


{{-- Drivers Section --}}
@foreach ($data['drivers'] as $key => $driver)
    @if ($driver->vehicles->isNotEmpty())

        <tr>
            <th class="thead" colspan="4">مالک: <span style="margin-left:10px;">{{ $driver->name }}</span></th>
        </tr>

        @php
            $total_vehicle_credit = 0;
            $total_vehicle_debit = 0;
            $vehicle_count = 0;
        @endphp

        @forelse ($driver->vehicles as $vehicle)
            @php
                $vehicle_debit = $vehicle->vehicle_advance_ledgers->sum('debit');
                $vehicle_credit = $vehicle->vehicle_advance_ledgers->sum('credit');
                $net_balance = $vehicle_credit - $vehicle_debit;

                // Update totals correctly
                if ($net_balance > 0) {
                    $total_credit += abs($net_balance);
                    $total_vehicle_credit += abs($net_balance);
                } else {
                    $total_debit += abs($net_balance);
                    $total_vehicle_debit += abs($net_balance);
                }

                // Skip vehicles with zero balance
                if ($net_balance == 0) continue;

                $vehicle_count += 1;
            @endphp

            <tr>
                <td class="thead">{{ $vehicle_count }}</td>
                <td class="thead">{{ $vehicle->vehicle_no }} A/c</td>
                <td class="thead">
                    {{ $net_balance < 0 ? number_format(abs($net_balance), 0) : 0 }}
                </td>
                <td class="thead">
                    {{ $net_balance > 0 ? number_format(abs($net_balance), 0) : 0 }}
                </td>
            </tr>
        @empty
            <!-- Handle empty state if needed -->
        @endforelse

        <tr>
            <td colspan="2" style="text-align: right;"><u><i>Sub Total:</i></u></td>
            <th class="thead">{{ number_format($total_vehicle_debit, 0) }}</th>
            <th class="thead">{{ number_format($total_vehicle_credit, 0) }}</th>
        </tr>

    @endif
@endforeach




    {{-- Grand Total --}}
    {{-- <tr>
        <td colspan="2" style="text-align: right;"><u><i>Grand Total:</i></u></td>
        <th class="thead">{{ number_format($overall_debit, 0) }}</th>
        <th class="thead">{{ number_format($overall_credit, 0) }}</th>
    </tr> --}}





    <tr>
        <td colspan="2" style="text-align: right; margin-right: 10px;"><u><i>Grand 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>
