@extends('layouts.app') @section('content')

{{ $summary['sub_tag_name'] }} ({{ $summary['parent_tag_name'] }})

{{ $summary['total_resellers'] }} resellers

Back to Report
Invoice Details
@if($invoices->count() > 0)
@php $customerData = []; $fromDate = request('from_date') ? \Carbon\Carbon::parse(request('from_date')) : null; $toDate = request('to_date') ? \Carbon\Carbon::parse(request('to_date')) : null; // Group invoices by customer foreach($invoices as $invoice) { $customerId = $invoice->customer_id; if (!isset($customerData[$customerId])) { $customerData[$customerId] = [ 'customer' => $invoice->customer, 'current_month_bill' => 0, 'previous_due' => 0, 'total_outstanding' => 0, 'current_month_receive' => 0, 'current_month_invoice_due' => 0, 'month_to_month_due' => 0, 'closing_due_balance' => 0, 'advance' => 0, ]; } } // Calculate for each customer foreach($customerData as $customerId => $data) { $customer = $data['customer']; // Get all invoice IDs for this customer $allInvoiceIds = \App\Models\SalesInvoice::where('customer_id', $customerId)->pluck('id'); // 1. Current Month Bill $currentMonthBill = \App\Models\SalesInvoice::where('customer_id', $customerId) ->when($fromDate, fn($q) => $q->whereDate('invoice_date', '>=', $fromDate)) ->when($toDate, fn($q) => $q->whereDate('invoice_date', '<=', $toDate)) ->sum('invoice_amount') ?? 0; // 2. Previous Due $previousInvoices = \App\Models\SalesInvoice::where('customer_id', $customerId) ->when($fromDate, fn($q) => $q->whereDate('invoice_date', '<', $fromDate)) ->get(); $previousDue = 0; foreach($previousInvoices as $prevInv) { $paid = \DB::table('sales_invoice_transections') ->where('sales_invoice_id', $prevInv->id) ->where('transaction_type', 'payment') ->when($fromDate, fn($q) => $q->whereDate('transaction_date', '<', $fromDate)) ->sum('amount') ?? 0; $previousDue += max(0, $prevInv->invoice_amount - $paid); } // 3. Total Outstanding $totalOutstanding = $previousDue + $currentMonthBill; // 4. Current Month Receive $currentMonthReceive = \DB::table('sales_invoice_transections') ->whereIn('sales_invoice_id', $allInvoiceIds) ->where('transaction_type', 'payment') ->when($fromDate, fn($q) => $q->whereDate('transaction_date', '>=', $fromDate)) ->when($toDate, fn($q) => $q->whereDate('transaction_date', '<=', $toDate)) ->sum('amount') ?? 0; // 5. Current Month Invoice Due $currentMonthInvoices = \App\Models\SalesInvoice::where('customer_id', $customerId) ->when($fromDate, fn($q) => $q->whereDate('invoice_date', '>=', $fromDate)) ->when($toDate, fn($q) => $q->whereDate('invoice_date', '<=', $toDate)) ->get(); $currentMonthInvoiceDue = 0; foreach($currentMonthInvoices as $currInv) { $paid = \DB::table('sales_invoice_transections') ->where('sales_invoice_id', $currInv->id) ->where('transaction_type', 'payment') ->sum('amount') ?? 0; $currentMonthInvoiceDue += max(0, $currInv->invoice_amount - $paid); } // 6. Month to Month Due $monthToMonthDue = max(0, $currentMonthBill - $currentMonthReceive); // 7. Closing Due Balance $closingDueBalance = max(0, $totalOutstanding - $currentMonthReceive); // 8. Advance $advance = \DB::table('advance_payments') ->where('user_id', $customerId) ->when($fromDate, fn($q) => $q->whereDate('payment_date', '>=', $fromDate)) ->when($toDate, fn($q) => $q->whereDate('payment_date', '<=', $toDate)) ->sum('amount') ?? 0; // Payment Status $paymentStatus = $closingDueBalance <= 0 ? 'Paid' : ($currentMonthReceive > 0 ? 'Partial' : 'Unpaid'); $statusClass = $closingDueBalance <= 0 ? 'success' : ($currentMonthReceive > 0 ? 'warning' : 'danger'); // Calculate Payment Rate $paymentRate = $totalOutstanding > 0 ? ($currentMonthReceive / $totalOutstanding) * 100 : 0; $customerData[$customerId] = array_merge($data, [ 'current_month_bill' => $currentMonthBill, 'previous_due' => $previousDue, 'total_outstanding' => $totalOutstanding, 'current_month_receive' => $currentMonthReceive, 'current_month_invoice_due' => $currentMonthInvoiceDue, 'month_to_month_due' => $monthToMonthDue, 'closing_due_balance' => $closingDueBalance, 'advance' => $advance, 'payment_rate' => $paymentRate, 'payment_status' => $paymentStatus, 'status_class' => $statusClass, ]); } @endphp @foreach($customerData as $data) @endforeach
Customer Current Month Bill Previous Due Total Outstanding Current Month Receive Current Month Invoice Due Month to Month Due Closing Due Balance Advance Payment Rate
{{ $data['customer']->name ?? 'Unknown' }} @if($data['customer']->company_name)
{{ $data['customer']->company_name }} @endif
৳ {{ number_format($data['current_month_bill'], 0) }} ৳ {{ number_format($data['previous_due'], 0) }} ৳ {{ number_format($data['total_outstanding'], 0) }} ৳ {{ number_format($data['current_month_receive'], 0) }} ৳ {{ number_format($data['current_month_invoice_due'], 0) }} ৳ {{ number_format($data['month_to_month_due'], 0) }} ৳ {{ number_format($data['closing_due_balance'], 0) }} ৳ {{ number_format($data['advance'], 0) }} {{ number_format($data['payment_rate'], 1) }}%
TOTAL: ৳ {{ number_format(array_sum(array_column($customerData, 'current_month_bill')), 0) }} ৳ {{ number_format(array_sum(array_column($customerData, 'previous_due')), 0) }} ৳ {{ number_format(array_sum(array_column($customerData, 'total_outstanding')), 0) }} ৳ {{ number_format(array_sum(array_column($customerData, 'current_month_receive')), 0) }} ৳ {{ number_format(array_sum(array_column($customerData, 'current_month_invoice_due')), 0) }} ৳ {{ number_format(array_sum(array_column($customerData, 'month_to_month_due')), 0) }} ৳ {{ number_format(array_sum(array_column($customerData, 'closing_due_balance')), 0) }} ৳ {{ number_format(array_sum(array_column($customerData, 'advance')), 0) }} @php $totalOutstandingSum = array_sum(array_column($customerData, 'total_outstanding')); $totalReceiveSum = array_sum(array_column($customerData, 'current_month_receive')); $overallPaymentRate = $totalOutstandingSum > 0 ? ($totalReceiveSum / $totalOutstandingSum) * 100 : 0; @endphp {{ number_format($overallPaymentRate, 1) }}%
@else

No invoices found for this sub-tag in the selected date range.

This could mean:
  • No resellers are assigned to this sub-tag
  • No invoices exist in the selected date range
  • Try selecting a different date range
@endif
@endsection