fix(科目映射): 淘宝天猫按同一平台处理 (#1747)

This commit is contained in:
yueyang 2026-12-05 09:53:10 +08:00
parent 46825fc165
commit 773681773f
8 changed files with 253 additions and 16 deletions

View File

@ -46,8 +46,8 @@ class AccountMappingCoverage
];
private const PLATFORM_PREFIX = [
'天猫' => '天猫',
'淘宝' => '淘宝',
'天猫' => AccountMappingPlatform::TAOBAO_TMALL,
'淘宝' => AccountMappingPlatform::TAOBAO_TMALL,
'京东' => '京东',
'京喜' => '京喜',
'京喜小店' => '京喜',
@ -330,7 +330,7 @@ class AccountMappingCoverage
$items = [];
$missingRows = 0;
$excludedRows = 0;
foreach ($raw['pairs'] as $pair) {
foreach ($this->canonicalPairs((array) ($raw['pairs'] ?? [])) as $pair) {
if (! $this->isPlatformBill($pair)) {
$excludedRows += (int) $pair['source_rows'];
continue;
@ -376,7 +376,7 @@ class AccountMappingCoverage
$missingRows = 0;
$excludedRows = 0;
foreach ($raw['pairs'] as $pair) {
foreach ($this->canonicalPairs((array) ($raw['pairs'] ?? [])) as $pair) {
if (! $this->isPlatformBill($pair)) {
$excludedRows += (int) $pair['source_rows'];
continue;
@ -971,7 +971,7 @@ class AccountMappingCoverage
{
$keys = [];
foreach ($rules as $rule) {
$platform = trim((string) ($rule['platform'] ?? ''));
$platform = AccountMappingPlatform::canonical((string) ($rule['platform'] ?? ''));
$item = trim((string) ($rule['item'] ?? ''));
if ($platform === '' || $item === '') {
continue;
@ -984,7 +984,7 @@ class AccountMappingCoverage
private function isMapped(array $pair, array $mapping): bool
{
$platform = (string) ($pair['platform'] ?? '');
$platform = AccountMappingPlatform::canonical((string) ($pair['platform'] ?? ''));
$item = (string) ($pair['item'] ?? '');
if ($platform === '' || $item === '') {
return false;
@ -1001,6 +1001,69 @@ class AccountMappingCoverage
return $platform !== '' && $platform !== '支付宝';
}
/**
* 历史快照仍可能保存“淘宝”“天猫”两组;读取时统一口径并合并同名项目。
*
* @param list<array<string,mixed>> $pairs
* @return list<array<string,mixed>>
*/
private function canonicalPairs(array $pairs): array
{
$normalized = [];
foreach ($pairs as $pair) {
if (! is_array($pair)) {
continue;
}
$platform = AccountMappingPlatform::canonical((string) ($pair['platform'] ?? ''));
$item = trim((string) ($pair['item'] ?? ''));
if ($platform === '' && $item === '') {
continue;
}
$pair['platform'] = $platform;
$pair['platform_label'] = $platform;
$key = $platform . "\0" . $item;
if (! isset($normalized[$key])) {
$normalized[$key] = $pair;
continue;
}
$normalized[$key]['source_rows'] += (int) ($pair['source_rows'] ?? 0);
$normalized[$key]['store_count'] += (int) ($pair['store_count'] ?? 0);
$normalized[$key]['source_labels'] = array_values(array_unique(array_merge(
(array) ($normalized[$key]['source_labels'] ?? []),
(array) ($pair['source_labels'] ?? []),
)));
$normalized[$key]['raw_items'] = array_values(array_unique(array_merge(
(array) ($normalized[$key]['raw_items'] ?? []),
(array) ($pair['raw_items'] ?? []),
)));
$normalized[$key]['months'] = array_values(array_unique(array_merge(
(array) ($normalized[$key]['months'] ?? []),
(array) ($pair['months'] ?? []),
)));
if (isset($pair['month_rows']) || isset($normalized[$key]['month_rows'])) {
$monthRows = (array) ($normalized[$key]['month_rows'] ?? []);
foreach ((array) ($pair['month_rows'] ?? []) as $month => $rows) {
$monthRows[$month] = (int) ($monthRows[$month] ?? 0) + (int) $rows;
}
ksort($monthRows);
$normalized[$key]['month_rows'] = $monthRows;
$normalized[$key]['months'] = array_keys($monthRows);
}
}
foreach ($normalized as &$pair) {
if (isset($pair['months'])) {
sort($pair['months']);
}
}
unset($pair);
return array_values($normalized);
}
/** @return list<string> */
private function generations(string $periodsTable, string $start, string $end): array
{

View File

@ -35,7 +35,8 @@ class AccountMappingMatcher
{
$candidates = [];
foreach ($this->store->all() as $rule) {
if ($this->sameText((string) $rule['platform'], $platform) && $this->sameText((string) $rule['item'], $item)) {
if (AccountMappingPlatform::equivalent((string) $rule['platform'], $platform)
&& $this->sameText((string) $rule['item'], $item)) {
$candidates[] = $rule;
}
}
@ -45,13 +46,14 @@ class AccountMappingMatcher
}
$hits = [];
$fallback = null;
$fallbacks = [];
foreach ($candidates as $rule) {
$memo = trim((string) ($rule['memo'] ?? ''));
$goods = trim((string) ($rule['goods'] ?? ''));
if ($memo === '' && $goods === '') {
$fallback ??= $rule;
$rank = AccountMappingPlatform::matchRank((string) $rule['platform'], $platform);
$fallbacks[$rank] ??= $rule;
continue;
}
@ -62,11 +64,21 @@ class AccountMappingMatcher
continue;
}
$hits[] = ['rule' => $rule, 'score' => ($goods !== '' ? 2 : 0) + ($memo !== '' ? 1 : 0)];
$hits[] = [
'rule' => $rule,
'score' => ($goods !== '' ? 2 : 0) + ($memo !== '' ? 1 : 0),
'platform_rank' => AccountMappingPlatform::matchRank((string) $rule['platform'], $platform),
];
}
if ($hits !== []) {
usort($hits, static fn (array $a, array $b): int => $b['score'] <=> $a['score'] ?: ((int) $a['rule']['id'] <=> (int) $b['rule']['id']));
usort($hits, static fn (array $a, array $b): int => (
$b['score'] <=> $a['score']
) ?: (
$a['platform_rank'] <=> $b['platform_rank']
) ?: (
(int) $a['rule']['id'] <=> (int) $b['rule']['id']
));
return [
'rule' => $hits[0]['rule'],
@ -75,6 +87,8 @@ class AccountMappingMatcher
];
}
ksort($fallbacks);
$fallback = reset($fallbacks) ?: null;
$rule = $fallback ?? $candidates[0];
return [

View File

@ -0,0 +1,48 @@
<?php
namespace Modules\EcommerceFinance\Services;
/**
* 科目映射的业务平台口径。
*
* 淘宝和天猫在实际经营中按同一平台处理;规则仍保留原始平台名,
* 这样历史操作记录和精确规则不会丢失。
*/
final class AccountMappingPlatform
{
public const TAOBAO = '淘宝';
public const TMALL = '天猫';
public const TAOBAO_TMALL = '淘宝天猫';
public static function canonical(string $platform): string
{
$platform = trim($platform);
return match ($platform) {
self::TAOBAO, self::TMALL, self::TAOBAO_TMALL => self::TAOBAO_TMALL,
default => $platform,
};
}
public static function equivalent(string $left, string $right): bool
{
return self::canonical($left) === self::canonical($right);
}
/**
* 排序优先级:原平台精确匹配优先,同口径平台其次,其它平台最后。
*/
public static function matchRank(string $candidate, string $target): int
{
$candidate = trim($candidate);
$target = trim($target);
if (mb_strtolower($candidate) === mb_strtolower($target)) {
return 0;
}
return self::equivalent($candidate, $target) ? 1 : 2;
}
}

View File

@ -80,8 +80,8 @@ class AccountMappingSuggestion
}
usort($matches, static fn (array $left, array $right): int => (
((string) ($left['platform'] ?? '') === $platform ? 0 : 1)
<=> ((string) ($right['platform'] ?? '') === $platform ? 0 : 1)
AccountMappingPlatform::matchRank((string) ($left['platform'] ?? ''), $platform)
<=> AccountMappingPlatform::matchRank((string) ($right['platform'] ?? ''), $platform)
) ?: (((int) ($left['id'] ?? 0)) <=> ((int) ($right['id'] ?? 0))));
$templates = [];

View File

@ -894,7 +894,7 @@
>
<div class="am-coverage-toolbar">
<div class="am-coverage-toolbar__fields">
<span class="am-coverage-range">统计最近 60 个月;清单生成后持续保留,每月自动更新一次</span>
<span class="am-coverage-range">统计最近 60 个月;清单生成后持续保留,每月自动更新一次;淘宝、天猫按“淘宝天猫”同一平台口径统计</span>
<button type="button" class="am-btn" id="amCoverageRefresh">立即刷新清单</button>
</div>
<div class="am-coverage-summary" id="amCoverageSummary" aria-live="polite">

View File

@ -150,6 +150,33 @@ class AccountMappingMatcherTest extends FeatureTestCase
$this->assertSame($rule['id'], $hit['rule']['id']);
}
/** 淘宝、天猫按同一平台匹配;原平台精确规则仍优先 */
public function test_taobao_and_tmall_share_rules_with_exact_platform_priority(): void
{
$tmallOnly = $this->rule([
'platform' => '天猫',
'item' => '共享项目',
'memo' => '技术服务费',
]);
$fallback = $this->matcher->match('淘宝', '共享项目', ['技术服务费']);
$this->assertNotNull($fallback);
$this->assertTrue($fallback['matched']);
$this->assertSame($tmallOnly['id'], $fallback['rule']['id']);
$taobaoExact = $this->rule([
'platform' => '淘宝',
'item' => '共享项目',
'memo' => '技术服务费',
'dAcct' => '管理费用',
'dSub' => '软件服务费',
]);
$exact = $this->matcher->match('淘宝', '共享项目', ['技术服务费']);
$this->assertNotNull($exact);
$this->assertSame($taobaoExact['id'], $exact['rule']['id']);
}
/** 校验:同项目下有兜底规则时给出提示,提醒它只做兜底 */
public function test_validator_hints_fallback_rule(): void
{

View File

@ -97,9 +97,9 @@ class AccountMappingCoverageTest extends TestCase
$this->assertSame(3, $first['missing_item_count']);
$this->assertSame(4, $first['missing_row_count']);
$this->assertSame([
'商家货款入账|天猫|2',
'商家货款入账|淘宝天猫|2',
'未映射售后费|京东|1',
'(未填写账单项目)|天猫|1',
'(未填写账单项目)|淘宝天猫|1',
], $this->missingSignature($first['items']));
$this->assertNotContains('上期项目', array_column($first['items'], 'item'));
$this->assertNotContains('未知平台项目', array_column($first['items'], 'item'));
@ -259,6 +259,79 @@ class AccountMappingCoverageTest extends TestCase
$this->assertSame('平台账单项目', $summary['items'][0]['item']);
}
public function test_retained_snapshot_treats_taobao_and_tmall_as_one_platform(): void
{
$store = app(AccountMappingStore::class);
$this->rule($store, '天猫', '两边共同项目');
$directory = $this->storageRoot . '/app/account-mapping/coverage-snapshots';
File::ensureDirectoryExists($directory);
File::put($directory . '/all-taobao-tmall.json', json_encode([
'version' => 1,
'month' => 'all',
'source_key' => 'taobao-tmall',
'refreshed_at' => '2026-12-01 08:00:00',
'raw' => [
'status' => 'ready',
'source_rows' => 5,
'pending_sources' => [],
'pairs' => [
[
'platform' => '天猫',
'platform_label' => '天猫',
'item' => '两边共同项目',
'source_rows' => 2,
'store_count' => 1,
'source_labels' => ['淘系支付宝'],
'raw_items' => ['两边共同项目'],
'months' => ['2026-11'],
],
[
'platform' => '淘宝',
'platform_label' => '淘宝',
'item' => '两边共同项目',
'source_rows' => 1,
'store_count' => 1,
'source_labels' => ['淘系支付宝'],
'raw_items' => ['两边共同项目'],
'months' => ['2026-10'],
],
[
'platform' => '天猫',
'platform_label' => '天猫',
'item' => '统一口径未映射项目',
'source_rows' => 1,
'store_count' => 1,
'source_labels' => ['淘系支付宝'],
'raw_items' => ['统一口径未映射项目'],
'months' => ['2026-10'],
],
[
'platform' => '淘宝',
'platform_label' => '淘宝',
'item' => '统一口径未映射项目',
'source_rows' => 1,
'store_count' => 1,
'source_labels' => ['淘系微信'],
'raw_items' => ['统一口径未映射项目'],
'months' => ['2026-11'],
],
],
],
], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR));
$summary = app(AccountMappingCoverage::class)->missingAcrossMonths();
$this->assertSame('ready', $summary['status']);
$this->assertSame(5, $summary['source_rows']);
$this->assertSame(1, $summary['missing_item_count']);
$this->assertSame(2, $summary['missing_row_count']);
$this->assertSame('淘宝天猫', $summary['items'][0]['platform']);
$this->assertSame('统一口径未映射项目', $summary['items'][0]['item']);
$this->assertSame(['2026-10', '2026-11'], $summary['items'][0]['months']);
$this->assertSame(['淘系支付宝', '淘系微信'], $summary['items'][0]['source_labels']);
}
public function test_interrupted_refresh_request_is_recovered_after_stale_timeout(): void
{
$coverage = app(AccountMappingCoverage::class);

View File

@ -30,6 +30,18 @@ class AccountMappingSuggestionTest extends TestCase
$this->assertSame('冲减收入', $result['values']['memo']);
}
public function test_taobao_and_tmall_are_preferred_as_the_same_platform(): void
{
$result = app(AccountMappingSuggestion::class)->suggestFromRules([
['id' => 10] + $this->rule('拼多多', '平台技术服务费', '技术服务费', '费用', '销售费用'),
['id' => 20] + $this->rule('天猫', '平台技术服务费', '技术服务费', '费用', '销售费用'),
], '淘宝天猫', '平台技术服务费');
$this->assertTrue($result['auto_fill']);
$this->assertSame(20, $result['sources'][0]['id']);
$this->assertSame('天猫', $result['sources'][0]['platform']);
}
public function test_conflicting_templates_are_not_applied_automatically(): void
{
$result = app(AccountMappingSuggestion::class)->suggestFromRules([