diff --git a/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php b/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php index 73d3b864..87ecdf7e 100644 --- a/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php +++ b/modules/EcommerceFinance/Services/StoreReceivingBalanceOverviewService.php @@ -738,25 +738,17 @@ class StoreReceivingBalanceOverviewService $settlePeriod = "JSON_UNQUOTE(JSON_EXTRACT(payload, '$.settle_period'))"; $query->whereRaw($settlePeriod . ' >= ?', [substr($start, 0, 7)]) ->whereRaw($settlePeriod . ' <= ?', [substr($end, 0, 7)]); - - $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); + } else { + $query->where('occurred_at', '>=', $from) + ->where('occurred_at', '<', $until); } - $query->where('occurred_at', '>=', $from) - ->where('occurred_at', '<', $until) - ->selectRaw( - 'DATE(occurred_at) AS occurred_at, ' - . "COALESCE(TRIM(store_name), '') AS store_name, " - . 'COALESCE(SUM(income), 0) AS income, ' - . 'COALESCE(SUM(expense), 0) AS expense' - )->groupByRaw('DATE(occurred_at), store_name'); + $query->selectRaw( + 'DATE(occurred_at) AS occurred_at, ' + . "COALESCE(TRIM(store_name), '') AS store_name, " + . 'COALESCE(SUM(income), 0) AS income, ' + . 'COALESCE(SUM(expense), 0) AS expense' + )->groupByRaw('DATE(occurred_at), store_name'); return $this->normalizeDailyRows($query->cursor()); } @@ -809,37 +801,6 @@ class StoreReceivingBalanceOverviewService } } - /** - * Vipshop rows carry a settlement month rather than a usable occurrence - * date for balance reporting. Book the month's movement on its final day, - * or on the selected end date when that precedes the month end. - * - * @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); - if (! $month instanceof CarbonImmutable || $month->format('Y-m') !== $settlementMonth) { - continue; - } - - $monthEnd = $month->endOfMonth()->format('Y-m-d'); - - yield [ - 'store_name' => (string) ($row->store_name ?? ''), - 'occurred_at' => min($monthEnd, $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 86167171..3f269748 100644 --- a/tests/Unit/EcommerceFinance/StoreReceivingBalanceOverviewServiceTest.php +++ b/tests/Unit/EcommerceFinance/StoreReceivingBalanceOverviewServiceTest.php @@ -495,33 +495,6 @@ 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 {