From 0ef83aa5423f44b7246d7aa4aff8c1807ba52f8b Mon Sep 17 00:00:00 2001 From: Nethma-Yasith Date: Fri, 3 Jul 2026 15:28:09 +0530 Subject: [PATCH] Implement dynamic payment columns for Table 1 --- ...DailySalePaymentReportModuleController.php | 54 ++++++------------ Resources/views/index.blade.php | 55 ++++++++----------- 2 files changed, 39 insertions(+), 70 deletions(-) diff --git a/Http/Controllers/DailySalePaymentReportModuleController.php b/Http/Controllers/DailySalePaymentReportModuleController.php index d9a70f4..95651f1 100644 --- a/Http/Controllers/DailySalePaymentReportModuleController.php +++ b/Http/Controllers/DailySalePaymentReportModuleController.php @@ -18,36 +18,12 @@ class DailySalePaymentReportModuleController extends Controller { $report_date = $request->input('report_date', Carbon::today()->toDateString()); - // Helper function (Closure) to calculate payment breakdown from a collection of payments - $calculatePayments = function ($payments) { - $breakdown = [ - 'cash' => 0, - 'card1' => 0, - 'card2' => 0, - 'card3' => 0, - 'online_transfer' => 0, - 'link_pay' => 0 - ]; - foreach ($payments as $payment) { - $method = strtolower(trim($payment->paying_method)); - if ($method == 'cash') { - $breakdown['cash'] += $payment->total_amount; - } elseif ($method == 'card 1' || $method == 'card1') { - $breakdown['card1'] += $payment->total_amount; - } elseif ($method == 'card 2' || $method == 'card2') { - $breakdown['card2'] += $payment->total_amount; - } elseif ($method == 'card 3' || $method == 'card3') { - $breakdown['card3'] += $payment->total_amount; - } elseif (str_contains($method, 'online')) { - $breakdown['online_transfer'] += $payment->total_amount; - } elseif (str_contains($method, 'link')) { - $breakdown['link_pay'] += $payment->total_amount; - } - } - return $breakdown; - }; + // Fetch dynamic payment options using DB facade since the model is missing in this branch + $paymentOptions = DB::table('payment_options')->where('is_active', 1) + ->orderBy('id') + ->get(['id', 'name']); + - // --- TABLE 1: Daily Sales --- $sales = DB::table('sales') ->leftJoin('users', 'sales.user_id', '=', 'users.id') ->leftJoin('customers', 'sales.customer_id', '=', 'customers.id') @@ -68,25 +44,27 @@ class DailySalePaymentReportModuleController extends Controller $payments = DB::table('payments') ->where('sale_id', $sale->id) ->whereDate('created_at', $report_date) - ->select('paying_method', DB::raw('SUM(amount) as total_amount')) - ->groupBy('paying_method') + ->select('payment_option_id', DB::raw('SUM(amount) as total_amount')) + ->groupBy('payment_option_id') ->get(); - $breakdown = $calculatePayments($payments); + $methodTotals = []; + foreach ($payments as $payment) { + $methodTotals[$payment->payment_option_id] = $payment->total_amount; + } - $sales_data[] = (object) array_merge([ + $sales_data[] = (object) [ 'date' => Carbon::parse($sale->date)->format('Y-m-d'), 'reference_no' => $sale->reference_no, 'cashier' => $sale->cashier ?? 'N/A', 'customer' => $sale->customer ?? 'N/A', 'grand_total' => $sale->grand_total, - 'due_amount' => $sale->due_amount - ], $breakdown); + 'due_amount' => $sale->due_amount, + 'methods' => $methodTotals + ]; } - - - return view('dailysalepaymentreportmodule::index', compact('sales_data', 'report_date')); + return view('dailysalepaymentreportmodule::index', compact('sales_data', 'report_date', 'paymentOptions')); } /** diff --git a/Resources/views/index.blade.php b/Resources/views/index.blade.php index 556184b..2ba8cca 100644 --- a/Resources/views/index.blade.php +++ b/Resources/views/index.blade.php @@ -80,26 +80,21 @@ Cashier Customer Grand Total - Cash - Card 1 - Card 2 - Card 3 - Online Transfer - Link Pay + @foreach($paymentOptions as $option) + {{ $option->name }} + @endforeach Due Amount @php - // යටින් පෙන්නන්න ඕන එකතුවට බින්දුවෙන් විචල්‍යයන් හදාගන්නවා $tot_grand = 0; - $tot_cash = 0; - $tot_card1 = 0; - $tot_card2 = 0; - $tot_card3 = 0; - $tot_online = 0; - $tot_link = 0; $tot_due = 0; + // Initialize dynamic option totals + $option_totals = []; + foreach ($paymentOptions as $opt) { + $option_totals[$opt->id] = 0; + } @endphp @foreach ($sales_data as $sale) @@ -109,25 +104,22 @@ {{ $sale->cashier }} {{ $sale->customer }} {{ number_format($sale->grand_total, 2) }} - {{ number_format($sale->cash, 2) }} - {{ number_format($sale->card1, 2) }} - {{ number_format($sale->card2, 2) }} - {{ number_format($sale->card3, 2) }} - {{ number_format($sale->online_transfer, 2) }} - {{ number_format($sale->link_pay, 2) }} + + @foreach($paymentOptions as $option) + {{ number_format($sale->methods[$option->id] ?? 0, 2) }} + @endforeach + {{ number_format($sale->due_amount, 2) }} @php - // ටේබල් එක යටින් පෙන්වන්න ඔක්කොම එකතු කරනවා $tot_grand += $sale->grand_total; - $tot_cash += $sale->cash; - $tot_card1 += $sale->card1; - $tot_card2 += $sale->card2; - $tot_card3 += $sale->card3; - $tot_online += $sale->online_transfer; - $tot_link += $sale->link_pay; $tot_due += $sale->due_amount; + + // Accumulate dynamic option totals + foreach ($paymentOptions as $opt) { + $option_totals[$opt->id] += ($sale->methods[$opt->id] ?? 0); + } @endphp @endforeach @@ -135,12 +127,11 @@ Total {{ number_format($tot_grand, 2) }} - {{ number_format($tot_cash, 2) }} - {{ number_format($tot_card1, 2) }} - {{ number_format($tot_card2, 2) }} - {{ number_format($tot_card3, 2) }} - {{ number_format($tot_online, 2) }} - {{ number_format($tot_link, 2) }} + + @foreach($paymentOptions as $option) + {{ number_format($option_totals[$option->id], 2) }} + @endforeach + {{ number_format($tot_due, 2) }}