CaiWuXiTong/app/Console/Commands/SyncFinanceLedgerShopData.php
92451 1cb04adc7b feat: 店铺台账接入每日余额、预留标准与预警
完成店铺台账每日余额来源、日期筛选、预留标准和连续3天预警。
Co-authored-by: 92451 <924518135@qq.com>
Co-committed-by: 92451 <924518135@qq.com>
2026-10-27 18:13:00 +08:00

88 lines
3.9 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Modules\EcommerceFinance\Services\FinanceLedgerShopDataSynchronizer;
use Throwable;
class SyncFinanceLedgerShopData extends Command
{
protected $signature = 'finance-ledger:sync-shop-data
{--from= : 提现业务日期起,默认从台账期初日}
{--to= : 提现业务日期止,最晚只同步到前一晚}
{--days= : 只同步最近 N 天}
{--rebuild : 先用最新余额快照清空并重建店铺台账}
{--balance-only : 只生成店铺余额快照并更新台账,不处理平台提现}
{--no-recalculate : 只同步数据,不重算流水}';
protected $description = '生成上一晚店铺余额快照(店铺收款账户账单优先),并把平台提现对应到财务账户收入';
public function handle(FinanceLedgerShopDataSynchronizer $synchronizer): int
{
$days = (int) ($this->option('days') ?: 0);
$from = $this->option('from') ?: null;
if ($days > 0) {
$from = now()->subDays(max(1, $days) - 1)->toDateString();
}
try {
if ($this->option('balance-only')) {
$balance = $synchronizer->syncShopBalances(
$this->option('to') ?: null,
(bool) $this->option('rebuild'),
);
$result = [
'balance' => $balance,
'withdrawals' => [
'processed' => 0,
'written' => 0,
'upstream_duplicates' => 0,
'unmatched' => 0,
'account_missing' => 0,
'income' => 0.0,
'expense' => 0.0,
],
'recalculated' => null,
];
} else {
$result = $synchronizer->sync(
$from,
$this->option('to') ?: null,
! $this->option('no-recalculate'),
(bool) $this->option('rebuild'),
);
}
} catch (Throwable $exception) {
$this->error($exception->getMessage());
return self::FAILURE;
}
$this->table(['项目', '值'], [
['店铺余额日期', $result['balance']['date'] ?: '-'],
['余额源记录', $result['balance']['source']],
['店铺账单余额来源', $result['balance']['receiving_bill']],
['聚水潭补录来源', $result['balance']['jushuitan']],
['补充确认店铺', $result['balance']['added']],
['重建店铺台账', $result['balance']['rebuilt']],
['已更新店铺余额', $result['balance']['matched']],
['店铺余额合计', number_format((float) $result['balance']['amount'], 2)],
['提现处理笔数', $result['withdrawals']['processed']],
['提现新增或更新', $result['withdrawals']['written']],
['上游已存在提现', $result['withdrawals']['upstream_duplicates']],
['店铺未匹配', $result['withdrawals']['unmatched']],
['账户缺失', $result['withdrawals']['account_missing']],
['提现收入合计', number_format((float) $result['withdrawals']['income'], 2)],
['提现冲回支出', number_format((float) $result['withdrawals']['expense'], 2)],
['余额预警待办新增', $result['balance']['alerts']['created']],
['余额预警待办更新', $result['balance']['alerts']['updated']],
['每日流水行数', $result['recalculated']['flows'] ?? '-'],
['PHP 峰值内存', number_format(memory_get_peak_usage(true) / 1048576, 1) . ' MiB'],
]);
$this->info('财务台账店铺余额与提现同步完成。');
return self::SUCCESS;
}
}