diff --git a/modules/EcommerceFinance/Http/Controllers/StoreReceivingBalanceOverview.php b/modules/EcommerceFinance/Http/Controllers/StoreReceivingBalanceOverview.php index 86b0fa459..de7be005b 100644 --- a/modules/EcommerceFinance/Http/Controllers/StoreReceivingBalanceOverview.php +++ b/modules/EcommerceFinance/Http/Controllers/StoreReceivingBalanceOverview.php @@ -150,7 +150,7 @@ class StoreReceivingBalanceOverview extends Controller $cacheKey = implode(':', [ 'ecommerce-finance', 'account-balance-daily', - 'v7', + 'v8', (string) company_id(), $period[0], $period[1] ?? '', @@ -189,7 +189,7 @@ class StoreReceivingBalanceOverview extends Controller $cacheKey = implode(':', [ 'ecommerce-finance', 'account-balance-daily-stores', - 'v7', + 'v8', (string) company_id(), $period[0], $period[1] ?? '', diff --git a/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php b/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php index 47b9d1f3c..5dbf08581 100644 --- a/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php +++ b/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php @@ -753,11 +753,20 @@ class StoreReceivingBalanceOverviewService $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', '>=', $from) - ->where('occurred_at', '<', $until); + + $query->selectRaw( + $settlePeriod . ' AS settle_period, ' + . "COALESCE(TRIM(store_name), '') AS store_name, " + . 'COALESCE(SUM(income), 0) AS income, ' + . 'COALESCE(SUM(expense), 0) AS expense' + )->groupByRaw($settlePeriod . ', store_name'); + + return $this->normalizeSettlementDailyRows($query->cursor(), $end); } + $query->where('occurred_at', '>=', $from) + ->where('occurred_at', '<', $until); + $query->selectRaw( 'DATE(occurred_at) AS occurred_at, ' . "COALESCE(TRIM(store_name), '') AS store_name, " @@ -816,6 +825,38 @@ class StoreReceivingBalanceOverviewService } } + /** + * 唯品会按结算月份记账,月结算净发生额统一落入月末, + * 查询截止日早于月末时落入查询截止日。 + * + * @return iterable + */ + private function normalizeSettlementDailyRows(iterable $rows, string $end): iterable + { + foreach ($rows as $row) { + $settlementMonth = trim((string) ($row->settle_period ?? '')); + if (preg_match('/^\d{4}-\d{2}$/', $settlementMonth) !== 1) { + continue; + } + + $month = CarbonImmutable::createFromFormat( + '!Y-m', + $settlementMonth, + 'Asia/Shanghai', + ); + if (! $month instanceof CarbonImmutable || $month->format('Y-m') !== $settlementMonth) { + continue; + } + + yield [ + 'store_name' => (string) ($row->store_name ?? ''), + 'occurred_at' => min($month->endOfMonth()->format('Y-m-d'), $end), + 'income' => (float) ($row->income ?? 0), + 'expense' => (float) ($row->expense ?? 0), + ]; + } + } + /** @return list */ private function dateRange(string $start, string $end): array { diff --git a/tests/Unit/EcommerceFinance/StoreReceivingBalanceOverviewServiceTest.php b/tests/Unit/EcommerceFinance/StoreReceivingBalanceOverviewServiceTest.php index aa0163f9f..2a776c9e3 100644 --- a/tests/Unit/EcommerceFinance/StoreReceivingBalanceOverviewServiceTest.php +++ b/tests/Unit/EcommerceFinance/StoreReceivingBalanceOverviewServiceTest.php @@ -502,6 +502,33 @@ class StoreReceivingBalanceOverviewServiceTest extends TestCase $this->assertSame(0, $result['summary']['available_store_count']); } + public function test_vipshop_settlement_activity_is_booked_on_month_end_or_selected_end(): void + { + $service = new class extends StoreReceivingBalanceOverviewService { + public function __construct() + { + } + }; + $method = new \ReflectionMethod($service, 'normalizeSettlementDailyRows'); + $method->setAccessible(true); + $rows = [ + (object) [ + 'settle_period' => '2026-10', + 'store_name' => '唯品会店铺', + 'income' => 120.0, + 'expense' => 20.0, + ], + ]; + + $monthEnd = iterator_to_array($method->invoke($service, $rows, '2026-10-31')); + $selectedEnd = iterator_to_array($method->invoke($service, $rows, '2026-10-20')); + + $this->assertSame('2026-10-31', $monthEnd[0]['occurred_at']); + $this->assertSame(120.0, $monthEnd[0]['income']); + $this->assertSame(20.0, $monthEnd[0]['expense']); + $this->assertSame('2026-10-20', $selectedEnd[0]['occurred_at']); + } + private function service(callable $summary): StoreReceivingBalanceOverviewService { return new class($summary) extends StoreReceivingBalanceOverviewService {