ifnex/04_Laravel/scripts/test_pdf_generation.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

158 lines
6.4 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* اسکریپت تست تولید PDF برای IFNEX
*
* این اسکریپت یه محموله نمونه می‌سازه و سه تا PDF (AWB, Invoice, Label) تولید می‌کنه
* و اون‌ها رو توی پوشه storage/app/pdf-test/ ذخیره می‌کنه.
*
* استفاده:
* php test_pdf_generation.php
*
* پس از اجرا، فایل‌های تولیدشده رو بررسی کنید:
* storage/app/pdf-test/AWB-test.pdf
* storage/app/pdf-test/INVOICE-test.pdf
* storage/app/pdf-test/LABEL-test.pdf
*/
require __DIR__ . '/vendor/autoload.php';
$app = require_once __DIR__ . '/bootstrap/app.php';
$app->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);
}
// ─── ۱.۵. تست تولید بارکد ───
echo "1.5️⃣ Testing barcode generation...\n";
try {
$generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
$barcode = $generator->getBarcode('TEST123', $generator::TYPE_CODE_128, 3, 80);
$b64 = 'data:image/png;base64,' . base64_encode($barcode);
echo " ✅ Barcode generated, length: " . strlen($b64) . " chars\n";
echo " Preview: " . substr($b64, 0, 50) . "...\n\n";
} catch (\Throwable $e) {
echo " ❌ Barcode generation failed: " . $e->getMessage() . "\n\n";
}
// ─── ۲. ایجاد پوشه تست ───
$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";