fix: 业绩明细仅保留订单实付精确原因

This commit is contained in:
yueyang 2026-10-22 15:59:32 +08:00
parent 0893f3d8d3
commit 459844aefe
3 changed files with 158 additions and 1 deletions

View File

@ -442,6 +442,29 @@ class Pages extends Controller
// 京喜分位尾差:具体原因都没命中时,1–3 分钱按四舍五入尾差标注。
$rules['差额=分位四舍五入尾差'] = "platform = '京喜账单' AND ABS(" . $effective . ') <= 0.0301 AND ' . $notBasisMarked;
// 剩余待核实行只补充可精确对平的原因。仅有退款、优惠/冲减或小额尾差
// 只能说明差额周围存在这些项目,不能证明差额就是由它们产生,因此不再作为原因。
$plantingAdjusted = '(' . $effective . ' - (COALESCE(planting_amount, 0)'
. ' - COALESCE(paid_amount, 0) + COALESCE(discount_amount, 0) + COALESCE(offset_amount, 0)))';
$rules['订单实付含退款、优惠和冲减(金额对平)'] = "match_status = '已匹配'"
. ' AND ABS(COALESCE(refund_amount, 0)) > 0.005'
. ' AND (ABS(COALESCE(discount_amount, 0)) > 0.005 OR ABS(COALESCE(offset_amount, 0)) > 0.005)'
. ' AND ABS(' . $effective . ' - (COALESCE(refund_amount, 0)'
. ' + COALESCE(discount_amount, 0) + COALESCE(offset_amount, 0))) < 0.02'
. ' AND ' . $notBasisMarked;
$rules['订单实付含已退款部分(金额对平)'] = "match_status = '已匹配'"
. ' AND ABS(COALESCE(refund_amount, 0)) > 0.005'
. ' AND ABS(' . $effective . ' - COALESCE(refund_amount, 0)) < 0.02'
. ' AND ' . $notBasisMarked;
$rules['订单实付未减优惠和冲减(金额对平)'] = "match_status = '已匹配'"
. ' AND (ABS(COALESCE(discount_amount, 0)) > 0.005 OR ABS(COALESCE(offset_amount, 0)) > 0.005)'
. ' AND ABS(' . $effective . ' - (COALESCE(discount_amount, 0) + COALESCE(offset_amount, 0))) < 0.02'
. ' AND ' . $notBasisMarked;
$rules['订单实付含种菜收入(金额对平)'] = "match_status = '已匹配'"
. ' AND ABS(COALESCE(planting_amount, 0)) > 0.005'
. ' AND ABS(' . $plantingAdjusted . ') < 0.02'
. ' AND ' . $notBasisMarked;
return $rules;
}
@ -762,6 +785,29 @@ class Pages extends Controller
return '差额=分位四舍五入尾差';
}
$refund = (float) ($row->refund_amount ?? 0);
$hasRefund = abs($refund) > 0.005;
$hasDiscountOffset = abs($discount) > 0.005 || abs($offset) > 0.005;
if ($hasRefund && $hasDiscountOffset
&& abs($difference - ($refund + $discount + $offset)) < 0.02) {
return '订单实付含退款、优惠和冲减(金额对平)';
}
if ($hasRefund && abs($difference - $refund) < 0.02) {
return '订单实付含已退款部分(金额对平)';
}
if (
$hasDiscountOffset
&& abs($difference - ($discount + $offset)) < 0.02
) {
return '订单实付未减优惠和冲减(金额对平)';
}
$planting = (float) ($row->planting_amount ?? 0);
$plantingDifference = $planting - (float) ($row->paid_amount ?? 0)
+ $discount + $offset;
if (abs($planting) > 0.005 && abs($difference - $plantingDifference) < 0.02) {
return '订单实付含种菜收入(金额对平)';
}
// 有差额但没命中任何已知原因:标「差额待核实」,页面上不再出现"有差额但备注空白"的行。
return '差额待核实';
}

View File

