From 5ef5e179844481b78252e7d3befd9ff5d1ceb894 Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Thu, 19 Nov 2026 10:05:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=8C=E6=AD=A5=E5=BF=AB=E9=80=92?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E5=B9=B6=E6=8E=92=E9=99=A4=E5=B7=A5=E5=8E=82?= =?UTF-8?q?=E5=BF=AB=E9=80=92=E9=85=8D=E4=BB=B6=E8=BF=90=E8=B4=B9=E6=89=A3?= =?UTF-8?q?=E6=AC=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Business/Pages.php | 5 +- docs/上游业务镜像清单.md | 2 + docs/供应商配件运费扣除.md | 2 + .../SupplierAccessoryFreightRepository.php | 12 +- .../SupplierAccessoryFreightMatcher.php | 15 ++- .../View/SupplierAccessoryFreightPageData.php | 4 +- .../courier-information.blade.php | 7 +- .../accessory-freight-deductions.blade.php | 6 +- .../Business/CourierInformationPageTest.php | 6 + tests/Unit/SupplierAccessoryFreightTest.php | 104 +++++++++++++++++- 10 files changed, 145 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index 0dfe1e83..89fabc7e 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -9086,13 +9086,13 @@ class Pages extends Controller fputs($csv, chr(0xEF) . chr(0xBB) . chr(0xBF)); fputcsv($csv, [ - 'ID', '快递公司代码', '快递公司名称', '外部编码', '杉橙快递代码', '运费打款方式', + 'ID', '快递公司代码', '快递公司名称', '快递属性', '外部编码', '杉橙快递代码', '运费打款方式', '是否启用', '可达服务', '是否快运', '自动寻号', '配货单号寻号', '数量拆单', '重量0寻号', '停运时间', '合同主体', '合同开始时间', '合同结束时间', '签订公司', '联系人', '联系电话', '自定义1', '自定义2', '扣款方式', ...array_values(\App\Models\Business\CourierProfile::SHIPPING_FIELDS), '体积0寻号', '手工单号', '解密', '电子签章经办人', '最后修改', '入驻单号', '入驻状态', '入驻主体', - ...array_map(static fn (string $field): string => \App\Models\Business\CourierOnboarding::PROFILE_FIELDS[$field][0], \App\Business\CourierProfileData::APPENDED_PROFILE_FIELDS), + ...array_map(static fn (string $field): string => $field === 'courier_attribute' ? '入驻快递属性' : \App\Models\Business\CourierOnboarding::PROFILE_FIELDS[$field][0], \App\Business\CourierProfileData::APPENDED_PROFILE_FIELDS), '财务系统已修改字段', ]); @@ -9119,6 +9119,7 @@ class Pages extends Controller $row->Id ?? '', $this->csvText($row->expcode ?? null), $this->csvText($row->expname ?? null), + $this->csvText($row->exp_properties ?? null), $this->csvText($row->outcode ?? null), $this->csvText($row->scexpcode ?? null), $this->csvText($row->paymethod ?? null), diff --git a/docs/上游业务镜像清单.md b/docs/上游业务镜像清单.md index c90bafa9..0e328d32 100644 --- a/docs/上游业务镜像清单.md +++ b/docs/上游业务镜像清单.md @@ -97,6 +97,8 @@ ## JianSun 旧业务库(jiansun) +快递资料列表与导出的“快递属性”直接读取现有 `js_wp_express.exp_properties`,与财务系统的“入驻快递属性”分别展示。供应商配件运费扣除明细复用该字段,属性为“工厂”的包裹不参与扣款;不新增上游数据集或改变原同步频率。 + | 业务范围 | 上游对象 | | --- | --- | | 店铺收款账单旧来源(已停用) | `dbo.alipay_Bill_Detail` | diff --git a/docs/供应商配件运费扣除.md b/docs/供应商配件运费扣除.md index 812374b0..2fd2992e 100644 --- a/docs/供应商配件运费扣除.md +++ b/docs/供应商配件运费扣除.md @@ -8,6 +8,8 @@ 未匹配商品、商品未维护供应商、未匹配供应商档案、联系人省缺失、供应商或省份冲突、运费无效,均列为待核实,扣款额为零。同一条形码的多条商品记录若供应商相同,或同一供应商多份档案的省份经上述归一后一致,只计算一笔运费。零运费列为不扣款。金额按整数分汇总,页面和导出口径一致。 +快递属性取工作平台 `wp_express.exp_properties` 的现有共享镜像,按明细显示的快递名称(`express_name1`,为空时使用 `express_name2`)精确匹配 `expname`,不做名称包含搜索,不使用财务入驻属性代替。属性为“工厂”的包裹统一不扣款,金额为零,并保留商品、供应商和原始运费供核对;可扣款筛选与汇总、CSV 导出共同使用该规则。多个同名档案属性一致时只算一次,属性冲突时原可扣款记录改为待核实;空属性或未匹配快递沿用原有其余规则。快递镜像未完成首次同步或读取失败时,页面和导出拒绝返回不完整扣款结果。明细及导出增加“快递属性”列。 + 默认展示可扣款,可切换全部、不扣款、待核实;支持月份、交易单号、快递单号、供应商、处理后条形码、联系人省筛选及每页50条翻页。上方汇总统计当前文字筛选范围内全部状态。列设置支持隐藏、拖动排序、恢复默认,按用户和公司存于浏览器;CSV导出覆盖当前状态及筛选结果全部行,按可见列顺序导出,额外保留月份、发货记录ID和仓库供核对,长单号按文本保护并转义公式字符。 只读取现有共享镜像,不新增数据库表、不写回商品、供应商或发货数据,不自动进入供应商应付汇总或执行财务入账。页面与导出沿用供应商应付页级权限。 diff --git a/modules/EcommerceFinance/Repositories/SupplierAccessoryFreightRepository.php b/modules/EcommerceFinance/Repositories/SupplierAccessoryFreightRepository.php index c2ed3794..4fa6e7fb 100644 --- a/modules/EcommerceFinance/Repositories/SupplierAccessoryFreightRepository.php +++ b/modules/EcommerceFinance/Repositories/SupplierAccessoryFreightRepository.php @@ -21,14 +21,19 @@ class SupplierAccessoryFreightRepository { $productDataset = $this->datasets->get(ProductCatalogMirrorRepository::DATASET_KEY); $supplierDataset = $this->datasets->get(SupplierManagementMirrorRepository::DATASET); - if (!$this->mirror->ready($productDataset) || !$this->mirror->ready($supplierDataset)) { - throw new RuntimeException('商品或供应商资料尚未完成首次同步'); + $courierDataset = $this->datasets->get(CourierInformationMirrorRepository::DATASET_KEY); + if (!$this->mirror->ready($productDataset) || !$this->mirror->ready($supplierDataset) || !$this->mirror->ready($courierDataset)) { + throw new RuntimeException('商品、供应商或快递资料尚未完成首次同步'); } $db = $this->mirror->connection(); // 供应商档案数量较小,一次读取,批量商品查询避免逐单访问数据库。 $suppliers = $db->table($supplierDataset->targetTable())->get(['scmname', 'linkerprovience']) ->groupBy(static fn ($row) => trim((string) $row->scmname)) ->map(static fn ($group) => $group->map(static fn ($row) => (array) $row)->all()); + // 快递资料约 1,900 条,仅读取名称和原始属性;同名候选保留供规则核实,不逐单查询。 + $couriers = $db->table($courierDataset->targetTable())->get(['expname', 'exp_properties']) + ->groupBy(static fn ($row) => trim((string) $row->expname)) + ->map(static fn ($group) => $group->map(static fn ($row) => (array) $row)->all()); $start = \Carbon\Carbon::createFromFormat('!Y-m', $month); foreach ($this->shipping->accessoryFreightBatches($start->format('Y-m-d'), $start->copy()->addMonth()->format('Y-m-d')) as $batch) { $barcodes = array_values(array_unique(array_filter(array_map(static fn ($row) => AccessoryReshipMatcher::barcode((string) ($row['barcode'] ?? '')), $batch), static fn ($value) => $value !== ''))); @@ -39,7 +44,8 @@ class SupplierAccessoryFreightRepository $barcode = AccessoryReshipMatcher::barcode((string) ($row['barcode'] ?? '')); $candidates = $products->get($barcode, []); $supplierName = trim((string) ($candidates[0]['supplier_name'] ?? '')); - yield $this->matcher->match($row, $candidates, $suppliers->get($supplierName, [])); + $courierName = trim((string) ($row['express_name1'] ?? '')) ?: trim((string) ($row['express_name2'] ?? '')); + yield $this->matcher->match($row, $candidates, $suppliers->get($supplierName, []), $couriers->get($courierName, [])); } } } diff --git a/modules/EcommerceFinance/Services/SupplierAccessoryFreightMatcher.php b/modules/EcommerceFinance/Services/SupplierAccessoryFreightMatcher.php index 49ababc5..38abf88a 100644 --- a/modules/EcommerceFinance/Services/SupplierAccessoryFreightMatcher.php +++ b/modules/EcommerceFinance/Services/SupplierAccessoryFreightMatcher.php @@ -6,15 +6,26 @@ class SupplierAccessoryFreightMatcher { public const STATES = ['ready' => '可扣款', 'excluded' => '不扣款', 'pending' => '待核实']; - public function match(array $row, array $products, array $suppliers): array + public function match(array $row, array $products, array $suppliers, array $couriers = []): array { $result = $this->matchSupplier($row, $products, $suppliers); + $attributes = array_values(array_unique(array_map(static fn ($courier) => trim((string) ($courier['exp_properties'] ?? '')), $couriers))); + $result['courier_attribute'] = implode(' / ', array_filter($attributes, static fn ($value) => $value !== '')); 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; + } + if ($attributes === ['工厂']) { + $result['state'] = 'excluded'; + $result['deduction_cents'] = 0; + $result['reason'] = '快递属性为工厂,不扣配件运费'; + } elseif (count($attributes) > 1 && $result['state'] === 'ready') { + $result['state'] = 'pending'; + $result['deduction_cents'] = 0; + $result['reason'] = '同名快递属性冲突,需核实'; } return $result; } diff --git a/modules/EcommerceFinance/View/SupplierAccessoryFreightPageData.php b/modules/EcommerceFinance/View/SupplierAccessoryFreightPageData.php index b6785284..28d02671 100644 --- a/modules/EcommerceFinance/View/SupplierAccessoryFreightPageData.php +++ b/modules/EcommerceFinance/View/SupplierAccessoryFreightPageData.php @@ -10,7 +10,7 @@ class SupplierAccessoryFreightPageData { public const TITLE = '新河北仓、曹县仓配件运费扣除明细'; public const TEXT_FILTERS = ['trade_no' => '交易单号', 'express_no' => '快递单号', 'supplier_name' => '供应商', 'normalized_barcode' => '处理后条形码', 'province' => '联系人省']; - public const COLUMNS = ['state' => '扣款状态', 'trade_no' => '交易单号', 'express_no' => '快递单号', 'courier' => '快递名称', 'ship_time' => '发货时间', 'barcode' => '原始条形码', 'normalized_barcode' => '处理后条形码', 'product_name' => '商品名称', 'supplier_name' => '供应商', 'province' => '联系人省', 'freight' => '配件运费(元)', 'deduction' => '扣款金额(元)', 'reason' => '说明']; + public const COLUMNS = ['state' => '扣款状态', 'trade_no' => '交易单号', 'express_no' => '快递单号', 'courier' => '快递名称', 'courier_attribute' => '快递属性', 'ship_time' => '发货时间', 'barcode' => '原始条形码', 'normalized_barcode' => '处理后条形码', 'product_name' => '商品名称', 'supplier_name' => '供应商', 'province' => '联系人省', 'freight' => '配件运费(元)', 'deduction' => '扣款金额(元)', 'reason' => '说明']; public function exportColumns(): array { @@ -69,7 +69,7 @@ class SupplierAccessoryFreightPageData } } catch (\Throwable $exception) { report($exception); - $error = '发货、商品或供应商资料暂时无法读取,或尚未完成同步,请稍后重试。'; + $error = '发货、商品、供应商或快递资料暂时无法读取,或尚未完成同步,请稍后重试。'; $summary = array_fill_keys(array_keys($summary), 0); $items = []; $total = 0; diff --git a/resources/views/business/pages/courier-payables/courier-information.blade.php b/resources/views/business/pages/courier-payables/courier-information.blade.php index 68bc516a..00cc7ffb 100644 --- a/resources/views/business/pages/courier-payables/courier-information.blade.php +++ b/resources/views/business/pages/courier-payables/courier-information.blade.php @@ -502,12 +502,12 @@ - + @foreach (\App\Models\Business\CourierProfile::SHIPPING_FIELDS as $shippingLabel)@endforeach - @foreach (\App\Business\CourierProfileData::APPENDED_PROFILE_FIELDS as $profileField)@endforeach + @foreach (\App\Business\CourierProfileData::APPENDED_PROFILE_FIELDS as $profileField)@endforeach @@ -531,6 +531,7 @@ @endif + @@ -562,7 +563,7 @@ @include('business.pages.courier-payables.partials.courier-onboarding-cells', ['info' => $row->_info ?? null, 'platformName' => $row->expname ?? null, 'platformCode' => $row->expcode ?? null]) @empty - + @endforelse
ID快递公司代码快递公司名称外部编码杉橙快递代码运费打款方式ID快递公司代码快递公司名称快递属性外部编码杉橙快递代码运费打款方式 是否启用可达服务是否快运自动寻号配货单号寻号数量拆单重量0寻号 停运时间合同主体合同开始时间合同结束时间签订公司联系人联系电话自定义1自定义2扣款方式{{ $shippingLabel }}体积0寻号手工单号解密电子签章经办人最后修改入驻单号入驻状态ERP 开通入驻主体{{ \App\Models\Business\CourierOnboarding::PROFILE_FIELDS[$profileField][0] }}入驻单号入驻状态ERP 开通入驻主体{{ $profileField === 'courier_attribute' ? '入驻快递属性' : \App\Models\Business\CourierOnboarding::PROFILE_FIELDS[$profileField][0] }}证照生效报价
{{ $row->expname ?? '-' }}{{ filled($row->exp_properties ?? null) ? $row->exp_properties : '-' }} {{ $row->outcode ?? '-' }} {{ $row->scexpcode ?? '-' }} {{ $row->paymethod ?? '-' }}
{{ $courierError ? '数据暂时不可用。' : '当前筛选条件下没有快递资料。' }}
{{ $courierError ? '数据暂时不可用。' : '当前筛选条件下没有快递资料。' }}
diff --git a/resources/views/business/pages/supplier-payables/accessory-freight-deductions.blade.php b/resources/views/business/pages/supplier-payables/accessory-freight-deductions.blade.php index c11f253a..8fae2220 100644 --- a/resources/views/business/pages/supplier-payables/accessory-freight-deductions.blade.php +++ b/resources/views/business/pages/supplier-payables/accessory-freight-deductions.blade.php @@ -21,16 +21,16 @@
待核实{{ number_format($freightSummary['pending']) }}
可扣运费合计(元){{ number_format($freightSummary['amount'] / 100, 2) }}
-
扣款规则与金额口径

