143 lines
5.5 KiB
PHP
143 lines
5.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\LingxingErp\Model\Shipment;
|
|
|
|
use Esun\LingxingErp\Model\Api\Client;
|
|
use Esun\LingxingErp\Model\Api\Endpoint;
|
|
use Esun\LingxingErp\Model\Config;
|
|
use Esun\LingxingErp\Model\Sync\LogWriter;
|
|
use Esun\LingxingErp\Model\Sync\OrderSyncRepository;
|
|
use Esun\LingxingErp\Model\Sync\ShipmentSyncRepository;
|
|
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
|
|
use Magento\Framework\FlagManager;
|
|
|
|
class ShipmentPuller
|
|
{
|
|
/**
|
|
* Lingxing API documentation:
|
|
* https://apidoc.lingxing.com/#/docs/Warehouse/WmsOrderList
|
|
*/
|
|
private const ORDER_FILTER_BATCH_SIZE = 100;
|
|
|
|
public function __construct(
|
|
private readonly Client $client,
|
|
private readonly Config $config,
|
|
private readonly TimezoneInterface $timezone,
|
|
private readonly FlagManager $flagManager,
|
|
private readonly FulfillmentNormalizer $normalizer,
|
|
private readonly OrderSyncRepository $orderSyncRepository,
|
|
private readonly ShipmentSyncRepository $repository,
|
|
private readonly LogWriter $logWriter
|
|
) {
|
|
}
|
|
|
|
public function pull(): void
|
|
{
|
|
$now = $this->timezone->date();
|
|
$lastSuccess = (string)$this->flagManager->getFlagData('esun_lingxing_shipment_last_success_date');
|
|
$earliest = (clone $now)->modify('-30 days')->format('Y-m-d');
|
|
$start = $lastSuccess !== '' && $lastSuccess >= $earliest
|
|
? $lastSuccess
|
|
: (clone $now)->modify('-1 day')->format('Y-m-d');
|
|
$end = $now->format('Y-m-d');
|
|
$pageSize = $this->config->getShipmentPageSize();
|
|
$platformOrderNos = $this->orderSyncRepository->getTrackablePlatformOrderNos();
|
|
if ($platformOrderNos === []) {
|
|
return;
|
|
}
|
|
|
|
$completedRange = true;
|
|
foreach (array_chunk($platformOrderNos, self::ORDER_FILTER_BATCH_SIZE) as $orderNumberBatch) {
|
|
if (!$this->pullOrderNumberBatch($orderNumberBatch, $pageSize, $start, $end)) {
|
|
$completedRange = false;
|
|
break;
|
|
}
|
|
}
|
|
if ($completedRange) {
|
|
$this->flagManager->saveFlag('esun_lingxing_shipment_last_success_date', $end);
|
|
}
|
|
}
|
|
|
|
/** @param string[] $platformOrderNos */
|
|
private function pullOrderNumberBatch(
|
|
array $platformOrderNos,
|
|
int $pageSize,
|
|
string $start,
|
|
string $end
|
|
): bool {
|
|
for ($page = 1; $page <= $this->config->getShipmentMaxPages(); $page++) {
|
|
$request = [
|
|
'page' => $page,
|
|
'page_size' => $pageSize,
|
|
'platform_order_no_arr' => $platformOrderNos,
|
|
'status_arr' => [3],
|
|
'time_type' => 'update_at',
|
|
'start_date' => $start,
|
|
'end_date' => $end,
|
|
];
|
|
$started = microtime(true);
|
|
$response = $this->client->post(Endpoint::WMS_ORDER_LIST, $request);
|
|
$rows = is_array($response['data'] ?? null) ? $response['data'] : [];
|
|
$this->enqueueOutbounds($rows, $response);
|
|
$this->logWriter->write([
|
|
'direction' => 'pull', 'operation' => 'shipment_pull', 'entity_type' => 'shipment',
|
|
'status' => 'success', 'attempt_number' => 1,
|
|
'request_id' => (string)($response['request_id'] ?? ''),
|
|
'request_payload' => $request,
|
|
'response_payload' => $response,
|
|
'duration_ms' => (int)round((microtime(true) - $started) * 1000),
|
|
]);
|
|
if (count($rows) < $pageSize) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/** @param array<int, array<string, mixed>> $outbounds */
|
|
private function enqueueOutbounds(array $outbounds, array $response): void
|
|
{
|
|
foreach ($outbounds as $outbound) {
|
|
if (!$this->belongsToSynchronizedMagentoOrder($outbound)) {
|
|
continue;
|
|
}
|
|
try {
|
|
foreach ($this->normalizer->normalize($outbound) as $payload) {
|
|
$this->repository->enqueue($payload, (string)($response['request_id'] ?? ''));
|
|
}
|
|
} catch (\Throwable $exception) {
|
|
$this->logWriter->write([
|
|
'direction' => 'pull',
|
|
'operation' => 'shipment_normalize',
|
|
'entity_type' => 'shipment',
|
|
'external_id' => (string)($outbound['wo_number'] ?? ''),
|
|
'status' => 'failed',
|
|
'attempt_number' => 1,
|
|
'error_code' => (string)$exception->getCode(),
|
|
'error_message' => $exception->getMessage(),
|
|
'request_id' => (string)($response['request_id'] ?? ''),
|
|
'response_payload' => $outbound,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function belongsToSynchronizedMagentoOrder(array $outbound): bool
|
|
{
|
|
$globalOrderNo = (string)($outbound['order_number'] ?? $outbound['orderNumber'] ?? '');
|
|
if ($globalOrderNo !== '' && $this->orderSyncRepository->findByGlobalOrderNo($globalOrderNo) !== null) {
|
|
return true;
|
|
}
|
|
$platformOrderNos = (array)($outbound['platform_order_no'] ?? [$outbound['amazonOrderId'] ?? '']);
|
|
foreach ($platformOrderNos as $incrementId) {
|
|
if ((string)$incrementId !== ''
|
|
&& $this->orderSyncRepository->findByIncrementId((string)$incrementId) !== null
|
|
) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|