64 lines
3.1 KiB
PHP
64 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Services;
|
|
|
|
class SupplierAccessoryFreightMatcher
|
|
{
|
|
public const STATES = ['ready' => '可扣款', 'excluded' => '不扣款', 'pending' => '待核实'];
|
|
|
|
public function match(array $row, array $products, array $suppliers): array
|
|
{
|
|
$result = $this->matchSupplier($row, $products, $suppliers);
|
|
foreach (['express_name1', 'express_name2'] as $field) {
|
|
if (trim((string) ($row[$field] ?? '')) !== '外采快递') continue;
|
|
$result['state'] = 'excluded';
|
|
$result['deduction_cents'] = 0;
|
|
$result['reason'] = '外采快递不扣配件运费';
|
|
break;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
private function matchSupplier(array $row, array $products, array $suppliers): array
|
|
{
|
|
$barcode = AccessoryReshipMatcher::barcode((string) ($row['barcode'] ?? ''));
|
|
$row = array_merge($row, ['normalized_barcode' => $barcode, 'product_name' => '', 'supplier_name' => '',
|
|
'province' => '', 'freight_cents' => AccessoryReshipMatcher::cents($row['freight'] ?? null),
|
|
'deduction_cents' => 0, 'state' => 'pending', 'reason' => '未匹配到商品资料']);
|
|
if ($barcode === '' || $products === []) return $row;
|
|
$names = array_values(array_unique(array_map(static fn ($product) => trim((string) ($product['supplier_name'] ?? '')), $products)));
|
|
$row['product_name'] = implode(' / ', array_unique(array_column($products, 'name')));
|
|
$row['supplier_name'] = implode(' / ', $names);
|
|
if (count($names) !== 1 || $names[0] === '') {
|
|
$row['reason'] = count($names) > 1 ? '同一条形码对应多个供应商' : '商品资料未维护供应商';
|
|
return $row;
|
|
}
|
|
if ($suppliers === []) {
|
|
$row['reason'] = '未匹配到供应商管理资料';
|
|
return $row;
|
|
}
|
|
$provinces = array_values(array_unique(array_map(static function ($supplier) {
|
|
$province = trim((string) ($supplier['linkerprovience'] ?? ''));
|
|
return match ($province) { '山东省' => '山东', '河北省' => '河北', default => $province };
|
|
}, $suppliers)));
|
|
$row['province'] = implode(' / ', array_filter($provinces, static fn ($value) => $value !== ''));
|
|
if (count($provinces) !== 1 || $provinces[0] === '') {
|
|
$row['reason'] = count($provinces) > 1 ? '供应商联系人省存在冲突' : '供应商未维护联系人省';
|
|
return $row;
|
|
}
|
|
if (!in_array($provinces[0], ['山东', '河北'], true)) {
|
|
$row['state'] = 'excluded';
|
|
$row['reason'] = '联系人省不属于山东、河北';
|
|
return $row;
|
|
}
|
|
if ($row['freight_cents'] === null) {
|
|
$row['reason'] = '运费无效,需核实';
|
|
return $row;
|
|
}
|
|
$row['state'] = $row['freight_cents'] > 0 ? 'ready' : 'excluded';
|
|
$row['deduction_cents'] = $row['freight_cents'];
|
|
$row['reason'] = $row['freight_cents'] > 0 ? '按山东、河北供应商扣除全部配件运费' : '运费为零,不扣款';
|
|
return $row;
|
|
}
|
|
}
|