仅取所选月份仓库为“常州简尚家居有限公司”的发货包裹。取第一个逗号前的条形码,再去掉最后一个“-”及其后缀,用处理后的条形码匹配商品资料条形码,再按商品供应商名称匹配供应商管理。联系人省为山东、山东省、河北、河北省的供应商,扣除对应包裹全部运费。每个发货包裹只计一次;不需要匹配首发单。外采快递不扣运费;其他省份不扣,未匹配、资料缺失、供应商或省份冲突、运费异常的记录待核实。此处展示扣除明细,不自动入账。

+
扣款规则与金额口径

仅取所选月份仓库为“常州简尚家居有限公司”的发货包裹。取第一个逗号前的条形码,再去掉最后一个“-”及其后缀,用处理后的条形码匹配商品资料条形码,再按商品供应商名称匹配供应商管理。联系人省为山东、山东省、河北、河北省的供应商,扣除对应包裹全部运费。每个发货包裹只计一次;不需要匹配首发单。外采快递不扣运费;按快递名称匹配工作平台快递资料,快递属性为“工厂”的包裹不扣运费。同名快递属性冲突时待核实;属性为空或未匹配时沿用其余规则。其他省份不扣,未匹配、资料缺失、供应商或省份冲突、运费异常的记录待核实。此处展示扣除明细,不自动入账。

