CaiWuXiTong/modules/EcommerceFinance/Repositories/LinehaulFreightRepository.php

88 lines
3.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Modules\EcommerceFinance\Repositories;
use Modules\EcommerceFinance\Services\BusinessMirrorDatasetRegistry;
/** 工作平台「专线运费」的逐出库单金额;页面和核算只读共享镜像。 */
class LinehaulFreightRepository
{
public const DATASET = 'jiansun.dbo.jst_shipFreight_view';
public const TABLE = 'js_jst_ship_freight';
public const CARRIERS = ['中铁物流', '武进安能', '壹米滴答专线'];
public static function supports(string $carrier): bool
{
return in_array(trim($carrier), self::CARRIERS, true);
}
/** @return array<int,array{error:?string,row:?array}> */
public function forPackages(array $packages): array
{
$packages = array_filter($packages, static fn (array $p): bool => self::supports((string) ($p['express_name1'] ?? '')));
if ($packages === []) {
return [];
}
$mirror = app(GenericBusinessMirrorRepository::class);
$dataset = app(BusinessMirrorDatasetRegistry::class)->get(self::DATASET);
if (! $mirror->ready($dataset)) {
return array_fill_keys(array_column($packages, 'source_id'), ['error' => '工作平台专线运费尚未完成同步', 'row' => null]);
}
$variants = [];
foreach ($packages as $package) {
$number = self::number($package['express_no'] ?? '');
if ($number !== '') {
$variants[] = $number;
$variants[] = '@' . $number;
}
}
$byNumber = [];
foreach (array_chunk(array_values(array_unique($variants)), 800) as $chunk) {
foreach ($mirror->connection()->table(self::TABLE)->whereIn('Expressno', $chunk)
->get(['Id', 'Shiptime', 'ExName', 'Expressno', 'FreightNew', 'checksgn', 'status', 'combsgn']) as $row) {
$byNumber[self::number($row->Expressno)][] = (array) $row;
}
}
$result = [];
foreach ($packages as $package) {
$result[(int) $package['source_id']] = $this->match($package, $byNumber[self::number($package['express_no'] ?? '')] ?? []);
}
return $result;
}
private static function number(mixed $value): string
{
return ltrim(trim((string) $value), '@');
}
/** @return array{error:?string,row:?array} */
private function match(array $package, array $rows): array
{
$carrier = trim((string) $package['express_name1']);
$rows = array_values(array_filter($rows, static fn (array $row): bool => trim((string) $row['ExName']) === $carrier));
$sourceId = (int) $package['source_id'];
if ($sourceId >= 2000000000 && $sourceId < 3000000000) {
// 出库补漏的来源键直接包含 io_id,不能拿同单号另一次出库的金额。
$rows = array_values(array_filter($rows, static fn (array $row): bool => (int) $row['Id'] === $sourceId - 2000000000));
} else {
$time = substr((string) ($package['ship_time'] ?? ''), 0, 19);
$exact = array_values(array_filter($rows, static fn (array $row): bool => substr((string) $row['Shiptime'], 0, 19) === $time));
$rows = $exact !== [] ? $exact : array_values(array_filter($rows,
static fn (array $row): bool => substr((string) $row['Shiptime'], 0, 10) === substr($time, 0, 10)));
}
if ($rows === []) {
return ['error' => '工作平台专线运费未找到对应出库记录', 'row' => null];
}
if (count($rows) !== 1) {
return ['error' => '工作平台专线运费匹配到多条出库记录,待核对', 'row' => null];
}
$row = $rows[0];
if (! is_numeric($row['FreightNew']) || (float) $row['FreightNew'] < 0) {
return ['error' => '工作平台专线运费尚未填写有效金额', 'row' => $row];
}
return ['error' => null, 'row' => $row];
}
}