From 5dbf4ecfa6ea8613b03dab3f70c39614cb2179b5 Mon Sep 17 00:00:00 2001 From: yueyang <2495544247@qq.com> Date: Fri, 23 Oct 2026 15:36:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E7=A7=91=E7=9B=AE?= =?UTF-8?q?=E6=98=A0=E5=B0=84=E6=9C=AC=E5=9C=B0=E6=A8=A1=E6=8B=9F=E9=87=8D?= =?UTF-8?q?=E8=B7=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/SyncStoreReceiptOrderMatches.php | 6 ++- .../Services/AccountLedgerRemarkMap.php | 25 +++++++++- ...StoreReceiptMappingPreviewOverrideTest.php | 47 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/EcommerceFinance/StoreReceiptMappingPreviewOverrideTest.php diff --git a/app/Console/Commands/SyncStoreReceiptOrderMatches.php b/app/Console/Commands/SyncStoreReceiptOrderMatches.php index 661b6647..2c512a78 100644 --- a/app/Console/Commands/SyncStoreReceiptOrderMatches.php +++ b/app/Console/Commands/SyncStoreReceiptOrderMatches.php @@ -121,9 +121,13 @@ class SyncStoreReceiptOrderMatches extends Command '优惠收入', ]; - /** 「冲减金额」列口径:账单的总账备注命中以下取值,不参与收入金额与差额计算。 */ + /** + * 「冲减金额」列口径:账单的总账备注命中以下取值,不参与收入金额与差额计算。 + * 「违约赔付(冲减收入)」是冲减收入的细分项,总额仍计入冲减金额。 + */ private const OFFSET_REMARKS = [ '冲减收入', + '违约赔付(冲减收入)', ]; /** 「退款金额」列口径:账单的总账备注命中以下取值,单独列示,不参与收入金额与差额计算。 */ diff --git a/modules/EcommerceFinance/Services/AccountLedgerRemarkMap.php b/modules/EcommerceFinance/Services/AccountLedgerRemarkMap.php index fd33d349..883b2440 100644 --- a/modules/EcommerceFinance/Services/AccountLedgerRemarkMap.php +++ b/modules/EcommerceFinance/Services/AccountLedgerRemarkMap.php @@ -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>|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 { diff --git a/tests/Unit/EcommerceFinance/StoreReceiptMappingPreviewOverrideTest.php b/tests/Unit/EcommerceFinance/StoreReceiptMappingPreviewOverrideTest.php new file mode 100644 index 00000000..38e4a8a4 --- /dev/null +++ b/tests/Unit/EcommerceFinance/StoreReceiptMappingPreviewOverrideTest.php @@ -0,0 +1,47 @@ +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); + } + } +}