Implement dynamic payment columns for Table 1
This commit is contained in:
@@ -18,36 +18,12 @@ class DailySalePaymentReportModuleController extends Controller
|
|||||||
{
|
{
|
||||||
$report_date = $request->input('report_date', Carbon::today()->toDateString());
|
$report_date = $request->input('report_date', Carbon::today()->toDateString());
|
||||||
|
|
||||||
// Helper function (Closure) to calculate payment breakdown from a collection of payments
|
// Fetch dynamic payment options using DB facade since the model is missing in this branch
|
||||||
$calculatePayments = function ($payments) {
|
$paymentOptions = DB::table('payment_options')->where('is_active', 1)
|
||||||
$breakdown = [
|
->orderBy('id')
|
||||||
'cash' => 0,
|
->get(['id', 'name']);
|
||||||
'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;
|
|
||||||
};
|
|
||||||
|
|
||||||
// --- TABLE 1: Daily Sales ---
|
|
||||||
$sales = DB::table('sales')
|
$sales = DB::table('sales')
|
||||||
->leftJoin('users', 'sales.user_id', '=', 'users.id')
|
->leftJoin('users', 'sales.user_id', '=', 'users.id')
|
||||||
->leftJoin('customers', 'sales.customer_id', '=', 'customers.id')
|
->leftJoin('customers', 'sales.customer_id', '=', 'customers.id')
|
||||||
@@ -68,25 +44,27 @@ class DailySalePaymentReportModuleController extends Controller
|
|||||||
$payments = DB::table('payments')
|
$payments = DB::table('payments')
|
||||||
->where('sale_id', $sale->id)
|
->where('sale_id', $sale->id)
|
||||||
->whereDate('created_at', $report_date)
|
->whereDate('created_at', $report_date)
|
||||||
->select('paying_method', DB::raw('SUM(amount) as total_amount'))
|
->select('payment_option_id', DB::raw('SUM(amount) as total_amount'))
|
||||||
->groupBy('paying_method')
|
->groupBy('payment_option_id')
|
||||||
->get();
|
->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'),
|
'date' => Carbon::parse($sale->date)->format('Y-m-d'),
|
||||||
'reference_no' => $sale->reference_no,
|
'reference_no' => $sale->reference_no,
|
||||||
'cashier' => $sale->cashier ?? 'N/A',
|
'cashier' => $sale->cashier ?? 'N/A',
|
||||||
'customer' => $sale->customer ?? 'N/A',
|
'customer' => $sale->customer ?? 'N/A',
|
||||||
'grand_total' => $sale->grand_total,
|
'grand_total' => $sale->grand_total,
|
||||||
'due_amount' => $sale->due_amount
|
'due_amount' => $sale->due_amount,
|
||||||
], $breakdown);
|
'methods' => $methodTotals
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return view('dailysalepaymentreportmodule::index', compact('sales_data', 'report_date', 'paymentOptions'));
|
||||||
|
|
||||||
return view('dailysalepaymentreportmodule::index', compact('sales_data', 'report_date'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -80,26 +80,21 @@
|
|||||||
<th>Cashier</th>
|
<th>Cashier</th>
|
||||||
<th>Customer</th>
|
<th>Customer</th>
|
||||||
<th>Grand Total</th>
|
<th>Grand Total</th>
|
||||||
<th>Cash</th>
|
@foreach($paymentOptions as $option)
|
||||||
<th>Card 1</th>
|
<th>{{ $option->name }}</th>
|
||||||
<th>Card 2</th>
|
@endforeach
|
||||||
<th>Card 3</th>
|
|
||||||
<th>Online Transfer</th>
|
|
||||||
<th>Link Pay</th>
|
|
||||||
<th>Due Amount</th>
|
<th>Due Amount</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@php
|
@php
|
||||||
// යටින් පෙන්නන්න ඕන එකතුවට බින්දුවෙන් විචල්යයන් හදාගන්නවා
|
|
||||||
$tot_grand = 0;
|
$tot_grand = 0;
|
||||||
$tot_cash = 0;
|
|
||||||
$tot_card1 = 0;
|
|
||||||
$tot_card2 = 0;
|
|
||||||
$tot_card3 = 0;
|
|
||||||
$tot_online = 0;
|
|
||||||
$tot_link = 0;
|
|
||||||
$tot_due = 0;
|
$tot_due = 0;
|
||||||
|
// Initialize dynamic option totals
|
||||||
|
$option_totals = [];
|
||||||
|
foreach ($paymentOptions as $opt) {
|
||||||
|
$option_totals[$opt->id] = 0;
|
||||||
|
}
|
||||||
@endphp
|
@endphp
|
||||||
|
|
||||||
@foreach ($sales_data as $sale)
|
@foreach ($sales_data as $sale)
|
||||||
@@ -109,25 +104,22 @@
|
|||||||
<td>{{ $sale->cashier }}</td>
|
<td>{{ $sale->cashier }}</td>
|
||||||
<td>{{ $sale->customer }}</td>
|
<td>{{ $sale->customer }}</td>
|
||||||
<td class="text-right">{{ number_format($sale->grand_total, 2) }}</td>
|
<td class="text-right">{{ number_format($sale->grand_total, 2) }}</td>
|
||||||
<td class="text-right">{{ number_format($sale->cash, 2) }}</td>
|
|
||||||
<td class="text-right">{{ number_format($sale->card1, 2) }}</td>
|
@foreach($paymentOptions as $option)
|
||||||
<td class="text-right">{{ number_format($sale->card2, 2) }}</td>
|
<td class="text-right">{{ number_format($sale->methods[$option->id] ?? 0, 2) }}</td>
|
||||||
<td class="text-right">{{ number_format($sale->card3, 2) }}</td>
|
@endforeach
|
||||||
<td class="text-right">{{ number_format($sale->online_transfer, 2) }}</td>
|
|
||||||
<td class="text-right">{{ number_format($sale->link_pay, 2) }}</td>
|
|
||||||
<td class="text-right">{{ number_format($sale->due_amount, 2) }}</td>
|
<td class="text-right">{{ number_format($sale->due_amount, 2) }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
@php
|
@php
|
||||||
// ටේබල් එක යටින් පෙන්වන්න ඔක්කොම එකතු කරනවා
|
|
||||||
$tot_grand += $sale->grand_total;
|
$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;
|
$tot_due += $sale->due_amount;
|
||||||
|
|
||||||
|
// Accumulate dynamic option totals
|
||||||
|
foreach ($paymentOptions as $opt) {
|
||||||
|
$option_totals[$opt->id] += ($sale->methods[$opt->id] ?? 0);
|
||||||
|
}
|
||||||
@endphp
|
@endphp
|
||||||
@endforeach
|
@endforeach
|
||||||
</tbody>
|
</tbody>
|
||||||
@@ -135,12 +127,11 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td colspan="4" class="text-right">Total</td>
|
<td colspan="4" class="text-right">Total</td>
|
||||||
<td class="text-right">{{ number_format($tot_grand, 2) }}</td>
|
<td class="text-right">{{ number_format($tot_grand, 2) }}</td>
|
||||||
<td class="text-right">{{ number_format($tot_cash, 2) }}</td>
|
|
||||||
<td class="text-right">{{ number_format($tot_card1, 2) }}</td>
|
@foreach($paymentOptions as $option)
|
||||||
<td class="text-right">{{ number_format($tot_card2, 2) }}</td>
|
<td class="text-right">{{ number_format($option_totals[$option->id], 2) }}</td>
|
||||||
<td class="text-right">{{ number_format($tot_card3, 2) }}</td>
|
@endforeach
|
||||||
<td class="text-right">{{ number_format($tot_online, 2) }}</td>
|
|
||||||
<td class="text-right">{{ number_format($tot_link, 2) }}</td>
|
|
||||||
<td class="text-right">{{ number_format($tot_due, 2) }}</td>
|
<td class="text-right">{{ number_format($tot_due, 2) }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tfoot>
|
</tfoot>
|
||||||
|
|||||||
Reference in New Issue
Block a user