CaiWuXiTong/app/Console/Commands/SyncFinanceLedgerShopData.php
92451 88b3f1c830 fix: 支持财务台账店铺台账重建
支持从最新余额快照清空并重建财务台账店铺表。

PR #623
Co-authored-by: 92451 <924518135@qq.com>
Co-committed-by: 92451 <924518135@qq.com>
2026-10-26 18:01:31 +08:00

63 lines
2.7 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 : 先用最新余额快照清空并重建店铺台账}
{--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 {
$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']['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['recalculated']['flows'] ?? '-'],
['PHP 峰值内存', number_format(memory_get_peak_usage(true) / 1048576, 1) . ' MiB'],
]);
$this->info('财务台账店铺余额与提现同步完成。');
return self::SUCCESS;
}
}