input('report_date', Carbon::today()->toDateString()); // 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']); $sales = DB::table('sales') ->leftJoin('users', 'sales.user_id', '=', 'users.id') ->leftJoin('customers', 'sales.customer_id', '=', 'customers.id') ->whereDate('sales.created_at', $report_date) ->select( 'sales.id', 'sales.created_at as date', 'sales.reference_no', 'users.name as cashier', 'customers.name as customer', 'sales.grand_total', 'sales.due_amount' )->get(); $saleIds = $sales->pluck('id')->all(); // 1. Get all payments for these sales (avoids N+1 query problem) $paymentsTable = DB::table('payments') ->whereIn('sale_id', $saleIds) ->whereDate('created_at', $report_date) ->get(['id', 'sale_id', 'amount', 'payment_option_id']); $usedPaymentIds = $paymentsTable->pluck('id')->all(); // 2. Get additional payments from sales_payments table $salesPaymentsTable = DB::table('sales_payments') ->whereIn('sale_id', $saleIds) ->whereDate('created_at', $report_date) ->where(function ($q) use ($usedPaymentIds) { $q->whereNull('payment_id'); if (!empty($usedPaymentIds)) { $q->orWhereNotIn('payment_id', $usedPaymentIds); } }) ->get(['id', 'sale_id', 'amount', 'payment_id']); $linkedPaymentIds = $salesPaymentsTable->whereNotNull('payment_id')->pluck('payment_id')->unique(); $linkedOptionMap = DB::table('payments') ->whereIn('id', $linkedPaymentIds) ->pluck('payment_option_id', 'id'); $sales_data = []; foreach ($sales as $sale) { $methodTotals = []; // Sum from payments table foreach ($paymentsTable->where('sale_id', $sale->id) as $p) { $optionId = $p->payment_option_id; $methodTotals[$optionId] = ($methodTotals[$optionId] ?? 0) + $p->amount; } // Sum from sales_payments table foreach ($salesPaymentsTable->where('sale_id', $sale->id) as $sp) { $optionId = $sp->payment_id ? ($linkedOptionMap[$sp->payment_id] ?? null) : null; if ($optionId) { $methodTotals[$optionId] = ($methodTotals[$optionId] ?? 0) + $sp->amount; } } $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, 'methods' => $methodTotals ]; } return view('dailysalepaymentreportmodule::index', compact('sales_data', 'report_date', 'paymentOptions')); } /** * Show the form for creating a new resource. * @return Renderable */ public function create() { return view('dailysalepaymentreportmodule::create'); } /** * Store a newly created resource in storage. * @param Request $request * @return Renderable */ public function store(Request $request) { // } /** * Show the specified resource. * @param int $id * @return Renderable */ public function show($id) { return view('dailysalepaymentreportmodule::show'); } /** * Show the form for editing the specified resource. * @param int $id * @return Renderable */ public function edit($id) { return view('dailysalepaymentreportmodule::edit'); } /** * Update the specified resource in storage. * @param Request $request * @param int $id * @return Renderable */ public function update(Request $request, $id) { // } /** * Remove the specified resource from storage. * @param int $id * @return Renderable */ public function destroy($id) { // } public function exportReport(Request $request) { $date = $request->query('date'); $type = $request->query('type'); return "Export function is working. Selected Date: " . $date . " | Export Type: " . $type; } }