ifnex/04_Laravel/app/Http/Controllers/ShipmentPdfController.php
Kazem Alghasi f12dff5338 - Add picqer/php-barcode-generator dependency
- Redesign AWB PDF with barcode, From/To, Value, Service, Type
- Redesign Invoice PDF with 9-row table, legal declaration, barcode
- Redesign Label PDF to A5 landscape with large barcode
- Add DOC/PARCEL condition for Invoice (only PARCEL has invoice)
- Fix ShipmentType enum names (DocNormal/DocEconomy)
- Add migration for shipment_items (row_number, unit_price)
- Add migration to make name nullable
- Update PdfService with base64 barcode embedding
- Update ShipmentPdfController with DOC type handling
- Add test_pdf_generation.php script

Phase 3.5 — PDF redesign complete"
2026-08-29 06:40:08 +03:30

132 lines
4.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Enums\ShipmentType;
use App\Models\Shipment;
use App\Services\PdfService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class ShipmentPdfController extends Controller
{
public function __construct(protected PdfService $pdf) {}
/**
* تولید AWB PDF (برای همه نوع محموله‌ها)
*/
public function awb(Shipment $shipment)
{
try {
Log::info('Generating AWB PDF', ['shipment_id' => $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);
}
}
}