接入店铺维度报表店铺主体数据 #636
128
modules/EcommerceFinance/Services/FinanceLedgerShopDirectory.php
Normal file
128
modules/EcommerceFinance/Services/FinanceLedgerShopDirectory.php
Normal file
@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\EcommerceFinance\Services;
|
||||
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Modules\EcommerceFinance\Repositories\BusinessInformationMirrorRepository;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* 财务台账店铺台账的店铺目录。
|
||||
*
|
||||
* 平台和 ERP 店铺名称来自店铺维度报表的店铺资料;店铺主体只取当前
|
||||
* 失效时间为「存续中」的主体期间。财务台账自己的账户和余额字段按 ERP
|
||||
* 店铺名称合并,避免把报表销量或其他财务字段混入店铺台账。
|
||||
*/
|
||||
class FinanceLedgerShopDirectory
|
||||
{
|
||||
public function __construct(
|
||||
private readonly BusinessInformationMirrorRepository $storeInformation,
|
||||
private readonly StoreReportPeriodBuilder $periods,
|
||||
private readonly DatabaseManager $databases,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 null 表示店铺维度报表镜像尚未就绪,调用方应回退到财务台账原表。
|
||||
*
|
||||
* @return list<array{
|
||||
* id:int,
|
||||
* platform:?string,
|
||||
* shop_name:string,
|
||||
* company_name:string,
|
||||
* account_short:?string,
|
||||
* status:string,
|
||||
* payout_account:?string,
|
||||
* shop_balance:float,
|
||||
* shop_balance_date:?string,
|
||||
* remark:?string
|
||||
* }>|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<string,object> */
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -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<array<string,mixed>> $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) {
|
||||
|
||||
71
tests/Feature/Business/FinanceLedgerShopDirectoryTest.php
Normal file
71
tests/Feature/Business/FinanceLedgerShopDirectoryTest.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Business;
|
||||
|
||||
use App\Models\Auth\Permission;
|
||||
use Modules\EcommerceFinance\Repositories\BusinessInformationMirrorRepository;
|
||||
use Modules\EcommerceFinance\Services\StoreReportPeriodBuilder;
|
||||
use Tests\Feature\FeatureTestCase;
|
||||
|
||||
class FinanceLedgerShopDirectoryTest extends FeatureTestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->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',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user