@if($freightError)@endif
@foreach(\Modules\EcommerceFinance\View\SupplierAccessoryFreightPageData::COLUMNS as $key => $column)@endforeach @forelse($freightRows as $row) - + @empty - + @endforelse
{{ $column }}
{{ $states[$row['state']] }}{{ $row['trade_no'] ?: '—' }}{{ $row['express_no'] ?: '—' }}{{ ($row['express_name1'] ?? '') ?: ($row['express_name2'] ?? '') ?: '—' }}{{ $row['ship_time'] }}{{ $row['barcode'] ?: '—' }}{{ $row['normalized_barcode'] ?: '—' }}{{ $row['product_name'] ?: '—' }}{{ $row['supplier_name'] ?: '—' }}{{ $row['province'] ?: '—' }}{{ $row['freight_cents'] === null ? '待核实' : number_format($row['freight_cents'] / 100, 2) }}{{ number_format($row['deduction_cents'] / 100, 2) }}{{ $row['reason'] }}
{{ $states[$row['state']] }}{{ $row['trade_no'] ?: '—' }}{{ $row['express_no'] ?: '—' }}{{ ($row['express_name1'] ?? '') ?: ($row['express_name2'] ?? '') ?: '—' }}{{ $row['courier_attribute'] ?? '' ?: '—' }}{{ $row['ship_time'] }}{{ $row['barcode'] ?: '—' }}{{ $row['normalized_barcode'] ?: '—' }}{{ $row['product_name'] ?: '—' }}{{ $row['supplier_name'] ?: '—' }}{{ $row['province'] ?: '—' }}{{ $row['freight_cents'] === null ? '待核实' : number_format($row['freight_cents'] / 100, 2) }}{{ number_format($row['deduction_cents'] / 100, 2) }}{{ $row['reason'] }}
{{ $freightError ?: '当前筛选条件下暂无记录,请调整月份、状态或筛选条件。' }}
{{ $freightError ?: '当前筛选条件下暂无记录,请调整月份、状态或筛选条件。' }}
diff --git a/tests/Feature/Business/CourierInformationPageTest.php b/tests/Feature/Business/CourierInformationPageTest.php index 2eebbdb6..282da922 100644 --- a/tests/Feature/Business/CourierInformationPageTest.php +++ b/tests/Feature/Business/CourierInformationPageTest.php @@ -68,6 +68,7 @@ class CourierInformationPageTest extends FeatureTestCase 'Id' => 1, 'expcode' => 'SF', 'expname' => '顺丰速运', + 'exp_properties' => '工厂', 'status' => 1, 'tradebody' => '乙方顺丰物流有限公司', 'signcom' => '甲方商贸有限公司', @@ -88,6 +89,8 @@ class CourierInformationPageTest extends FeatureTestCase ->assertOk() ->assertSeeText('顺丰速运') ->assertSeeText('SF') + ->assertSeeText('快递属性')->assertSeeText('工厂') + ->assertSeeText('入驻快递属性') ->assertSeeText('同步正常') ->assertSeeText('可达服务') ->assertSeeText('合同主体')->assertSeeText('乙方顺丰物流有限公司') @@ -110,6 +113,7 @@ class CourierInformationPageTest extends FeatureTestCase $repository->shouldReceive('exportRows')->once()->andReturnUsing(static function (): \Generator { yield (object) [ 'Id' => 1, 'expcode' => 'EXP-1', 'expname' => '导出快递简称', + 'exp_properties' => '=工厂', 'tradebody' => '导出乙方有限公司', 'signcom' => '=甲方公式样式名称', ]; }); @@ -121,5 +125,7 @@ class CourierInformationPageTest extends FeatureTestCase $this->assertSame('导出乙方有限公司', $row['合同主体']); $this->assertSame("'=甲方公式样式名称", $row['签订公司']); $this->assertSame('导出快递简称', $row['快递公司名称']); + $this->assertSame("'=工厂", $row['快递属性']); + $this->assertSame('', $row['入驻快递属性']); } } diff --git a/tests/Unit/SupplierAccessoryFreightTest.php b/tests/Unit/SupplierAccessoryFreightTest.php index 16da32c5..f3636005 100644 --- a/tests/Unit/SupplierAccessoryFreightTest.php +++ b/tests/Unit/SupplierAccessoryFreightTest.php @@ -9,6 +9,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Facade; use Modules\EcommerceFinance\Contracts\BusinessMirrorDataset; use Modules\EcommerceFinance\Repositories\GenericBusinessMirrorRepository; +use Modules\EcommerceFinance\Repositories\CourierInformationMirrorRepository; use Modules\EcommerceFinance\Repositories\ProductCatalogMirrorRepository; use Modules\EcommerceFinance\Repositories\ShippingOrderRepository; use Modules\EcommerceFinance\Repositories\SupplierAccessoryFreightRepository; @@ -91,6 +92,38 @@ class SupplierAccessoryFreightTest extends TestCase } } + public function test_factory_attribute_exempts_freight_even_when_other_details_are_missing(): void + { + $matcher = new SupplierAccessoryFreightMatcher; + foreach (['12.35', '0', null] as $freight) { + foreach ([[], [['name' => '成品', 'supplier_name' => '甲']]] as $products) { + $row = $matcher->match($this->row(['freight' => $freight]), $products, [['linkerprovience' => '河北省']], [ + ['exp_properties' => ' 工厂 '], ['exp_properties' => '工厂'], + ]); + $this->assertSame('excluded', $row['state']); + $this->assertSame(0, $row['deduction_cents']); + $this->assertSame('工厂', $row['courier_attribute']); + $this->assertSame('快递属性为工厂,不扣配件运费', $row['reason']); + } + } + } + + public function test_other_attributes_keep_existing_rules_and_conflicting_attributes_require_review(): void + { + $matcher = new SupplierAccessoryFreightMatcher; + $products = [['name' => '成品', 'supplier_name' => '甲']]; + $suppliers = [['linkerprovience' => '山东']]; + foreach ([[], [['exp_properties' => '']], [['exp_properties' => '简尚']]] as $couriers) { + $row = $matcher->match($this->row(), $products, $suppliers, $couriers); + $this->assertSame('ready', $row['state']); + $this->assertSame(1235, $row['deduction_cents']); + } + $row = $matcher->match($this->row(), $products, $suppliers, [['exp_properties' => '工厂'], ['exp_properties' => '简尚']]); + $this->assertSame('pending', $row['state']); + $this->assertSame(0, $row['deduction_cents']); + $this->assertSame('同名快递属性冲突,需核实', $row['reason']); + } + public function test_other_provinces_zero_invalid_and_equivalent_duplicate_provinces(): void { $matcher = new SupplierAccessoryFreightMatcher; @@ -117,13 +150,19 @@ class SupplierAccessoryFreightTest extends TestCase $connection = app('db')->connection(); $connection->statement('CREATE TABLE products (sku_id TEXT, sku_type TEXT, name TEXT, supplier_name TEXT)'); $connection->statement('CREATE TABLE suppliers (scmname TEXT, linkerprovience TEXT)'); + $connection->statement('CREATE TABLE couriers (expname TEXT, exp_properties TEXT)'); + $connection->table('couriers')->insert([ + ['expname' => '工厂快递', 'exp_properties' => '工厂'], + ['expname' => '补发快递', 'exp_properties' => '简尚'], + ['expname' => '补发快递-其他', 'exp_properties' => '工厂'], + ]); $connection->table('products')->insert([ ['sku_id' => 'GBGJ-SJ038024N1410V02', 'sku_type' => 'normal', 'name' => '成品', 'supplier_name' => '商品供应商'], ['sku_id' => 'GBGJ-SJ038024N1410V02', 'sku_type' => 'combine', 'name' => '组合', 'supplier_name' => '其他供应商'], ]); $connection->table('suppliers')->insert(['scmname' => '商品供应商', 'linkerprovience' => '河北省']); $registry = Mockery::mock(BusinessMirrorDatasetRegistry::class); - foreach ([ProductCatalogMirrorRepository::DATASET_KEY => 'products', SupplierManagementMirrorRepository::DATASET => 'suppliers'] as $key => $table) { + foreach ([ProductCatalogMirrorRepository::DATASET_KEY => 'products', SupplierManagementMirrorRepository::DATASET => 'suppliers', CourierInformationMirrorRepository::DATASET_KEY => 'couriers'] as $key => $table) { $dataset = Mockery::mock(BusinessMirrorDataset::class); $dataset->shouldReceive('targetTable')->andReturn($table); $registry->shouldReceive('get')->with($key)->andReturn($dataset); @@ -132,12 +171,25 @@ class SupplierAccessoryFreightTest extends TestCase $mirror->shouldReceive('ready')->andReturnTrue(); $mirror->shouldReceive('connection')->andReturn($connection); $shipping = Mockery::mock(ShippingOrderRepository::class); - $shipping->shouldReceive('accessoryFreightBatches')->once()->with('2026-09-01', '2026-10-01')->andReturn((function () { yield [$this->row(['supplier_name' => '错误的历史供应商'])]; })()); + $shipping->shouldReceive('accessoryFreightBatches')->once()->with('2026-09-01', '2026-10-01')->andReturn((function () { + yield [$this->row(['supplier_name' => '错误的历史供应商']), + $this->row(['express_name1' => '工厂快递']), + $this->row(['express_name1' => '', 'express_name2' => '工厂快递']), + $this->row(['express_name1' => '补发快递', 'express_name2' => '工厂快递'])]; + })()); $repo = new SupplierAccessoryFreightRepository($shipping, $registry, $mirror, new SupplierAccessoryFreightMatcher); $rows = iterator_to_array($repo->rows('2026-09')); - $this->assertCount(1, $rows); + $this->assertCount(4, $rows); $this->assertSame('商品供应商', $rows[0]['supplier_name']); $this->assertSame(1235, $rows[0]['deduction_cents']); + $this->assertSame('简尚', $rows[0]['courier_attribute']); + foreach ([1, 2] as $index) { + $this->assertSame('工厂', $rows[$index]['courier_attribute']); + $this->assertSame('excluded', $rows[$index]['state']); + $this->assertSame(0, $rows[$index]['deduction_cents']); + } + $this->assertSame('ready', $rows[3]['state']); + $this->assertSame('简尚', $rows[3]['courier_attribute']); } private function data(): SupplierAccessoryFreightPageData @@ -151,6 +203,52 @@ class SupplierAccessoryFreightTest extends TestCase return new SupplierAccessoryFreightPageData($repository); } + public function test_factory_rows_are_zero_in_summary_ready_filter_and_csv(): void + { + $repository = Mockery::mock(SupplierAccessoryFreightRepository::class); + $repository->shouldReceive('rows')->with('2026-09')->andReturnUsing(function () { + $matcher = new SupplierAccessoryFreightMatcher; + foreach (['工厂', '简尚'] as $attribute) { + yield $matcher->match($this->row(['express_no' => $attribute === '工厂' ? 'FACTORY-1' : 'NORMAL-1']), + [['name' => '成品', 'supplier_name' => '甲']], [['linkerprovience' => '河北']], [['exp_properties' => $attribute]]); + } + }); + $data = new SupplierAccessoryFreightPageData($repository); + app()->instance('request', Request::create('/?month=2026-09&state=ready')); + $page = $data->page(); + $this->assertSame(['all' => 2, 'ready' => 1, 'excluded' => 1, 'pending' => 0, 'amount' => 1235], $page['freightSummary']); + $this->assertCount(1, $page['freightRows']); + $this->assertSame('NORMAL-1', $page['freightRows']->items()[0]['express_no']); + $controller = new \App\Http\Controllers\Business\SupplierAccessoryFreight; + $response = $controller->export($data); + ob_start(); $response->sendContent(); $csv = ob_get_clean(); + $this->assertStringNotContainsString('FACTORY-1', $csv); + app()->instance('request', Request::create('/?month=2026-09&state=excluded&columns=express_no,courier_attribute,deduction,reason')); + $response = $controller->export($data); + ob_start(); $response->sendContent(); $csv = ob_get_clean(); + $lines = explode("\n", trim(substr($csv, 3))); + $this->assertCount(2, $lines); + $this->assertSame(['FACTORY-1', '工厂', '0.00', '快递属性为工厂,不扣配件运费'], array_slice(str_getcsv($lines[1]), 3)); + } + + public function test_unready_courier_mirror_does_not_produce_deductions(): void + { + $registry = Mockery::mock(BusinessMirrorDatasetRegistry::class); + foreach ([ProductCatalogMirrorRepository::DATASET_KEY, SupplierManagementMirrorRepository::DATASET, CourierInformationMirrorRepository::DATASET_KEY] as $key) { + $dataset = Mockery::mock(BusinessMirrorDataset::class); + $registry->shouldReceive('get')->with($key)->andReturn($dataset); + $datasets[$key] = $dataset; + } + $mirror = Mockery::mock(GenericBusinessMirrorRepository::class); + foreach ($datasets as $key => $dataset) $mirror->shouldReceive('ready')->with($dataset)->andReturn($key !== CourierInformationMirrorRepository::DATASET_KEY); + $mirror->shouldNotReceive('connection'); + $shipping = Mockery::mock(ShippingOrderRepository::class); + $shipping->shouldNotReceive('accessoryFreightBatches'); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('商品、供应商或快递资料尚未完成首次同步'); + iterator_to_array((new SupplierAccessoryFreightRepository($shipping, $registry, $mirror, new SupplierAccessoryFreightMatcher))->rows('2026-09')); + } + public function test_summary_pagination_and_filters_use_full_month_without_multiplying_amount(): void { app()->instance('request', Request::create('/?month=2026-09&state=all&page=2')); -- 2.47.2