From ef33a32b88a0c4ea08b1d6b72ced415fa10e1053 Mon Sep 17 00:00:00 2001 From: qiuxiang <394405259@qq.com> Date: Mon, 9 Nov 2026 23:27:38 +0800 Subject: [PATCH] feat: filter account balance overview by occurrence date (#1025) --- .../StoreReceivingBalanceOverview.php | 44 +-- .../StoreReceivingBalanceOverviewService.php | 74 ++-- .../StoreReceivingBalanceOverview.vue | 352 +++++++++++++++--- ...reReceivingBalanceOverviewEndpointTest.php | 38 ++ ...oreReceivingBalanceOverviewServiceTest.php | 22 ++ .../StoreReceivingBalanceOverviewTest.php | 12 +- 6 files changed, 445 insertions(+), 97 deletions(-) diff --git a/modules/EcommerceFinance/Http/Controllers/StoreReceivingBalanceOverview.php b/modules/EcommerceFinance/Http/Controllers/StoreReceivingBalanceOverview.php index d2cc0f74..fa41877e 100644 --- a/modules/EcommerceFinance/Http/Controllers/StoreReceivingBalanceOverview.php +++ b/modules/EcommerceFinance/Http/Controllers/StoreReceivingBalanceOverview.php @@ -13,14 +13,9 @@ class StoreReceivingBalanceOverview extends Controller public function index(Request $request, StoreReceivingBalanceOverviewService $service) { $this->authorizeStoreReceivingBills(); - $validated = $request->validate([ - 'month' => ['nullable', 'date_format:Y-m'], - ]); try { - return response()->json($service->overview( - (string) ($validated['month'] ?? now('Asia/Shanghai')->format('Y-m')), - )); + return response()->json($service->overview(...$this->requestedPeriod($request))); } catch (Throwable $exception) { report($exception); @@ -33,14 +28,9 @@ class StoreReceivingBalanceOverview extends Controller public function incomeDetail(Request $request, StoreReceivingBalanceOverviewService $service) { $this->authorizeStoreReceivingBills(); - $validated = $request->validate([ - 'month' => ['nullable', 'date_format:Y-m'], - ]); try { - return response()->json($service->incomeDetail( - (string) ($validated['month'] ?? now('Asia/Shanghai')->format('Y-m')), - )); + return response()->json($service->incomeDetail(...$this->requestedPeriod($request))); } catch (Throwable $exception) { report($exception); @@ -53,14 +43,9 @@ class StoreReceivingBalanceOverview extends Controller public function expenseDetail(Request $request, StoreReceivingBalanceOverviewService $service) { $this->authorizeStoreReceivingBills(); - $validated = $request->validate([ - 'month' => ['nullable', 'date_format:Y-m'], - ]); try { - return response()->json($service->expenseDetail( - (string) ($validated['month'] ?? now('Asia/Shanghai')->format('Y-m')), - )); + return response()->json($service->expenseDetail(...$this->requestedPeriod($request))); } catch (Throwable $exception) { report($exception); @@ -73,14 +58,9 @@ class StoreReceivingBalanceOverview extends Controller public function balanceDetail(Request $request, StoreReceivingBalanceOverviewService $service) { $this->authorizeStoreReceivingBills(); - $validated = $request->validate([ - 'month' => ['nullable', 'date_format:Y-m'], - ]); try { - return response()->json($service->balanceDetail( - (string) ($validated['month'] ?? now('Asia/Shanghai')->format('Y-m')), - )); + return response()->json($service->balanceDetail(...$this->requestedPeriod($request))); } catch (Throwable $exception) { report($exception); @@ -90,6 +70,22 @@ class StoreReceivingBalanceOverview extends Controller } } + /** @return array{0:string,1?:string} */ + private function requestedPeriod(Request $request): array + { + $validated = $request->validate([ + 'month' => ['nullable', 'date_format:Y-m'], + 'start' => ['nullable', 'date_format:Y-m-d', 'required_with:end'], + 'end' => ['nullable', 'date_format:Y-m-d', 'required_with:start', 'after_or_equal:start'], + ]); + + if (! empty($validated['start']) && ! empty($validated['end'])) { + return [(string) $validated['start'], (string) $validated['end']]; + } + + return [(string) ($validated['month'] ?? now('Asia/Shanghai')->format('Y-m'))]; + } + private function authorizeStoreReceivingBills(): void { $section = collect(SidebarPermissions::navigationFor(user())) diff --git a/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php b/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php index acf433f2..f81e21e7 100644 --- a/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php +++ b/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php @@ -41,9 +41,9 @@ class StoreReceivingBalanceOverviewService ) {} /** @return array */ - public function overview(string $month): array + public function overview(string $periodStart, ?string $periodEnd = null): array { - [$periodStart, $periodEnd] = $this->periodForMonth($month); + [$periodStart, $periodEnd, $periodMonth] = $this->periodForRange($periodStart, $periodEnd); $platforms = []; $availableTotals = [ 'opening_balance' => 0.0, @@ -105,7 +105,7 @@ class StoreReceivingBalanceOverviewService $expense = $complete ? round($availableTotals['expense'], 2) : null; return [ - 'month' => $month, + 'month' => $periodMonth, 'period' => [ 'start' => $periodStart, 'end' => $periodEnd, @@ -133,9 +133,9 @@ class StoreReceivingBalanceOverviewService * * @return array */ - public function incomeDetail(string $month): array + public function incomeDetail(string $periodStart, ?string $periodEnd = null): array { - return $this->flowDetail($month, 'income'); + return $this->flowDetail($periodStart, $periodEnd, 'income'); } /** @@ -146,9 +146,9 @@ class StoreReceivingBalanceOverviewService * * @return array */ - public function expenseDetail(string $month): array + public function expenseDetail(string $periodStart, ?string $periodEnd = null): array { - return $this->flowDetail($month, 'expense'); + return $this->flowDetail($periodStart, $periodEnd, 'expense'); } /** @@ -159,9 +159,9 @@ class StoreReceivingBalanceOverviewService * * @return array */ - public function balanceDetail(string $month): array + public function balanceDetail(string $periodStart, ?string $periodEnd = null): array { - $overview = $this->overview($month); + $overview = $this->overview($periodStart, $periodEnd); $details = []; foreach ($overview['platforms'] as $platform) { @@ -179,7 +179,7 @@ class StoreReceivingBalanceOverviewService } return [ - 'month' => $month, + 'month' => $overview['month'], 'period' => $overview['period'], 'summary' => [ 'balance' => $overview['summary']['balance'], @@ -192,9 +192,9 @@ class StoreReceivingBalanceOverviewService } /** @return array */ - private function flowDetail(string $month, string $flow): array + private function flowDetail(string $periodStart, ?string $periodEnd, string $flow): array { - [$periodStart, $periodEnd] = $this->periodForMonth($month); + [$periodStart, $periodEnd, $periodMonth] = $this->periodForRange($periodStart, $periodEnd); $platformOrder = []; $details = []; $totalAmount = 0.0; @@ -266,7 +266,7 @@ class StoreReceivingBalanceOverviewService }); return [ - 'month' => $month, + 'month' => $periodMonth, 'period' => [ 'start' => $periodStart, 'end' => $periodEnd, @@ -281,17 +281,42 @@ class StoreReceivingBalanceOverviewService ]; } - /** @return array{0:string,1:string} */ - private function periodForMonth(string $month): array + /** @return array{0:string,1:string,2:string} */ + private function periodForRange(string $start, ?string $end): array { - $start = CarbonImmutable::createFromFormat('!Y-m', $month, 'Asia/Shanghai'); - if (! $start instanceof CarbonImmutable || $start->format('Y-m') !== $month) { - throw new RuntimeException('invalid_balance_overview_month'); + if ($end === null && preg_match('/^\d{4}-\d{2}$/', $start) === 1) { + $month = CarbonImmutable::createFromFormat('!Y-m', $start, 'Asia/Shanghai'); + if ($month instanceof CarbonImmutable && $month->format('Y-m') === $start) { + return [ + $month->startOfMonth()->format('Y-m-d'), + $month->endOfMonth()->format('Y-m-d'), + $start, + ]; + } + } + + try { + $periodStart = CarbonImmutable::createFromFormat('!Y-m-d', $start, 'Asia/Shanghai'); + $periodEnd = $end !== null + ? CarbonImmutable::createFromFormat('!Y-m-d', $end, 'Asia/Shanghai') + : null; + } catch (Throwable) { + throw new RuntimeException('invalid_balance_overview_period'); + } + + if (! $periodStart instanceof CarbonImmutable + || $periodStart->format('Y-m-d') !== $start + || ! $periodEnd instanceof CarbonImmutable + || $periodEnd->format('Y-m-d') !== (string) $end + || $periodEnd->lt($periodStart) + ) { + throw new RuntimeException('invalid_balance_overview_period'); } return [ - $start->startOfMonth()->format('Y-m-d'), - $start->endOfMonth()->format('Y-m-d'), + $periodStart->format('Y-m-d'), + $periodEnd->format('Y-m-d'), + $periodStart->format('Y-m'), ]; } @@ -365,10 +390,9 @@ class StoreReceivingBalanceOverviewService ->where($flow, '>', 0); if (in_array($platform, ['vipshop', 'vipshop-jx'], true)) { - $query->whereRaw( - "JSON_UNQUOTE(JSON_EXTRACT(payload, '$.settle_period')) = ?", - [substr($start, 0, 7)], - ); + $settlePeriod = "JSON_UNQUOTE(JSON_EXTRACT(payload, '$.settle_period'))"; + $query->whereRaw($settlePeriod . ' >= ?', [substr($start, 0, 7)]) + ->whereRaw($settlePeriod . ' <= ?', [substr($end, 0, 7)]); } else { $query->where('occurred_at', '>=', $start . ' 00:00:00') ->where('occurred_at', '<', CarbonImmutable::parse($end, 'Asia/Shanghai')->addDay()->format('Y-m-d') . ' 00:00:00'); @@ -457,7 +481,7 @@ class StoreReceivingBalanceOverviewService return match ($message) { '平台期初余额未就绪' => $message, '平台账单汇总数据不完整' => $message, - 'invalid_balance_overview_month' => '月份格式无效', + 'invalid_balance_overview_period' => '日期范围无效', 'unsupported_balance_overview_platform' => '平台类型无效', '' => '平台账单暂时无法读取', default => mb_substr($message, 0, 160), diff --git a/resources/assets/js/components/business/StoreReceivingBalanceOverview.vue b/resources/assets/js/components/business/StoreReceivingBalanceOverview.vue index bc15b224..8f1b02c6 100644 --- a/resources/assets/js/components/business/StoreReceivingBalanceOverview.vue +++ b/resources/assets/js/components/business/StoreReceivingBalanceOverview.vue @@ -4,16 +4,81 @@

店铺收款账户

账户余额总览

-

聚合 8 个收款平台的月度期初、收入与支出(单位:万元)

+

聚合 8 个收款平台在选定发生时间范围内的期初、收入与支出(单位:万元)

-
- - {{ monthLabel }} - +
+
+ 发生时间 +
+ + + + + + 至 + + + + + +
+ +
+ + +

时间条件已修改,点击查询后生效。

@@ -73,7 +138,7 @@
{{ detailTitle }} -

{{ monthLabel }} · {{ detailDescription }}

+

{{ queryPeriodLabel }} · {{ detailDescription }}

当前筛选合计(万元) @@ -179,6 +244,10 @@