Introduce PDF generation capabilities for shipments using laravel-dompdf. This includes a new service layer, dedicated controller, and routes to handle the generation of Air Waybills (AWB), Invoices, and Shipping Labels. - Add `PdfService` to encapsulate PDF generation logic. - Add `ShipmentPdfController` to handle PDF download requests. - Integrate `barryvdh/laravel-dompdf` dependency. - Add Filament header actions to view shipment pages for quick PDF access. - Update order success view to provide direct download links for documents. - Add label methods to `ShipmentDirection` and `ShipmentType` enums. - Update project status documentation to reflect initial PDF implementation.
97 lines
3.0 KiB
PHP
97 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Shipment;
|
|
use App\Models\ShipmentItem;
|
|
use Dompdf\Dompdf;
|
|
use Dompdf\Options;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class PdfService
|
|
{
|
|
public function awb(Shipment $shipment): string
|
|
{
|
|
$shipment->loadMissing(['fromCountry', 'toCountry', 'items']);
|
|
|
|
$data = [
|
|
'shipment' => $shipment,
|
|
'shipper' => $this->formatAddress($shipment, 'sender'),
|
|
'receiver' => $this->formatAddress($shipment, 'receiver'),
|
|
'items' => $shipment->items ?? collect(),
|
|
'invoice_total_usd' => optional($shipment->items)->sum('total_usd') ?? 0,
|
|
];
|
|
|
|
$html = view('pdfs.awb', $data)->render();
|
|
|
|
return $this->generatePdf($html, '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->items ?? collect(),
|
|
'invoice_total_usd' => optional($shipment->items)->sum('total_usd') ?? 0,
|
|
];
|
|
|
|
$html = view('pdfs.invoice', $data)->render();
|
|
|
|
return $this->generatePdf($html, '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'),
|
|
];
|
|
|
|
$html = view('pdfs.label', $data)->render();
|
|
|
|
return $this->generatePdf($html, [0, 0, 80, 120], 'portrait');
|
|
}
|
|
|
|
private function generatePdf(string $html, array|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);
|
|
|
|
if (is_array($paper)) {
|
|
$dompdf->setPaper($paper, $orientation);
|
|
} else {
|
|
$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'},
|
|
'zip' => $shipment->{$prefix . '_zip'},
|
|
'id_number' => $shipment->{$prefix . '_id_number'},
|
|
]);
|
|
}
|
|
}
|