Files
2026-09-15 10:35:56 +08:00

116 lines
4.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\LingxingErp\Model\Shipment;
class FulfillmentNormalizer
{
/**
* A Lingxing order can have multiple wo_number records. If package-level item quantities
* are present, each package becomes a Magento shipment; otherwise one outbound record
* becomes one shipment with all tracking numbers attached.
*/
public function normalize(array $outbound): array
{
$woNumber = (string)($outbound['wo_number'] ?? $outbound['woNumber'] ?? '');
if ($woNumber === '') {
throw new \RuntimeException('Lingxing outbound data is missing wo_number.');
}
$packages = $outbound['package_delivered_data'] ?? $outbound['packageDeliveredData'] ?? [];
if (is_array($packages) && count($packages) > 1 && $this->allPackagesHaveProducts($packages)) {
$result = [];
foreach (array_values($packages) as $index => $package) {
$packageNo = (string)($package['package_no'] ?? $package['packageNo'] ?? '');
$tracks = $this->extractTracks($package, $outbound);
$packageIdentity = $packageNo ?: (($tracks[0]['number'] ?? '') ?: (string)($index + 1));
$result[] = $this->makePayload(
$outbound,
$woNumber . '|' . $packageIdentity,
$packageNo,
$package['product_info'] ?? $package['productInfo'],
$tracks
);
}
return $result;
}
$tracks = $this->extractTracks($outbound);
foreach (is_array($packages) ? $packages : [] as $package) {
foreach ($this->extractTracks($package) as $track) {
$tracks[] = $track;
}
}
$tracks = $this->uniqueTracks($tracks);
return [$this->makePayload(
$outbound,
$woNumber,
(string)($outbound['package_no'] ?? $outbound['packageNo'] ?? ''),
$outbound['product_info'] ?? $outbound['productInfo'] ?? [],
$tracks
)];
}
private function allPackagesHaveProducts(array $packages): bool
{
foreach ($packages as $package) {
$products = $package['product_info'] ?? $package['productInfo'] ?? null;
if (!is_array($products) || $products === []) {
return false;
}
}
return true;
}
private function makePayload(array $outbound, string $externalKey, string $packageNo, array $products, array $tracks): array
{
$normalizedProducts = [];
foreach ($products as $product) {
$sku = trim((string)($product['sku'] ?? ''));
$quantity = (float)($product['count'] ?? $product['quantity'] ?? 0);
if ($sku === '' || $quantity <= 0 || (int)($product['bundle_type'] ?? 0) === 2) {
continue;
}
$normalizedProducts[] = ['erp_sku' => $sku, 'quantity' => $quantity];
}
return [
'external_key' => mb_substr($externalKey, 0, 191),
'global_order_no' => (string)($outbound['order_number'] ?? $outbound['orderNumber'] ?? ''),
'platform_order_nos' => array_values(array_filter((array)($outbound['platform_order_no'] ?? [$outbound['amazonOrderId'] ?? '']))),
'wo_id' => (int)($outbound['wo_id'] ?? $outbound['woId'] ?? 0),
'wid' => (int)($outbound['wid'] ?? $outbound['warehouse_id'] ?? $outbound['warehouseId'] ?? 0),
'wo_number' => (string)($outbound['wo_number'] ?? $outbound['woNumber'] ?? ''),
'package_no' => $packageNo,
'carrier' => (string)($outbound['logistics_provider_name'] ?? $outbound['actualCarrier'] ?? ''),
'products' => $normalizedProducts,
'tracks' => $this->uniqueTracks($tracks),
];
}
private function extractTracks(array $primary, array $fallback = []): array
{
$tracks = [];
foreach ([
$primary['tracking_no'] ?? $primary['trackingNo'] ?? '',
$primary['waybill_no'] ?? $primary['waybillNo'] ?? '',
] as $number) {
$number = trim((string)$number);
if ($number !== '') {
$tracks[] = ['number' => $number, 'title' => (string)($primary['logistics_provider_name'] ?? $primary['actualCarrier'] ?? $fallback['logistics_provider_name'] ?? 'Lingxing')];
}
}
return $tracks;
}
private function uniqueTracks(array $tracks): array
{
$result = [];
foreach ($tracks as $track) {
$number = trim((string)($track['number'] ?? ''));
if ($number !== '') {
$result[$number] = ['number' => $number, 'title' => (string)($track['title'] ?? 'Lingxing')];
}
}
return array_values($result);
}
}