From cdb7e83a99f8ff246b38a18858666ff644d270ad Mon Sep 17 00:00:00 2001 From: SUM2046 Date: Fri, 3 Jul 2026 14:47:43 +0530 Subject: [PATCH] credit sale collection --- ...DailySalePaymentReportModuleController.php | 90 ++++++++++++++++++- Resources/views/index.blade.php | 15 +++- 2 files changed, 100 insertions(+), 5 deletions(-) diff --git a/Http/Controllers/DailySalePaymentReportModuleController.php b/Http/Controllers/DailySalePaymentReportModuleController.php index a1661cb..e7b1ae1 100644 --- a/Http/Controllers/DailySalePaymentReportModuleController.php +++ b/Http/Controllers/DailySalePaymentReportModuleController.php @@ -5,6 +5,8 @@ namespace Modules\DailySalePaymentReportModule\Http\Controllers; use Illuminate\Contracts\Support\Renderable; use Illuminate\Http\Request; use Illuminate\Routing\Controller; +use Illuminate\Support\Facades\DB; +use Illuminate\Validation\Rules\Unique; use Modules\DailySalePaymentReportModule\Entities\PaymentOption; class DailySalePaymentReportModuleController extends Controller @@ -13,17 +15,101 @@ class DailySalePaymentReportModuleController extends Controller * Display a listing of the resource. * @return Renderable */ - public function index() + public function index(Request $request) { $paymentOptions = PaymentOption::where('is_active', 1) ->orderBy('id') ->get(['id', 'name']); - + + $reportDate = $request->input('report_date', date('Y-m-d')); + + $creditCollections = $this->getCreditSaleCollections($reportDate); + return view('dailysalepaymentreportmodule::index', [ 'paymentOptions' => $paymentOptions, + 'reportDate' => $reportDate, + 'creditCollections' => $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); + } + /** * Show the form for creating a new resource. * @return Renderable diff --git a/Resources/views/index.blade.php b/Resources/views/index.blade.php index fdf98b2..e7c5903 100644 --- a/Resources/views/index.blade.php +++ b/Resources/views/index.blade.php @@ -127,11 +127,21 @@ @foreach ($paymentOptions as $option) {{ $option->name }} @endforeach - Due Amount - + @foreach ($creditCollections as $row) + + {{ \Carbon\Carbon::parse($row['date'])->format('Y-m-d') }} + {{ $row['reference'] }} + {{ $row['cashier'] }} + {{ $row['customer'] }} + {{ number_format($row['grand_total'], 2) }} + @foreach ($paymentOptions as $option) + {{ number_format($row['methods'][$option->id] ?? 0, 2) }} + @endforeach + + @endforeach @@ -140,7 +150,6 @@ @foreach ($paymentOptions as $option) 0.00 @endforeach - 0.00