按供应商付款回单的付款账号匹配银行账户,避免同一公司多账户误归属。 Co-authored-by: 92451 <924518135@qq.com> Co-committed-by: 92451 <924518135@qq.com>
96 lines
3.0 KiB
PHP
96 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Services;
|
|
|
|
use App\Utilities\FinanceLedgerIndependentAccounts;
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Database\DatabaseManager;
|
|
|
|
final class FinanceLedgerPayableAccountMatcher
|
|
{
|
|
private readonly ConnectionInterface $connection;
|
|
|
|
/** @var array<string,array<string,array{account_short:string,account_type:string,company_name:string}>>|null */
|
|
private ?array $accountsByNumber = null;
|
|
|
|
public function __construct(DatabaseManager $database)
|
|
{
|
|
$this->connection = $database->connection(
|
|
(string) config('upstream_mirrors.connection', 'business_mirror'),
|
|
);
|
|
}
|
|
|
|
public function reset(): void
|
|
{
|
|
$this->accountsByNumber = null;
|
|
}
|
|
|
|
public function normalizeAccountNo(mixed $value): string
|
|
{
|
|
return preg_replace(
|
|
'/\s+/u',
|
|
'',
|
|
ltrim(trim((string) ($value ?? '')), "'"),
|
|
) ?? '';
|
|
}
|
|
|
|
/**
|
|
* @return array<string,array{account_short:string,account_type:string,company_name:string}>
|
|
*/
|
|
public function accountsFor(mixed $accountNo): array
|
|
{
|
|
$accountNo = $this->normalizeAccountNo($accountNo);
|
|
if ($accountNo === '') {
|
|
return [];
|
|
}
|
|
|
|
return $this->accountsByNumber()[$accountNo] ?? [];
|
|
}
|
|
|
|
public function resolveAccountShort(mixed $accountNo): string
|
|
{
|
|
$accounts = array_values($this->accountsFor($accountNo));
|
|
if (count($accounts) === 1) {
|
|
return $accounts[0]['account_short'];
|
|
}
|
|
|
|
$publicAccounts = array_values(array_filter(
|
|
$accounts,
|
|
static fn (array $account): bool => $account['account_type'] === '公户',
|
|
));
|
|
|
|
return count($publicAccounts) === 1 ? $publicAccounts[0]['account_short'] : '';
|
|
}
|
|
|
|
/**
|
|
* @return array<string,array<string,array{account_short:string,account_type:string,company_name:string}>>
|
|
*/
|
|
private function accountsByNumber(): array
|
|
{
|
|
if ($this->accountsByNumber !== null) {
|
|
return $this->accountsByNumber;
|
|
}
|
|
|
|
$this->accountsByNumber = [];
|
|
$accounts = FinanceLedgerIndependentAccounts::exclude($this->connection->table('fin_accounts'))
|
|
->whereNotNull('account_no')
|
|
->get(['account_no', 'account_short', 'account_type', 'company_name']);
|
|
|
|
foreach ($accounts as $account) {
|
|
$accountNo = $this->normalizeAccountNo($account->account_no);
|
|
$accountShort = trim((string) ($account->account_short ?? ''));
|
|
if ($accountNo === '' || $accountShort === '') {
|
|
continue;
|
|
}
|
|
|
|
$this->accountsByNumber[$accountNo][$accountShort] = [
|
|
'account_short' => $accountShort,
|
|
'account_type' => trim((string) ($account->account_type ?? '')),
|
|
'company_name' => trim((string) ($account->company_name ?? '')),
|
|
];
|
|
}
|
|
|
|
return $this->accountsByNumber;
|
|
}
|
|
}
|