325 lines
11 KiB
PHP
325 lines
11 KiB
PHP
<?php
|
|
|
|
namespace Modules\DailySalePaymentReportModule\Http\Controllers;
|
|
|
|
use Illuminate\Contracts\Support\Renderable;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Carbon\Carbon;
|
|
|
|
class DailySalePaymentReportModuleController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
* @return Renderable
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$report_date = $request->input('report_date', Carbon::today()->toDateString());
|
|
|
|
$data = $this->getReportData($report_date);
|
|
|
|
return view('dailysalepaymentreportmodule::index', $data);
|
|
}
|
|
|
|
private function getReportData($report_date)
|
|
{
|
|
// 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']);
|
|
|
|
$creditCollections = $this->getCreditSaleCollections($report_date);
|
|
|
|
$creditTotals = [
|
|
'grand_total' => $creditCollections->sum('grand_total'),
|
|
'methods' => [],
|
|
];
|
|
|
|
foreach ($paymentOptions as $option) {
|
|
$creditTotals['methods'][$option->id] = $creditCollections->sum(
|
|
fn($row) => $row['methods'][$option->id] ?? 0
|
|
);
|
|
}
|
|
|
|
|
|
$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
|
|
];
|
|
}
|
|
|
|
|
|
// --- Table 3: Payment Method Summary Calculation ---
|
|
$summaryData = [];
|
|
|
|
// මුලින්ම හැම Payment Option එකටම බින්දුව දාලා Array එකක් හදාගන්නවා
|
|
foreach ($paymentOptions as $option) {
|
|
$summaryData[$option->id] = [
|
|
'name' => $option->name,
|
|
'sale_amount' => 0,
|
|
'received_amount' => 0,
|
|
'total_amount' => 0
|
|
];
|
|
}
|
|
|
|
// Table 1 (Daily Sales) වල සල්ලි ටික එකතු කරනවා
|
|
foreach ($sales_data as $sale) {
|
|
foreach ($paymentOptions as $option) {
|
|
$amount = $sale->methods[$option->id] ?? 0;
|
|
$summaryData[$option->id]['sale_amount'] += $amount;
|
|
$summaryData[$option->id]['total_amount'] += $amount;
|
|
}
|
|
}
|
|
|
|
// Table 2 (Credit Collections) වල සල්ලි ටික එකතු කරනවා
|
|
foreach ($creditCollections as $credit) {
|
|
foreach ($paymentOptions as $option) {
|
|
$amount = $credit['methods'][$option->id] ?? 0;
|
|
$summaryData[$option->id]['received_amount'] += $amount;
|
|
$summaryData[$option->id]['total_amount'] += $amount;
|
|
}
|
|
}
|
|
|
|
return compact(
|
|
'sales_data',
|
|
'report_date',
|
|
'paymentOptions',
|
|
'creditCollections',
|
|
'creditTotals',
|
|
'summaryData'
|
|
);
|
|
}
|
|
|
|
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
|
|
*/
|
|
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', Carbon::today()->toDateString());
|
|
$type = $request->query('type', 'excel');
|
|
|
|
$data = $this->getReportData($date);
|
|
|
|
$filename = 'Daily_Sale_Payment_Report_' . $date;
|
|
|
|
if ($type === 'pdf') {
|
|
// Use DOMPDF directly for PDF to preserve advanced CSS and layout
|
|
$pdf = app('dompdf.wrapper')->loadView('dailysalepaymentreportmodule::export', $data);
|
|
$pdf->setPaper('a4', 'landscape');
|
|
return $pdf->download($filename . '.pdf');
|
|
}
|
|
|
|
// For Excel and CSV, use Maatwebsite Excel
|
|
$export = new \Modules\DailySalePaymentReportModule\Exports\DailySalePaymentReportExport($data);
|
|
|
|
if ($type === 'csv') {
|
|
return \Maatwebsite\Excel\Facades\Excel::download($export, $filename . '.csv', \Maatwebsite\Excel\Excel::CSV);
|
|
}
|
|
|
|
// Default to Excel
|
|
return \Maatwebsite\Excel\Facades\Excel::download($export, $filename . '.xlsx', \Maatwebsite\Excel\Excel::XLSX);
|
|
}
|
|
}
|