ifnex/04_Laravel/app/Services/PdfService.php
Kazem Alghasi 157ce1bdd4 feat(shipment): add multi-package support and AWB integration
Introduce a new shipment packages system to allow handling shipments
consisting of multiple individual packages. This includes a new
ShipmentPackage model, a dedicated database table, and a repeater
field in the Filament ShipmentResource for managing package details
such as weight, dimensions, and declared value.

Additionally, update the AWB PDF generation service and template to
include a detailed breakdown of all associated packages when multiple
items are present.
2026-08-24 01:47:45 +03:30

98 lines
3.3 KiB
PHP

<?php
namespace App\Services;
use App\Models\Shipment;
use Dompdf\Dompdf;
use Dompdf\Options;
use Illuminate\Support\Collection;
use Picqer\Barcode\BarcodeGeneratorHTML;
class PdfService
{
public function awb(Shipment $shipment): string
{
$shipment->loadMissing(['fromCountry', 'toCountry', 'items', 'packages']);
$data = [
'shipment' => $shipment,
'shipper' => $this->formatAddress($shipment, 'sender'),
'receiver' => $this->formatAddress($shipment, 'receiver'),
'items' => $shipment->items ?? collect(),
'barcode' => $this->generateBarcode($shipment->awb_no),
];
return $this->generatePdf(view('pdfs.awb', $data)->render(), 'A4', 'portrait');
}
public function invoice(Shipment $shipment): string
{
$shipment->loadMissing(['fromCountry', 'toCountry', 'items']);
$data = [
'shipment' => $shipment,
'shipper' => $this->formatAddress($shipment, 'sender'),
'receiver' => $this->formatAddress($shipment, 'receiver'),
'items' => $shipment->isParcel() ? ($shipment->items ?? collect()) : collect(),
'invoice_total_usd' => $shipment->invoice_total_usd,
'barcode' => $this->generateBarcode($shipment->awb_no),
];
return $this->generatePdf(view('pdfs.invoice', $data)->render(), 'A4', 'portrait');
}
public function label(Shipment $shipment): string
{
$shipment->loadMissing(['fromCountry', 'toCountry']);
$data = [
'shipment' => $shipment,
'shipper' => $this->formatAddress($shipment, 'sender'),
'receiver' => $this->formatAddress($shipment, 'receiver'),
'barcode' => $this->generateBarcode($shipment->awb_no),
];
return $this->generatePdf(view('pdfs.label', $data)->render(), 'A4', 'portrait');
}
private function generatePdf(string $html, string $paper, string $orientation): string
{
$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true);
$options->set('defaultFont', 'DejaVu Sans');
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->setPaper($paper, $orientation);
$dompdf->render();
return $dompdf->output();
}
private function formatAddress(Shipment $shipment, string $prefix): Collection
{
return collect([
'name' => $shipment->{"{$prefix}_name"},
'company' => $shipment->{"{$prefix}_company"},
'phone' => $shipment->{"{$prefix}_phone"},
'email' => $shipment->{"{$prefix}_email"},
'address' => $shipment->{"{$prefix}_address"},
'city' => $shipment->{"{$prefix}_city"},
'state' => $shipment->{"{$prefix}_state"},
'zip' => $shipment->{"{$prefix}_zip"},
'id_number' => $shipment->{"{$prefix}_id_number"},
]);
}
private function generateBarcode(string $code): string
{
try {
$generator = new BarcodeGeneratorHTML();
return $generator->getBarcode($code, $generator::TYPE_CODE_128, 2, 60);
} catch (\Throwable $e) {
return '<div style="color:#999;font-size:10px;">Barcode unavailable</div>';
}
}
}