CaiWuXiTong/tests/Unit/EcommerceFinance/PlantingQuotaCalculatorTest.php

165 lines
9.7 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 Modules\EcommerceFinance\Services\PlantingQuotaCalculator as Calculator;
use PHPUnit\Framework\TestCase;
class PlantingQuotaCalculatorTest extends TestCase
{
private function row(string $workOrder, string $amount, array $patch = []): array
{
return array_replace([
'work_order_no' => $workOrder, 'status_code' => 'WAIT_CONFIRM', 'paid_at' => '2026-11-26 16:03:21',
'shop_name' => '天猫-亿家达旗舰店', 'platform' => '淘宝天猫', 'platform_product_id' => '1001',
'actual_paid_amount' => $amount, 'reimbursement_amount' => $amount,
], $patch);
}
private function rule(string $type, array $patch = []): array
{
return array_replace([
'rule_type' => $type, 'platform' => '', 'shop_name' => '', 'channel' => '', 'position_level' => '', 'ider' => '',
'month_limit' => null, 'day_limit' => null, 'single_limit' => null, 'is_enabled' => true,
], $patch);
}
private function calc(array $rows, array $rules, array $owners = [], array $levels = [], array $shops = []): array
{
return (new Calculator())->calculate(
$rows,
$shops + ['天猫-亿家达旗舰店' => ['platform' => '天猫', 'shop_head' => '店长甲'], '天猫-欧意朗旗舰店' => ['platform' => '天猫', 'shop_head' => '無尘']],
$owners + ['1001' => ['ider' => '無尘', 'curr_cater' => '無尘'], '2001' => ['ider' => '子远', 'curr_cater' => '子龙']],
$levels + ['無尘' => 'M4', '子远' => 'P2', '店长甲' => 'P3'],
$rules,
);
}
/** 交付文档附录 B 的实测样本:4 单同域累加 320 未超日限额;子远命中 ID负责人规则,单笔 10 → 单笔超额。 */
public function test_matches_delivery_document_sample(): void
{
$rules = [
$this->rule('platform', ['platform' => '天猫', 'position_level' => 'P2', 'day_limit' => '500', 'single_limit' => '300']),
$this->rule('platform', ['platform' => '天猫', 'position_level' => 'M4', 'day_limit' => '1500', 'single_limit' => '500']),
$this->rule('ider', ['ider' => '子远', 'single_limit' => '10']),
];
$rows = [];
foreach (['GD662946294', 'GD662946295', 'GD662946296', 'GD662946297'] as $workOrder) {
$rows[] = $this->row($workOrder, '80', ['shop_name' => '天猫-欧意朗旗舰店']);
}
$rows[] = $this->row('GD662974885', '30', ['shop_name' => '天猫-欧意朗旗舰店', 'platform_product_id' => "1100551039905\n2001"]);
$result = $this->calc($rows, $rules);
$this->assertSame('ok', $result['GD662946294']['risk_level']);
$this->assertSame('320.00', $result['GD662946294']['day_total']);
$this->assertSame('1500.00', $result['GD662946294']['day_limit']);
$this->assertSame('平台+职级:天猫/M4', $result['GD662946294']['rule_label']);
$this->assertSame('無尘|platform|天猫|M4', $result['GD662946294']['scope_key']);
$ziyuan = $result['GD662974885'];
$this->assertSame('danger', $ziyuan['risk_level']);
$this->assertSame(['单笔超额'], $ziyuan['risk_types']);
$this->assertSame('子远', $ziyuan['ider']);
$this->assertSame('子龙', $ziyuan['curr_cater']);
$this->assertSame('2001', $ziyuan['product_id']);
$this->assertSame('ID负责人:子远', $ziyuan['rule_label']);
$this->assertSame('单笔 30.00 超过上限 10.00', $ziyuan['risk_message']);
$this->assertSame('20.00', $ziyuan['over_amount']);
}
public function test_daily_and_monthly_totals_include_other_statuses_and_days(): void
{
$rules = [$this->rule('platform', ['platform' => '天猫', 'position_level' => 'M4', 'day_limit' => '100', 'month_limit' => '250'])];
$result = $this->calc([
$this->row('A', '60', ['paid_at' => '2026-11-26 09:00:00']),
$this->row('B', '50', ['paid_at' => '2026-11-26 18:00:00', 'status_code' => 'CONFIRMED']),
$this->row('C', '100', ['paid_at' => '2026-11-03 10:00:00', 'status_code' => 'CONFIRMED']),
$this->row('D', '500', ['paid_at' => '2026-11-26 10:00:00', 'status_code' => 'DISABLED']),
$this->row('E', '40', ['paid_at' => '2026/11/2 8:05']),
], $rules);
$this->assertArrayNotHasKey('D', $result);
// 日累计 = 当天全部单合计(含已完成),每单相同;作废的不算。
$this->assertSame('110.00', $result['A']['day_total']);
$this->assertSame('110.00', $result['B']['day_total']);
$this->assertSame(['每日超额'], $result['A']['risk_types']);
$this->assertSame('250.00', $result['C']['month_total']);
$this->assertSame('ok', $result['C']['risk_level']);
$this->assertSame('2026-11-02 08:05:00', $result['E']['paid_at']);
$result = $this->calc([
$this->row('A', '60', ['paid_at' => '2026-11-26 09:00:00']),
$this->row('C', '200', ['paid_at' => '2026-11-03 10:00:00']),
], $rules);
$this->assertSame(['月度超额'], $result['A']['risk_types']);
$this->assertSame('本月累计 260.00 超过上限 250.00', $result['A']['risk_message']);
$this->assertSame(['每日超额', '月度超额'], $result['C']['risk_types']);
}
public function test_owner_fallback_and_configuration_risks(): void
{
$rules = [
$this->rule('platform', ['platform' => '天猫', 'position_level' => 'P3', 'single_limit' => '300']),
$this->rule('platform', ['platform' => '天猫', 'position_level' => 'P2', 'single_limit' => '300']),
];
$result = $this->calc([
// 商品没负责人 → 店长兜底,部门负责人为空。
$this->row('SHOPHEAD', '10', ['platform_product_id' => '9999']),
// 店铺查不到、商品也没负责人 → 未匹配负责人,不判额度。
$this->row('NOOWNER', '999', ['shop_name' => '拼多多-新店', 'platform' => '拼多多', 'platform_product_id' => '9999']),
$this->row('NOLEVEL', '10', ['platform_product_id' => '3001']),
$this->row('BADLEVEL', '10', ['platform_product_id' => '3002']),
$this->row('NORULE', '10', ['platform_product_id' => '3003']),
], $rules, ['3001' => ['ider' => '新人', 'curr_cater' => null], '3002' => ['ider' => '高手', 'curr_cater' => null], '3003' => ['ider' => '主管', 'curr_cater' => null]], ['高手' => '高级', '主管' => 'M2']);
$this->assertSame('店长甲', $result['SHOPHEAD']['ider']);
$this->assertNull($result['SHOPHEAD']['curr_cater']);
$this->assertSame('ok', $result['SHOPHEAD']['risk_level']);
$this->assertSame(['未匹配负责人'], $result['NOOWNER']['risk_types']);
$this->assertSame('warning', $result['NOOWNER']['risk_level']);
$this->assertSame('拼多多', $result['NOOWNER']['platform']);
$this->assertSame(['职级为空'], $result['NOLEVEL']['risk_types']);
$this->assertStringStartsWith('职级为空', $result['NOLEVEL']['risk_message']);
$this->assertSame(['职级未配置'], $result['BADLEVEL']['risk_types']);
$this->assertSame(['额度规则未配置'], $result['NORULE']['risk_types']);
$this->assertSame('未命中ID负责人、店铺或平台规则;平台 天猫,职级 M2', $result['NORULE']['risk_message']);
}
public function test_rule_priority_channel_and_disabled_rules(): void
{
$rules = [
$this->rule('platform', ['platform' => '天猫', 'position_level' => 'M4', 'channel' => '天猫', 'single_limit' => '1']),
$this->rule('platform', ['platform' => '天猫', 'position_level' => 'M4', 'single_limit' => '500']),
$this->rule('shop', ['shop_name' => '天猫-亿家达旗舰店', 'single_limit' => '200']),
$this->rule('shop_level', ['shop_name' => '天猫-亿家达旗舰店', 'position_level' => 'M4', 'single_limit' => '50', 'is_enabled' => false]),
];
$result = $this->calc([$this->row('A', '100')], $rules);
// 停用的店铺+职级不参与,命中店铺规则(优先于平台规则)。
$this->assertSame('店铺:天猫-亿家达旗舰店', $result['A']['rule_label']);
$this->assertSame('無尘|SHOP|天猫-亿家达旗舰店|M4', $result['A']['scope_key']);
$result = $this->calc([$this->row('A', '100', ['shop_name' => '天猫-欧意朗旗舰店'])], $rules);
// 平台规则优先渠道为空的那条。
$this->assertSame('平台+职级:天猫/M4', $result['A']['rule_label']);
$this->assertSame('ok', $result['A']['risk_level']);
}
public function test_amount_platform_and_band_normalization(): void
{
$this->assertSame(4150, Calculator::amount(['actual_paid_amount' => '41.5', 'reimbursement_amount' => '99']));
$this->assertSame(9900, Calculator::amount(['actual_paid_amount' => '', 'reimbursement_amount' => '99']));
$this->assertSame(0, Calculator::amount(['actual_paid_amount' => '0', 'reimbursement_amount' => '99']));
$this->assertSame('京喜', Calculator::platform('京喜京东'));
$this->assertSame('京东', Calculator::platform('京东自营'));
$this->assertSame('天猫', Calculator::platform('淘宝天猫'));
$this->assertSame('拼多多', Calculator::platform('pdd'));
$this->assertSame('阿里巴巴', Calculator::platform('1688'));
$this->assertSame('未归属', Calculator::platform('快手'));
$this->assertSame('P2', Calculator::band('P2-运营'));
$this->assertSame('M4', Calculator::band('m4'));
$this->assertNull(Calculator::band('高级'));
$this->assertNull(Calculator::band('M6'));
}
}