From 941b1acd19e074dac7fff0276d4a8a54464a8eee Mon Sep 17 00:00:00 2001 From: shanshi <3316401569@qq.com> Date: Wed, 18 Nov 2026 17:09:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=B2=BE=E7=AE=80=E8=B4=A2=E5=8A=A1?= =?UTF-8?q?=E5=8F=B0=E8=B4=A6=E6=9D=A5=E6=BA=90=E4=B8=8E=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E9=A2=91=E7=8E=87=20(#1242)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/SyncFinanceLedgerRpt.php | 14 +- app/Console/Kernel.php | 8 +- deploy/activate-incremental-release.sh | 6 +- deploy/activate-release.sh | 6 +- docs/财务台账开发说明.md | 10 +- .../Services/FinanceLedgerRptSynchronizer.php | 123 +++++++----------- .../FinanceLedgerTransactionWriter.php | 15 ++- .../View/FinanceLedgerPageData.php | 20 ++- .../EcommerceFinance/View/ShopFlowQuery.php | 2 +- .../pages/financial-ledger/ledger.blade.php | 18 ++- .../FinanceLedgerHeaderControlsTest.php | 45 +++++++ .../FinanceLedgerManualTransactionTest.php | 47 ++++++- .../Deployment/BusinessMirrorScheduleTest.php | 19 ++- 13 files changed, 226 insertions(+), 107 deletions(-) diff --git a/app/Console/Commands/SyncFinanceLedgerRpt.php b/app/Console/Commands/SyncFinanceLedgerRpt.php index 0bf1ed8c..204cffc7 100644 --- a/app/Console/Commands/SyncFinanceLedgerRpt.php +++ b/app/Console/Commands/SyncFinanceLedgerRpt.php @@ -14,6 +14,7 @@ class SyncFinanceLedgerRpt extends Command {--days= : 只拉取最近 N 天并合并到现有明细} {--merge : 合并到现有明细,不删除历史} {--dingtalk-only : 只同步钉钉付款明细,用于高频增量} + {--sources= : 只同步指定来源,逗号分隔:dingtalk,payable,store,withdrawal} {--no-recalculate : 只同步明细,不重算流水}'; protected $description = '从 AI 库 rpt 同步财务台账动账明细,并重算每日流水'; @@ -24,17 +25,26 @@ class SyncFinanceLedgerRpt extends Command $from = $this->option('from') ?: null; $merge = (bool) $this->option('merge'); $dingtalkOnly = (bool) $this->option('dingtalk-only'); + $sources = array_values(array_filter(array_map( + 'trim', + explode(',', (string) $this->option('sources')), + ))); + if ($dingtalkOnly) { + $sources = ['dingtalk']; + } if ($days > 0) { $from = now()->subDays(max(1, $days) - 1)->toDateString(); $merge = true; } try { - $result = $dingtalkOnly - ? $synchronizer->syncDingTalk( + $result = $sources !== [] + ? $synchronizer->syncSources( + $sources, ! $this->option('no-recalculate'), $from, $this->option('to') ?: null, + $merge, ) : $synchronizer->sync( ! $this->option('no-recalculate'), diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index ba7e5805..eb2fc546 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -113,15 +113,15 @@ class Kernel extends ConsoleKernel // 财务台账:从 AI 库 rpt 同步动账明细,按日重算账户流水。 // 生产宿主机 cron 仍需配置对应的 finance-ledger:sync-rpt 入口。 - $schedule->command('finance-ledger:sync-rpt --days=7 --merge') + $schedule->command('finance-ledger:sync-rpt --days=7 --merge --sources=store,withdrawal') ->dailyAt('02:30') ->timezone('Asia/Shanghai') ->withoutOverlapping(120) ->runInBackground(); - // 钉钉付款明细每 5 分钟增量进入财务台账,保留每日 7 天回看校准。 - $schedule->command('finance-ledger:sync-rpt --days=2 --merge --dingtalk-only --quiet') - ->cron('3-59/5 * * * *') + // 钉钉审批和供应商应付每小时增量;店铺相关来源每天同步一次。 + $schedule->command('finance-ledger:sync-rpt --days=2 --merge --sources=dingtalk,payable --quiet') + ->hourlyAt(5) ->timezone('Asia/Shanghai') ->withoutOverlapping(10) ->runInBackground(); diff --git a/deploy/activate-incremental-release.sh b/deploy/activate-incremental-release.sh index f9ca3d7b..ac54f987 100644 --- a/deploy/activate-incremental-release.sh +++ b/deploy/activate-incremental-release.sh @@ -240,9 +240,9 @@ CRON_TZ=Asia/Shanghai */2 * * * * root /usr/bin/flock -n /run/caiwu-esign-polling.lock /usr/bin/docker exec -u www-data caiwu_app php artisan esign:poll-signatures --quiet >/dev/null 2>&1 # 钉钉入驻审批通过后回传内部快递资料;与上游全量同步分开,单轮最多25单、115秒。 */2 * * * * root /usr/bin/flock -n /run/caiwu-courier-profile-sync.lock /usr/bin/timeout 115 /usr/bin/docker exec -u www-data caiwu_app php artisan courier-onboarding:sync-dingtalk-profiles --limit=25 --quiet >/dev/null 2>&1 -# 财务台账:钉钉付款明细每 5 分钟增量重算;每天 02:30 做 7 天校准;10:30 生成店铺余额快照。 -30 2 * * * root /usr/bin/flock -w 1800 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=7 --merge --quiet >/dev/null 2>&1 -3-59/5 * * * * root /usr/bin/flock -w 120 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=2 --merge --dingtalk-only --quiet >/dev/null 2>&1 +# 财务台账:店铺提现/账单提现/开店每天同步;钉钉和供应商应付每小时同步;10:30生成店铺余额快照。 +30 2 * * * root /usr/bin/flock -w 1800 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=7 --merge --sources=store,withdrawal --quiet >/dev/null 2>&1 +5 * * * * root /usr/bin/flock -w 600 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=2 --merge --sources=dingtalk,payable --quiet >/dev/null 2>&1 30 10 * * * root /usr/bin/flock -w 1800 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-shop-data --balance-only --quiet >/dev/null 2>&1 # 财务流程明细每周完整校准一次,修正超过 48 小时重叠窗口才出现的上游改单或晚到付款。 20 2 * * 0 root /usr/bin/flock -w 3600 /run/caiwu-ding-process-details.lock /bin/sh /opt/caiwuxitong/run-finance-batch.sh business-mirror:sync-ding-process-details --full --quiet >/dev/null 2>&1 diff --git a/deploy/activate-release.sh b/deploy/activate-release.sh index a41f52ad..f4a0bfda 100644 --- a/deploy/activate-release.sh +++ b/deploy/activate-release.sh @@ -244,9 +244,9 @@ CRON_TZ=Asia/Shanghai */2 * * * * root /usr/bin/flock -n /run/caiwu-esign-polling.lock /usr/bin/docker exec -u www-data caiwu_app php artisan esign:poll-signatures --quiet >/dev/null 2>&1 # 钉钉入驻审批通过后回传内部快递资料;与上游全量同步分开,单轮最多25单、115秒。 */2 * * * * root /usr/bin/flock -n /run/caiwu-courier-profile-sync.lock /usr/bin/timeout 115 /usr/bin/docker exec -u www-data caiwu_app php artisan courier-onboarding:sync-dingtalk-profiles --limit=25 --quiet >/dev/null 2>&1 -# 财务台账:钉钉付款明细每 5 分钟增量重算;每天 02:30 做 7 天校准;10:30 生成店铺余额快照。 -30 2 * * * root /usr/bin/flock -w 1800 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=7 --merge --quiet >/dev/null 2>&1 -3-59/5 * * * * root /usr/bin/flock -w 120 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=2 --merge --dingtalk-only --quiet >/dev/null 2>&1 +# 财务台账:店铺提现/账单提现/开店每天同步;钉钉和供应商应付每小时同步;10:30生成店铺余额快照。 +30 2 * * * root /usr/bin/flock -w 1800 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=7 --merge --sources=store,withdrawal --quiet >/dev/null 2>&1 +5 * * * * root /usr/bin/flock -w 600 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=2 --merge --sources=dingtalk,payable --quiet >/dev/null 2>&1 30 10 * * * root /usr/bin/flock -w 1800 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-shop-data --balance-only --quiet >/dev/null 2>&1 # 财务流程明细每周完整校准一次,修正超过 48 小时重叠窗口才出现的上游改单或晚到付款。 20 2 * * 0 root /usr/bin/flock -w 3600 /run/caiwu-ding-process-details.lock /bin/sh /opt/caiwuxitong/run-finance-batch.sh business-mirror:sync-ding-process-details --full --quiet >/dev/null 2>&1 diff --git a/docs/财务台账开发说明.md b/docs/财务台账开发说明.md index b9e9fa1f..10136da5 100644 --- a/docs/财务台账开发说明.md +++ b/docs/财务台账开发说明.md @@ -68,7 +68,7 @@ docker compose -f docker-compose.local.yml exec -u www-data app \ - 账户总表“可用金额合计”按全部账户的每日可用金额与全部店铺台账余额相加;点击卡片可查看银行可用、店铺可用和合计明细,金额统一按万元显示,统计不受当前筛选影响 - 账户异常待办显示当前筛选范围的处理进度与逾期预警:“已跟进”包含进行中、未完成和已完成,开始超过 7 天仍未完成的任务计入逾期预警 - 写权限:账户映射、账户状态和账户类型修改仅系统管理员及配置的财务台账管理员角色;其他账号只读 -- 调度:钉钉付款明细使用 `finance-ledger:sync-rpt --days=2 --merge --dingtalk-only` 每 5 分钟增量重算;每天 02:30 使用 `--days=7 --merge` 做完整回看校准。店铺台账默认不自动回填。 +- 调度:钉钉审批和供应商应付每小时使用 `finance-ledger:sync-rpt --days=2 --merge --sources=dingtalk,payable` 增量重算;店铺提现、店铺账单提现和店铺开店每天 02:30 使用 `--days=7 --merge --sources=store,withdrawal` 回看。公司支付宝、公司微信和聚工单不再作为财务台账来源自动同步。 账户状态独立保存在 `fin_account_statuses`,账户类型人工维护值独立保存在 `fin_account_type_overrides`,重新导入账户基础数据均不会覆盖;页面修改使用 @@ -90,7 +90,6 @@ docker compose -f docker-compose.local.yml exec -u www-data app \ 生产同步读取以下轻量视图,不再直接查大表: -- `rpt.fin_ledger_workorder` - `rpt.fin_ledger_withdrawal` - `rpt.fin_ledger_dingtalk_workflow` - `rpt.fin_ledger_dingtalk_scheme` @@ -107,7 +106,7 @@ php artisan finance-ledger:sync-rpt --days=7 --merge 历史全量(会删除并重建 `fin_transactions`)使用 `--from/--to`,仅在明确需要重建快照时执行。 -支付宝“账务组合查询”账户流水使用独立导入命令。该文件以账户余额为权威来源, +支付宝独立账户“账务组合查询”账户流水使用独立导入命令。该文件以账户余额为权威来源, 会保留对方信息、余额和付款备注;指定 `--replace-sources` 时按有效账户和日期范围 替换重复来源,不会改动其他账户。 @@ -115,7 +114,7 @@ php artisan finance-ledger:sync-rpt --days=7 --merge php artisan finance-ledger:import-alipay-statement /path/账务组合查询.xls \ --account=<支付宝账号> --account-short=<账户简称> \ --from=2026-10-01 --to=2026-10-31 --opening-balance=<导入期初> \ - --replace-sources=workorder,alipay-company + --replace-sources=alipay-company ``` ## 数据安全 @@ -138,7 +137,8 @@ docker exec -u www-data caiwu_app php artisan finance-ledger:verify 4. 在宿主机 `/etc/cron.d/caiwu-jiansun-snapshots` 增加: ```cron -30 2 * * * root /usr/bin/flock -w 1800 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=7 --merge --quiet >/dev/null 2>&1 +30 2 * * * root /usr/bin/flock -w 1800 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=7 --merge --sources=store,withdrawal --quiet >/dev/null 2>&1 +5 * * * * root /usr/bin/flock -w 600 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=2 --merge --sources=dingtalk,payable --quiet >/dev/null 2>&1 ``` 店铺台账默认保持现有数据,不随每日动账同步自动回填。需要按上游重新拉取时, diff --git a/modules/EcommerceFinance/Services/FinanceLedgerRptSynchronizer.php b/modules/EcommerceFinance/Services/FinanceLedgerRptSynchronizer.php index 4516ffa6..cb60e366 100644 --- a/modules/EcommerceFinance/Services/FinanceLedgerRptSynchronizer.php +++ b/modules/EcommerceFinance/Services/FinanceLedgerRptSynchronizer.php @@ -2,6 +2,7 @@ namespace Modules\EcommerceFinance\Services; +use InvalidArgumentException; use RuntimeException; use Illuminate\Support\Facades\DB; @@ -18,7 +19,7 @@ class FinanceLedgerRptSynchronizer private const DING_SINCE = '2025-10-18'; - private const JST_WORK_ORDER_TYPE = '9E5A531F48D94AF4B8611FA6918ADA29'; + private const AUTO_SOURCES = ['dingtalk', 'payable', 'store', 'withdrawal']; private const FIXED_ASSET_PROCESS = 'PROC-6407E98B-040A-4C96-AA7D-696650FC1A25'; @@ -51,17 +52,40 @@ class FinanceLedgerRptSynchronizer public function sync(bool $recalculate = true, ?string $from = null, ?string $to = null, bool $merge = false): array { + return $this->syncSources(self::AUTO_SOURCES, $recalculate, $from, $to, $merge); + } + + /** + * @param array $sources + */ + public function syncSources( + array $sources, + bool $recalculate = true, + ?string $from = null, + ?string $to = null, + bool $merge = false, + ): array { $from = trim((string) ($from ?? config('finance_ledger.window_start', '2026-04-18'))); $to = trim((string) ($to ?? config('finance_ledger.window_end', ''))) ?: now()->toDateString(); $lookback = $merge ? $from : self::DING_SINCE; + $sources = array_values(array_unique(array_intersect(self::AUTO_SOURCES, $sources))); + if ($sources === []) { + throw new InvalidArgumentException('没有可同步的财务台账来源。'); + } - $transactions = array_merge( - $this->dingTalkRows($lookback, $to), - $this->workOrderRows(), - $this->payableRows($from), - $this->storeOpeningRows($from), - $this->withdrawalRows($from), - ); + $transactions = []; + if (in_array('dingtalk', $sources, true)) { + array_push($transactions, ...$this->dingTalkRows($lookback, $to)); + } + if (in_array('payable', $sources, true)) { + array_push($transactions, ...$this->payableRows($from)); + } + if (in_array('store', $sources, true)) { + array_push($transactions, ...$this->storeOpeningRows($from)); + } + if (in_array('withdrawal', $sources, true)) { + array_push($transactions, ...$this->withdrawalRows($from)); + } $transactions = array_values(array_filter( $transactions, @@ -69,6 +93,17 @@ class FinanceLedgerRptSynchronizer )); if ($transactions === []) { + if ($merge) { + return [ + 'rows' => 0, + 'income' => 0.0, + 'expense' => 0.0, + 'from' => $from, + 'to' => $to, + 'source_counts' => [], + 'recalculated' => null, + ]; + } throw new RuntimeException('rpt 没有返回窗口内的动账明细。'); } @@ -88,30 +123,13 @@ class FinanceLedgerRptSynchronizer ?string $from = null, ?string $to = null, ): array { - $from = trim((string) ($from ?? now()->subDay()->toDateString())); - $to = trim((string) ($to ?? now()->toDateString())) ?: now()->toDateString(); - $transactions = $this->dingTalkRows($from, $to); - $transactions = array_values(array_filter( - $transactions, - static fn (array $row): bool => $row['txn_date'] >= $from && $row['txn_date'] <= $to, - )); - - if ($transactions === []) { - return [ - 'rows' => 0, - 'income' => 0.0, - 'expense' => 0.0, - 'from' => $from, - 'to' => $to, - 'source_counts' => [], - 'recalculated' => null, - ]; - } - - $result = $this->writer->merge($transactions, $recalculate); - $result['source_counts'] = array_count_values(array_column($transactions, 'process_type')); - - return $result; + return $this->syncSources( + ['dingtalk'], + $recalculate, + $from ?? now()->subDay()->toDateString(), + $to, + true, + ); } /** @return array> */ @@ -385,47 +403,6 @@ class FinanceLedgerRptSynchronizer return $result; } - /** @return array> */ - private function workOrderRows(): array - { - $rows = $this->query()->select( - 'SELECT Id, woNo, createby, created, completeTime, businessFieldList FROM rpt.fin_ledger_workorder', - ); - - $result = []; - foreach ($rows as $row) { - $fields = json_decode((string) ($row['businessFieldList'] ?? ''), true); - if (! is_array($fields)) { - continue; - } - $map = []; - foreach ($fields as $field) { - $id = (string) ($field['fieldId'] ?? ''); - $map[substr($id, -6)] = $field['value'] ?? null; - } - $amount = $this->toNumber($map['091551'] ?? null); - $kind = trim((string) ($map['101125'] ?? '')); - $state = trim((string) ($map['101747'] ?? '')); - if ($amount <= 0 || $kind !== '入账' || ! in_array($state, ['已到账', '已收到'], true)) { - continue; - } - $result[] = $this->row([ - 'date' => $this->date($map['101432'] ?? null), - 'account_short' => trim((string) ($map['101730'] ?? '')), - 'source_value' => trim((string) ($map['101730'] ?? '')), - 'source' => 'workorder', - 'process_type' => '聚工单查账流程', - 'approval_no' => trim((string) ($row['woNo'] ?? '')), - 'detail' => $this->joinParts([trim((string) ($row['createby'] ?? '')), '查账工单', $state]), - 'income' => $amount, - 'expense' => 0, - 'pid' => 'JST-' . (string) ($row['Id'] ?? ''), - ]); - } - - return $result; - } - /** @return array> */ private function payableRows(string $from): array { diff --git a/modules/EcommerceFinance/Services/FinanceLedgerTransactionWriter.php b/modules/EcommerceFinance/Services/FinanceLedgerTransactionWriter.php index fa0a7603..4f746e5d 100644 --- a/modules/EcommerceFinance/Services/FinanceLedgerTransactionWriter.php +++ b/modules/EcommerceFinance/Services/FinanceLedgerTransactionWriter.php @@ -20,6 +20,15 @@ class FinanceLedgerTransactionWriter public const INTERNAL_TRANSFER_SOURCE = 'internal_transfer'; + private const PRESERVED_SOURCES = [ + self::MANUAL_SOURCE, + self::SHOP_WITHDRAWAL_SOURCE, + self::INTERNAL_TRANSFER_SOURCE, + 'alipay-company', + 'wechat-statement', + 'workorder', + ]; + public function __construct( private readonly FinanceLedgerTransactionLocalOverrideGuard $localOverrides, ) { @@ -109,11 +118,7 @@ class FinanceLedgerTransactionWriter $connection->transaction(function () use ($connection, $rows, $now, $replaceExisting, &$writtenRows): void { if ($replaceExisting) { $delete = $connection->table('fin_transactions') - ->whereNotIn('source', [ - self::MANUAL_SOURCE, - self::SHOP_WITHDRAWAL_SOURCE, - self::INTERNAL_TRANSFER_SOURCE, - ]) + ->whereNotIn('source', self::PRESERVED_SOURCES) ->whereNotIn('account_short', FinanceLedgerIndependentAccounts::names()); $this->localOverrides->excludeFromReplacement($delete); $delete->delete(); diff --git a/modules/EcommerceFinance/View/FinanceLedgerPageData.php b/modules/EcommerceFinance/View/FinanceLedgerPageData.php index 9c6076e4..e81dfb92 100644 --- a/modules/EcommerceFinance/View/FinanceLedgerPageData.php +++ b/modules/EcommerceFinance/View/FinanceLedgerPageData.php @@ -71,6 +71,12 @@ class FinanceLedgerPageData 'accounts' => true, ]; + private const RETIRED_TRANSACTION_SOURCES = [ + 'alipay-company', + 'wechat-statement', + 'workorder', + ]; + public const TRANSACTION_VIEWS = [ 'all' => ['title' => '全部明细', 'account_short' => null, 'independent' => false], 'zhu-junxiang-alipay' => ['title' => '朱俊祥支付宝', 'account_short' => '朱俊祥支付宝', 'independent' => true], @@ -1666,6 +1672,7 @@ class FinanceLedgerPageData $query->whereRaw($effectiveAccount . ' = ?', [$forcedAccountShort]); } else { $this->excludeIndependentEffectiveAccount($query); + $this->excludeRetiredTransactionSources($query); $account = trim((string) request('txns_account', '')); if ($account !== '') { $query->whereRaw($effectiveAccount . ' LIKE ?', ['%' . $account . '%']); @@ -1673,7 +1680,7 @@ class FinanceLedgerPageData } $this->applyDateRange($query, 't.txn_date', $period); $source = trim((string) request('txns_source', '')); - if ($source !== '') { + if ($source !== '' && ! in_array($source, self::RETIRED_TRANSACTION_SOURCES, true)) { $query->where('t.source', $source); } @@ -1845,6 +1852,7 @@ class FinanceLedgerPageData { $query = $this->transactionScopeQuery(); $this->excludeIndependentEffectiveAccount($query); + $this->excludeRetiredTransactionSources($query); return (clone $query)->count(); } @@ -1912,6 +1920,11 @@ class FinanceLedgerPageData ); } + private function excludeRetiredTransactionSources(Builder $query): void + { + $query->whereNotIn('t.source', self::RETIRED_TRANSACTION_SOURCES); + } + private function transactionQuery(): Builder { $effectiveAccount = $this->transactionHeaderColumns()['effective_account_short']['sort']; @@ -1954,6 +1967,7 @@ class FinanceLedgerPageData $effectiveAccount = $this->transactionHeaderColumns()['effective_account_short']['sort']; $query = $this->transactionScopeQuery(); $this->excludeIndependentEffectiveAccount($query); + $this->excludeRetiredTransactionSources($query); return $query ->selectRaw($effectiveAccount . ' AS filter_value') @@ -1971,6 +1985,7 @@ class FinanceLedgerPageData { $query = $this->transactionScopeQuery(); $this->excludeIndependentEffectiveAccount($query); + $this->excludeRetiredTransactionSources($query); return $query ->select('t.txn_date') @@ -2083,6 +2098,7 @@ class FinanceLedgerPageData $effectiveAccount . ' NOT IN (' . implode(', ', array_fill(0, count($names), '?')) . ')', $names, ); + $this->excludeRetiredTransactionSources($query); $this->applyDateRange($query, 't.txn_date', $period); return $query; @@ -2093,6 +2109,7 @@ class FinanceLedgerPageData $t = $this->queryAlias('t'); $query = $this->transactionScopeQuery(); $this->excludeIndependentEffectiveAccount($query); + $this->excludeRetiredTransactionSources($query); $query ->selectRaw("{$t}.source, {$t}.process_type, COUNT(*) AS rows_count, SUM({$t}.income) AS income, SUM({$t}.expense) AS expense, MIN({$t}.txn_date) AS first_date, MAX({$t}.txn_date) AS last_date") ->groupBy('t.source', 't.process_type') @@ -2117,6 +2134,7 @@ class FinanceLedgerPageData { $transactionQuery = $this->transactionScopeQuery(); $this->excludeIndependentEffectiveAccount($transactionQuery); + $this->excludeRetiredTransactionSources($transactionQuery); $this->applyDateRange($transactionQuery, 't.txn_date', $transactionPeriod); $flowQuery = FinanceLedgerIndependentAccounts::exclude($this->db()->table('fin_daily_flows')); $this->applyDateRange($flowQuery, 'flow_date', $flowPeriod); diff --git a/modules/EcommerceFinance/View/ShopFlowQuery.php b/modules/EcommerceFinance/View/ShopFlowQuery.php index a10d1912..d32e9d55 100644 --- a/modules/EcommerceFinance/View/ShopFlowQuery.php +++ b/modules/EcommerceFinance/View/ShopFlowQuery.php @@ -30,7 +30,6 @@ class ShopFlowQuery 'shop_bill' => '店铺账单提现', 'payable' => '供应商应付', 'store' => '店铺开店', - 'workorder' => '聚工单', 'manual' => '手工录入', ]; @@ -475,6 +474,7 @@ class ShopFlowQuery $join->on('m.source', '=', 't.source') ->on('m.source_value', '=', 't.source_value'); }) + ->where('t.source', '<>', 'workorder') ->where(function ($query) use ($accountShort): void { $query->where(function ($mapped) use ($accountShort): void { $mapped->whereNotNull('m.account_short') diff --git a/resources/views/business/pages/financial-ledger/ledger.blade.php b/resources/views/business/pages/financial-ledger/ledger.blade.php index 0bda27e1..ba4d659e 100644 --- a/resources/views/business/pages/financial-ledger/ledger.blade.php +++ b/resources/views/business/pages/financial-ledger/ledger.blade.php @@ -130,7 +130,15 @@ $transactionViewResetQuery['ledger_embed'] = '1'; } $transactionViewResetUrl = $ledgerPageUrl . '?' . http_build_query($transactionViewResetQuery); - $sourceLabels = ['dingtalk' => '钉钉审批', 'alipay-company' => '公司支付宝', 'wechat-statement' => '公司微信', 'wangshang-bank' => '邮箱', 'shop' => '店铺提现', 'shop_bill' => '店铺账单提现', 'payable' => '供应商应付', 'store' => '店铺开店', 'workorder' => '聚工单', 'manual' => '手工录入', 'internal_transfer' => '内部转账']; + $sourceLabels = ['dingtalk' => '钉钉审批', 'wangshang-bank' => '邮箱', 'shop' => '店铺提现', 'shop_bill' => '店铺账单提现', 'payable' => '供应商应付', 'store' => '店铺开店', 'manual' => '手工录入', 'internal_transfer' => '内部转账']; + $retiredTransactionSources = ['alipay-company', 'wechat-statement', 'workorder']; + $transactionSourceLabel = static function (string $source, ?string $accountShort = null) use ($sourceLabels, $retiredTransactionSources): string { + if (in_array($source, $retiredTransactionSources, true)) { + return trim((string) $accountShort) !== '' ? trim((string) $accountShort) : '历史导入'; + } + + return $sourceLabels[$source] ?? $source; + }; $shopBalanceDate = $ledgerShopBalanceDate ?? null; $shopBalanceDates = $ledgerShopBalanceDates ?? []; $manualTransactionAccountOptions = $ledgerManualTransactionAccountOptions ?? []; @@ -2388,7 +2396,7 @@ {{ $row->balance === null ? '' : $money($row->balance) }} {{ $row->payment_remark }} {{ $row->detail }} - {{ $sourceLabels[$row->source] ?? $row->source }} + {{ $transactionSourceLabel((string) $row->source, $row->effective_account_short) }} {{ $row->approval_no }} @if ($canEditTransactions) @@ -4766,7 +4774,11 @@ if (manualEditExpense) manualEditExpense.value = Number(row.dataset.expense) > 0 ? row.dataset.expense : ''; if (manualEditDetail) manualEditDetail.required = !independentlyEditable; if (manualEditSource) { - manualEditSource.value = row.dataset.sourceValue || 'manual'; + const sourceValue = row.dataset.sourceValue || 'manual'; + if (!Array.from(manualEditSource.options).some((option) => option.value === sourceValue)) { + manualEditSource.add(new Option('当前来源', sourceValue)); + } + manualEditSource.value = sourceValue; manualEditSource.disabled = locallyEditable; } if (manualEditTitle) { diff --git a/tests/Feature/Business/FinanceLedgerHeaderControlsTest.php b/tests/Feature/Business/FinanceLedgerHeaderControlsTest.php index 3930201e..5e3084ff 100644 --- a/tests/Feature/Business/FinanceLedgerHeaderControlsTest.php +++ b/tests/Feature/Business/FinanceLedgerHeaderControlsTest.php @@ -336,6 +336,51 @@ class FinanceLedgerHeaderControlsTest extends FeatureTestCase $this->assertStringNotContainsString('999999.00', $html); } + public function test_retired_transaction_sources_are_hidden_from_shared_ledger_views(): void + { + $connection = DB::connection('business_mirror'); + foreach ([ + ['source' => 'alipay-company', 'detail' => '退役支付宝来源', 'row_key' => 'retired-alipay-source'], + ['source' => 'wechat-statement', 'detail' => '退役微信来源', 'row_key' => 'retired-wechat-source'], + ['source' => 'workorder', 'detail' => '退役聚工单来源', 'row_key' => 'retired-workorder-source'], + ] as $index => $row) { + $connection->table('fin_transactions')->insert([ + 'txn_date' => '2026-10-18', + 'account_short' => '账户甲', + 'detail' => $row['detail'], + 'income' => 10 + $index, + 'expense' => 0, + 'approval_no' => strtoupper($row['source']) . '-RETIRED', + 'source' => $row['source'], + 'source_value' => 'RETIRED-' . $index, + 'process_type' => $row['detail'], + 'row_key' => $row['row_key'], + ]); + $connection->table('fin_account_map')->insert([ + 'source' => $row['source'], + 'source_value' => 'RETIRED-' . $index, + 'account_short' => '账户甲', + 'txn_count' => 1, + 'income_sum' => 10 + $index, + 'expense_sum' => 0, + ]); + } + + $html = $this->loginAs()->get($this->pageUrl([ + 'txns_from' => '2026-10-01', + 'txns_to' => '2026-10-31', + ]))->assertOk()->getContent(); + + foreach (['退役支付宝来源', '退役微信来源', '退役聚工单来源'] as $detail) { + $this->assertStringNotContainsString($detail, $html); + } + foreach (['alipay-company', 'wechat-statement', 'workorder'] as $source) { + $this->assertStringNotContainsString('', $html); + $this->assertStringContainsString('', $html); + } + public function test_shop_ledger_header_labels_and_column_order(): void { $html = $this->loginAs()->get($this->pageUrl())->assertOk()->getContent(); diff --git a/tests/Feature/Business/FinanceLedgerManualTransactionTest.php b/tests/Feature/Business/FinanceLedgerManualTransactionTest.php index 18f66b7a..89d84034 100644 --- a/tests/Feature/Business/FinanceLedgerManualTransactionTest.php +++ b/tests/Feature/Business/FinanceLedgerManualTransactionTest.php @@ -693,6 +693,51 @@ class FinanceLedgerManualTransactionTest extends FeatureTestCase ], 'business_mirror'); } + public function test_retired_transaction_sources_survive_snapshot_replacement(): void + { + $connection = DB::connection('business_mirror'); + foreach (['alipay-company', 'wechat-statement', 'workorder'] as $index => $source) { + $connection->table('fin_transactions')->insert([ + 'txn_date' => '2026-10-18', + 'account_short' => '手工账户甲', + 'detail' => '历史来源保留-' . $source, + 'income' => 10 + $index, + 'expense' => 0, + 'approval_no' => 'RETIRED-' . $index, + 'source' => $source, + 'source_value' => 'RETIRED-' . $index, + 'process_type' => '历史来源', + 'row_key' => 'retired-source-' . $index, + ]); + } + + app(FinanceLedgerTransactionWriter::class)->replace([ + [ + 'txn_date' => '2026-10-18', + 'account_short' => '手工账户甲', + 'detail' => '当前自动来源', + 'income' => 1, + 'expense' => 0, + 'approval_no' => 'CURRENT-1', + 'source' => 'dingtalk', + 'source_value' => 'CURRENT-VALUE-1', + 'process_type' => '报销', + ], + ], false); + + foreach (['alipay-company', 'wechat-statement', 'workorder'] as $source) { + $this->assertDatabaseHas('fin_transactions', [ + 'account_short' => '手工账户甲', + 'source' => $source, + 'detail' => '历史来源保留-' . $source, + ], 'business_mirror'); + } + $this->assertDatabaseHas('fin_transactions', [ + 'source' => 'dingtalk', + 'detail' => '当前自动来源', + ], 'business_mirror'); + } + public function test_yutu_can_edit_every_imported_independent_statement_field_and_custom_value(): void { $this->addZhuJunxiangAccounts(); @@ -1042,7 +1087,7 @@ class FinanceLedgerManualTransactionTest extends FeatureTestCase 'income' => 5, 'expense' => 0, 'approval_no' => 'NORMAL-LOCAL', - 'source' => 'workorder', + 'source' => 'dingtalk', 'source_value' => 'NORMAL-LOCAL-VALUE', 'row_key' => 'normal-imported-local-row', ]); diff --git a/tests/Unit/Deployment/BusinessMirrorScheduleTest.php b/tests/Unit/Deployment/BusinessMirrorScheduleTest.php index 950da414..97e86068 100644 --- a/tests/Unit/Deployment/BusinessMirrorScheduleTest.php +++ b/tests/Unit/Deployment/BusinessMirrorScheduleTest.php @@ -145,17 +145,17 @@ class BusinessMirrorScheduleTest extends TestCase $this->assertStringContainsString('/run/caiwu-courier-profile-sync.lock /usr/bin/timeout 115', $contents, $script); $this->assertSame(1, substr_count($contents, 'finance-ledger:sync-shop-data --balance-only --quiet'), $script); $this->assertStringContainsString( - '# 财务台账:钉钉付款明细每 5 分钟增量重算;每天 02:30 做 7 天校准;10:30 生成店铺余额快照。', + '# 财务台账:店铺提现/账单提现/开店每天同步;钉钉和供应商应付每小时同步;10:30生成店铺余额快照。', $contents, $script, ); $this->assertStringContainsString( - '30 2 * * * root /usr/bin/flock -w 1800 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=7 --merge --quiet >/dev/null 2>&1', + '30 2 * * * root /usr/bin/flock -w 1800 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=7 --merge --sources=store,withdrawal --quiet >/dev/null 2>&1', $contents, $script, ); $this->assertStringContainsString( - '3-59/5 * * * * root /usr/bin/flock -w 120 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=2 --merge --dingtalk-only --quiet >/dev/null 2>&1', + '5 * * * * root /usr/bin/flock -w 600 /run/caiwu-upstream-sync.lock /usr/bin/docker exec -u www-data caiwu_app php artisan finance-ledger:sync-rpt --days=2 --merge --sources=dingtalk,payable --quiet >/dev/null 2>&1', $contents, $script, ); @@ -228,14 +228,21 @@ class BusinessMirrorScheduleTest extends TestCase ); } - public function test_laravel_scheduler_runs_dingtalk_ledger_sync_every_five_minutes(): void + public function test_laravel_scheduler_runs_finance_ledger_sources_at_configured_frequencies(): void { $contents = file_get_contents(dirname(__DIR__, 3) . '/app/Console/Kernel.php'); $this->assertIsString($contents); $this->assertStringContainsString( - "\$schedule->command('finance-ledger:sync-rpt --days=2 --merge --dingtalk-only --quiet')\n" - . " ->cron('3-59/5 * * * *')\n" + "\$schedule->command('finance-ledger:sync-rpt --days=7 --merge --sources=store,withdrawal')\n" + . " ->dailyAt('02:30')\n" + . " ->timezone('Asia/Shanghai')\n" + . ' ->withoutOverlapping(120)', + $contents, + ); + $this->assertStringContainsString( + "\$schedule->command('finance-ledger:sync-rpt --days=2 --merge --sources=dingtalk,payable --quiet')\n" + . " ->hourlyAt(5)\n" . " ->timezone('Asia/Shanghai')\n" . ' ->withoutOverlapping(10)', $contents,