CaiWuXiTong/modules/EcommerceFinance/Services/BankBillStatementDirectoryImporter.php
shanshi 39769abd26 feat(财务台账): 接入招商银行邮件账单 (#1392)
接入招商银行邮件账单下载、整目录导入和余额同步。
2026-11-25 10:11:08 +08:00

133 lines
4.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Modules\EcommerceFinance\Services;
use FilesystemIterator;
use InvalidArgumentException;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
use Throwable;
final class BankBillStatementDirectoryImporter
{
public function __construct(
private readonly BankBillStatementImporter $statements,
private readonly FinanceLedgerBankBalanceSynchronizer $balanceSynchronizer,
) {
}
/**
* @return array{
* bank:string,files:int,imported_files:int,failed_files:int,rows:int,
* inserted:int,updated:int,skipped:int,accounts:int,synced_accounts:int,
* first_date:?string,last_date:?string,dry_run:bool,failures:list<string>
* }
*/
public function import(
string $directory,
string $bankName,
bool $dryRun = false,
): array {
if (! is_dir($directory) || ! is_readable($directory)) {
throw new InvalidArgumentException('银行账单目录不存在或不可读取。');
}
$files = $this->files($directory);
if ($files === []) {
throw new InvalidArgumentException('银行账单目录中没有 XLSX、XLS 或 CSV 文件。');
}
$result = [
'bank' => trim($bankName),
'files' => count($files),
'imported_files' => 0,
'failed_files' => 0,
'rows' => 0,
'inserted' => 0,
'updated' => 0,
'skipped' => 0,
'accounts' => 0,
'synced_accounts' => 0,
'first_date' => null,
'last_date' => null,
'dry_run' => $dryRun,
'failures' => [],
];
$accountNumbers = [];
foreach ($files as $file) {
try {
$fileResult = $this->statements->import(
$file,
$bankName,
$dryRun,
false,
false,
);
$result['imported_files']++;
$result['rows'] += $fileResult['rows'];
$result['inserted'] += $fileResult['inserted'];
$result['updated'] += $fileResult['updated'];
$result['skipped'] += $fileResult['skipped'];
foreach ($fileResult['account_numbers'] as $accountNumber) {
$accountNumbers[(string) $accountNumber] = true;
}
if ($fileResult['first_date'] !== null
&& ($result['first_date'] === null
|| $fileResult['first_date'] < $result['first_date'])) {
$result['first_date'] = $fileResult['first_date'];
}
if ($fileResult['last_date'] !== null
&& ($result['last_date'] === null
|| $fileResult['last_date'] > $result['last_date'])) {
$result['last_date'] = $fileResult['last_date'];
}
} catch (Throwable $exception) {
$result['failed_files']++;
$result['failures'][] = basename($file) . ':' . $exception->getMessage();
}
}
$result['accounts'] = count($accountNumbers);
if (! $dryRun && $accountNumbers !== []) {
$result['synced_accounts'] = $this->balanceSynchronizer->syncAccountNumbers(
array_keys($accountNumbers),
$result['last_date'],
);
}
return $result;
}
/**
* @return list<string>
*/
private function files(string $directory): array
{
$files = [];
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$directory,
FilesystemIterator::SKIP_DOTS,
),
);
foreach ($iterator as $file) {
if (! $file instanceof SplFileInfo || ! $file->isFile()) {
continue;
}
if (! in_array(
strtolower($file->getExtension()),
['xlsx', 'xls', 'csv'],
true,
)) {
continue;
}
$files[] = $file->getPathname();
}
sort($files, SORT_NATURAL | SORT_FLAG_CASE);
return $files;
}
}