$shipment->id]); $content = $this->pdf->awb($shipment); Log::info('AWB PDF generated successfully', ['awb' => $shipment->awb_no]); return response($content, 200, [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="AWB-' . $shipment->awb_no . '.pdf"', ]); } catch (\Throwable $e) { Log::error('AWB PDF generation failed', [ 'shipment_id' => $shipment->id, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); return response('PDF generation failed: ' . $e->getMessage(), 500); } } /** * تولید Invoice PDF — فقط برای محموله‌های دارای کالا (PARCEL) * * محموله‌های DOC (DOC_NORMAL/DOC_ECONOMY) که کالا ندارن، * فاکتور صادراتی ندارن و این متد براشون ارور برمی‌گردونه. */ public function invoice(Shipment $shipment) { try { // بارگذاری items برای بررسی $shipment->loadMissing(['items']); // بررسی نوع محموله $isDocType = in_array($shipment->type, [ ShipmentType::DocNormal, ShipmentType::DocEconomy, ]); if ($isDocType && $shipment->items->isEmpty()) { Log::info('Invoice PDF not available for DOC shipment without items', [ 'shipment_id' => $shipment->id, 'type' => $shipment->type?->value, ]); return response()->json([ 'success' => false, 'message' => 'فاکتور فقط برای محموله‌های دارای کالا (PARCEL) صادر می‌شود.', 'hint' => 'محموله‌های DOC (مدارک) فاکتور صادراتی ندارند.', ], 400); } $content = $this->pdf->invoice($shipment); Log::info('Invoice PDF generated successfully', [ 'awb' => $shipment->awb_no, 'type' => $shipment->type?->value, 'items_count' => $shipment->items->count(), ]); return response($content, 200, [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="INVOICE-' . $shipment->awb_no . '.pdf"', ]); } catch (\InvalidArgumentException $e) { // خطای مربوط به نوع محموله Log::info('Invoice PDF skipped', [ 'shipment_id' => $shipment->id, 'reason' => $e->getMessage(), ]); return response()->json([ 'success' => false, 'message' => $e->getMessage(), ], 400); } catch (\Throwable $e) { Log::error('Invoice PDF generation failed', [ 'shipment_id' => $shipment->id, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); return response('PDF generation failed: ' . $e->getMessage(), 500); } } /** * تولید Label PDF — اندازه استاندارد لیبل پستی (100x150mm) */ public function label(Shipment $shipment) { try { $content = $this->pdf->label($shipment); Log::info('Label PDF generated successfully', ['awb' => $shipment->awb_no]); return response($content, 200, [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="LABEL-' . $shipment->awb_no . '.pdf"', ]); } catch (\Throwable $e) { Log::error('Label PDF generation failed', [ 'shipment_id' => $shipment->id, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); return response('PDF generation failed: ' . $e->getMessage(), 500); } } }