connection = $databases->connection( (string) config('upstream_mirrors.connection', 'business_mirror'), ); } /** * 按账户期初和未删除动账明细计算滚动余额。 * 该计算不读取 fin_accounts.current_balance 或 fin_daily_flows。 * 没有实际期初的账户不返回余额,避免把缺失期初当成 0 后推算金额。 * * @param list $accountShorts * @return array */ public function balances(array $accountShorts, ?string $asOf = null): array { $accountShorts = $this->normalizeAccountShorts($accountShorts); if ($accountShorts === []) { return []; } $openingRows = $this->connection->table('fin_account_opening') ->whereIn('account_short', $accountShorts) ->when($asOf !== null, fn ($query) => $query->where('opening_date', '<=', $asOf)) ->orderBy('opening_date') ->orderBy('id') ->get(['account_short', 'opening_date', 'opening_balance']) ->groupBy('account_short') ->map(static fn ($rows) => $rows->last()); if ($openingRows->isEmpty()) { 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, ); $balances = []; $starts = []; foreach ($openingRows as $accountShort => $opening) { $balances[(string) $accountShort] = round((float) $opening->opening_balance, 2); $starts[(string) $accountShort] = substr((string) $opening->opening_date, 0, 10); } $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 $accountShorts * @return array */ 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 */ private function normalizeAccountShorts(array $accountShorts): array { return array_values(array_unique(array_filter(array_map( static fn (mixed $accountShort): string => trim((string) $accountShort), $accountShorts, )))); } }