CaiWuXiTong/app/Console/Commands/ImportCompanyAlipayStatement.php

75 lines
3.0 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Modules\EcommerceFinance\Services\CompanyAlipayStatementImporter;
use Throwable;
class ImportCompanyAlipayStatement extends Command
{
protected $signature = 'finance-ledger:import-alipay-statement
{path : 支付宝账务组合查询 XLS/XLSX 文件}
{--account= : 支付宝登录账号}
{--account-short= : 账户总表简称}
{--from= : 起始日期 YYYY-MM-DD}
{--to= : 结束日期 YYYY-MM-DD}
{--opening-balance= : 起始日期零时余额}
{--replace-sources= : 需要替换的重复来源,多个用逗号分隔}
{--dry-run : 只校验,不写入财务台账}';
protected $description = '导入支付宝账务组合查询流水,保留对方信息、余额和付款备注';
public function handle(CompanyAlipayStatementImporter $importer): int
{
$account = trim((string) $this->option('account'));
$from = trim((string) $this->option('from'));
$to = trim((string) $this->option('to'));
if ($account === '' || $from === '' || $to === '') {
$this->error('必须指定 --account、--from 和 --to。');
return self::FAILURE;
}
$openingOption = trim((string) $this->option('opening-balance'));
$openingBalance = $openingOption === '' ? null : (float) $openingOption;
$replaceSources = array_values(array_filter(array_map(
'trim',
explode(',', (string) $this->option('replace-sources')),
)));
try {
$result = $importer->import(
(string) $this->argument('path'),
$account,
$from,
$to,
$this->option('account-short') ?: null,
$openingBalance,
$replaceSources,
(bool) $this->option('dry-run'),
);
} catch (Throwable $exception) {
$this->error('支付宝流水未导入,保留原有数据。' . $exception->getMessage());
return self::FAILURE;
}
$this->table(['项目', '值'], [
['校验模式', $result['dry_run'] ? '仅校验' : '正式导入'],
['日期范围', $result['from'] . ' 至 ' . $result['to']],
['账户简称', $result['account_short']],
['期初余额', 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']],
['每日流水行数', $result['recalculated']['flows'] ?? '-'],
]);
$this->info($result['dry_run'] ? '支付宝流水校验完成。' : '支付宝流水已导入财务台账。');
return self::SUCCESS;
}
}