Feat: Adding Elta integrations
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Shipping\Carriers\Elta;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
/**
|
||||
* Resolves a Greek postcode to its ELTA station code/name via PELTKNEW
|
||||
* (the *NEW-family replacement for the old, differently-authenticated
|
||||
* PELSTATION — see EltaClient's docblock). Unlike Acs\AreaResolver,
|
||||
* there's no daily bulk-warm job — PELTKNEW only accepts a single pel_tk
|
||||
* (postcode), with no fetch-all variant to warm a whole-country cache
|
||||
* from, so this caches per-postcode on first lookup instead.
|
||||
*/
|
||||
class AreaResolver
|
||||
{
|
||||
public function __construct(private readonly EltaClient $client) {}
|
||||
|
||||
public function resolve(string $postcode): EltaStation
|
||||
{
|
||||
return Cache::remember(
|
||||
"elta.station.{$postcode}",
|
||||
now()->addDays(30),
|
||||
function () use ($postcode) {
|
||||
$response = $this->client->getStation([
|
||||
'pel_tk' => $postcode,
|
||||
'sender_station' => '',
|
||||
])->throwIfError();
|
||||
|
||||
$fields = $response->data['pel_fields'] ?? [];
|
||||
|
||||
return new EltaStation(
|
||||
code: $fields['rec_station'] ?? '',
|
||||
name: $fields['rec_station_t'] ?? '',
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Shipping\Carriers\Elta;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMXPath;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Modules\Core\Shipping\Carriers\Elta\Exceptions\EltaApiException;
|
||||
|
||||
/**
|
||||
* SOAP transport for ELTA Courier's web services.
|
||||
*
|
||||
* ELTA runs two parallel operation families at the same endpoint
|
||||
* (212.205.47.226:9003). The original CREATEAWB/PELB64VG/PELTT01/PELSTATION
|
||||
* set (sourced from the unofficial chinchillabrains/eltaws reference client)
|
||||
* rejects this account's credentials with st_flag=3 ("invalid password").
|
||||
* Decompiling ELTA's own official desktop client (ELTA_PEL.exe, from their
|
||||
* public .msi installer) showed it never calls CREATEAWB at all — it
|
||||
* exclusively uses a newer "*NEW" family (PELVGNEW, PELLOGINNEW,
|
||||
* PELTTNEW01, PELTKNEW, PELVGDEL, PELPARALVGNEW1). PELVGNEW was
|
||||
* live-verified directly against the production endpoint with this same
|
||||
* account and succeeded (st_flag=0, real voucher number returned) — it
|
||||
* requires no password at all, just the account/user code pair.
|
||||
*
|
||||
* No .wsdl files exist for the *NEW family (only reconstructed from the
|
||||
* decompiled C# proxy classes' method signatures). PHP's SoapClient in
|
||||
* non-WSDL mode refuses to serialize these calls at all (SoapFault "Error
|
||||
* in client request message" regardless of style/use/SoapVar wrapping —
|
||||
* this legacy Micro Focus/CICS gateway's document/literal shape doesn't
|
||||
* match what ext-soap's non-WSDL client is willing to build), so *NEW
|
||||
* operations are sent as hand-built SOAP 1.1 envelopes over plain HTTP —
|
||||
* proven directly against the live endpoint — with the response parsed
|
||||
* back via DOMDocument/DOMXPath.
|
||||
*
|
||||
* The old family's label operation (PELB64VG) is deliberately NOT
|
||||
* exposed here — it fails with the same st_flag=3 as CREATEAWB (same
|
||||
* legacy family), and has no *NEW replacement since ELTA's own client
|
||||
* renders labels locally instead of fetching them (see
|
||||
* EltaLabelRenderer).
|
||||
*/
|
||||
class EltaClient
|
||||
{
|
||||
public function __construct(private readonly array $config) {}
|
||||
|
||||
public function login(array $params): EltaResponse
|
||||
{
|
||||
return $this->call('PELLOGINNEW', $params);
|
||||
}
|
||||
|
||||
public function createVoucher(array $params): EltaResponse
|
||||
{
|
||||
return $this->call('PELVGNEW', $params);
|
||||
}
|
||||
|
||||
public function listVouchers(array $params): EltaResponse
|
||||
{
|
||||
return $this->call('PELPARALVGNEW1', $params);
|
||||
}
|
||||
|
||||
public function deleteVoucher(array $params): EltaResponse
|
||||
{
|
||||
return $this->call('PELVGDEL', $params);
|
||||
}
|
||||
|
||||
public function getTracking(array $params): EltaResponse
|
||||
{
|
||||
return $this->call('PELTTNEW01', $params);
|
||||
}
|
||||
|
||||
public function getStation(array $params): EltaResponse
|
||||
{
|
||||
return $this->call('PELTKNEW', $params);
|
||||
}
|
||||
|
||||
private function call(string $operation, array $params): EltaResponse
|
||||
{
|
||||
$namespace = '/'.$operation;
|
||||
|
||||
$body = '';
|
||||
foreach ($params as $key => $value) {
|
||||
$body .= '<cre:'.$key.'>'.htmlspecialchars((string) $value, ENT_XML1).'</cre:'.$key.'>';
|
||||
}
|
||||
|
||||
$envelope = '<?xml version="1.0" encoding="UTF-8"?>'
|
||||
.'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cre="'.$namespace.'">'
|
||||
.'<soapenv:Header/><soapenv:Body><cre:READ>'.$body.'</cre:READ></soapenv:Body></soapenv:Envelope>';
|
||||
|
||||
$response = Http::withHeaders([
|
||||
'SOAPAction' => 'Get',
|
||||
'Content-Type' => 'text/xml',
|
||||
])
|
||||
->timeout($this->config['timeout'])
|
||||
->withBody($envelope, 'text/xml')
|
||||
->post($this->config['service_location']);
|
||||
|
||||
if ($response->failed()) {
|
||||
throw new EltaApiException('ELTA request failed: HTTP '.$response->status());
|
||||
}
|
||||
|
||||
return EltaResponse::fromSoapResult($this->parseEnvelope($response->body()));
|
||||
}
|
||||
|
||||
private function parseEnvelope(string $xml): array
|
||||
{
|
||||
$dom = new DOMDocument();
|
||||
|
||||
if (! $dom->loadXML($xml)) {
|
||||
throw new EltaApiException('ELTA returned a response that could not be parsed as XML.');
|
||||
}
|
||||
|
||||
$xpath = new DOMXPath($dom);
|
||||
$xpath->registerNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
|
||||
$readResponse = $xpath->query('//soapenv:Body/*')->item(0);
|
||||
|
||||
if (! $readResponse instanceof DOMElement) {
|
||||
throw new EltaApiException('ELTA response did not contain a SOAP body.');
|
||||
}
|
||||
|
||||
return $this->nodeToArray($readResponse);
|
||||
}
|
||||
|
||||
private function nodeToArray(DOMElement $element): array
|
||||
{
|
||||
$data = [];
|
||||
|
||||
foreach ($element->childNodes as $child) {
|
||||
if (! $child instanceof DOMElement) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $child->localName;
|
||||
$hasElementChildren = false;
|
||||
|
||||
foreach ($child->childNodes as $grandchild) {
|
||||
if ($grandchild instanceof DOMElement) {
|
||||
$hasElementChildren = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$value = $hasElementChildren ? $this->nodeToArray($child) : trim($child->textContent);
|
||||
|
||||
if (array_key_exists($name, $data)) {
|
||||
if (! is_array($data[$name]) || ! array_is_list($data[$name])) {
|
||||
$data[$name] = [$data[$name]];
|
||||
}
|
||||
$data[$name][] = $value;
|
||||
} else {
|
||||
$data[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Shipping\Carriers\Elta;
|
||||
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Lunar\Models\Order;
|
||||
use Modules\Core\Shipping\Carriers\Elta\Exceptions\EltaApiException;
|
||||
use Modules\Core\Shipping\Contracts\CarrierFulfillmentInterface;
|
||||
use Modules\Core\Shipping\Contracts\SupportsTracking;
|
||||
use Modules\Core\Shipping\DTOs\ShipmentRequest;
|
||||
use Modules\Core\Shipping\DTOs\TrackingCheckpoint;
|
||||
use Modules\Core\Shipping\Enums\TrackingStatus;
|
||||
use Modules\Core\Shipping\Models\Shipment;
|
||||
|
||||
/**
|
||||
* Not SupportsManifestBatching — no manifest/pickup-list operation was
|
||||
* found in either ELTA operation family, unlike ACS's
|
||||
* ACS_Issue_Pickup_List. Same shape as Box Now, which has no manifest
|
||||
* step either.
|
||||
*/
|
||||
class EltaFulfillmentService implements CarrierFulfillmentInterface, SupportsTracking
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EltaClient $client,
|
||||
private readonly EltaLabelRenderer $labelRenderer,
|
||||
) {}
|
||||
|
||||
public function createShipment(Order $order, ShipmentRequest $request): Shipment
|
||||
{
|
||||
$address = $order->shippingAddress;
|
||||
$weight = $request->weight ?? 0.5;
|
||||
|
||||
$params = [
|
||||
'pel_apost_code' => config('elta.apost_code'),
|
||||
'pel_paral_name' => trim("{$address->first_name} {$address->last_name}"),
|
||||
'pel_paral_address' => $address->line_one,
|
||||
'pel_paral_area' => $address->city,
|
||||
'pel_paral_tk' => $address->postcode,
|
||||
'pel_paral_thl_1' => $address->contact_phone,
|
||||
'pel_paral_thl_2' => '',
|
||||
'pel_service' => '',
|
||||
'pel_baros' => number_format($weight, 3, '.', ''),
|
||||
'pel_baros_xyz' => '',
|
||||
'pel_x' => '',
|
||||
'pel_y' => '',
|
||||
'pel_z' => '',
|
||||
'pel_temaxia' => (string) $request->packageCount,
|
||||
'pel_paral_sxolia' => '',
|
||||
'pel_sur_1' => '',
|
||||
'pel_sur_2' => '',
|
||||
'pel_sur_3' => '',
|
||||
'pel_ant_poso' => '',
|
||||
'pel_ant_poso1' => '',
|
||||
'pel_ant_poso2' => '',
|
||||
'pel_ant_poso3' => '',
|
||||
'pel_ant_poso4' => '',
|
||||
'pel_ant_date1' => '',
|
||||
'pel_ant_date2' => '',
|
||||
'pel_ant_date3' => '',
|
||||
'pel_ant_date4' => '',
|
||||
'pel_asf_poso' => '',
|
||||
'pel_user' => config('elta.user_code'),
|
||||
'pel_ref_no' => (string) $order->id,
|
||||
'pel_insert_flag' => '',
|
||||
'pel_paral_code' => '',
|
||||
'pel_retur_code' => '',
|
||||
];
|
||||
|
||||
$codAmount = null;
|
||||
|
||||
if ($request->paymentMode === 'cod') {
|
||||
$codAmount = $request->amountToCollect ?? $order->total->decimal;
|
||||
$params['pel_ant_poso'] = number_format($codAmount, 2, '.', '');
|
||||
}
|
||||
|
||||
$response = $this->client->createVoucher($params)->throwIfError();
|
||||
|
||||
$voucherNo = (string) $response->data['vg_code'];
|
||||
|
||||
$shipment = Shipment::create([
|
||||
'order_id' => $order->id,
|
||||
'carrier' => 'elta',
|
||||
'tracking_reference' => $voucherNo,
|
||||
'meta' => [
|
||||
'weight' => $weight,
|
||||
'ocr_line' => $response->data['ocr_line'] ?? null,
|
||||
'date_time' => $response->data['date_time'] ?? null,
|
||||
'package_count' => $request->packageCount,
|
||||
'cod_amount' => $codAmount,
|
||||
],
|
||||
]);
|
||||
|
||||
// PELVGNEW's own response already carries every child voucher
|
||||
// number for a multi-piece shipment in vg_child_no — no second
|
||||
// request needed, same as the old CREATEAWB/vg_child shape.
|
||||
foreach (array_filter((array) ($response->data['vg_child_no'] ?? [])) as $childVoucherNo) {
|
||||
Shipment::create([
|
||||
'order_id' => $order->id,
|
||||
'carrier' => 'elta',
|
||||
'tracking_reference' => $childVoucherNo,
|
||||
'parent_reference' => $voucherNo,
|
||||
'meta' => $shipment->meta?->toArray() ?? [],
|
||||
]);
|
||||
}
|
||||
|
||||
return $shipment;
|
||||
}
|
||||
|
||||
/**
|
||||
* PELB64VG (the old, deprecated operation this used to call) fails
|
||||
* with the same st_flag=3 as CREATEAWB — it's the same legacy family.
|
||||
* There's no *NEW replacement: ELTA's own official client renders
|
||||
* labels entirely locally from a bundled report template, never
|
||||
* fetching one from the server at all (see EltaLabelRenderer's
|
||||
* docblock). This reproduces that local rendering instead.
|
||||
*/
|
||||
public function printLabel(Shipment $shipment): string
|
||||
{
|
||||
$pdf = $this->labelRenderer->render($shipment);
|
||||
|
||||
$shipment->update(['label_printed_at' => now()]);
|
||||
|
||||
return $pdf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlike the old, deprecated operation set (which genuinely has no
|
||||
* delete operation), the *NEW family does support cancellation via
|
||||
* PELVGDEL — but it identifies a voucher by its internal pel_vg_id,
|
||||
* not the AWB/tracking number PELVGNEW returns (vg_code). Getting
|
||||
* pel_vg_id requires first listing the account's vouchers via
|
||||
* PELPARALVGNEW1 and matching the row whose pel_paral_vg equals our
|
||||
* tracking_reference.
|
||||
*/
|
||||
public function cancelShipment(Shipment $shipment): void
|
||||
{
|
||||
$voucherId = $this->findVoucherId($shipment->tracking_reference);
|
||||
|
||||
if ($voucherId === null) {
|
||||
throw new EltaApiException(
|
||||
'Could not find voucher '.$shipment->tracking_reference.' in ELTA\'s voucher list — it may already be too old or cancelled to look up.',
|
||||
);
|
||||
}
|
||||
|
||||
$this->client->deleteVoucher(['pel_vg_id' => $voucherId])->throwIfError();
|
||||
|
||||
$shipment->update(['cancelled_at' => now()]);
|
||||
}
|
||||
|
||||
private function findVoucherId(string $trackingReference): ?string
|
||||
{
|
||||
$inId = '0';
|
||||
|
||||
// PELPARALVGNEW1 paginates via in_id, same cursor pattern the
|
||||
// official client uses — walk pages until the voucher is found
|
||||
// or a page comes back empty. flag_1/flag_2 map to the real
|
||||
// client's "Show All"/"All Users" checkboxes — live-confirmed
|
||||
// both must be '1', otherwise even a voucher created moments ago
|
||||
// is filtered out of the (default, unfiltered-looking) list.
|
||||
for ($page = 0; $page < 50; $page++) {
|
||||
$response = $this->client->listVouchers([
|
||||
'pel_code' => config('elta.apost_code'),
|
||||
'pel_user_code' => config('elta.user_code'),
|
||||
'flag_1' => '1',
|
||||
'flag_2' => '1',
|
||||
'in_id' => $inId,
|
||||
])->throwIfError();
|
||||
|
||||
$rows = array_filter(
|
||||
(array) ($response->data['vg_rec'] ?? []),
|
||||
fn (array $row) => filled($row['pel_vg_id'] ?? null),
|
||||
);
|
||||
|
||||
if ($rows === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$match = collect($rows)->first(
|
||||
fn (array $row) => ($row['pel_paral_vg'] ?? null) === $trackingReference,
|
||||
);
|
||||
|
||||
if ($match !== null) {
|
||||
return $match['pel_vg_id'];
|
||||
}
|
||||
|
||||
$inId = (string) end($rows)['pel_vg_id'];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function trackShipment(Shipment $shipment): Collection
|
||||
{
|
||||
$response = $this->client->getTracking([
|
||||
'web_vg' => $shipment->tracking_reference,
|
||||
'pel_code' => config('elta.apost_code'),
|
||||
])->throwIfError();
|
||||
|
||||
$rows = array_filter(
|
||||
(array) ($response->data['web_status'] ?? []),
|
||||
fn (array $row) => filled($row['web_date_time'] ?? null),
|
||||
);
|
||||
|
||||
// No explicit delivered-confirmation field like the old
|
||||
// PELTT01's pod_name — pel_rec.a_rec_date_time is blank until
|
||||
// delivery per the real client's own rendering, used as the
|
||||
// delivered signal here; unconfirmed against a genuinely
|
||||
// delivered parcel yet.
|
||||
$isDelivered = filled(trim($response->data['pel_rec']['a_rec_date_time'] ?? '', ' /:'));
|
||||
|
||||
return collect(array_values($rows))->map(function (array $row, int $index) use ($rows, $isDelivered) {
|
||||
$isLast = $index === count($rows) - 1;
|
||||
$dateTime = $row['web_date_time'] ?? '';
|
||||
|
||||
return new TrackingCheckpoint(
|
||||
status: $isLast && $isDelivered
|
||||
? TrackingStatus::Delivered
|
||||
: $this->guessStatusFromTitle($row['web_status_name'] ?? ''),
|
||||
carrierStatus: $row['web_status_name'] ?? null,
|
||||
message: $row['web_sxolia'] ?: ($row['web_status_name'] ?? null),
|
||||
location: $row['web_station'] ?? null,
|
||||
occurredAt: Carbon::createFromFormat('YmdHi', substr($dateTime, 0, 12)),
|
||||
meta: $row,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* web_status_name is free text with no structured status code — same
|
||||
* limitation Acs\AcsFulfillmentService::guessStatusFromAction() has.
|
||||
* Live-confirmed text seen so far: "ΔΗΜΙΟΥΡΓΙΑ ΣΥ.ΔΕ.ΤΑ. ΑΠΟ ΠΕΛΑΤΗ"
|
||||
* (voucher created) — matched via a Pending-ish fallback below since
|
||||
* it isn't yet in transit. Other statuses are best-guess substring
|
||||
* matches to refine as more real tracking text is observed.
|
||||
*/
|
||||
private function guessStatusFromTitle(string $title): TrackingStatus
|
||||
{
|
||||
$title = mb_strtolower($title);
|
||||
|
||||
return match (true) {
|
||||
str_contains($title, 'παραδόθηκε') => TrackingStatus::Delivered,
|
||||
str_contains($title, 'διανομή') => TrackingStatus::OutForDelivery,
|
||||
str_contains($title, 'παραλαβή') => TrackingStatus::CollectedFromSender,
|
||||
str_contains($title, 'μεταφορά') || str_contains($title, 'διαμετακόμιση') => TrackingStatus::InTransit,
|
||||
default => TrackingStatus::Pending,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
<?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));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Shipping\Carriers\Elta;
|
||||
|
||||
use Lunar\DataTypes\ShippingOption;
|
||||
use Lunar\Shipping\DataTransferObjects\ShippingOptionRequest;
|
||||
use Lunar\Shipping\Interfaces\ShippingRateInterface;
|
||||
use Lunar\Shipping\Models\ShippingRate;
|
||||
use Modules\Core\Shipping\Concerns\ExcludesRestrictedProducts;
|
||||
use Modules\Core\Shipping\Concerns\ResolvesFixedPricing;
|
||||
use Modules\Core\Shipping\Contracts\DeclaresFulfillmentType;
|
||||
use Modules\Core\Shipping\Contracts\SupportsCashCollection;
|
||||
|
||||
/**
|
||||
* None of ELTA's four SOAP operations (create/label/track/station) expose
|
||||
* a price-quote/calculation call, so — like Box Now — this always
|
||||
* resolves the method's own charge_by + price-break configuration, never
|
||||
* live pricing. Does not implement SupportsLivePricing.
|
||||
*
|
||||
* Implements SupportsCashCollection: an ELTA courier hands a parcel to
|
||||
* the recipient in person, the same fact as AcsRateDriver, unlike Box
|
||||
* Now's unattended lockers.
|
||||
*/
|
||||
class EltaRateDriver implements ShippingRateInterface, DeclaresFulfillmentType, SupportsCashCollection
|
||||
{
|
||||
use ResolvesFixedPricing;
|
||||
use ExcludesRestrictedProducts;
|
||||
|
||||
public ShippingRate $shippingRate;
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return 'ELTA Courier';
|
||||
}
|
||||
|
||||
public function fulfillmentType(): string
|
||||
{
|
||||
return 'carrier';
|
||||
}
|
||||
|
||||
public function collectsCash(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return 'Delivery via ELTA Courier.';
|
||||
}
|
||||
|
||||
public function resolve(ShippingOptionRequest $shippingOptionRequest): ?ShippingOption
|
||||
{
|
||||
if ($this->cartHasExcludedProducts($shippingOptionRequest->shippingRate, $shippingOptionRequest->cart)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->resolveFixedPrice(
|
||||
$shippingOptionRequest->shippingRate,
|
||||
$shippingOptionRequest->shippingRate->shippingMethod,
|
||||
$shippingOptionRequest->cart,
|
||||
);
|
||||
}
|
||||
|
||||
public function on(ShippingRate $shippingRate): self
|
||||
{
|
||||
$this->shippingRate = $shippingRate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Shipping\Carriers\Elta;
|
||||
|
||||
use Modules\Core\Shipping\Carriers\Elta\Exceptions\EltaApiException;
|
||||
|
||||
/**
|
||||
* Normalizes a SoapClient::READ() stdClass result — every ELTA operation's
|
||||
* response carries the same st_flag (0=success, 1-99=error)/st_title
|
||||
* shape, live-verified against the real production endpoint (see
|
||||
* EltaClient's own docblock). Mirrors Acs\AcsResponse's hasError/data
|
||||
* shape so the rest of the integration (rate driver, fulfillment service)
|
||||
* reads identically regardless of transport (SOAP here vs. ACS's JSON).
|
||||
*/
|
||||
class EltaResponse
|
||||
{
|
||||
private function __construct(
|
||||
public readonly bool $hasError,
|
||||
public readonly ?string $errorMessage,
|
||||
public readonly array $data,
|
||||
) {}
|
||||
|
||||
public static function fromSoapResult(mixed $result): self
|
||||
{
|
||||
$data = json_decode(json_encode($result), true) ?? [];
|
||||
$flag = (int) ($data['st_flag'] ?? 0);
|
||||
|
||||
return new self(
|
||||
hasError: $flag !== 0,
|
||||
errorMessage: $flag !== 0 ? ($data['st_title'] ?? 'Unknown ELTA error') : null,
|
||||
data: $data,
|
||||
);
|
||||
}
|
||||
|
||||
public function throwIfError(): self
|
||||
{
|
||||
if ($this->hasError) {
|
||||
throw new EltaApiException($this->errorMessage ?? 'Unknown ELTA API error', $this->data);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Shipping\Carriers\Elta;
|
||||
|
||||
class EltaStation
|
||||
{
|
||||
public function __construct(
|
||||
public readonly string $code,
|
||||
public readonly string $name,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Shipping\Carriers\Elta\Exceptions;
|
||||
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
class EltaApiException extends RuntimeException
|
||||
{
|
||||
public function __construct(string $message, public readonly array $data = [], ?Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, previous: $previous);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user