Feature: Shipping Carrier Additions, Creating a voucher without an order, verifying order details before creating voucher, ELTA Courier voucher print updates

This commit is contained in:
arvanitakis committed 2026-10-01 01:51:13 +03:00
1 parent c455c8adf2
commit 5ff0d60f54
27 files changed
+2189 -376

No files matched your search

+123 -25
View File
@@ -5,7 +5,9 @@ namespace Modules\Core\Shipping\Carriers\Elta;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Modules\Core\Shipping\DTOs\VoucherRecipient;
use Modules\Core\Shipping\Models\Shipment;
use Picqer\Barcode\BarcodeGeneratorPNG;
@@ -38,6 +40,7 @@ class EltaLabelRenderer
'special_handling' => '002 ΕΙΔΙΚΗ ΔΙΑΧΕΙΡΗΣΗ',
'timed_delivery' => '003 ΠΡΟΚΑΘΟΡΙΣΜΕΝΗ ΩΡΑ',
'saturday_delivery' => '004 ΕΠΙΔΟΣΗ ΣΑΒΒΑΤΟΥ',
'post_box_delivery' => '* ΠΡΟΣΟΧΗ PostBox *',
];
public function __construct(private readonly AreaResolver $areaResolver) {}
@@ -54,7 +57,7 @@ class EltaLabelRenderer
* <head> (the styles are the same for every label) with a page break
* between them.
*
* @param Collection<int, Shipment> $shipments
* @param Collection<int, Shipment|EltaReturnLabel> $shipments
*/
public function renderMany(Collection $shipments): string
{
@@ -65,7 +68,9 @@ class EltaLabelRenderer
$pages = [];
foreach ($shipments as $shipment) {
$data = $this->data($shipment);
$data = $shipment instanceof EltaReturnLabel
? $this->returnData($shipment->shipment)
: $this->data($shipment);
// A6 prints every parcel on the same label; on A4 the extra
// parcels of a multi-parcel send use ELTA's own separate, smaller
@@ -113,8 +118,6 @@ class EltaLabelRenderer
*/
private function data(Shipment $shipment): array
{
$order = $shipment->order;
$address = $order->shippingAddress;
$paperSize = config('elta.label_paper_size', 'a4');
$piece = $this->pieceOf($shipment);
$master = $piece['master'] ?? null;
@@ -126,21 +129,30 @@ class EltaLabelRenderer
array_filter($shipment->meta?->toArray() ?? [], fn ($value) => $value !== null),
);
// Who it goes to, as saved on the voucher — the label never reads
// the order.
$recipient = $shipment->recipient() ?? $master?->recipient()
?? throw new \RuntimeException("Shipment {$shipment->id} has no recipient to print.");
// Issuing a pending voucher (PELVG01NEW1) returns the real
// destination station and service; vouchers issued before that flow
// existed don't have them, so fall back to the postcode lookup.
$station = filled($meta['rec_station'] ?? null)
? ['code' => $meta['rec_station'], 'name' => $meta['rec_station_t'] ?? '']
: (fn ($s) => ['code' => $s->code, 'name' => $s->name])($this->areaResolver->resolve($address->postcode));
: (fn ($s) => ['code' => $s->code, 'name' => $s->name])($this->areaResolver->resolve($recipient->postcode));
$isChild = $piece !== null && $piece['number'] > 1;
$masterVoucher = $isChild ? ($master?->tracking_reference ?? $shipment->parent_reference) : null;
$services = $isChild ? [] : (array) ($meta['services'] ?? []);
$surcharges = $this->surchargeSlots($services);
$cod = $isChild
? $this->childCodFields(($master?->meta['cod_amount'] ?? null) > 0)
: $this->codFields(isset($meta['cod_amount']) ? (float) $meta['cod_amount'] : null);
$cod = match (true) {
$isChild => $this->childCodFields(($master?->meta['cod_amount'] ?? null) > 0),
// PostBox never has COD (ELTA refuses it); its antik_1 is the
// PostBox warning instead (print_vg()).
in_array('post_box_delivery', $services, true) => $this->postBoxCodFields(),
default => $this->codFields(isset($meta['cod_amount']) ? (float) $meta['cod_amount'] : null),
};
$dateTime = (string) ($meta['date_time'] ?? '');
$occurredAt = strlen($dateTime) >= 12
@@ -162,11 +174,11 @@ class EltaLabelRenderer
'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,
'recipient_name' => $recipient->name,
'recipient_address' => $recipient->address,
'recipient_postcode' => $recipient->postcode,
'recipient_area' => $recipient->city,
'recipient_phone' => $recipient->phones(),
// Mirrors Sydeta.cs::print_vg()'s own dataRow["sender_1..5"] /
// dataRow["rec_1..5"] construction: name on its own line, then
@@ -182,12 +194,7 @@ class EltaLabelRenderer
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,
],
'recipient_lines' => $this->partyLines($recipient),
'date' => $occurredAt->format('d/m/Y'),
'time' => $occurredAt->format('H:i'),
@@ -217,7 +224,9 @@ class EltaLabelRenderer
// voucher (*NZ…GR*) under "ΜASTER ΣΥΔΕΤΑ" on an A4 child.
'copy' => $isChild ? ($paperSize === 'a6' ? 'ΠΟΛΛΑΠΛH' : '*'.$masterVoucher.'*') : null,
'station_apo' => config('elta.origin_station_code'),
// Our deposit station: configured, or the one ELTA's login
// reported (cached by EltaFulfillmentService::senderStation()).
'station_apo' => config('elta.origin_station_code') ?: Cache::get('elta.user_station'),
'station_pros' => $station['code'],
'station_pros_title' => $station['name'],
@@ -238,7 +247,7 @@ class EltaLabelRenderer
// The reference we send ELTA as pel_ref_no (order reference;
// older vouchers were sent the order id).
'order_reference' => $isChild ? null : (string) $order->reference,
'order_reference' => $isChild ? null : $shipment->reference(),
// ΠΑΡΑΤΗΡΗΣΕΙΣ: the remarks we sent ELTA (the customer's delivery
// instructions), in the client's 27-character lines — up to 3
@@ -256,7 +265,10 @@ class EltaLabelRenderer
// we never send any, so the box stays empty like the client's.
'volumetric_weight' => $meta['volumetric_weight'] ?? null,
'eltaLogo' => $this->logoDataUri(),
'eltaLogo' => $this->logoDataUri('elta-courier-logo.png'),
// The A4 payment stub's "Hellenic Post" logo (the RDLC's
// Point5_ELTACourier_ΑποστολήμαςΕσείς image).
'hellenicPostLogo' => $this->logoDataUri('elta-logo.png'),
];
}
@@ -275,6 +287,8 @@ class EltaLabelRenderer
if ($has('special_handling')) {
$slots[1] = self::SERVICE_TEXT['special_handling'];
} elseif ($has('post_box_delivery')) {
$slots[1] = self::SERVICE_TEXT['post_box_delivery'];
}
if ($has('timed_delivery')) {
@@ -327,11 +341,95 @@ class EltaLabelRenderer
...$lines,
'antik_lines' => array_values(array_filter($lines)),
'antik_minima' => '* ΠΡΟΣΟΧΗ ΑΝΤΙΚΑΤΑΒΟΛΗ *',
'apodiksi' => '* Απόδειξη Είσπαξης *',
'apodiksi' => '* Απόδειξη Είσπραξης *',
'antik_poso' => $sent,
];
}
/**
* rec_1..4 as print_vg() builds them: name, address, "TK:" line, phones
* (also the RETOUR voucher's sender lines).
*
* @return array<int, string>
*/
private function partyLines(VoucherRecipient $recipient): array
{
return [
$recipient->name,
$recipient->address,
'TK:'.$recipient->postcode.' '.$recipient->city,
'ΤΗΛ: '.$recipient->phones(),
];
}
/**
* A PostBox voucher's COD column: just the PostBox warning (antik_1).
*
* @return array<string, mixed>
*/
private function postBoxCodFields(): array
{
return [
...$this->codFields(null),
'antik_1' => self::SERVICE_TEXT['post_box_delivery'],
'antik_lines' => [self::SERVICE_TEXT['post_box_delivery']],
];
}
/**
* The return voucher ("RETOUR", docs/elta.md B6), as the client fills
* it from the main voucher: the parties swap (our customer sends it
* back to us, the return-to party), it's charged to the recipient,
* 0.100 kg, one piece, no COD, and the remarks name the main voucher.
* Service 461 when the main voucher's service is in-city (1xx), else
* 462. It goes from the main voucher's destination station back to
* ours.
*
* @return array<string, mixed>
*/
private function returnData(Shipment $master): array
{
$data = $this->data($master);
$meta = $master->meta?->toArray() ?? [];
$returnVoucher = (string) $meta['return_vg'];
$ocrLine = (string) ($meta['r_ocr_line'] ?? '');
$inCity = str_starts_with((string) ($meta['rec_srv'] ?? ''), '1');
return [
...$data,
...$this->codFields(null),
'sender_lines' => $this->partyLines($master->recipient()),
'recipient_lines' => $data['sender_lines'],
'voucher_no' => $returnVoucher,
'barcode_voucher' => $this->barcodeDataUri(new BarcodeGeneratorPNG(), $returnVoucher),
'ocr_line' => $ocrLine,
'ocr_reference' => strlen($ocrLine) >= 14 ? substr($ocrLine, 1, 13) : null,
'service_code' => $inCity ? '461' : '462',
'service_name' => $inCity ? 'RETOUR ΕΝΤΟΣ ΠΟΛΗΣ' : 'RETOUR ΠΟΛΗ ΠΟΛΗ',
'xreosi' => 'ΧΡΕΩΣΗ ΠΑΡΑΛΗΠΤΗ ΠΙΣΤΩΣΗ',
'copy' => 'RETOUR',
'station_apo' => $data['station_pros'],
'station_pros' => $data['station_apo'],
'station_pros_title' => '',
'weight' => '0.100',
'package_count' => 1,
'package_label' => '1',
'piece_label' => '001/001',
'multiPiece' => false,
'polaplo' => null,
'isChild' => false,
'sur_1' => null, 'sur_2' => null, 'sur_3' => null, 'sur_4' => null,
'sxolia' => array_slice(mb_str_split('ΣΥΔΕΤΑ '.$master->tracking_reference.' Χρέωση Πελάτη '.config('elta.apost_code'), 27), 0, 3),
];
}
/**
* A child parcel's COD text (print_child()): only the warning, never the
* amount — the cash is collected against the main voucher.
@@ -404,9 +502,9 @@ class EltaLabelRenderer
* 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
private function logoDataUri(string $file): string
{
$path = __DIR__.'/../../../../resources/logos/elta-courier-logo.png';
$path = __DIR__.'/../../../../resources/logos/'.$file;
return 'data:image/png;base64,'.base64_encode(file_get_contents($path));
}