CaiWuXiTong/app/Console/Commands/AssignFinanceLedgerCompanyPlans.php
shanshi e76e87e4db feat(财务台账): 发布银行账单与账户资金管理 (#1385)
发布银行账单、账户资金管理、公司规划分布与余额联动。
2026-11-25 09:14:24 +08:00

170 lines
5.8 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 App\Console\Commands;
use App\Utilities\FinanceLedgerAccountShouldKeep;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Throwable;
class AssignFinanceLedgerCompanyPlans extends Command
{
private const PRESERVED_ACCOUNT_TYPES = ['支付宝', '网商银行'];
protected $signature = 'finance-ledger:assign-company-plans
{path : 公司保留名单 XLSX/XLS 文件}
{--dry-run : 只核对公司和账户数量,不写入数据}';
protected $description = '按公司保留名单设置账户公司规划与是否保留';
public function handle(): int
{
try {
$companies = $this->companyNames((string) $this->argument('path'));
if ($companies === []) {
throw new \RuntimeException('公司保留名单中没有可识别的公司名称。');
}
$connection = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror'));
$accounts = $connection->table('fin_accounts')
->orderBy('id')
->get(['id', 'company_name', 'account_type']);
$retainedIds = [];
$temporaryIds = [];
$preservedIds = [];
$matchedKeys = [];
foreach ($accounts as $account) {
$key = $this->normalizeCompanyName((string) ($account->company_name ?? ''));
$accountId = (int) $account->id;
if ($key !== '' && isset($companies[$key])) {
$matchedKeys[$key] = true;
}
if (in_array(trim((string) ($account->account_type ?? '')), self::PRESERVED_ACCOUNT_TYPES, true)) {
$preservedIds[] = $accountId;
} elseif ($key !== '' && isset($companies[$key])) {
$retainedIds[] = $accountId;
} else {
$temporaryIds[] = $accountId;
}
}
$unmatchedCompanies = array_values(array_diff_key($companies, $matchedKeys));
$this->table(
['名单公司', '匹配名单公司', '账户总数', '更新为保留', '更新为暂时保留', '保持原状'],
[[
count($companies),
count($matchedKeys),
$accounts->count(),
count($retainedIds),
count($temporaryIds),
count($preservedIds),
]],
);
if ($unmatchedCompanies !== []) {
$this->warn('以下名单公司没有匹配到账户:' . implode('、', $unmatchedCompanies));
}
if ((bool) $this->option('dry-run')) {
$this->info('核对完成,未写入数据。');
return self::SUCCESS;
}
$now = now();
$connection->transaction(function () use (
$connection,
$retainedIds,
$temporaryIds,
$now,
): void {
foreach (array_chunk($retainedIds, 500) as $ids) {
$connection->table('fin_accounts')
->whereIn('id', $ids)
->update([
'company_plan' => '保留',
'account_should_keep' => '是',
'updated_at' => $now,
]);
}
foreach (array_chunk($temporaryIds, 500) as $ids) {
$connection->table('fin_accounts')
->whereIn('id', $ids)
->update([
'company_plan' => '不保留',
'account_should_keep' => FinanceLedgerAccountShouldKeep::TEMPORARY,
'updated_at' => $now,
]);
}
});
$this->info(sprintf(
'已更新 %d 个账户:保留 %d 个,暂时保留 %d 个;另有 %d 个支付宝或网商银行账户保持原状。',
count($retainedIds) + count($temporaryIds),
count($retainedIds),
count($temporaryIds),
count($preservedIds),
));
return self::SUCCESS;
} catch (Throwable $exception) {
$this->error($exception->getMessage());
return self::FAILURE;
}
}
/**
* @return array<string,string>
*/
private function companyNames(string $path): array
{
if (! is_file($path)) {
throw new \RuntimeException('公司保留名单文件不存在:' . $path);
}
$worksheet = IOFactory::load($path)->getActiveSheet();
$rows = $worksheet->toArray(null, true, true, false);
if ($rows === []) {
return [];
}
$header = array_map(
fn (mixed $value): string => $this->normalizeCompanyName((string) $value),
$rows[0],
);
$column = array_search('公司名称', $header, true);
if ($column === false) {
$column = 0;
} else {
array_shift($rows);
}
$companies = [];
foreach ($rows as $row) {
$name = trim((string) ($row[$column] ?? ''));
$key = $this->normalizeCompanyName($name);
if ($key !== '') {
$companies[$key] = $name;
}
}
return $companies;
}
private function normalizeCompanyName(string $name): string
{
$name = str_replace(
['(', ')', '【', '】', ' '],
['(', ')', '[', ']', ' '],
trim($name),
);
$name = (string) preg_replace('/\s+/u', '', $name);
return mb_strtolower($name, 'UTF-8');
}
}