CaiWuXiTong/tests/Unit/EcommerceFinance/SupplierBookkeepingDeductionRuleTest.php
limeng 8b56efb66f fix(厂家记账): 按财务确认恢复缺文件核价与免扣判定
厂家记账对已确认未发出的单据仍显示待确认;缺月度发货文件时无法继续成本核价;同一快递号的其他发货来源会干扰工单对应商品的运费替代。

本次调整:全部未发出的工单明确不扣款;缺文件且无原单匹配的补偿/全款,保留原小额免记账顺序,按完整工单逐商品数量与当前成本、20%替代运费核价,再和原金额取小值;成本核价排除同号无关商品来源;增加公司级单工单免扣设置,应用财务确认的特例而不扩大到所有缺供应商工单。原单部分匹配、数量含糊、缺成本及运费损耗无记录等情况继续待确认。

修改范围:核价Repository、核价服务、商品组合解析、扣款规则、页面数据服务、对应单元/页面回归及规则文档。无数据库迁移;不改权限、上游、发货重量或发货运费计算。上线仅保存已获财务明确授权的一个工单免扣设置,并备份旧设置,不改原始工单。

验证:Docker 中以 www-data、禁用网络、内存 SQLite 运行相关核价、商品解析、规则、Repository 与页面回归,68 tests / 649 assertions 全通过(96.69秒,PHPUnit峰值121MB)。覆盖50元/40%边界、两轮实付、未发出、同号不同来源、多商品数量、缺项、页面筛选与CSV一致性。生产399条工单只读试算0.51秒、峰值52.5MiB,确认只有预期范围变化,敏感明细未放入Git或PR。
2026-12-06 09:26:54 +08:00

