make('Illuminate\Contracts\Console\Kernel')->bootstrap(); use App\Models\Shipment; use App\Models\ShipmentItem; use App\Models\Country; use App\Services\PdfService; use Illuminate\Support\Facades\File; echo "═══════════════════════════════════════════\n"; echo "🧪 IFNEX PDF Generation Test\n"; echo "═══════════════════════════════════════════\n\n"; // ─── ۱. بررسی نصب بودن کتابخانه بارکد ─── echo "1️⃣ Checking barcode library...\n"; if (class_exists(\Picqer\Barcode\BarcodeGeneratorPNG::class)) { echo " ✅ picqer/php-barcode-generator is installed\n\n"; } else { echo " ❌ picqer/php-barcode-generator is NOT installed\n"; echo " Run: composer require picqer/php-barcode-generator\n\n"; exit(1); } // ─── ۲. ایجاد پوشه تست ─── $testDir = storage_path('app/pdf-test'); if (!File::exists($testDir)) { File::makeDirectory($testDir, 0755, true); echo "2️⃣ Created test directory: {$testDir}\n\n"; } else { echo "2️⃣ Test directory exists: {$testDir}\n\n"; } // ─── ۳. پیدا کردن یه محموله نمونه ─── echo "3️⃣ Finding sample shipment...\n"; $shipment = Shipment::with(['fromCountry', 'toCountry', 'items'])->latest()->first(); if (!$shipment) { echo " ❌ No shipment found in database. Please create one first.\n"; exit(1); } echo " ✅ Found shipment: {$shipment->awb_no}\n"; echo " - Type: {$shipment->type?->value}\n"; echo " - Direction: {$shipment->direction?->value}\n"; echo " - From: {$shipment->fromCountry?->name}\n"; echo " - To: {$shipment->toCountry?->name}\n"; echo " - Items: {$shipment->items->count()}\n\n"; // ─── ۴. اگر آیتم نداره، یه آیتم تستی اضافه کن ─── if ($shipment->items->isEmpty()) { echo " ⚠️ Shipment has no items. Adding test item...\n"; ShipmentItem::create([ 'shipment_id' => $shipment->id, 'row_number' => 1, 'description' => 'Electronics PCB Board', 'hs_code' => '8542390001', 'quantity' => 104, 'unit_price' => 1.10, 'total_usd' => 114.40, ]); $shipment->load('items'); echo " ✅ Added test item\n\n"; } // ─── ۵. تولید AWB PDF ─── echo "4️⃣ Generating AWB PDF...\n"; try { $pdfService = app(PdfService::class); $awbContent = $pdfService->awb($shipment); $awbPath = $testDir . '/AWB-' . $shipment->awb_no . '.pdf'; File::put($awbPath, $awbContent); echo " ✅ AWB PDF saved: {$awbPath}\n"; echo " Size: " . number_format(strlen($awbContent) / 1024, 2) . " KB\n\n"; } catch (\Throwable $e) { echo " ❌ AWB PDF failed: " . $e->getMessage() . "\n\n"; } // ─── ۶. تولید Invoice PDF ─── echo "5️⃣ Generating Invoice PDF...\n"; try { $invoiceContent = $pdfService->invoice($shipment); $invoicePath = $testDir . '/INVOICE-' . $shipment->awb_no . '.pdf'; File::put($invoicePath, $invoiceContent); echo " ✅ Invoice PDF saved: {$invoicePath}\n"; echo " Size: " . number_format(strlen($invoiceContent) / 1024, 2) . " KB\n\n"; } catch (\InvalidArgumentException $e) { echo " ⚠️ Invoice skipped: " . $e->getMessage() . "\n"; echo " (این طبیعی است اگر محموله DOC است)\n\n"; } catch (\Throwable $e) { echo " ❌ Invoice PDF failed: " . $e->getMessage() . "\n\n"; } // ─── ۷. تولید Label PDF ─── echo "6️⃣ Generating Label PDF...\n"; try { $labelContent = $pdfService->label($shipment); $labelPath = $testDir . '/LABEL-' . $shipment->awb_no . '.pdf'; File::put($labelPath, $labelContent); echo " ✅ Label PDF saved: {$labelPath}\n"; echo " Size: " . number_format(strlen($labelContent) / 1024, 2) . " KB\n\n"; } catch (\Throwable $e) { echo " ❌ Label PDF failed: " . $e->getMessage() . "\n\n"; } // ─── ۸. تست محموله DOC (بدون فاکتور) ─── echo "7️⃣ Testing DOC shipment (should skip invoice)...\n"; $docShipment = Shipment::where('type', 'DOC_NORMAL')->first(); if ($docShipment) { try { $docShipment->load('items'); if ($docShipment->items->isEmpty()) { $pdfService->invoice($docShipment); echo " ❌ ERROR: Should have thrown exception for DOC shipment!\n\n"; } else { echo " ℹ️ DOC shipment has items, invoice would work\n\n"; } } catch (\InvalidArgumentException $e) { echo " ✅ Correctly skipped invoice for DOC: " . $e->getMessage() . "\n\n"; } } else { echo " ℹ️ No DOC shipment found for testing (skipped)\n\n"; } echo "═══════════════════════════════════════════\n"; echo "✅ Test completed!\n"; echo "═══════════════════════════════════════════\n\n"; echo "📁 Check the generated PDFs at:\n"; echo " {$testDir}\n\n";