@extends('layout.app')


@section('content')

    <div class="mt-2 card">
        <div class="card-body">

            <table class="table">
                <thead>
                    <tr>
                        <th>id</th>
                        <th>Name</th>
                        <th>Currtent Month Total Bill Generate</th>
                        <th>Currtent Month Total Bill Payment</th>

                        <th>Currtent Month Total Balance Adjusstment</th>
                        <th>Diffence</th>
                        <th>Due</th>
                    </tr>
                </thead>
                @php
                     $sum = 0;
                     $sum_billgenerate = 0;
                     $sum_payment = 0;
                     $sum_discount = 0;
                     $suma_djustment = 0;
                     $current_due_sum = 0;
                @endphp
                <tbody>
                    @foreach ($clients as $client)
                    @php

                        $totalBillGenerate = $client->currentMonthgeneratedBill->sum('bill_amount');
                        $totalBillPayment = $client->currentMonthpaymentBill->sum('paid_amount')+ $client->currentMonthpaymentBill->sum('discount_amount');
                        $totalBalanceAdjustment = $client->currentmonthbalanceAdjustment->sum('amount');
                        $due = $totalBillGenerate - $totalBillPayment - $totalBalanceAdjustment;


                    @endphp
                    @if ($totalBillGenerate != $totalBillPayment )
                    @php
                        $sum +=  $totalBillPayment - $totalBillGenerate ;
                        $sum_billgenerate += $totalBillGenerate;
                        $sum_payment += $client->currentMonthpaymentBill->sum('paid_amount') + $client->currentMonthpaymentBill->sum('discount_amount');
                        $sum_discount += $client->currentMonthpaymentBill->sum('discount_amount');
                        $suma_djustment += $totalBalanceAdjustment;

                        if($totalBillGenerate >= $totalBillPayment){
                            $current_due_sum += $totalBillGenerate;
                            $c_due = $totalBillGenerate;
                        }else{
                            $current_due_sum += $totalBillPayment;
                            $c_due = $totalBillPayment;

                        }
                    @endphp
                        <tr >
                            <td>{{ $client->id }}</td>
                            <td>{{ $client->clientsInfo->clients_name }}</td>
                            <td>{{ $client->currentMonthgeneratedBill->sum('bill_amount') }}</td>
                            <td>{{ $client->currentMonthpaymentBill->sum('paid_amount')+ $client->currentMonthpaymentBill->sum('discount_amount')}}</td>
                            <td>{{ $client->currentmonthbalanceAdjustment->sum('amount')}}</td>
                            <td>{{ $totalBillPayment - $totalBillGenerate}}</td>
                            <td>{{ $client->customerAccount->dueAmount}}</td>
                        </tr>
                    @endif

                     @endforeach
                     <tr>
                            <td colspan="2" class="text-right">Total</td>
                            <td>{{ $sum_billgenerate }}</td>
                            <td>{{ $sum_payment }}</td>

                            <td>{{ $suma_djustment }}</td>
                            <td>{{ $sum }}</td>
                            <td>{{ $current_due_sum }}</td>
                     </tr>
                </tbody>
            </table>

        </div>
    </div>





@endsection
