generatePdf: serve uploaded conclusion file instead of system-generated PDF

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-07 11:07:10 +05:00
parent 5423f80fe6
commit ea2f24f0a4
2 changed files with 28 additions and 83 deletions

View File

@@ -287,53 +287,23 @@ class AutoController extends Controller
public function generatePdf($id) public function generatePdf($id)
{ {
$order = DB::table('auto_orders as o') // If uploaded conclusion exists — serve it directly
->selectRaw('o.*, p.uz as purpose, c.uz as category, CONCAT_WS(\' \', o.owner_last_name, o.owner_first_name, o.owner_patronymic) as "ownerName"') $conclusionFile = DB::table('files')
->leftJoin('purposes as p', 'p.id', '=', 'o.purpose_id') ->where('order_id', $id)
->leftJoin('concerns as c', 'c.id', '=', 'o.car_category') ->where('order_type', 'conclusion_')
->where('o.id', $id) ->orderByDesc('created_at')
->first(); ->first();
if (!$order) { if ($conclusionFile) {
abort(404); $path = storage_path('app/public' . str_replace('/storage', '', $conclusionFile->path));
if (file_exists($path)) {
return response()->file($path, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="' . $conclusionFile->name . '.pdf"',
]);
}
} }
$pdf = new Fpdi(); abort(404, 'Xulosa fayli topilmadi');
$pdf->AddPage();
// Add Title
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(0, 10, 'BAXOLASH XULOSASI (AVTO)', 0, 1, 'C');
$pdf->Ln(10);
// Add Details
$pdf->SetFont('Arial', '', 12);
$fields = [
'Tartib raqami' => $order->number,
'Sana' => date('d.m.Y', strtotime($order->created_at)),
'Maqsadi' => $order->purpose,
'Kategoriya' => $order->category,
'Markasi' => $order->car_mark,
'Davlat raqami' => $order->car_number,
'Egasi' => $order->ownerName,
'Ishlab chiqarilgan yili' => $order->made_date,
'Baholangan summa' => number_format($order->object_price ?: 0, 0, '.', ' ') . ' UZS',
];
foreach ($fields as $label => $value) {
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(60, 10, $label . ':', 0, 0);
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(0, 10, $value ?: '-', 0, 1);
}
$pdf->Ln(20);
$pdf->SetFont('Arial', 'I', 10);
$pdf->Cell(0, 10, 'Ushbu hujjat tizim tomonidan avtomatik generatsiya qilindi.', 0, 1, 'C');
return response($pdf->Output('S'), 200)
->header('Content-Type', 'application/pdf')
->header('Content-Disposition', 'attachment; filename="Conclusion_'.$order->number.'.pdf"');
} }
} }

View File

@@ -238,49 +238,24 @@ class EstateController extends Controller
public function generatePdf($id) public function generatePdf($id)
{ {
$order = DB::table('estate_orders as o') // If uploaded conclusion exists — serve it directly
->selectRaw('o.*, p.uz as purpose, r.uz as region_name, d.uz as district_name, CONCAT_WS(\' \', o.owner_last_name, o.owner_first_name, o.owner_patronymic) as "ownerName"') $conclusionFile = DB::table('files')
->leftJoin('purposes as p', 'p.id', '=', 'o.purpose_id') ->where('order_id', $id)
->leftJoin('regions as r', 'r.id', '=', 'o.region') ->where('order_type', 'conclusion_')
->leftJoin('districts as d', 'd.id', '=', 'o.district') ->orderByDesc('created_at')
->where('o.id', $id)
->first(); ->first();
if (!$order) abort(404); if ($conclusionFile) {
$path = storage_path('app/public' . str_replace('/storage', '', $conclusionFile->path));
$pdf = new \setasign\Fpdi\Fpdi(); if (file_exists($path)) {
$pdf->AddPage(); return response()->file($path, [
$pdf->SetFont('Arial', 'B', 16); 'Content-Type' => 'application/pdf',
$pdf->Cell(0, 10, 'BAXOLASH XULOSASI (KO\'CHMAS MULK)', 0, 1, 'C'); 'Content-Disposition' => 'attachment; filename="' . $conclusionFile->name . '.pdf"',
$pdf->Ln(10); ]);
$pdf->SetFont('Arial', '', 12); }
$fields = [
'Tartib raqami' => $order->number,
'Sana' => date('d.m.Y', strtotime($order->created_at)),
'Maqsadi' => $order->purpose,
'Ob\'yekt nomi' => $order->name_of_object,
'Viloyat' => $order->region_name,
'Tuman' => $order->district_name,
'Manzil' => $order->address,
'Egasi' => $order->ownerName,
'Baholangan summa'=> number_format($order->object_price ?: 0, 0, '.', ' ') . ' UZS',
];
foreach ($fields as $label => $value) {
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(60, 10, $label . ':', 0, 0);
$pdf->SetFont('Arial', '', 12);
$pdf->Cell(0, 10, $value ?: '-', 0, 1);
} }
$pdf->Ln(20); abort(404, 'Xulosa fayli topilmadi');
$pdf->SetFont('Arial', 'I', 10);
$pdf->Cell(0, 10, 'Ushbu hujjat tizim tomonidan avtomatik generatsiya qilindi.', 0, 1, 'C');
return response($pdf->Output('S'), 200)
->header('Content-Type', 'application/pdf')
->header('Content-Disposition', 'attachment; filename="Conclusion_'.$order->number.'.pdf"');
} }
public function showDocuments($order, Request $request) public function showDocuments($order, Request $request)