feat: 支持科目映射本地模拟重跑

This commit is contained in:
yueyang 2026-10-23 15:36:42 +08:00
parent 4644146cce
commit 5dbf4ecfa6
3 changed files with 76 additions and 2 deletions

View File

@ -121,9 +121,13 @@ class SyncStoreReceiptOrderMatches extends Command
'优惠收入',
];
/** 「冲减金额」列口径:账单的总账备注命中以下取值,不参与收入金额与差额计算。 */
/**
* 「冲减金额」列口径:账单的总账备注命中以下取值,不参与收入金额与差额计算。
* 「违约赔付(冲减收入)」是冲减收入的细分项,总额仍计入冲减金额。
*/
private const OFFSET_REMARKS = [
'冲减收入',
'违约赔付(冲减收入)',
];
/** 「退款金额」列口径:账单的总账备注命中以下取值,单独列示,不参与收入金额与差额计算。 */

View File

@ -466,7 +466,9 @@ class AccountLedgerRemarkMap
$index = [];
try {
foreach (app(AccountMappingStore::class)->all() as $rule) {
$rules = $this->accountMappingOverrideRules()
?? app(AccountMappingStore::class)->all();
foreach ($rules as $rule) {
if (! is_array($rule)) {
continue;
}
@ -485,6 +487,27 @@ class AccountLedgerRemarkMap
return $this->accountMappingIndex = $index;
}
/**
* 本地模拟映射覆盖:仅在显式设置 ACCOUNT_MAPPING_OVERRIDE_FILE 时启用。
* 正常页面和定时任务不设置该变量,继续读取共享系统的科目映射表。
*
* @return list<array<string,mixed>>|null
*/
private function accountMappingOverrideRules(): ?array
{
$path = trim((string) (getenv('ACCOUNT_MAPPING_OVERRIDE_FILE') ?: ''));
if ($path === '' || ! is_file($path)) {
return null;
}
$decoded = json_decode((string) file_get_contents($path), true);
if (! is_array($decoded) || ! array_is_list($decoded)) {
throw new \RuntimeException('本地科目映射覆盖文件格式无效:' . $path);
}
return $decoded;
}
/** 同平台同账单项目的「财务备注」;没有规则或财务备注为空时返回 null(由调用方回落)。 */
private function accountMappingRemark(string $platform, string $item, array $businessRemarks, string $goodsName): ?string
{

View File

@ -0,0 +1,47 @@
<?php
namespace Tests\Unit\EcommerceFinance;
use App\Console\Commands\SyncStoreReceiptOrderMatches;
use Modules\EcommerceFinance\Services\AccountLedgerRemarkMap;
use ReflectionClass;
use ReflectionMethod;
use Tests\TestCase;
class StoreReceiptMappingPreviewOverrideTest extends TestCase
{
public function test_default_compensation_is_included_in_offset_remarks(): void
{
$reflection = new ReflectionClass(SyncStoreReceiptOrderMatches::class);
$remarks = $reflection->getConstant('OFFSET_REMARKS');
$this->assertContains('冲减收入', $remarks);
$this->assertContains('违约赔付(冲减收入)', $remarks);
}
public function test_local_mapping_override_file_is_used_when_configured(): void
{
$path = tempnam(sys_get_temp_dir(), 'caiwu-mapping-override-');
$this->assertNotFalse($path);
file_put_contents($path, json_encode([
['platform' => '京东', 'item' => '结算单', 'memo' => '订单收入'],
['platform' => '京东', 'item' => '售后卖家赔付费', 'memo' => '违约赔付(冲减收入)'],
], JSON_UNESCAPED_UNICODE));
$previous = getenv('ACCOUNT_MAPPING_OVERRIDE_FILE');
putenv('ACCOUNT_MAPPING_OVERRIDE_FILE=' . $path);
try {
$method = new ReflectionMethod(AccountLedgerRemarkMap::class, 'accountMappingOverrideRules');
$rules = $method->invoke(new AccountLedgerRemarkMap());
$this->assertCount(2, $rules);
$this->assertSame('违约赔付(冲减收入)', $rules[1]['memo']);
} finally {
putenv($previous === false
? 'ACCOUNT_MAPPING_OVERRIDE_FILE'
: 'ACCOUNT_MAPPING_OVERRIDE_FILE=' . $previous);
@unlink($path);
}
}
}