diff --git a/database/migrations/2026_11_24_000000_create_fin_bank_direct_bills.php b/database/migrations/2026_11_24_000000_create_fin_bank_direct_bills.php new file mode 100644 index 00000000..aa5d8509 --- /dev/null +++ b/database/migrations/2026_11_24_000000_create_fin_bank_direct_bills.php @@ -0,0 +1,58 @@ +connection()); + if ($schema->hasTable('fin_bank_direct_bills')) { + return; + } + + $schema->create('fin_bank_direct_bills', function (Blueprint $table): void { + $table->bigIncrements('id'); + $table->string('bank_name', 80); + $table->unsignedBigInteger('finance_account_id')->nullable(); + $table->string('company_name', 100)->nullable(); + $table->string('account_short', 100); + $table->string('account_no', 64); + $table->date('txn_date'); + $table->timestamp('txn_at')->nullable(); + $table->string('record_code', 128)->nullable(); + $table->string('counterparty_name', 255)->nullable(); + $table->string('counterparty_account', 100)->nullable(); + $table->string('counterparty_bank', 100)->nullable(); + $table->text('detail')->nullable(); + $table->text('payment_remark')->nullable(); + $table->decimal('income', 18, 2)->default(0); + $table->decimal('expense', 18, 2)->default(0); + $table->decimal('balance', 18, 2)->nullable(); + $table->string('currency', 16)->default('人民币'); + $table->string('source', 32)->default('bank-direct'); + $table->string('source_file_name', 255)->nullable(); + $table->char('source_file_sha256', 64)->nullable(); + $table->char('row_key', 64)->unique('fin_bank_direct_bill_row_key_unique'); + $table->timestamp('imported_at')->nullable(); + $table->timestamps(); + + $table->index(['bank_name', 'txn_date'], 'fin_bank_direct_bill_bank_date_index'); + $table->index(['account_no', 'txn_date'], 'fin_bank_direct_bill_account_date_index'); + $table->index('account_short', 'fin_bank_direct_bill_account_short_index'); + $table->index('imported_at', 'fin_bank_direct_bill_imported_index'); + }); + } + + public function down(): void + { + Schema::connection($this->connection())->dropIfExists('fin_bank_direct_bills'); + } +}; diff --git a/modules/EcommerceFinance/View/BankBillPageData.php b/modules/EcommerceFinance/View/BankBillPageData.php new file mode 100644 index 00000000..e2200202 --- /dev/null +++ b/modules/EcommerceFinance/View/BankBillPageData.php @@ -0,0 +1,160 @@ +filters(); + $query = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror')) + ->table(self::TABLE); + + if ($filters['from'] !== '') { + $query->where('txn_date', '>=', $filters['from']); + } + if ($filters['to'] !== '') { + $query->where('txn_date', '<=', $filters['to']); + } + if ($filters['bank'] !== 'all') { + $query->where('bank_name', $filters['bank']); + } + if ($filters['account_no'] !== '') { + $query->where('account_no', $filters['account_no']); + } + if ($filters['search'] !== '') { + $search = $filters['search']; + $query->where(function ($builder) use ($search): void { + foreach ([ + 'company_name', + 'account_short', + 'account_no', + 'counterparty_name', + 'counterparty_account', + 'counterparty_bank', + 'detail', + 'payment_remark', + 'record_code', + ] as $column) { + $builder->orWhere($column, 'like', '%' . $search . '%'); + } + }); + } + + $summaryQuery = clone $query; + $summary = [ + 'count' => (int) (clone $summaryQuery)->count(), + 'income' => round((float) (clone $summaryQuery)->sum('income'), 2), + 'expense' => round((float) (clone $summaryQuery)->sum('expense'), 2), + 'latest_imported_at' => (clone $summaryQuery)->max('imported_at'), + ]; + + $page = min( + max(1, (int) request('bills_page', 1)), + max(1, (int) ceil($summary['count'] / self::PER_PAGE)), + ); + $rows = (clone $query) + ->orderByDesc('txn_date') + ->orderByDesc('txn_at') + ->orderByDesc('id') + ->forPage($page, self::PER_PAGE) + ->get(); + $paginator = new LengthAwarePaginator( + $rows->values()->all(), + $summary['count'], + self::PER_PAGE, + $page, + ['path' => request()->url(), 'query' => request()->query()], + ); + $paginator->setPageName('bills_page'); + + return [ + 'filters' => $filters, + 'period' => ['from' => $filters['from'], 'to' => $filters['to']], + 'summary' => $summary, + 'page' => $paginator, + 'bankOptions' => $this->bankOptions(), + 'accountOptions' => $this->accountOptions($filters['bank']), + ]; + } + + private function filters(): array + { + $defaultFrom = now()->startOfMonth()->toDateString(); + $defaultTo = now()->endOfMonth()->toDateString(); + $from = $this->date((string) request('bills_from', '')) ?? $defaultFrom; + $to = $this->date((string) request('bills_to', '')) ?? $defaultTo; + if ($from > $to) { + [$from, $to] = [$to, $from]; + } + + return [ + 'from' => $from, + 'to' => $to, + 'bank' => trim((string) request('bills_bank', 'all')) ?: 'all', + 'account_no' => preg_replace('/\D+/', '', (string) request('bills_account_no', '')) ?? '', + 'search' => trim((string) request('bills_search', '')), + ]; + } + + private function date(string $value): ?string + { + if ($value === '') { + return null; + } + $date = \DateTimeImmutable::createFromFormat('!Y-m-d', $value); + + return $date !== false && $date->format('Y-m-d') === $value ? $value : null; + } + + private function bankOptions(): array + { + return DB::connection((string) config('upstream_mirrors.connection', 'business_mirror')) + ->table(self::TABLE) + ->whereNotNull('bank_name') + ->where('bank_name', '<>', '') + ->distinct() + ->orderBy('bank_name') + ->pluck('bank_name') + ->map(static fn (mixed $value): string => trim((string) $value)) + ->filter() + ->values() + ->all(); + } + + private function accountOptions(string $bank): array + { + $query = DB::connection((string) config('upstream_mirrors.connection', 'business_mirror')) + ->table(self::TABLE) + ->select(['bank_name', 'company_name', 'account_short', 'account_no']) + ->distinct(); + if ($bank !== 'all') { + $query->where('bank_name', $bank); + } + + return $query + ->orderBy('bank_name') + ->orderBy('account_short') + ->get() + ->map(static fn (object $row): array => [ + 'bank_name' => (string) $row->bank_name, + 'company_name' => (string) ($row->company_name ?? ''), + 'account_short' => (string) $row->account_short, + 'account_no' => (string) $row->account_no, + 'label' => trim(implode(' · ', array_filter([ + (string) $row->account_short, + (string) $row->account_no, + (string) ($row->company_name ?? ''), + ]))), + ]) + ->values() + ->all(); + } +} diff --git a/modules/EcommerceFinance/View/FinanceLedgerPageData.php b/modules/EcommerceFinance/View/FinanceLedgerPageData.php index 676856e9..c6c24cdd 100644 --- a/modules/EcommerceFinance/View/FinanceLedgerPageData.php +++ b/modules/EcommerceFinance/View/FinanceLedgerPageData.php @@ -32,6 +32,7 @@ class FinanceLedgerPageData public const TABS = [ 'accounts' => ['title' => '账户总表', 'icon' => 'account_balance'], 'overview' => ['title' => '账户总览', 'icon' => 'account_balance'], + 'bills' => ['title' => '银行账单', 'icon' => 'receipt_long'], 'shops' => ['title' => '店铺台账', 'icon' => 'storefront'], 'transactions' => ['title' => '动账明细', 'icon' => 'receipt_long'], 'mapping' => ['title' => '账户映射', 'icon' => 'alt_route'], @@ -138,6 +139,7 @@ class FinanceLedgerPageData private readonly FinanceLedgerShopDirectory $shopDirectory, private readonly JushuitanShopBalanceRepository $shopBalances, private readonly NonStoreAccountMonitorPageData $nonStoreAccountMonitor, + private readonly BankBillPageData $bankBills, ) { } @@ -192,6 +194,7 @@ class FinanceLedgerPageData 'ledgerTab' => $tab, 'ledgerTabs' => array_diff_key(self::TABS, self::HIDDEN_TABS), 'ledgerOverview' => $ledgerOverview, + 'ledgerBankBills' => $this->bankBills->page(), 'ledgerOpeningDate' => $openingDate, 'ledgerAccountView' => $accountView, 'ledgerAccounts' => $accounts['rows'], diff --git a/resources/views/business/pages/financial-ledger/ledger.blade.php b/resources/views/business/pages/financial-ledger/ledger.blade.php index 3fe39bff..1db66ffc 100644 --- a/resources/views/business/pages/financial-ledger/ledger.blade.php +++ b/resources/views/business/pages/financial-ledger/ledger.blade.php @@ -1108,6 +1108,11 @@ @endif + {{-- 银行账单 --}} + + {{-- 账户总表 --}}