connection = $databases->connection( (string) config('upstream_mirrors.connection', 'business_mirror'), ); } /** @return array */ public function adjustments(array $accountShorts): array { $accountShorts = array_values(array_unique(array_filter(array_map( static fn (mixed $accountShort): string => trim((string) $accountShort), $accountShorts, )))); if ($accountShorts === [] || ! $this->connection->getSchemaBuilder() ->hasTable('fin_bank_balance_current_adjustments')) { return []; } return $this->connection->table('fin_bank_balance_current_adjustments') ->whereIn('account_short', $accountShorts) ->pluck('adjustment_amount', 'account_short') ->map(static fn (mixed $amount): float => round((float) $amount, 2)) ->all(); } /** * @return array{ * account_short:string, * current_balance:float, * base_balance:float, * adjustment_amount:float, * is_direct:bool * } */ public function save(string $accountShort, float $currentBalance): array { $accountShort = trim($accountShort); if ($accountShort === '' || ! $this->connection->table('fin_accounts') ->where('account_short', $accountShort) ->exists()) { throw new NotFoundHttpException('银行账户不存在。'); } if (FinanceLedgerIndependentAccounts::contains($accountShort)) { throw ValidationException::withMessages([ 'account_short' => '独立账单账户不参与银行余额调整。', ]); } $isDirect = isset(FinanceLedgerBankDirectAccounts::map($this->connection)[$accountShort]); $baseBalance = $this->baseBalance($accountShort, $isDirect); $adjustment = round($currentBalance - $baseBalance, 2); $this->connection->transaction(function () use ( $accountShort, $currentBalance, $baseBalance, $adjustment, $isDirect, ): void { $now = now(); $before = $this->connection->table('fin_bank_balance_current_adjustments') ->where('account_short', $accountShort) ->first(); $values = [ 'adjustment_amount' => $adjustment, 'updated_by' => (string) (user()?->name ?? ''), 'updated_at' => $now, ]; if ($before === null) { $this->connection->table('fin_bank_balance_current_adjustments')->insert([ 'account_short' => $accountShort, ...$values, 'created_at' => $now, ]); } else { $this->connection->table('fin_bank_balance_current_adjustments') ->where('account_short', $accountShort) ->update($values); } $this->connection->table('fin_ledger_edit_events')->insert([ 'entity_type' => 'bank_balance_update', 'entity_key' => 'account|' . $accountShort, 'changes' => json_encode([ 'base_balance' => $baseBalance, 'current_balance' => $currentBalance, 'adjustment_amount' => $adjustment, 'is_direct' => $isDirect, ], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR), 'user_id' => user()?->id, 'user_name' => (string) (user()?->name ?? ''), 'created_at' => $now, ]); }); return [ 'account_short' => $accountShort, 'current_balance' => round($currentBalance, 2), 'base_balance' => $baseBalance, 'adjustment_amount' => $adjustment, 'is_direct' => $isDirect, ]; } private function baseBalance(string $accountShort, bool $isDirect): float { $row = collect($this->accountMonitor->page()['fundOverview']['bank_accounts'] ?? []) ->first(static fn (array $account): bool => (string) ($account['account_short'] ?? '') === $accountShort); if ($row === null) { throw new NotFoundHttpException('银行余额账户不存在。'); } if (! $isDirect) { return round((float) ($row['current_balance'] ?? 0), 2); } $directBalance = round((float) ($row['current_balance'] ?? 0), 2); $movement = (float) ($this->currentCalculator->movementsOnDate( [$accountShort], now()->toDateString(), )[$accountShort] ?? 0); return round($directBalance + $movement, 2); } }