$orders * @return array> 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; } /** * Az "items" blokk rendelésenként (specifikáció 3.3 pont). * * A sumPrice a specifikáció szerint számított: quantity * unitMultiplier * price. Figyelem: az order_archives_items * price oszlopa double(8,2), ezért ahol a megrendeléskori egységár több tizedesjegyű volt, a tételösszegek * a fejléc összegétől (order_archives.sumPrice) centekkel eltérhetnek (backlog B-11). * * @param Collection $orders * @return array>> rendelés id => tételek */ public function itemBlocks(Collection $orders): array { $items = OrderArchivesItem::whereIn('order_archive_id', $orders->pluck('id')) ->orderBy('id') ->get(['id', 'order_archive_id', 'name', 'supplierProductNumber', 'unitMultiplier', 'sellerUnit', 'producer_name', 'quantity', 'price']) ->groupBy('order_archive_id'); $blocks = []; foreach ($orders as $order) { $blocks[$order->id] = $items->get($order->id, collect())->map(fn (OrderArchivesItem $item) => [ // string marad, a vezető nullák megmaradnak 'sellerItemNumber' => (string) $item->supplierProductNumber, 'name' => (string) $item->name, 'packMultiplier' => $this->number($item->unitMultiplier), 'packName' => (string) $item->sellerUnit, 'producerName' => (string) $item->producer_name, 'orderNumber' => $this->number($item->quantity), 'price' => $this->number($item->price), 'sumPrice' => $this->itemSum($item), ])->values()->all(); } return $blocks; } /** * Pénzösszeg / mennyiség a partnerrel egyeztetett formátumban: legfeljebb 4 tizedesjegy (specifikáció 2. pont). * A tárolt double lebegőpontos zajt hordoz (pl. 258817.12149533), ezért kerekítjük. */ public function number(float|int|string|null $value): ?float { return $value === null ? null : round((float) $value, self::DECIMALS); } private function itemSum(OrderArchivesItem $item): ?float { if ($item->quantity === null || $item->unitMultiplier === null || $item->price === null) { return null; } return $this->number($item->quantity * $item->unitMultiplier * $item->price); } /** * 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)); } }