fix: 修复无单号优惠被业绩明细重复写入 #1647
@ -1563,10 +1563,12 @@ class SyncStoreReceiptOrderMatches extends Command
|
||||
$attachedDiscount = 0.0;
|
||||
$attachedOffset = 0.0;
|
||||
$attachedDefaultCompensation = 0.0;
|
||||
$attachedRefund = 0.0;
|
||||
foreach ($groups as $group) {
|
||||
$attachedDiscount += (float) $group['discount_amount'];
|
||||
$attachedOffset += (float) $group['offset_amount'];
|
||||
$attachedDefaultCompensation += (float) $group['default_compensation_amount'];
|
||||
$attachedRefund += (float) $group['refund_amount'];
|
||||
}
|
||||
$unattachedRows = 0;
|
||||
foreach ($promoRowsByKey as $key => $count) {
|
||||
@ -1576,11 +1578,32 @@ class SyncStoreReceiptOrderMatches extends Command
|
||||
}
|
||||
// A 方案:挂不到订单行的优惠/冲减也补成行(收入/商品/差额留空),金额不丢。
|
||||
foreach ($promoOnlyGroups as $key => $group) {
|
||||
if ($group['order_identity'] !== '' && isset($groups[$group['shop_key'] . '|' . $group['order_identity']])) {
|
||||
$mainKey = $group['shop_key'] . '|' . $group['order_identity'];
|
||||
if (isset($groups[$mainKey])) {
|
||||
unset($promoOnlyGroups[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$promoOnlyDiscount = 0.0;
|
||||
$promoOnlyOffset = 0.0;
|
||||
$promoOnlyDefaultCompensation = 0.0;
|
||||
$promoOnlyRefund = 0.0;
|
||||
foreach ($promoOnlyGroups as $group) {
|
||||
$promoOnlyDiscount += (float) $group['discount_amount'];
|
||||
$promoOnlyOffset += (float) $group['offset_amount'];
|
||||
$promoOnlyDefaultCompensation += (float) $group['default_compensation_amount'];
|
||||
$promoOnlyRefund += (float) $group['refund_amount'];
|
||||
}
|
||||
self::assertSourceAmountConserved('优惠', $discountTotal, $attachedDiscount, $promoOnlyDiscount);
|
||||
self::assertSourceAmountConserved('冲减', $offsetTotal, $attachedOffset, $promoOnlyOffset);
|
||||
self::assertSourceAmountConserved(
|
||||
'违约赔付',
|
||||
$defaultCompensationTotal,
|
||||
$attachedDefaultCompensation,
|
||||
$promoOnlyDefaultCompensation,
|
||||
);
|
||||
self::assertSourceAmountConserved('退款', $refundTotal, $attachedRefund, $promoOnlyRefund);
|
||||
|
||||
return compact(
|
||||
'groups', 'promoOnlyGroups', 'paymentTotal', 'incomeTotal', 'discountTotal',
|
||||
'offsetTotal', 'defaultCompensationTotal', 'refundTotal', 'moneyRemarkRows',
|
||||
@ -1589,6 +1612,25 @@ class SyncStoreReceiptOrderMatches extends Command
|
||||
);
|
||||
}
|
||||
|
||||
private static function assertSourceAmountConserved(
|
||||
string $label,
|
||||
float $sourceTotal,
|
||||
float $attachedTotal,
|
||||
float $promoOnlyTotal,
|
||||
): void {
|
||||
if (abs(round($sourceTotal - $attachedTotal - $promoOnlyTotal, 2)) <= 0.005) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new RuntimeException(sprintf(
|
||||
'账单聚合金额校验失败:%s 原始 %s,主行 %s,补行 %s。',
|
||||
$label,
|
||||
number_format($sourceTotal, 2, '.', ''),
|
||||
number_format($attachedTotal, 2, '.', ''),
|
||||
number_format($promoOnlyTotal, 2, '.', ''),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* A 方案补行的入库数据:收入/商品/差额留空,只带优惠/冲减金额与原因标注。
|
||||
*
|
||||
|
||||
@ -202,7 +202,7 @@ class StoreReceiptMemoryTest extends TestCase
|
||||
$this->assertSame(['未匹配', null, '订单侧同号多条'], $ambiguous);
|
||||
}
|
||||
|
||||
public function test_empty_order_promotions_remain_separate_by_original_store(): void
|
||||
public function test_empty_order_promotion_is_folded_into_existing_main_row_and_other_promo_stays(): void
|
||||
{
|
||||
$result = $this->aggregate([
|
||||
$this->bill('订单收入', '', 12),
|
||||
@ -211,16 +211,47 @@ class StoreReceiptMemoryTest extends TestCase
|
||||
$this->bill('优惠收入', '50005', 4, 0, ['online_real_store' => '另一家店']),
|
||||
]);
|
||||
$this->assertCount(1, $result['groups']);
|
||||
$this->assertCount(2, $result['promoOnlyGroups']);
|
||||
$shop = SyncStoreReceiptOrderMatches::normalizeShop('天猫_测试店');
|
||||
$this->assertSame(2.0, $result['groups'][$shop . '|']['discount_amount']);
|
||||
$this->assertCount(1, $result['promoOnlyGroups']);
|
||||
$first = reset($result['promoOnlyGroups']);
|
||||
$this->assertSame('2026-08-03', $first['occurred_date']);
|
||||
$this->assertSame(2.0, $first['discount_amount']);
|
||||
$this->assertSame('2026-08-01', $first['occurred_date']);
|
||||
$this->assertSame(7.0, $first['discount_amount']);
|
||||
$this->assertSame(
|
||||
7.0,
|
||||
$result['promoOnlyGroups'][SyncStoreReceiptOrderMatches::normalizeShop('天猫_测试店') . '|50005']['discount_amount'],
|
||||
$result['promoOnlyGroups'][$shop . '|50005']['discount_amount'],
|
||||
);
|
||||
}
|
||||
|
||||
public function test_empty_order_promotions_and_refunds_are_not_written_as_duplicate_supplement_rows(): void
|
||||
{
|
||||
$result = $this->aggregate([
|
||||
$this->bill('优惠收入', '', 0, 3),
|
||||
$this->bill('订单收入', '', 100),
|
||||
$this->bill('冲减收入', '', 0, 2),
|
||||
$this->bill('违约赔付(冲减收入)', '', 0, 5),
|
||||
$this->bill('退款', '', 0, 7),
|
||||
]);
|
||||
|
||||
$key = SyncStoreReceiptOrderMatches::normalizeShop('天猫_测试店') . '|';
|
||||
$this->assertCount(1, $result['groups']);
|
||||
$this->assertArrayHasKey($key, $result['groups']);
|
||||
$this->assertSame(1, $result['groups'][$key]['payment_count']);
|
||||
$this->assertSame(100.0, $result['groups'][$key]['income_amount']);
|
||||
$this->assertSame(-3.0, $result['groups'][$key]['discount_amount']);
|
||||
$this->assertSame(-2.0, $result['groups'][$key]['offset_amount']);
|
||||
$this->assertSame(-5.0, $result['groups'][$key]['default_compensation_amount']);
|
||||
$this->assertSame(-7.0, $result['groups'][$key]['refund_amount']);
|
||||
$this->assertSame([], $result['promoOnlyGroups']);
|
||||
$this->assertSame(-3.0, $result['attachedDiscount']);
|
||||
$this->assertSame(-2.0, $result['attachedOffset']);
|
||||
$this->assertSame(-5.0, $result['attachedDefaultCompensation']);
|
||||
$this->assertSame(-3.0, $result['discountTotal']);
|
||||
$this->assertSame(-2.0, $result['offsetTotal']);
|
||||
$this->assertSame(-5.0, $result['defaultCompensationTotal']);
|
||||
$this->assertSame(-7.0, $result['refundTotal']);
|
||||
}
|
||||
|
||||
public function test_promo_only_row_carries_default_compensation_separately(): void
|
||||
{
|
||||
$row = (new ReflectionMethod(SyncStoreReceiptOrderMatches::class, 'promoOnlyRow'))->invoke(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user