diff --git a/app/Http/Controllers/ConclusionController.php b/app/Http/Controllers/ConclusionController.php index d9ee9b8..6d357ba 100644 --- a/app/Http/Controllers/ConclusionController.php +++ b/app/Http/Controllers/ConclusionController.php @@ -3,21 +3,143 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; +use setasign\Fpdi\Fpdi; +use SimpleSoftwareIO\QrCode\Facades\QrCode; class ConclusionController extends Controller { public function create($id, $type) { - return view('conclusion.create', compact('id', 'type')); + // type is enum name: 'AUTO' or 'ESTATE' + $orderType = $type === 'AUTO' ? 'auto_' : 'estate_'; + $table = $type === 'AUTO' ? 'auto_orders' : 'estate_orders'; + + $order = DB::table($table . ' as o') + ->selectRaw('o.*, o.ordered_customer as customer, CONCAT_WS(\' \', o.owner_last_name, o.owner_first_name, o.owner_patronymic) as owner') + ->where('o.id', $id)->first(); + + if ($order) { + $dillerUser = $order->diller_id ? DB::table('users')->find($order->diller_id) : null; + $order->diller = $dillerUser->name ?? ''; + } + + return view('conclusion.add', compact('id', 'type', 'order')); } public function store(Request $request) { - return redirect()->back(); + $orderId = $request->input('order_id'); + $orderType = $request->input('order_type'); // 'AUTO' or 'ESTATE' + $dbType = $orderType === 'AUTO' ? 'auto_' : 'estate_'; + $table = $orderType === 'AUTO' ? 'auto_orders' : 'estate_orders'; + + // Update order prices + $updateData = ['updated_at' => now()]; + if ($request->filled('object_price')) { + $updateData['object_price'] = preg_replace('/\D/', '', $request->object_price); + } + if ($request->filled('price')) { + $updateData['price'] = preg_replace('/\D/', '', $request->price); + } + DB::table($table)->where('id', $orderId)->update($updateData); + + // Create debit if is_diller = yes + if ($request->input('is_diller') === 'yes') { + $order = DB::table($table)->find($orderId); + DB::table('debits')->insert([ + 'order_id' => $orderId, + 'order_type' => $dbType, + 'appraiser_id' => auth()->id(), + 'cost' => $order->cost ?? 0, + 'status' => 'nopaid', + 'expired' => $request->input('expired'), + 'customer' => $order->ordered_customer ?? '', + 'created_at' => now(), + 'updated_at' => now(), + ]); + } + + // Handle PDF file upload with QR watermark + if ($request->hasFile('file')) { + $file = $request->file('file'); + $ext = strtolower($file->getClientOriginalExtension()); + $name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); + $size = $file->getSize(); + + $dir = 'public/attachments/' . $dbType . $orderId; + Storage::makeDirectory($dir); + + $tmpPath = $file->getPathname(); + $outputName = Str::uuid() . '.pdf'; + $outputPath = storage_path('app/' . $dir . '/' . $outputName); + + if ($ext === 'pdf') { + // Add QR watermark to PDF + try { + $qrPath = storage_path('app/public/attachments/' . $dbType . $orderId . '/qr.png'); + if (!file_exists($qrPath)) { + Storage::makeDirectory('public/attachments/' . $dbType . $orderId); + $qrContent = url(($orderType === 'AUTO' ? 'auto' : 'estate') . '/show/' . $orderId); + $qrImage = QrCode::format('png')->size(150)->generate($qrContent); + file_put_contents($qrPath, $qrImage); + } + + $pdf = new Fpdi(); + $pageCount = $pdf->setSourceFile($tmpPath); + + for ($i = 1; $i <= $pageCount; $i++) { + $tpl = $pdf->importPage($i); + $size2 = $pdf->getTemplateSize($tpl); + $pdf->AddPage($size2['orientation'], [$size2['width'], $size2['height']]); + $pdf->useTemplate($tpl); + // Add QR to bottom-right corner + $pdf->Image($qrPath, $size2['width'] - 35, $size2['height'] - 35, 30, 30); + } + + $pdf->Output('F', $outputPath); + } catch (\Exception $e) { + // If PDF manipulation fails, just copy the original + copy($tmpPath, $outputPath); + } + } else { + $file->move(storage_path('app/' . $dir), $outputName); + } + + $storagePath = '/storage/attachments/' . $dbType . $orderId . '/' . $outputName; + + DB::table('files')->insert([ + 'id' => (string) Str::uuid(), + 'path' => $storagePath, + 'extension' => $ext, + 'order_id' => $orderId, + 'order_type' => 'conclusion_', + 'type' => 'conclusion', + 'name' => $name, + 'size' => round($size / 1024 / 1024, 2), + 'size_in_bytes' => $size, + 'created_at' => now(), + 'updated_at' => now(), + ]); + } + + $route = $orderType === 'AUTO' ? 'auto.show' : 'estate.show'; + return redirect()->route($route, $orderId); } public function reject(Request $request) { + $orderId = $request->input('order_id'); + $orderType = $request->input('order_type'); // 'AUTO' or 'ESTATE' + $table = $orderType === 'AUTO' ? 'auto_orders' : 'estate_orders'; + + DB::table($table)->where('id', $orderId)->update([ + 'status' => 'rejected', + 'updated_at' => now(), + ]); + return redirect()->back(); }