142 lines
8.6 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 Tests\Unit\EcommerceFinance;
use InvalidArgumentException;
use Modules\EcommerceFinance\Services\SupplierBookkeepingDeductionRule as Rule;
use PHPUnit\Framework\TestCase;
require_once dirname(__DIR__, 3) . '/modules/EcommerceFinance/Services/SupplierBookkeepingDeductionRule.php';
class SupplierBookkeepingDeductionRuleTest extends TestCase
{
public function test_parses_every_rule_text_in_finance_sheet(): void
{
$this->assertSame(['mode' => Rule::MODE_NONE, 'effective_from' => null, 'effective_to' => null], Rule::parseRuleText('不记账'));
$this->assertSame(Rule::MODE_NORMAL, Rule::parseRuleText('正常记账')['mode']);
$this->assertSame(Rule::MODE_DROPSHIP_WRONG, Rule::parseRuleText('代发仓发货错需记账,其他原因均不记账')['mode']);
$this->assertSame(Rule::MODE_DROPSHIP_WRONG, Rule::parseRuleText('厂家暂未出货,后期若建立代发仓,发货错需记账,其他原因均不记账')['mode']);
$this->assertSame(['mode' => Rule::MODE_NONE, 'effective_from' => '2025-03-01', 'effective_to' => '2026-02-28'], Rule::parseRuleText('不记账(2025.3月-2026.2月)'));
$this->assertSame(['mode' => Rule::MODE_NONE, 'effective_from' => '2025-03-01', 'effective_to' => '2027-12-30'], Rule::parseRuleText('不记账(2025.3月-2027.12.30)'));
$this->assertSame(Rule::MODE_DROPSHIP_WRONG, Rule::parseRuleText('仅代发仓发错货记账')['mode']);
}
public function test_finance_exclusion_applies_only_to_the_exact_work_order(): void
{
$exclusions = ['GD-EXCLUDED' => '未匹配供应商,财务确认不记账'];
$row = $this->row(['work_order_no' => 'GD-EXCLUDED', 'supplier' => '', 'amount' => '100.00']);
$result = Rule::decide($row, $this->configs(), $exclusions);
$this->assertSame(Rule::STATUS_SKIP, $result['deduction_status']);
$this->assertStringContainsString('财务确认不记账', $result['deduction_basis']);
$this->assertSame(Rule::STATUS_REVIEW, Rule::decide(array_replace($row, ['work_order_no' => 'GD-OTHER']), $this->configs(), $exclusions)['deduction_status']);
$this->assertSame(Rule::STATUS_REVIEW, Rule::decide($row, $this->configs(), ['GD-EXCLUDED' => ''])['deduction_status']);
}
public function test_rejects_unknown_rule_text(): void
{
$this->expectException(InvalidArgumentException::class);
Rule::parseRuleText('看情况');
}
private function row(array $patch = []): array
{
return array_replace([
'started_at' => '2026-09-05 10:00:00', 'reason_level_1' => '厂家记账', 'reason_level_2' => '孔位错误',
'shipping_warehouse' => '常州主仓', 'supplier' => '甲厂',
], $patch);
}
private function configs(): array
{
return [
'甲厂' => ['mode' => Rule::MODE_NORMAL, 'effective_from' => null, 'effective_to' => null],
'乙厂' => ['mode' => Rule::MODE_DROPSHIP_WRONG, 'effective_from' => null, 'effective_to' => null],
'丙厂' => ['mode' => Rule::MODE_NONE, 'effective_from' => null, 'effective_to' => null],
'丁厂' => ['mode' => Rule::MODE_NONE, 'effective_from' => '2025-03-01', 'effective_to' => '2026-02-28'],
];
}
private function judge(array $patch): string
{
return Rule::decide($this->row($patch), $this->configs())['deduction_status'];
}
public function test_supplier_reasons_follow_supplier_config(): void
{
$this->assertSame(Rule::STATUS_DEDUCT, $this->judge([]));
$this->assertSame(Rule::STATUS_SKIP, $this->judge(['supplier' => '乙厂']));
$this->assertSame(Rule::STATUS_SKIP, $this->judge(['supplier' => '丙厂']));
// 配置表中没有的供应商视同正常记账,可扣款。
$this->assertSame(Rule::STATUS_DEDUCT, $this->judge(['supplier' => '未知厂']));
$this->assertSame(Rule::STATUS_REVIEW, $this->judge(['supplier' => '']));
// 限期不记账:期外视同不在配置表,可扣款。
$this->assertSame(Rule::STATUS_DEDUCT, $this->judge(['supplier' => '丁厂']));
$this->assertSame(Rule::STATUS_SKIP, $this->judge(['supplier' => '丁厂', 'started_at' => '2026-02-28 23:00:00']));
}
public function test_warehouse_reasons_only_deduct_dropship_wrong_shipment(): void
{
$wrong = ['reason_level_1' => '仓库记账', 'reason_level_2' => '仓库发错货(面单照片,实物照片,条形码照片)', 'shipping_warehouse' => '曹县洪耀代发仓'];
$this->assertSame(Rule::STATUS_DEDUCT, $this->judge($wrong + ['supplier' => '乙厂']));
$this->assertSame(Rule::STATUS_DEDUCT, $this->judge($wrong));
$this->assertSame(Rule::STATUS_SKIP, $this->judge($wrong + ['supplier' => '丙厂']));
$this->assertSame(Rule::STATUS_DEDUCT, $this->judge($wrong + ['supplier' => '未知厂']));
// 非代发仓、或代发仓的其他仓库记账原因:一律不作为厂家扣款项,无需配置。
$this->assertSame(Rule::STATUS_SKIP, $this->judge(['shipping_warehouse' => '常州主仓'] + $wrong + ['supplier' => '乙厂']));
$this->assertSame(Rule::STATUS_SKIP, $this->judge(['reason_level_2' => '仓库双面单'] + $wrong + ['supplier' => '乙厂']));
$this->assertSame(Rule::STATUS_SKIP, $this->judge(['reason_level_2' => '退货运费'] + $wrong + ['supplier' => '未知厂']));
}
public function test_multiple_suppliers_need_review_when_outcomes_differ(): void
{
$this->assertSame(Rule::STATUS_DEDUCT, $this->judge(['supplier' => "甲厂\n甲厂"]));
$result = Rule::decide($this->row(['supplier' => "甲厂\n丙厂"]), $this->configs());
$this->assertSame(Rule::STATUS_REVIEW, $result['deduction_status']);
$this->assertStringContainsString('甲厂:扣款', $result['deduction_basis']);
$this->assertSame("正常记账\n不记账", $result['supplier_config']);
}
public function test_small_compensation_uses_bookkeeping_divided_by_buyer_paid_and_preserves_notes(): void
{
foreach (['补偿款项', '记账全款', '记账全额'] as $type) {
$row = $this->row(['bookkeeping_type' => $type, 'amount' => '30.00', 'buyer_paid_amount' => '100', 'remark' => '协商补偿']);
$result = Rule::decide($row, $this->configs());
$this->assertSame('0.00', $result['amount']);
$this->assertSame(Rule::STATUS_SKIP, $result['deduction_status']);
$this->assertSame("协商补偿\n小于50且小于0.4,记账改0", $result['remark']);
$this->assertSame('30.00', $row['amount']);
}
$multiple = Rule::decide($this->row(['bookkeeping_type' => '补偿款项', 'amount' => '30.00', 'buyer_paid_amount' => "40\n60"]), []);
$this->assertSame('0.00', $multiple['amount']);
}
public function test_small_compensation_requires_both_strict_limits_and_complete_positive_paid_amount(): void
{
foreach ([
['50.00', '200'], ['49.99', '124.975'], ['40.00', '100'], ['39.99', '99'],
['30.00', '10'], ['30.00', ''], ['30.00', '0'], ['30.00', '-100'], ['30.00', "100\n"],
['30.00', "\n100"], ['30.00', 'invalid'], ['0.00', '100'], ['-1.00', '100'], ['', '100'],
] as [$amount, $paid]) {
$result = Rule::decide($this->row(['bookkeeping_type' => '补偿款项', 'amount' => $amount, 'buyer_paid_amount' => $paid]), $this->configs());
$this->assertArrayNotHasKey('amount', $result, "$amount / $paid");
}
$hit = Rule::decide($this->row(['bookkeeping_type' => '补偿款项', 'amount' => '39.99', 'buyer_paid_amount' => '100']), $this->configs());
$this->assertSame('0.00', $hit['amount']);
$freight = Rule::decide($this->row(['bookkeeping_type' => '运费损耗', 'amount' => '10.00', 'buyer_paid_amount' => '100']), $this->configs());
$this->assertArrayNotHasKey('amount', $freight);
}
public function test_second_round_uses_order_actual_paid_when_buyer_paid_round_does_not_hit(): void
{
foreach (['10', '', '0', "100\n"] as $buyer) {
$result = Rule::decide($this->row(['bookkeeping_type' => '补偿款项', 'amount' => '30.00',
'buyer_paid_amount' => $buyer, 'actual_paid_amount' => '100.00']), $this->configs());
$this->assertSame('0.00', $result['amount']);
$this->assertSame(Rule::STATUS_SKIP, $result['deduction_status']);
}
$equal = Rule::decide($this->row(['bookkeeping_type' => '记账全款', 'amount' => '40.00',
'buyer_paid_amount' => '50', 'actual_paid_amount' => '100.00']), $this->configs());
$this->assertArrayNotHasKey('amount', $equal);
}
}