@ -822,7 +822,7 @@ $navigation = [
'directory' => [
'icon' => 'receipt_long',
'status' => '共享镜像',
'description' => '覆盖店铺收款账户账单各平台页签(总账备注=订单收入),按「店铺 + 订单号」匹配订单侧商品金额与订单实付金额;一行 = 一个店铺的一个订单,商品金额只挂一次。「商品金额」按「主单(拆单子单不参与)→ 原单行(merge_so_id 为空)→ 全部行」优先级取数,合单发货(原单与合并行共用同一线上单号、没有主单)时只取本单原单行,避免把合并的另一单算进来;并排除虚拟条码(X0000x:信誉订单 / 虚拟订单)明细行;「订单实付金额」取订单级实付(同一套优先级,末档兜底取该单号下最大值)再减去虚拟条码行实付。「优惠金额」「冲减金额」另取总账备注=优惠收入 / 冲减收入的账单金额,不计入收入金额;「退款金额」另取总账备注=退款的账单金额,只单独列示,不计入收入金额、也不参与差额;「种菜金额」是订单号在种菜明细表或聚水潭订单标签明细(是否种菜订单=是)任意一边出现过时,该订单的收入金额(两个条件是或的关系),只是收入金额里的一部分,不参与差额;差额=(收入金额+优惠金额+冲减金额)−订单实付金额,匹配状态与差额异常备注同样按订单实付金额判定,未匹配到订单实付金额的行按订单实付金额=0 计入差额,只反映真正对不上的部分。挂不到订单行的优惠/冲减会补成一行(收入/商品留空)。「差额异常备注」只说明差额为什么产生,按顺序自动判定:订单实付未匹配(未匹配到订单实付金额,具体原因看「匹配原因」列);订单实付取数为0(种菜)(订单实付=0 且是种菜单)、订单实付取数为0(订单实付=0 但不是种菜单)、订单实付未减联合营销费(仅得物负差额:联合营销费未让利尾数对上才标注)、订单实付取供货价(仅京喜正差额:差额=该单直营服务费(供货模式净收)时标注)、差额=直营补贴款(待开票结算)(仅京喜账单·京喜自营工厂店负差额且无退款(出类拔萃等 10 家已逐店核验,其余工厂店按实付≈商品金额的供货价特征识别):京东直营补贴款待开票结算时标注)、差额=直营补贴款(订单退款已冲回)(同批店负差额但订单已退款:补贴款已随退款冲回)、差额=订单实付包含商家承担优惠和以旧换新国补(仅京东账单·索菲亚客厅/办公/书房/功能、亿家达全屋、朴美家居店负差额、且该单含国补(聚水潭应付−账单收入>0):差额为商家承担优惠(店铺东券/单品折扣等)与以旧换新国补之和,已用营销对账逐单核实)、差额=订单实付包含商家承担优惠(同店负差额但不含国补的单:差额即商家承担优惠)、订单实付未减冲减(订单实付=收入+优惠 → 差额=冲减金额)、订单实付未减优惠(订单实付=收入+冲减 → 差额=优惠金额)、订单实付未减优惠和冲减(订单实付=收入 → 差额=优惠+冲减)(以上三档等式对带口径标识的行让位给口径兜底)、订单实付含已退款部分(抖音负差额:聚水潭退款单/取消子单金额≈差额,或退款合计+账单退款净额≈实付−收入,或整单全退(退款合计≈实付)对上才标注;淘系支付宝/微信负差额:差额=退款合计、或退款合计=实付−收入(收款缺口)、或整单全退(退款合计≈实付)时同样标注(退款取聚水潭已确认记录));都不满足时,若「卖家实收」或「应付/已付」能对上账单,差额按 0 计并在「匹配原因」列标注口径核对;其余标「差额待核实」。「匹配原因」单独一列,取值:账单无单号 / 账单无单号(采销收款)/ 单号格式异常 / 订单表无此单 / 1688采销收款(非销售单)/ 订单无实付金额 / 无收款账单(「采销收款」两档专指「支付宝-1688采销专用」账户:该账户收的是采销/供货结算款,不是店铺销售单收款,不按丢单处理)。',
'description' => '覆盖店铺收款账户账单各平台页签(总账备注=订单收入),按「店铺 + 订单号」匹配订单侧商品金额与订单实付金额;一行 = 一个店铺的一个订单,商品金额只挂一次。「商品金额」按「主单(拆单子单不参与)→ 原单行(merge_so_id 为空)→ 全部行」优先级取数,合单发货(原单与合并行共用同一线上单号、没有主单)时只取本单原单行,避免把合并的另一单算进来;并排除虚拟条码(X0000x:信誉订单 / 虚拟订单)明细行;「订单实付金额」取订单级实付(同一套优先级,末档兜底取该单号下最大值)再减去虚拟条码行实付。「优惠金额」「冲减金额」另取总账备注=优惠收入 / 冲减收入的账单金额,不计入收入金额;「退款金额」另取总账备注=退款的账单金额,只单独列示,不计入收入金额、也不参与差额;「种菜金额」是订单号在种菜明细表或聚水潭订单标签明细(是否种菜订单=是)任意一边出现过时,该订单的收入金额(两个条件是或的关系),只是收入金额里的一部分,不参与差额;差额=(收入金额+优惠金额+冲减金额)−订单实付金额,匹配状态与差额异常备注同样按订单实付金额判定,未匹配到订单实付金额的行按订单实付金额=0 计入差额,只反映真正对不上的部分。挂不到订单行的优惠/冲减会补成一行(收入/商品留空)。「差额异常备注」只说明差额为什么产生,按顺序自动判定:订单实付未匹配(未匹配到订单实付金额,具体原因看「匹配原因」列);订单实付取数为0(种菜)(订单实付=0 且是种菜单)、订单实付取数为0(订单实付=0 但不是种菜单)、订单实付未减联合营销费(仅得物负差额:联合营销费未让利尾数对上才标注)、订单实付取供货价(仅京喜正差额:差额=该单直营服务费(供货模式净收)时标注)、差额=直营补贴款(待开票结算)(仅京喜账单·京喜自营工厂店负差额且无退款(出类拔萃等 10 家已逐店核验,其余工厂店按实付≈商品金额的供货价特征识别):京东直营补贴款待开票结算时标注)、差额=直营补贴款(订单退款已冲回)(同批店负差额但订单已退款:补贴款已随退款冲回)、差额=订单实付包含商家承担优惠和以旧换新国补(仅京东账单·索菲亚客厅/办公/书房/功能、亿家达全屋、朴美家居店负差额、且该单含国补(聚水潭应付−账单收入>0):差额为商家承担优惠(店铺东券/单品折扣等)与以旧换新国补之和,已用营销对账逐单核实)、差额=订单实付包含商家承担优惠(同店负差额但不含国补的单:差额即商家承担优惠)、订单实付未减冲减(订单实付=收入+优惠 → 差额=冲减金额)、订单实付未减优惠(订单实付=收入+冲减 → 差额=优惠金额)、订单实付未减优惠和冲减(订单实付=收入 → 差额=优惠+冲减)(以上三档等式对带口径标识的行让位给口径兜底)、订单实付含已退款部分(抖音负差额:聚水潭退款单/取消子单金额≈差额,或退款合计+账单退款净额≈实付−收入,或整单全退(退款合计≈实付)对上才标注;淘系支付宝/微信负差额:差额=退款合计、或退款合计=实付−收入(收款缺口)、或整单全退(退款合计≈实付)时同样标注(退款取聚水潭已确认记录));以上具体规则均未命中时,仅在差额与「退款金额」「优惠+冲减」「退款+优惠+冲减」或「种菜收入−订单实付+优惠/冲减」在 2 分钱内精确对平时,才标注对应的「金额对平」原因;只有退款、优惠/冲减或 1 元内尾差共现但金额对不上时不判为原因,继续标「差额待核实」。都不满足时,若「卖家实收」或「应付/已付」能对上账单,差额按 0 计并在「匹配原因」列标注口径核对;其余标「差额待核实」。「匹配原因」单独一列,取值:账单无单号 / 账单无单号(采销收款)/ 单号格式异常 / 订单表无此单 / 1688采销收款(非销售单)/ 订单无实付金额 / 无收款账单(「采销收款」两档专指「支付宝-1688采销专用」账户:该账户收的是采销/供货结算款,不是店铺销售单收款,不按丢单处理)。',
'empty' => '店铺收款匹配订单明细尚未同步,请先执行 store-receipt:order-match:sync。',
'fields' => [
['name' => 'platform', 'label' => '平台'],

View File

@ -8,6 +8,16 @@ use Tests\TestCase;
class PerformanceDifferenceRemarkTest extends TestCase
{
private function remarkLabels(): array
{
return (new ReflectionMethod(Pages::class, 'receiptDifferenceRemarkLabels'))->invoke(null);
}
private function baseRules(): array
{
return (new ReflectionMethod(Pages::class, 'receiptDifferenceRemarkBaseRules'))->invoke(null);
}
private function remark(array $attributes): ?string
{
$row = (object) array_merge([
@ -68,4 +78,105 @@ class PerformanceDifferenceRemarkTest extends TestCase
'difference' => 30.00,
]));
}
public function test_combination_rules_are_available_to_page_and_sql(): void
{
$labels = $this->remarkLabels();
$rules = $this->baseRules();
foreach (
[
'订单实付含已退款部分(金额对平)',
'订单实付未减优惠和冲减(金额对平)',
'订单实付含退款、优惠和冲减(金额对平)',
'订单实付含种菜收入(金额对平)',
] as $label
) {
$this->assertContains($label, $labels);
$this->assertArrayHasKey($label, $rules);
}
$this->assertStringContainsString('planting_amount', $rules['订单实付含种菜收入(金额对平)']);
$this->assertStringContainsString('refund_amount', $rules['订单实付含已退款部分(金额对平)']);
$this->assertStringContainsString('discount_amount', $rules['订单实付未减优惠和冲减(金额对平)']);
$this->assertStringContainsString('offset_amount', $rules['订单实付未减优惠和冲减(金额对平)']);
$this->assertStringContainsString('offset_amount', $rules['订单实付含退款、优惠和冲减(金额对平)']);
$this->assertArrayNotHasKey('差额含已退款部分', $rules);
$this->assertArrayNotHasKey('差额含优惠/冲减项', $rules);
$this->assertArrayNotHasKey('差额=退款金额(金额对平)', $rules);
$this->assertArrayNotHasKey('差额=优惠+冲减(金额对平)', $rules);
$this->assertArrayNotHasKey('差额=小额尾差', $rules);
}
public function test_planting_income_requires_the_amounts_to_reconcile(): void
{
$this->assertSame('订单实付含种菜收入(金额对平)', $this->remark([
'platform' => '得物',
'income_amount' => 458.80,
'planting_amount' => 458.80,
'product_amount' => 220.00,
'paid_amount' => 120.80,
'difference' => 338.00,
]));
}
public function test_refund_difference_requires_the_refund_amount_to_reconcile(): void
{
$this->assertSame('订单实付含已退款部分(金额对平)', $this->remark([
'platform' => '京东账单',
'store_name' => '京东-普通旗舰店',
'income_amount' => 100.00,
'refund_amount' => -19.00,
'product_amount' => 119.00,
'paid_amount' => 119.00,
'difference' => -19.00,
]));
}
public function test_discount_or_offset_difference_requires_the_amounts_to_reconcile(): void
{
$this->assertSame('订单实付未减优惠和冲减(金额对平)', $this->remark([
'platform' => '得物',
'income_amount' => 100.00,
'discount_amount' => -10.00,
'offset_amount' => -29.00,
'product_amount' => 150.00,
'paid_amount' => 129.00,
'difference' => -39.00,
]));
}
public function test_refund_discount_combination_requires_all_amounts_to_reconcile(): void
{
$this->assertSame('订单实付含退款、优惠和冲减(金额对平)', $this->remark([
'platform' => '得物',
'income_amount' => 100.00,
'discount_amount' => -10.00,
'refund_amount' => -19.00,
'product_amount' => 129.00,
'paid_amount' => 129.00,
'difference' => -29.00,
]));
}
public function test_refund_or_discount_cooccurrence_alone_stays_pending(): void
{
$this->assertSame('差额待核实', $this->remark([
'platform' => '得物',
'income_amount' => 100.00,
'discount_amount' => -10.00,
'refund_amount' => -19.00,
'product_amount' => 150.00,
'paid_amount' => 129.00,
'difference' => -39.00,
]));
$this->assertSame('差额待核实', $this->remark([
'platform' => '得物',
'income_amount' => 99.50,
'product_amount' => 100.00,
'paid_amount' => 100.00,
'difference' => -0.50,
]));
}
}