- multiAddress telephelynél kiválasztott szállítási címmel a sellerProfitCenterId = {vevőkód}_{Address.id}
- egy címes telephelynél, vagy kiválasztott cím nélkül a sima vevőkód (utóbbinál figyelmeztetés a maryker naplóban)
- ideiglenes megoldás a szállítási alegység Apolló-kódja (2. szakasz) előtt, ld. backlog B-10
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
155 lines
6.8 KiB
PHP
155 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Address;
|
|
use App\Models\OrderArchive;
|
|
use App\Models\ProfitCenter;
|
|
use App\Models\ProfitCenterSupplierCode;
|
|
use Illuminate\Support\Collection;
|
|
|
|
/**
|
|
* Az OrderArchive sorokból a Mary-Ker API JSON-blokkjait állítja elő (EV3-461, specifikáció 3. pont).
|
|
*/
|
|
class MaryKerOrderMapper
|
|
{
|
|
/**
|
|
* A "profitCenter" blokk rendelésenként. A cím a megrendeléskor rögzített pillanatkép
|
|
* (order_archives.deliveryAddress / deliveryAddressName), nem az élő törzsadat.
|
|
*
|
|
* A pillanatkép egyetlen szöveg ("{irányítószám} {város} {utca}"), az API viszont külön postCode / city / address
|
|
* mezőt ad. A szerkezetet az élő rekordból vesszük, de csak akkor, ha az pontosan ugyanazt a címet adja ki, mint
|
|
* a pillanatkép (azaz a cím a rendelés óta nem változott) és az irányítószáma érvényes. Egyébként a szöveget
|
|
* bontjuk, ami becslés, ezért figyelmeztetést naplózunk.
|
|
*
|
|
* TODO 2.4: sellerDeliveryLocationId (szállítási alegység-azonosító) — a 2. szakasz új táblájából; addig lásd
|
|
* sellerProfitCenterId() ideiglenes összetett értékét.
|
|
*
|
|
* @param Collection<int, OrderArchive> $orders
|
|
* @return array<int, array<string, string>> rendelés id => blokk
|
|
*/
|
|
public function profitCenterBlocks(Collection $orders, int $supplierId): array
|
|
{
|
|
$profitCenterIds = $orders->pluck('profit_center_id')->filter()->unique()->values();
|
|
|
|
$profitCenters = ProfitCenter::withTrashed()
|
|
->whereIn('id', $profitCenterIds)
|
|
->with(['address' => fn ($query) => $query->orderBy('id')])
|
|
->get()->keyBy('id');
|
|
$customerCodes = ProfitCenterSupplierCode::where('supplier_id', $supplierId)
|
|
->whereIn('profit_center_id', $profitCenterIds)
|
|
->pluck('customerCode', 'profit_center_id');
|
|
$addresses = Address::whereIn('id', $orders->pluck('deliveryAddressId')->filter()->unique())
|
|
->get()->keyBy('id');
|
|
|
|
$blocks = [];
|
|
foreach ($orders as $order) {
|
|
$profitCenter = $profitCenters->get($order->profit_center_id);
|
|
$live = $order->deliveryAddressId
|
|
? $addresses->get($order->deliveryAddressId)
|
|
: $this->profitCenterAddress($profitCenter);
|
|
[$postCode, $city, $street] = $this->splitAddress($order, $live);
|
|
|
|
$blocks[$order->id] = [
|
|
// string marad, a vezető nullák megmaradnak
|
|
'sellerProfitCenterId' => $this->sellerProfitCenterId(
|
|
$order, $profitCenter, (string) $customerCodes->get($order->profit_center_id)
|
|
),
|
|
'name' => (string) ($order->deliveryAddressName ?: $order->profit_center_name ?: $profitCenter?->name),
|
|
'postCode' => $postCode,
|
|
'city' => $city,
|
|
'address' => $street,
|
|
'contactName' => (string) $order->contactName,
|
|
'contactEmail' => (string) $profitCenter?->email,
|
|
'contactPhone' => (string) $profitCenter?->phone,
|
|
'contactMobil' => (string) $profitCenter?->mobil,
|
|
];
|
|
}
|
|
|
|
return $blocks;
|
|
}
|
|
|
|
/**
|
|
* IDEIGLENES (EV3-461, 2. szakasz előtt): több szállítási címes telephelynél, ha a rendelésen van kiválasztott cím,
|
|
* a vevőkód után "_{Address.id}" kerül; egyébként a sima vevőkód. A több címes jelzőt a megrendelő felülethez
|
|
* hasonlóan a telephely "multiAddress" mezője adja, nem a címrekordok száma.
|
|
*
|
|
* Apolló ezt az értéket nem ismeri: a 2. szakaszban az Apolló által kiadott szállítási alegység-kód
|
|
* (sellerDeliveryLocationId) váltja ki, és a sellerProfitCenterId újra a sima vevőkód lesz (backlog B-10).
|
|
*/
|
|
private function sellerProfitCenterId(OrderArchive $order, ?ProfitCenter $profitCenter, string $customerCode): string
|
|
{
|
|
if (! $profitCenter || ! (int) $profitCenter->multiAddress) {
|
|
return $customerCode;
|
|
}
|
|
|
|
if (! $order->deliveryAddressId) {
|
|
MaryKerLogger::once('warning', "multi-address:{$order->id}", 'MaryKer: több címes telephely, de a rendelésen nincs kiválasztott szállítási cím, a sima vevőkód megy ki', [
|
|
'orderId' => $order->id, 'humanId' => $order->humanId, 'profitCenterId' => $order->profit_center_id,
|
|
]);
|
|
|
|
return $customerCode;
|
|
}
|
|
|
|
return $customerCode . '_' . $order->deliveryAddressId;
|
|
}
|
|
|
|
/** Ugyanazt a szabályt követi, mint a megrendelő felület (OrderController::getProfitCenterAddress). */
|
|
private function profitCenterAddress(?ProfitCenter $profitCenter): ProfitCenter|Address|null
|
|
{
|
|
if (! $profitCenter) {
|
|
return null;
|
|
}
|
|
|
|
return (int) $profitCenter->addressEqual ? $profitCenter : $profitCenter->address->first();
|
|
}
|
|
|
|
/** @return array{0: string, 1: string, 2: string} [irányítószám, város, utca] */
|
|
private function splitAddress(OrderArchive $order, ProfitCenter|Address|null $live): array
|
|
{
|
|
$snapshot = $this->normalize($order->deliveryAddress);
|
|
|
|
if ($live && $this->validPostCode($live->postCode)
|
|
&& ($snapshot === '' || $this->normalize($live->fullAddress) === $snapshot)) {
|
|
return [trim($live->postCode), trim((string) $live->city), trim((string) $live->street)];
|
|
}
|
|
|
|
if ($snapshot === '') {
|
|
$this->warn($order, 'nincs pillanatkép-cím, és az élő cím sem használható');
|
|
|
|
return ['', '', ''];
|
|
}
|
|
|
|
if (preg_match('/^(\d{4})\s+(\S+)\s*(.*)$/u', $snapshot, $parts)) {
|
|
$this->warn($order, 'a pillanatkép nem azonos az élő címmel (vagy az élő cím hibás), a bontás becslés', $snapshot);
|
|
|
|
return [$parts[1], rtrim($parts[2], ',;'), $parts[3]];
|
|
}
|
|
|
|
$this->warn($order, 'a pillanatkép-cím nem bontható irányítószámra, városra és utcára', $snapshot);
|
|
|
|
return ['', '', $snapshot];
|
|
}
|
|
|
|
private function warn(OrderArchive $order, string $reason, ?string $snapshot = null): void
|
|
{
|
|
MaryKerLogger::once('warning', "address:{$order->id}", 'MaryKer: címadat-eltérés a rendelésen: ' . $reason, [
|
|
'orderId' => $order->id,
|
|
'humanId' => $order->humanId,
|
|
'profitCenterId' => $order->profit_center_id,
|
|
'deliveryAddressId' => $order->deliveryAddressId,
|
|
'snapshot' => $snapshot ?? $order->deliveryAddress,
|
|
]);
|
|
}
|
|
|
|
private function validPostCode(?string $postCode): bool
|
|
{
|
|
return (bool) preg_match('/^\d{4}$/', trim((string) $postCode));
|
|
}
|
|
|
|
private function normalize(?string $text): string
|
|
{
|
|
return trim((string) preg_replace('/\s+/u', ' ', (string) $text));
|
|
}
|
|
}
|