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; }; // --- TABLE 1: Daily Sales --- $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(); $sales_data = []; foreach ($sales as $sale) { // Get payments ONLY made on the report_date $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') ->get(); $breakdown = $calculatePayments($payments); $sales_data[] = (object) array_merge([ '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); } return view('dailysalepaymentreportmodule::index', compact('sales_data', 'report_date')); } /** * 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; } }