185 lines
8.0 KiB
PHP
185 lines
8.0 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace Modules\Core\Shipping\Carriers\Elta;
|
||
|
|
|
||
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
||
|
|
use Illuminate\Support\Carbon;
|
||
|
|
use Modules\Core\Shipping\Models\Shipment;
|
||
|
|
use Picqer\Barcode\BarcodeGeneratorPNG;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Renders an ELTA shipping label as a PDF ourselves — there is no *NEW
|
||
|
|
* operation for fetching one (see EltaFulfillmentService::printLabel()'s
|
||
|
|
* docblock), so this reproduces ELTA's own official label design instead
|
||
|
|
* of a server-fetched file.
|
||
|
|
*
|
||
|
|
* Field layout/text is traced directly from ELTA's own 2024 Customer
|
||
|
|
* Client Installation Manual, which includes real sample images of both
|
||
|
|
* the A4 voucher (3 stacked copies: delivery copy, sender's copy, a
|
||
|
|
* ΤΑΧΥΠΛΗΡΩΜΗ payment-receipt stub) and the A6 thermal label (single
|
||
|
|
* copy, with a return-reason checkbox list the A4 version doesn't have)
|
||
|
|
* — not from decompiled client code, which an earlier version of this
|
||
|
|
* class was built from and turned out to look nothing like a real
|
||
|
|
* label. The barcode is Code 128 (single barcode per label, matching
|
||
|
|
* both real samples) encoding the voucher number itself — an earlier
|
||
|
|
* version guessed Code 39 with a second "OCR" barcode from misreading
|
||
|
|
* decompiled code; neither survived comparison against the real
|
||
|
|
* samples.
|
||
|
|
*/
|
||
|
|
class EltaLabelRenderer
|
||
|
|
{
|
||
|
|
public function __construct(private readonly AreaResolver $areaResolver) {}
|
||
|
|
|
||
|
|
public function render(Shipment $shipment): string
|
||
|
|
{
|
||
|
|
$order = $shipment->order;
|
||
|
|
$address = $order->shippingAddress;
|
||
|
|
$meta = $shipment->meta?->toArray() ?? [];
|
||
|
|
|
||
|
|
$destinationStation = $this->areaResolver->resolve($address->postcode);
|
||
|
|
|
||
|
|
$dateTime = (string) ($meta['date_time'] ?? '');
|
||
|
|
$occurredAt = strlen($dateTime) >= 12
|
||
|
|
? Carbon::createFromFormat('YmdHi', substr($dateTime, 0, 12))
|
||
|
|
: now();
|
||
|
|
|
||
|
|
$voucherNo = $shipment->tracking_reference;
|
||
|
|
$ocrLine = (string) ($meta['ocr_line'] ?? '');
|
||
|
|
$packageCount = (int) ($meta['package_count'] ?? 1);
|
||
|
|
|
||
|
|
$generator = new BarcodeGeneratorPNG();
|
||
|
|
|
||
|
|
$data = [
|
||
|
|
'sender_name' => config('elta.sender_name'),
|
||
|
|
'sender_address' => config('elta.sender_address'),
|
||
|
|
'sender_postcode' => config('elta.sender_postcode'),
|
||
|
|
'sender_area' => config('elta.sender_area'),
|
||
|
|
'sender_phone' => config('elta.sender_phone'),
|
||
|
|
'apost_code' => config('elta.apost_code'),
|
||
|
|
|
||
|
|
'recipient_name' => trim("{$address->first_name} {$address->last_name}"),
|
||
|
|
'recipient_address' => $address->line_one,
|
||
|
|
'recipient_postcode' => $address->postcode,
|
||
|
|
'recipient_area' => $address->city,
|
||
|
|
'recipient_phone' => $address->contact_phone,
|
||
|
|
|
||
|
|
// Mirrors Sydeta.cs::print_vg()'s own dataRow["sender_1..5"] /
|
||
|
|
// dataRow["rec_1..5"] construction: name on its own line, then
|
||
|
|
// address, then "TK:<postcode> <area> ΤΗΛ:<phone>" — each of
|
||
|
|
// the RDLC's rectangle8/rectangle9 boxes renders these as 3
|
||
|
|
// separate textbox lines (not 5; sender_2/sender_3 and
|
||
|
|
// rec_2/rec_3 only appear when a line's text exceeds the
|
||
|
|
// report's 40-char wrap width, which our own data never hits
|
||
|
|
// in practice for a name/address/contact triple).
|
||
|
|
'sender_lines' => [
|
||
|
|
config('elta.sender_name'),
|
||
|
|
config('elta.sender_address'),
|
||
|
|
'TK:'.config('elta.sender_postcode').' '.config('elta.sender_area').' ΤΗΛ:'.config('elta.sender_phone'),
|
||
|
|
],
|
||
|
|
'recipient_lines' => [
|
||
|
|
trim("{$address->first_name} {$address->last_name}"),
|
||
|
|
$address->line_one,
|
||
|
|
'TK:'.$address->postcode.' '.$address->city,
|
||
|
|
'ΤΗΛ: '.$address->contact_phone,
|
||
|
|
],
|
||
|
|
|
||
|
|
'date' => $occurredAt->format('d/m/Y'),
|
||
|
|
'time' => $occurredAt->format('H:i'),
|
||
|
|
|
||
|
|
'voucher_no' => $voucherNo,
|
||
|
|
'barcode_voucher' => $this->barcodeDataUri($generator, $voucherNo),
|
||
|
|
|
||
|
|
'weight' => $meta['weight'] ?? null,
|
||
|
|
'package_count' => $packageCount,
|
||
|
|
'package_label' => $packageCount > 1
|
||
|
|
? sprintf('001/%03d', $packageCount)
|
||
|
|
: '001',
|
||
|
|
'multiPiece' => $packageCount > 1,
|
||
|
|
|
||
|
|
'station_apo' => config('elta.origin_station_code'),
|
||
|
|
'station_pros' => $destinationStation->code,
|
||
|
|
'station_pros_title' => $destinationStation->name,
|
||
|
|
|
||
|
|
'service_code' => '214',
|
||
|
|
'service_name' => 'ΠΟΛΗ ΠΟΛΗ -ΠΘ- ΝΗΣΙ',
|
||
|
|
|
||
|
|
'xreosi' => 'ΧΡΕΩΣΗ ΑΠΟΣΤΟΛΕΑ ΠΙΣΤΩΣΗ',
|
||
|
|
'siimvasi' => '131775-9',
|
||
|
|
|
||
|
|
'cod_amount' => $meta['cod_amount'] ?? null,
|
||
|
|
'ocr_line' => $ocrLine,
|
||
|
|
// The A6 layout's 3-cell row shows a 13-digit reference derived
|
||
|
|
// from the OCR line, not the barcode's own voucher text —
|
||
|
|
// confirmed against the real sample: ">14260009814363<..."
|
||
|
|
// yields reference "1426000981436" (13 chars after the '>').
|
||
|
|
'ocr_reference' => strlen($ocrLine) >= 14 ? substr($ocrLine, 1, 13) : '',
|
||
|
|
|
||
|
|
'order_reference' => (string) $order->id,
|
||
|
|
|
||
|
|
// sydetaE.rdlc's "Πρόσθετες Υπηρεσίες" (sur_1..4) and the
|
||
|
|
// ΑΝΤΙΚΑΤΑΒΟΛΗ banner (antik_1) are only populated by ELTA's
|
||
|
|
// own client when the shipper ticks special-service checkboxes
|
||
|
|
// (Sydeta.cs::print_vg()'s checkBox2-5) or a COD amount is set
|
||
|
|
// — neither is tracked on our Shipment model today, so these
|
||
|
|
// render as empty boxes, matching what a plain "credit" send
|
||
|
|
// with no extras or COD looks like on a real label.
|
||
|
|
'sur_1' => null,
|
||
|
|
'sur_2' => null,
|
||
|
|
'sur_3' => null,
|
||
|
|
'sur_4' => null,
|
||
|
|
'antik_1' => $meta['cod_amount'] ?? null ? '* ΠΡΟΣΟΧΗ ΑΝΤΙΚΑΤΑΒΟΛΗ *' : null,
|
||
|
|
'volumetric_weight' => $meta['volumetric_weight'] ?? null,
|
||
|
|
|
||
|
|
'eltaLogo' => $this->logoDataUri(),
|
||
|
|
];
|
||
|
|
|
||
|
|
$paperSize = config('elta.label_paper_size', 'a4');
|
||
|
|
$view = $paperSize === 'a6'
|
||
|
|
? 'core::shipping.carriers.elta.label-a6'
|
||
|
|
: 'core::shipping.carriers.elta.label-a4';
|
||
|
|
|
||
|
|
$pdf = Pdf::loadView($view, $data);
|
||
|
|
|
||
|
|
// A6 = 104mm x 148mm, per RDLCPrinter.cs's own DeviceInfo
|
||
|
|
// override for printer_size==2 (1mm ≈ 2.8346pt) — note this is
|
||
|
|
// ELTA's own thermal-label size, not the ISO A6 (105x148mm) the
|
||
|
|
// old comment here named. A 4-value paper array plus an
|
||
|
|
// orientation string together confuse dompdf into doubling the
|
||
|
|
// canvas and silently overflowing to a second blank page
|
||
|
|
// (reproduced with a bare empty page); the array alone is
|
||
|
|
// already portrait (height > width), so the orientation
|
||
|
|
// argument is both redundant and the actual bug.
|
||
|
|
$paperSize === 'a6'
|
||
|
|
? $pdf->setPaper([0, 0, 294.80, 419.53])
|
||
|
|
: $pdf->setPaper('a4');
|
||
|
|
|
||
|
|
return $pdf->output();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* PNG over SVG — dompdf's inline <svg> support doesn't reliably
|
||
|
|
* render the barcode generator's <rect>-based bars (confirmed: the
|
||
|
|
* SVG output rendered as literal text in a live test), while a
|
||
|
|
* base64 PNG <img> is unambiguous.
|
||
|
|
*/
|
||
|
|
private function barcodeDataUri(BarcodeGeneratorPNG $generator, string $payload): string
|
||
|
|
{
|
||
|
|
$png = $generator->getBarcode($payload, BarcodeGeneratorPNG::TYPE_CODE_128);
|
||
|
|
|
||
|
|
return 'data:image/png;base64,'.base64_encode($png);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Embedded as a base64 data URI rather than referenced by URL —
|
||
|
|
* resources/logos/ is only copied into a consuming app's public/ on
|
||
|
|
* vendor:publish, which isn't guaranteed to have run, and dompdf
|
||
|
|
* needs either a real filesystem path or a working absolute URL.
|
||
|
|
*/
|
||
|
|
private function logoDataUri(): string
|
||
|
|
{
|
||
|
|
$path = __DIR__.'/../../../../resources/logos/elta-courier-logo.png';
|
||
|
|
|
||
|
|
return 'data:image/png;base64,'.base64_encode(file_get_contents($path));
|
||
|
|
}
|
||
|
|
}
|