- orderSum legfeljebb 4 tizedesjegyre kerekítve (lebegőpontos zaj nélkül) - profitCenter blokk: sellerProfitCenterId (vevőkód), név, cím és kontaktadatok a megrendelés pillanatképéből - a pillanatkép-cím szerkezetét az élő rekord adja, ha egyezik; különben bontás + figyelmeztetés - a vevőkód nélküli telephely rendelése kimarad az unprocessed listából, hibaként naplózva - saját maryker naplócsatorna (MaryKerLogger), ismétlődő események óránként egyszer Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
127 lines
5.3 KiB
PHP
127 lines
5.3 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.
|
|
*
|
|
* @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' => (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;
|
|
}
|
|
|
|
/** 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));
|
|
}
|
|
}
|