From 11637cc8275a96ca4424bb4605a2aeea5f4081af Mon Sep 17 00:00:00 2001 From: 92451 <924518135@qq.com> Date: Mon, 26 Oct 2026 19:31:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=85=A5=E5=BA=97=E9=93=BA=E7=BB=B4?= =?UTF-8?q?=E5=BA=A6=E6=8A=A5=E8=A1=A8=E5=BA=97=E9=93=BA=E4=B8=BB=E4=BD=93?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/FinanceLedgerShopDirectory.php | 128 ++++++++++++++++++ .../View/FinanceLedgerPageData.php | 94 ++++++++++++- .../FinanceLedgerShopDirectoryTest.php | 71 ++++++++++ 3 files changed, 292 insertions(+), 1 deletion(-) create mode 100644 modules/EcommerceFinance/Services/FinanceLedgerShopDirectory.php create mode 100644 tests/Feature/Business/FinanceLedgerShopDirectoryTest.php diff --git a/modules/EcommerceFinance/Services/FinanceLedgerShopDirectory.php b/modules/EcommerceFinance/Services/FinanceLedgerShopDirectory.php new file mode 100644 index 00000000..34ea3219 --- /dev/null +++ b/modules/EcommerceFinance/Services/FinanceLedgerShopDirectory.php @@ -0,0 +1,128 @@ +|null + */ + public function rows(): ?array + { + try { + $stores = $this->storeInformation->allStores(); + } catch (Throwable $exception) { + report($exception); + + return null; + } + + $openStores = array_values(array_filter( + $stores, + static fn (array $store): bool => in_array( + (string) ($store['store_status'] ?? ''), + ['0', '正常'], + true, + ) && trim((string) ($store['erp_store_name'] ?? '')) !== '', + )); + if ($openStores === []) { + return []; + } + + try { + $currentCompanies = $this->periods->currentCompanies($openStores); + } catch (Throwable $exception) { + report($exception); + + return null; + } + + $financeShops = $this->financeShops(); + $rows = []; + foreach ($openStores as $store) { + $erpStoreName = trim((string) ($store['erp_store_name'] ?? '')); + $companyName = trim((string) ($currentCompanies[$erpStoreName] ?? '')); + // 失效时间不是「存续中」的店铺不会出现在当前主体映射中。 + if ($companyName === '') { + continue; + } + + $financeShop = $financeShops[$erpStoreName] ?? null; + $rows[] = [ + 'id' => (int) ($store['id'] ?? 0), + 'platform' => $this->nullableString($store['platform'] ?? null), + 'shop_name' => $erpStoreName, + 'company_name' => $companyName, + 'account_short' => $this->nullableString($financeShop->account_short ?? null), + 'status' => '在营', + 'payout_account' => $this->nullableString( + $financeShop->payout_account ?? ($store['withdrawal_account'] ?? null), + ), + 'shop_balance' => round((float) ($financeShop->shop_balance ?? 0), 2), + 'shop_balance_date' => $this->nullableString($financeShop->shop_balance_date ?? null), + 'remark' => $this->nullableString($financeShop->remark ?? null), + ]; + } + + return $rows; + } + + /** @return array */ + private function financeShops(): array + { + $connection = $this->databases->connection( + (string) config('upstream_mirrors.connection', 'business_mirror'), + ); + if (! Schema::connection($connection->getName())->hasTable('fin_shops')) { + return []; + } + + return $connection->table('fin_shops') + ->get() + ->mapWithKeys(static function (object $row): array { + $name = trim((string) ($row->shop_name ?? '')); + + return $name === '' ? [] : [$name => $row]; + }) + ->all(); + } + + private function nullableString(mixed $value): ?string + { + $value = trim((string) $value); + + return $value === '' ? null : $value; + } +} diff --git a/modules/EcommerceFinance/View/FinanceLedgerPageData.php b/modules/EcommerceFinance/View/FinanceLedgerPageData.php index 3ca93349..dfabf5aa 100644 --- a/modules/EcommerceFinance/View/FinanceLedgerPageData.php +++ b/modules/EcommerceFinance/View/FinanceLedgerPageData.php @@ -6,10 +6,12 @@ use App\Utilities\FinanceLedgerAccountStatus; use App\Utilities\FinanceLedgerAccountShouldKeep; use App\Utilities\FinanceLedgerAccountType; use App\Utilities\FinanceLedgerAccess; -use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Database\Query\Builder; +use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Pagination\Paginator; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; +use Modules\EcommerceFinance\Services\FinanceLedgerShopDirectory; use Modules\EcommerceFinance\Repositories\JushuitanShopBalanceRepository; /** @@ -31,6 +33,11 @@ class FinanceLedgerPageData private const ACCOUNT_FILTER_EMPTY = '__empty__'; + public function __construct( + private readonly FinanceLedgerShopDirectory $shopDirectory, + ) { + } + public function page(?string $tab = null): array { $tab = (string) (request('tab') ?: $tab); @@ -214,6 +221,91 @@ class FinanceLedgerPageData } private function shops(): array + { + $directoryRows = $this->shopDirectory->rows(); + + return $directoryRows === null + ? $this->shopsFromLedgerTable() + : $this->shopsFromStoreReport($directoryRows); + } + + /** + * @param list> $rows + * @return array{rows:LengthAwarePaginator,summary:array{count:int,balance:float}} + */ + private function shopsFromStoreReport(array $rows): array + { + $keyword = trim((string) request('shops_q', '')); + $platform = trim((string) request('shops_platform', '')); + $company = trim((string) request('shops_company', '')); + + $rows = array_values(array_filter( + $rows, + static function (array $row) use ($keyword, $platform, $company): bool { + if ($platform !== '' && trim((string) ($row['platform'] ?? '')) !== $platform) { + return false; + } + if ($company !== '' && mb_stripos((string) ($row['company_name'] ?? ''), $company) === false) { + return false; + } + if ($keyword === '') { + return true; + } + + foreach (['shop_name', 'platform', 'company_name'] as $field) { + if (mb_stripos((string) ($row[$field] ?? ''), $keyword) !== false) { + return true; + } + } + + return false; + }, + )); + + $headerColumns = $this->shopHeaderColumns(); + $rows = $this->filterAndSortHeaderRows($rows, 'shops', $headerColumns); + if (! isset($headerColumns[trim((string) request('shops_sort', ''))])) { + usort($rows, static fn (array $left, array $right): int => [ + (string) ($left['company_name'] ?? ''), + (string) ($left['shop_name'] ?? ''), + ] <=> [ + (string) ($right['company_name'] ?? ''), + (string) ($right['shop_name'] ?? ''), + ]); + } + + $summary = [ + 'count' => count($rows), + 'balance' => round(array_sum(array_map( + static fn (array $row): float => (float) ($row['shop_balance'] ?? 0), + $rows, + )), 2), + ]; + $perPage = 100; + $currentPage = Paginator::resolveCurrentPage('shops_page'); + $items = array_map( + static fn (array $row): object => (object) $row, + array_slice($rows, ($currentPage - 1) * $perPage, $perPage), + ); + + return [ + 'rows' => (new LengthAwarePaginator( + $items, + count($rows), + $perPage, + $currentPage, + [ + 'path' => Paginator::resolveCurrentPath(), + 'query' => request()->query(), + 'pageName' => 'shops_page', + ], + ))->withQueryString(), + 'summary' => $summary, + ]; + } + + /** @return array{rows:LengthAwarePaginator,summary:array{count:int,balance:float}} */ + private function shopsFromLedgerTable(): array { $query = $this->db()->table('fin_shops'); foreach (['shops_q' => ['shop_name', 'platform', 'company_name'], 'shops_platform' => ['platform'], 'shops_company' => ['company_name']] as $param => $columns) { diff --git a/tests/Feature/Business/FinanceLedgerShopDirectoryTest.php b/tests/Feature/Business/FinanceLedgerShopDirectoryTest.php new file mode 100644 index 00000000..312db3db --- /dev/null +++ b/tests/Feature/Business/FinanceLedgerShopDirectoryTest.php @@ -0,0 +1,71 @@ +user->attachPermission( + Permission::where('name', 'read-admin-panel')->firstOrFail(), + ); + } + + public function test_shop_ledger_uses_store_report_current_subject_and_platform(): void + { + $openStore = [ + 'id' => 901, + 'platform' => '天猫', + 'erp_store_name' => '天猫-报表测试店', + 'company_name' => '历史主体', + 'store_status' => '0', + 'withdrawal_account' => '回款账户甲', + ]; + $closedStore = [ + 'id' => 902, + 'platform' => '京东', + 'erp_store_name' => '京东-已关店', + 'company_name' => '历史主体', + 'store_status' => '6', + 'withdrawal_account' => null, + ]; + + $this->mock(BusinessInformationMirrorRepository::class) + ->shouldReceive('allStores') + ->once() + ->andReturn([$openStore, $closedStore]); + $this->mock(StoreReportPeriodBuilder::class) + ->shouldReceive('currentCompanies') + ->once() + ->with([$openStore]) + ->andReturn(['天猫-报表测试店' => '当前存续主体']); + + $this->loginAs()->get($this->pageUrl()) + ->assertOk() + ->assertSeeTextInOrder([ + '天猫', + '天猫-报表测试店', + '当前存续主体', + '在营', + '回款账户甲', + ]) + ->assertDontSeeText('京东-已关店') + ->assertDontSeeText('历史主体'); + } + + private function pageUrl(): string + { + return route('business.pages.show', [ + 'company_id' => $this->company->id, + 'section' => 'financial-operations', + 'page' => 'ledger', + ]); + } +} -- 2.47.2