171 lines
6.6 KiB
PHP
171 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Services;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Database\DatabaseManager;
|
|
|
|
final class FinanceLedgerBankBalanceCurrentCalculator
|
|
{
|
|
private readonly ConnectionInterface $connection;
|
|
|
|
public function __construct(DatabaseManager $databases)
|
|
{
|
|
$this->connection = $databases->connection(
|
|
(string) config('upstream_mirrors.connection', 'business_mirror'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 按账户期初和未删除动账明细计算滚动余额。
|
|
* 该计算不读取 fin_accounts.current_balance 或 fin_daily_flows。
|
|
*
|
|
* @param list<string> $accountShorts
|
|
* @return array<string,float>
|
|
*/
|
|
public function balances(array $accountShorts, ?string $asOf = null): array
|
|
{
|
|
$accountShorts = $this->normalizeAccountShorts($accountShorts);
|
|
if ($accountShorts === []) {
|
|
return [];
|
|
}
|
|
|
|
$openingDate = (string) config('finance_ledger.opening_date', '2026-10-17');
|
|
$openingRows = $this->connection->table('fin_account_opening')
|
|
->whereIn('account_short', $accountShorts)
|
|
->orderBy('opening_date')
|
|
->orderBy('id')
|
|
->get(['account_short', 'opening_date', 'opening_balance'])
|
|
->groupBy('account_short')
|
|
->map(static fn ($rows) => $rows->last());
|
|
$mappings = $this->connection->table('fin_account_map')
|
|
->whereIn('account_short', $accountShorts)
|
|
->get(['source', 'source_value', 'account_short']);
|
|
$mappingByKey = $mappings->keyBy(
|
|
static fn (object $row): string => (string) $row->source . "\0" . (string) $row->source_value,
|
|
);
|
|
$balances = [];
|
|
$starts = [];
|
|
foreach ($accountShorts as $accountShort) {
|
|
$opening = $openingRows->get($accountShort);
|
|
$balances[$accountShort] = round((float) ($opening->opening_balance ?? 0), 2);
|
|
$starts[$accountShort] = $opening !== null
|
|
? substr((string) $opening->opening_date, 0, 10)
|
|
: $openingDate;
|
|
}
|
|
$earliestStart = min($starts);
|
|
|
|
$transactionQuery = $this->connection->table('fin_transactions')
|
|
->whereNull('deleted_at')
|
|
->where('txn_date', '>=', $earliestStart)
|
|
->where(function ($query) use ($accountShorts, $mappings): void {
|
|
$query->whereIn('account_short', $accountShorts);
|
|
foreach ($mappings as $mapping) {
|
|
$query->orWhere(function ($pair) use ($mapping): void {
|
|
$pair->where('source', (string) $mapping->source)
|
|
->where('source_value', (string) $mapping->source_value);
|
|
});
|
|
}
|
|
});
|
|
if ($asOf !== null && preg_match('/^\d{4}-\d{2}-\d{2}$/', $asOf) === 1) {
|
|
$transactionQuery->whereDate('txn_date', '<=', $asOf);
|
|
}
|
|
$transactions = $transactionQuery
|
|
->orderBy('txn_date')
|
|
->orderBy('id')
|
|
->get(['txn_date', 'account_short', 'source', 'source_value', 'income', 'expense']);
|
|
|
|
foreach ($transactions as $transaction) {
|
|
$mappingKey = (string) $transaction->source . "\0" . (string) $transaction->source_value;
|
|
$accountShort = (string) (
|
|
$mappingByKey->get($mappingKey)?->account_short
|
|
?? $transaction->account_short
|
|
?? ''
|
|
);
|
|
if (! array_key_exists($accountShort, $balances)) {
|
|
continue;
|
|
}
|
|
$date = substr((string) $transaction->txn_date, 0, 10);
|
|
if ($date < $starts[$accountShort]) {
|
|
continue;
|
|
}
|
|
|
|
$balances[$accountShort] = round(
|
|
$balances[$accountShort]
|
|
+ (float) $transaction->income
|
|
- (float) $transaction->expense,
|
|
2,
|
|
);
|
|
}
|
|
|
|
return $balances;
|
|
}
|
|
|
|
/**
|
|
* 计算指定日期的净动账。
|
|
*
|
|
* @param list<string> $accountShorts
|
|
* @return array<string,float>
|
|
*/
|
|
public function movementsOnDate(array $accountShorts, string $date): array
|
|
{
|
|
$accountShorts = $this->normalizeAccountShorts($accountShorts);
|
|
if ($accountShorts === [] || preg_match('/^\d{4}-\d{2}-\d{2}$/', $date) !== 1) {
|
|
return [];
|
|
}
|
|
|
|
$mappings = $this->connection->table('fin_account_map')
|
|
->whereIn('account_short', $accountShorts)
|
|
->get(['source', 'source_value', 'account_short']);
|
|
$mappingByKey = $mappings->keyBy(
|
|
static fn (object $row): string => (string) $row->source . "\0" . (string) $row->source_value,
|
|
);
|
|
$transactionQuery = $this->connection->table('fin_transactions')
|
|
->whereNull('deleted_at')
|
|
->where('txn_date', $date)
|
|
->where(function ($query) use ($accountShorts, $mappings): void {
|
|
$query->whereIn('account_short', $accountShorts);
|
|
foreach ($mappings as $mapping) {
|
|
$query->orWhere(function ($pair) use ($mapping): void {
|
|
$pair->where('source', (string) $mapping->source)
|
|
->where('source_value', (string) $mapping->source_value);
|
|
});
|
|
}
|
|
});
|
|
$transactions = $transactionQuery
|
|
->orderBy('txn_date')
|
|
->orderBy('id')
|
|
->get(['txn_date', 'account_short', 'source', 'source_value', 'income', 'expense']);
|
|
|
|
$movements = array_fill_keys($accountShorts, 0.0);
|
|
foreach ($transactions as $transaction) {
|
|
$mappingKey = (string) $transaction->source . "\0" . (string) $transaction->source_value;
|
|
$accountShort = (string) (
|
|
$mappingByKey->get($mappingKey)?->account_short
|
|
?? $transaction->account_short
|
|
?? ''
|
|
);
|
|
if (! array_key_exists($accountShort, $movements)) {
|
|
continue;
|
|
}
|
|
$movements[$accountShort] = round(
|
|
$movements[$accountShort]
|
|
+ (float) $transaction->income
|
|
- (float) $transaction->expense,
|
|
2,
|
|
);
|
|
}
|
|
|
|
return $movements;
|
|
}
|
|
|
|
/** @return list<string> */
|
|
private function normalizeAccountShorts(array $accountShorts): array
|
|
{
|
|
return array_values(array_unique(array_filter(array_map(
|
|
static fn (mixed $accountShort): string => trim((string) $accountShort),
|
|
$accountShorts,
|
|
))));
|
|
}
|
|
}
|