CaiWuXiTong/modules/EcommerceFinance/Services/DingPaymentAccountMatcher.php
shanshi ea6c7c380b fix: 完善华夏银行账户别名识别 (#1210)
补充“华夏”作为华夏银行简称,确保两字公司名的账户能通过银行和公司关键字匹配。

测试:运行中付款明细与账户匹配测试通过。
2026-11-16 19:10:10 +08:00

296 lines
9.5 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 Illuminate\Database\DatabaseManager;
/**
* Resolve DingTalk payment rows to a finance ledger account short name.
*
* The approval template is not consistent across processes: newer rows include
* 账户简称, while older rows only include 科目名称 and 科目编码. Exact fields win;
* keyword matching is used only when it produces one account or a clearly
* dominant ordinary account.
*/
class DingPaymentAccountMatcher
{
private const BANK_ALIASES = [
'中国农业银行' => '农业银行',
'中国工商银行' => '工商银行',
'中国建设银行' => '建设银行',
'江苏江南农村商业银行' => '江南农村商业银行',
'江南农村商业银行' => '江南农村商业银行',
'江南农商行' => '江南农村商业银行',
'农村商业银行' => '江南农村商业银行',
'农商银行' => '江南农村商业银行',
'农业银行' => '农业银行',
'工商银行' => '工商银行',
'中国银行' => '中国银行',
'招商银行' => '招商银行',
'建设银行' => '建设银行',
'交通银行' => '交通银行',
'华夏银行' => '华夏银行',
'华夏' => '华夏银行',
'兴业银行' => '兴业银行',
'中信银行' => '中信银行',
'光大银行' => '光大银行',
'浦发银行' => '浦发银行',
'平安银行' => '平安银行',
'网商银行' => '网商银行',
'邮政储蓄银行' => '邮政储蓄银行',
'农行' => '农业银行',
'工行' => '工商银行',
'中行' => '中国银行',
'招行' => '招商银行',
'建行' => '建设银行',
'交行' => '交通银行',
'华夏行' => '华夏银行',
'农商' => '江南农村商业银行',
'支付宝' => '支付宝',
'微信' => '微信',
];
private const SOURCE_PREFIXES = [
'银行存款_公司账户_',
'银行存款_个人账户_',
'银行存款_个体工商户_',
'银行存款_网商银行_',
'银行存款_支付宝_',
'银行存款_',
'库存现金_',
];
/** @var array<string,array<string,mixed>>|null */
private ?array $accounts = null;
/** @var array<string,string>|null */
private ?array $sourceMappings = null;
/** @var array<string,array<string,true>> */
private array $codeHints = [];
/** @var array<string,int>|null */
private ?array $usage = null;
public function __construct(private readonly DatabaseManager $databases)
{
}
public function reset(): void
{
$this->accounts = null;
$this->sourceMappings = null;
$this->codeHints = [];
$this->usage = null;
}
public function remember(string $accountCode, string $accountShort): void
{
$accountCode = trim($accountCode);
$accountShort = trim($accountShort);
if ($accountCode === '' || $accountShort === '' || ! isset($this->accounts()[$accountShort])) {
return;
}
$this->codeHints[$accountCode][$accountShort] = true;
}
public function resolve(
string $sourceName,
string $accountCode = '',
string $rawAccountShort = '',
): string {
$rawAccountShort = trim($rawAccountShort);
if ($rawAccountShort !== '' && isset($this->accounts()[$rawAccountShort])) {
return $rawAccountShort;
}
$codeHints = array_keys($this->codeHints[trim($accountCode)] ?? []);
if (count($codeHints) === 1) {
return (string) $codeHints[0];
}
$mapped = trim($this->sourceMappings()[trim($sourceName)] ?? '');
if ($mapped !== '' && isset($this->accounts()[$mapped])) {
return $mapped;
}
$sourceName = trim($sourceName);
if ($sourceName === '') {
return '';
}
$sourceBank = $this->bankOf($sourceName);
$sourceKey = $this->normalizeName($sourceName);
if ($sourceKey === '') {
return '';
}
$candidates = [];
foreach ($this->accounts() as $short => $account) {
if ($sourceBank !== '' && $account['bank'] !== $sourceBank) {
continue;
}
$shortKey = $this->normalizeName($short);
$companyKey = $this->normalizeName((string) ($account['company_name'] ?? ''));
if (! $this->sameKeyword($sourceKey, $shortKey)
&& ! $this->sameKeyword($sourceKey, $companyKey)) {
continue;
}
$candidates[$short] = true;
}
$candidates = array_keys($candidates);
if (count($candidates) === 1) {
return (string) $candidates[0];
}
if (count($candidates) < 2) {
return '';
}
$usage = $this->usage();
usort($candidates, static fn (string $left, string $right): int =>
($usage[$right] ?? 0) <=> ($usage[$left] ?? 0)
);
$top = $candidates[0];
$topUsage = (int) ($usage[$top] ?? 0);
$secondUsage = (int) ($usage[$candidates[1]] ?? 0);
// Normal RMB accounts normally dominate. Leave rare/currency-specific
// accounts to explicit 账户简称 or 科目编码 instead of guessing.
return $topUsage >= 3 && $topUsage >= max(3, $secondUsage * 3) ? $top : '';
}
/** @return array<string,array<string,mixed>> */
private function accounts(): array
{
if ($this->accounts !== null) {
return $this->accounts;
}
$accounts = [];
foreach ($this->connection()->table('fin_accounts')
->whereNotNull('account_short')
->where('account_short', '<>', '')
->get(['account_short', 'company_name', 'account_type', 'bank_name']) as $row) {
$short = trim((string) $row->account_short);
if ($short === '') {
continue;
}
$bank = $this->bankOf(
$short . ' ' . (string) ($row->bank_name ?? '') . ' ' . (string) ($row->company_name ?? ''),
);
$accounts[$short] = [
'company_name' => (string) ($row->company_name ?? ''),
'account_type' => (string) ($row->account_type ?? ''),
'bank' => $bank,
];
}
return $this->accounts = $accounts;
}
/** @return array<string,string> */
private function sourceMappings(): array
{
if ($this->sourceMappings !== null) {
return $this->sourceMappings;
}
$mappings = [];
foreach ($this->connection()->table('fin_account_map')
->where('source', 'dingtalk')
->whereNotNull('account_short')
->where('account_short', '<>', '')
->get(['source_value', 'account_short']) as $row) {
$source = trim((string) $row->source_value);
$short = trim((string) $row->account_short);
if ($source !== '' && $short !== '' && isset($this->accounts()[$short])) {
$mappings[$source] = $short;
}
}
return $this->sourceMappings = $mappings;
}
/** @return array<string,int> */
private function usage(): array
{
if ($this->usage !== null) {
return $this->usage;
}
$usage = [];
foreach ($this->connection()->table('fin_transactions')
->where('source', 'dingtalk')
->whereNotNull('account_short')
->where('account_short', '<>', '')
->selectRaw('account_short, COUNT(*) AS aggregate')
->groupBy('account_short')
->get() as $row) {
$usage[(string) $row->account_short] = (int) $row->aggregate;
}
return $this->usage = $usage;
}
private function sameKeyword(string $source, string $candidate): bool
{
if ($source === '' || $candidate === '') {
return false;
}
return $source === $candidate
|| (mb_strlen($source) >= 3 && str_contains($candidate, $source))
|| (mb_strlen($candidate) >= 3 && str_contains($source, $candidate));
}
private function bankOf(string $value): string
{
foreach (self::BANK_ALIASES as $alias => $canonical) {
if (str_contains($value, $alias)) {
return $canonical;
}
}
return '';
}
private function normalizeName(string $value): string
{
$value = mb_strtolower(trim($value), 'UTF-8');
foreach (self::SOURCE_PREFIXES as $prefix) {
$value = str_replace(mb_strtolower($prefix, 'UTF-8'), '', $value);
}
foreach (array_keys(self::BANK_ALIASES) as $bank) {
$value = str_replace(mb_strtolower($bank, 'UTF-8'), '', $value);
}
foreach ([
'股份有限公司',
'有限责任公司',
'有限公司',
'基本户',
'一般户',
'公司账户',
'个人账户',
'个体工商户',
'账户',
'银行',
'现金',
] as $suffix) {
$value = str_replace($suffix, '', $value);
}
return preg_replace('/[\s_\-—()()【】\[\].,,。::\/\\\\"]+/u', '', $value) ?? '';
}
private function connection()
{
return $this->databases->connection(
(string) config('upstream_mirrors.connection', 'business_mirror'),
);
}
}