CaiWuXiTong/app/Console/Commands/ImportWechatStatement.php

52 lines
2.1 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Modules\EcommerceFinance\Services\WechatStatementImporter;
use Throwable;
class ImportWechatStatement extends Command
{
protected $signature = 'finance-ledger:import-wechat-statement
{path : 朱俊祥微信月度收支表 XLS/XLSX 文件}
{--account-short= : 独立账单账户简称,默认朱俊祥微信}
{--opening-balance= : 指定期初余额;默认读取文件期初行}
{--dry-run : 只校验,不写入财务台账}';
protected $description = '导入朱俊祥微信月度收支表,保留期初、逐笔收支和余额';
public function handle(WechatStatementImporter $importer): int
{
$opening = trim((string) $this->option('opening-balance'));
try {
$result = $importer->import(
(string) $this->argument('path'),
$this->option('account-short') ?: null,
$opening === '' ? null : (float) $opening,
(bool) $this->option('dry-run'),
);
} catch (Throwable $exception) {
$this->error('微信收支表未导入,保留原有数据。' . $exception->getMessage());
return self::FAILURE;
}
$this->table(['项目', '值'], [
['校验模式', $result['dry_run'] ? '仅校验' : '正式导入'],
['账户简称', $result['account_short']],
['日期范围', $result['from'] . ' 至 ' . $result['to']],
['期初余额', number_format((float) $result['opening_balance'], 2)],
['有效笔数', $result['rows']],
['收入合计', number_format((float) $result['income'], 2)],
['支出合计', number_format((float) $result['expense'], 2)],
['期末余额', number_format((float) $result['closing_balance'], 2)],
['替换原记录', $result['replaced']],
]);
$this->info($result['dry_run'] ? '微信收支表校验完成。' : '微信收支表已导入独立账单。');
return self::SUCCESS;
}
}