export pdf
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Modules\DailySalePaymentReportModule\Http\Controllers;
|
||||
|
||||
use Barryvdh\DomPDF\Facade\Pdf;
|
||||
use Illuminate\Contracts\Support\Renderable;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller;
|
||||
@@ -140,7 +141,7 @@ class DailySalePaymentReportModuleController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$data = $this->buildReportData($report_date);
|
||||
|
||||
return view('dailysalepaymentreportmodule::index', compact(
|
||||
'sales_data',
|
||||
@@ -148,7 +149,8 @@ class DailySalePaymentReportModuleController extends Controller
|
||||
'paymentOptions',
|
||||
'creditCollections',
|
||||
'creditTotals',
|
||||
'summaryData'
|
||||
'summaryData',
|
||||
'data'
|
||||
));
|
||||
}
|
||||
|
||||
@@ -230,6 +232,115 @@ class DailySalePaymentReportModuleController extends Controller
|
||||
return collect($rows);
|
||||
}
|
||||
|
||||
private function buildReportData($report_date)
|
||||
{
|
||||
$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();
|
||||
|
||||
$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();
|
||||
|
||||
$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 = [];
|
||||
foreach ($paymentsTable->where('sale_id', $sale->id) as $p) {
|
||||
$optionId = $p->payment_option_id;
|
||||
$methodTotals[$optionId] = ($methodTotals[$optionId] ?? 0) + $p->amount;
|
||||
}
|
||||
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
|
||||
];
|
||||
}
|
||||
|
||||
$summaryData = [];
|
||||
foreach ($paymentOptions as $option) {
|
||||
$summaryData[$option->id] = [
|
||||
'name' => $option->name,
|
||||
'sale_amount' => 0,
|
||||
'received_amount' => 0,
|
||||
'total_amount' => 0
|
||||
];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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('report_date', 'paymentOptions', 'sales_data', 'creditCollections', 'creditTotals', 'summaryData');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
* @return Renderable
|
||||
@@ -297,6 +408,22 @@ class DailySalePaymentReportModuleController extends Controller
|
||||
$type = $request->query('type');
|
||||
|
||||
|
||||
return "Export function is working. Selected Date: " . $date . " | Export Type: " . $type;
|
||||
}
|
||||
|
||||
public function exportPDF(Request $request)
|
||||
{
|
||||
$date = $request->query('date', Carbon::today()->toDateString());
|
||||
$type = $request->query('type');
|
||||
|
||||
$data = $this->buildReportData($date);
|
||||
|
||||
if ($type === 'pdf') {
|
||||
$pdf = Pdf::loadView('dailysalepaymentreportmodule::export.pdf', $data)
|
||||
->setPaper('a4', 'landscape');
|
||||
return $pdf->download('daily-sale-payment-report-' . $date . '.pdf');
|
||||
}
|
||||
|
||||
return "Export function is working. Selected Date: " . $date . " | Export Type: " . $type;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user