fix(账户余额): 唯品会结算月并入月末 #1680

Open
qiuxiang wants to merge 1 commits from fix/qiuxiang-vipshop-settlement-month-end into main
3 changed files with 73 additions and 5 deletions

View File

@ -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] ?? '',

View File

@ -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<int,array{store_name:string,occurred_at:string,income:float,expense:float}>
*/
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<string> */
private function dateRange(string $start, string $end): array
{

View File

@ -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 {