From b7b2ba7b5686509fe6b4cf389693f9745117d04b Mon Sep 17 00:00:00 2001 From: SUM2046 Date: Fri, 3 Jul 2026 15:37:24 +0530 Subject: [PATCH] saleTable n credit sale collection --- ...DailySalePaymentReportModuleController.php | 82 ++- Resources/views/index.blade.php | 543 +++++++++--------- 2 files changed, 354 insertions(+), 271 deletions(-) diff --git a/Http/Controllers/DailySalePaymentReportModuleController.php b/Http/Controllers/DailySalePaymentReportModuleController.php index e5c108a..6b757c9 100644 --- a/Http/Controllers/DailySalePaymentReportModuleController.php +++ b/Http/Controllers/DailySalePaymentReportModuleController.php @@ -23,6 +23,8 @@ class DailySalePaymentReportModuleController extends Controller ->orderBy('id') ->get(['id', 'name']); + $creditCollections = $this->getCreditSaleCollections($report_date); + $sales = DB::table('sales') ->leftJoin('users', 'sales.user_id', '=', 'users.id') @@ -95,7 +97,85 @@ class DailySalePaymentReportModuleController extends Controller ]; } - return view('dailysalepaymentreportmodule::index', compact('sales_data', 'report_date', 'paymentOptions')); + return view('dailysalepaymentreportmodule::index', compact('sales_data', 'report_date', 'paymentOptions', 'creditCollections')); + } + + private function getCreditSaleCollections($date) + { + $paymentsTable = DB::table('payments') + ->whereNotNull('sale_id') + ->whereDate('created_at', $date) + ->get(['id', 'sale_id', 'amount', 'payment_option_id']); + + $usedPaymentIds = $paymentsTable->pluck('id')->all(); + + $salesPaymentsTable = DB::table('sales_payments') + ->whereDate('created_at', $date) + ->where(function ($q) use ($usedPaymentIds) { + $q->whereNull('payment_id'); + if (!empty($usedPaymentIds)) { + $q->orWhereNotIn('payment_id', $usedPaymentIds); + } + }) + ->get(['id', 'sale_id', 'amount', 'payment', 'payment_id']); + + $linkedPaymentIds = $salesPaymentsTable->whereNotNull('payment_id')->pluck('payment_id')->unique(); + + $linkedOptionMap = DB::table('payments') + ->whereIn('id', $linkedPaymentIds) + ->pluck('payment_option_id', 'id'); + + $saleIds = $paymentsTable->pluck('sale_id') + ->merge($salesPaymentsTable->pluck('sale_id')) + ->unique() + ->values(); + + if ($saleIds->isEmpty()) { + return collect(); + } + + $sales = DB::table('sales') + ->whereIn('id', $saleIds) + ->whereDate('created_at', '<', $date) + ->select('id', 'reference_no', 'user_id', 'customer_id', 'grand_total', 'paid_amount', 'created_at') + ->get() + ->keyBy('id'); + + if ($sales->isEmpty()) { + return collect(); + } + + $users = DB::table('users')->whereIn('id', $sales->pluck('user_id')->unique())->pluck('name', 'id'); + $customers = DB::table('customers')->whereIn('id', $sales->pluck('customer_id')->unique())->pluck('name', 'id'); + + $rows = []; + + foreach ($sales as $saleId => $sale) { + $methodTotals = []; + + foreach ($paymentsTable->where('sale_id', $saleId) as $p) { + $optionId = $p->payment_option_id; + $methodTotals[$optionId] = ($methodTotals[$optionId] ?? 0) + $p->amount; + } + + foreach ($salesPaymentsTable->where('sale_id', $saleId) as $sp) { + $optionId = $sp->payment_id ? ($linkedOptionMap[$sp->payment_id] ?? null) : null; + if ($optionId) { + $methodTotals[$optionId] = ($methodTotals[$optionId] ?? 0) + $sp->amount; + } + } + + $rows[] = [ + 'date' => $sale->created_at, + 'reference' => $sale->reference_no, + 'cashier' => $users[$sale->user_id] ?? '-', + 'customer' => $customers[$sale->customer_id] ?? '-', + 'grand_total' => $sale->grand_total - $sale->paid_amount, + 'methods' => $methodTotals, + ]; + } + + return collect($rows); } /** diff --git a/Resources/views/index.blade.php b/Resources/views/index.blade.php index 2ba8cca..7c4fc76 100644 --- a/Resources/views/index.blade.php +++ b/Resources/views/index.blade.php @@ -1,296 +1,299 @@ @extends('backend.layout.main') @section('content') - @if ($errors->has('title')) -
- {{ $errors->first('title') }} -
- @endif - @if (session()->has('message')) -
- {{ session()->get('message') }} -
- @endif - @if (session()->has('not_permitted')) -
- {{ session()->get('not_permitted') }} -
- @endif +@if ($errors->has('title')) +
+ {{ $errors->first('title') }} +
+@endif +@if (session()->has('message')) +
+ {{ session()->get('message') }} +
+@endif +@if (session()->has('not_permitted')) +
+ {{ session()->get('not_permitted') }} +
+@endif -
-
-
- -
-

Daily Sale Payment Report

-
+
+
+
+ +
+

Daily Sale Payment Report

+
-
-
-
- -
- -
-
-
- + +
+
+ +
+
- -
-
- -
- -
-
- - - - +
+ +
+ +
+
+ +
+ +
+
+ + + +
+
- -

Sales Table (Daily Sale)

-
- - - - - - - - - @foreach($paymentOptions as $option) - - @endforeach - - - - - @php - $tot_grand = 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) - - - - - - - - @foreach($paymentOptions as $option) - - @endforeach - - - - - @php - $tot_grand += $sale->grand_total; - $tot_due += $sale->due_amount; - - // Accumulate dynamic option totals - foreach ($paymentOptions as $opt) { - $option_totals[$opt->id] += ($sale->methods[$opt->id] ?? 0); - } - @endphp + +

Sales Table (Daily Sale)

+
+
DateReference NumberCashierCustomerGrand Total{{ $option->name }}Due Amount
{{ $sale->date }}{{ $sale->reference_no }}{{ $sale->cashier }}{{ $sale->customer }}{{ number_format($sale->grand_total, 2) }}{{ number_format($sale->methods[$option->id] ?? 0, 2) }}{{ number_format($sale->due_amount, 2) }}
+ + + + + + + + @foreach($paymentOptions as $option) + @endforeach - - - - - - - @foreach($paymentOptions as $option) - - @endforeach - - - - -
DateReference NumberCashierCustomerGrand Total{{ $option->name }}
Total{{ number_format($tot_grand, 2) }}{{ number_format($option_totals[$option->id], 2) }}{{ number_format($tot_due, 2) }}
-
+ Due Amount + + + + @php + $tot_grand = 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) + + {{ $sale->date }} + {{ $sale->reference_no }} + {{ $sale->cashier }} + {{ $sale->customer }} + {{ number_format($sale->grand_total, 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_due += $sale->due_amount; + + // Accumulate dynamic option totals + foreach ($paymentOptions as $opt) { + $option_totals[$opt->id] += ($sale->methods[$opt->id] ?? 0); + } + @endphp + @endforeach + + + + Total + {{ number_format($tot_grand, 2) }} + + @foreach($paymentOptions as $option) + {{ number_format($option_totals[$option->id], 2) }} + @endforeach + + {{ number_format($tot_due, 2) }} + + +
+
-
- -

Credit Sale Collections

-
- - - - - - - - - - - - - - - - - - {{-- @foreach ($credit_data as $credit) --}} - - {{-- @endforeach --}} - - - - - - - - - - - - - -
Original Bill DateReference NumberCashierCustomerGrand TotalCashCard 1Card 2Card 3Online TransferLink Pay
Total0.000.000.000.000.000.000.00
-
+
+ +

Credit Sale Collections

+
+ + + + + + + + + @foreach ($paymentOptions as $option) + + @endforeach + + + + @foreach ($creditCollections as $row) + + + + + + + @foreach ($paymentOptions as $option) + + @endforeach + + @endforeach + + + + + + @foreach ($paymentOptions as $option) + + @endforeach + + +
Original Bill DateReference NumberCashierCustomerGrand Total{{ $option->name }}
{{ \Carbon\Carbon::parse($row['date'])->format('Y-m-d') }}{{ $row['reference'] }}{{ $row['cashier'] }}{{ $row['customer'] }}{{ number_format($row['grand_total'], 2) }}{{ number_format($row['methods'][$option->id] ?? 0, 2) }}
Total0.000.00
+
-
- -

Payment Method Summary

-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Payment OptionSale AmountReceived AmountTotal Amount
Cash0.000.000.00
Card 10.000.000.00
Card 20.000.000.00
Card 30.000.000.00
Online Transfer0.000.000.00
Link Pay0.000.000.00
Grand Total0.000.000.00
-
+
+ +

Payment Method Summary

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Payment OptionSale AmountReceived AmountTotal Amount
Cash0.000.000.00
Card 10.000.000.00
Card 20.000.000.00
Card 30.000.000.00
Online Transfer0.000.000.00
Link Pay0.000.000.00
Grand Total0.000.000.00
-
+
+
- + table { + border-collapse: collapse !important; + } + @endsection @push('scripts') - + }); + + // මුළු රිපෝට් එකම එක්ස්පෝර්ට් කරන්න හදපු Function එක + function exportFullReport(exportType) { + var selectedDate = $('#report_date').val(); + + // ඔයාගේ Controller එකේ export route එකට මේක හරවන්න ඕනේ. + // උදාහරණයක් විදිහට: url('/dailysalepaymentreportmodule/export') + var exportUrl = "{{ url('/dailysalepaymentreportmodule/export') }}" + "?date=" + selectedDate + "&type=" + exportType; + + window.location.href = exportUrl; + } + @endpush \ No newline at end of file