瀏覽代碼

Merge branch 'clear-cg' into dev

shish 2 月之前
父節點
當前提交
e103008b30

+ 7 - 0
biz-ghs/clear/classes/OrderCgClearClass.php

@@ -242,8 +242,15 @@ class OrderCgClearClass extends BaseClass
         return 0;
     }
 
+    /**
+     * 待结金额:销售单/采购单用 remainDebtPrice(与 hd 充值销账 FIFO 一致),勿用 actPrice(实付会小于待结)
+     */
     protected static function resolveDebtAmount($entity)
     {
+        $remain = self::readField($entity, 'remainDebtPrice');
+        if ($remain !== '' && $remain !== null && bccomp(self::normalizeAmount($remain), '0', 2) > 0) {
+            return self::normalizeAmount($remain);
+        }
         return self::normalizeAmount(self::readField($entity, 'actPrice'));
     }
 

+ 13 - 1
biz-ghs/custom/services/CustomPaymentAllocateService.php

@@ -44,6 +44,9 @@ class CustomPaymentAllocateService
         $custom = $pair['custom'];
         $ghs = $pair['ghs'];
 
+        // 作废待付结账单,避免充值后误确认旧单(实付130+优惠106 与本次充值100 混在一起)
+        OrderClearClass::expireAwaitPayClears($custom->id ?? 0);
+
         // 2. 来款入账(balance += 本次充值额)
         $respond = CustomClass::rechargeBalance($custom, $amount, $shop, $staff, $payWay, $params);
         $custom = $respond['custom'] ?? $custom;
@@ -76,6 +79,7 @@ class CustomPaymentAllocateService
         $custom = $pair['custom'];
         $ghs = $pair['ghs'];
 
+        OrderClearClass::expireAwaitPayClears($custom->id ?? 0);
         self::syncDebtFlags($custom, $ghs);
 
         // thirdPay 已入账,此处只按「本次支付金额」销挂账,勿在 NoticeController 再调 useBalanceClear
@@ -128,7 +132,11 @@ class CustomPaymentAllocateService
         ]);
 
         $realClearAmount = OrderClearClass::sumClearAmountByClearId($clear->id ?? 0);
-        // 从净余额扣掉实销金额(替代旧逻辑里对整单 actPrice 的 consume + clearDebt 双计)
+        $payCap = bcadd((string)($clear->actPrice ?? '0'), '0', 2);
+        if (bccomp($realClearAmount, $payCap, 2) > 0) {
+            $realClearAmount = $payCap;
+        }
+        // 从净余额扣掉实销金额(不超过结账单 actPrice,兼容手工抹零结账单)
         if (bccomp($realClearAmount, '0', 2) > 0) {
             $custom = CustomClass::getLockById($custom->id ?? 0);
             $ghs = BizGhsClass::getLockById($ghs->id ?? 0);
@@ -331,6 +339,10 @@ class CustomPaymentAllocateService
         ]);
 
         $realClearAmount = OrderClearClass::sumClearAmountByClearId($clear->id ?? 0);
+        $poolAmount = bcadd((string)$poolAmount, '0', 2);
+        if (bccomp($realClearAmount, $poolAmount, 2) > 0) {
+            util::fail('销账金额超过本次来款' . floatval($poolAmount) . '元,请检查订单待结或联系技术员');
+        }
 
         // 入账后须把「用于销单」的部分从 balance 扣回,净余额才与订单状态一致
         if (bccomp($realClearAmount, '0', 2) > 0) {

+ 31 - 6
biz-ghs/order/classes/OrderClearClass.php

@@ -107,10 +107,15 @@ class OrderClearClass extends BaseClass
         if (empty($list)) {
             util::fail('请选择订单');
         }
-        $amount = 0;
-
-        $modifyPrice = $post['modifyPrice'] ?? 0.00;
+        $modifyPrice = bcadd((string)($post['modifyPrice'] ?? '0'), '0', 2);
 
+        $amount = '0.00';
+        if (!empty($orderAmountMap)) {
+            // 充值 FIFO 销账:prePrice=Σ每单销账额(对照 hd Settle,勿用订单全款算优惠)
+            foreach ($orderAmountMap as $mayClear) {
+                $amount = bcadd($amount, (string)$mayClear, 2);
+            }
+        }
         foreach ($list as $key => $val) {
             if ($val['sjId'] != $sjId) {
                 util::fail('只能结算自己的订单哦');
@@ -121,7 +126,9 @@ class OrderClearClass extends BaseClass
             if ($val['customId'] != $customId) {
                 util::fail('每次只能结算一个客户的订单');
             }
-            $amount = bcadd($amount, $val['remainDebtPrice'], 2);
+            if (empty($orderAmountMap)) {
+                $amount = bcadd($amount, $val['remainDebtPrice'], 2);
+            }
         }
 
         $custom = CustomClass::getLockById($customId);
@@ -201,11 +208,15 @@ class OrderClearClass extends BaseClass
             'hasNoticeCustom' => 0,
             'remark' => $remark,
         ];
-        if ($modifyPrice < $amount) {
+        if (!empty($orderAmountMap)) {
+            // 自动充值销账:结账单金额=本次来款池,不产生虚假「优惠」
+            $modifyPrice = $amount;
+        }
+        if (bccomp($modifyPrice, $amount, 2) < 0) {
             $clearData['discountAmount'] = bcsub($amount, $modifyPrice, 2);
             $clearData['discountType'] = dict::getDict('discountType', 'discount');
         }
-        if ($modifyPrice > $amount) {
+        if (bccomp($modifyPrice, $amount, 2) > 0) {
             //noticeUtil::push("结账金额大于总欠款金额 {$modifyPrice} {$amount}", '15280215347');
             util::fail('结账金额大于总欠款金额');
         }
@@ -273,6 +284,11 @@ class OrderClearClass extends BaseClass
 
         $clearTime = date("Y-m-d H:i:s");
         $totalSettled = '0.00';
+        // 手工结账单可能 actPrice < 明细合计(抹零);合并后销单以 actPrice 为本次付款上限
+        $settleBudget = bcadd((string)($clear->actPrice ?? '0'), '0', 2);
+        if ($paymentSettleOnly && bccomp($settleBudget, '0', 2) <= 0) {
+            $settleBudget = bcadd((string)($clear->prePrice ?? '0'), '0', 2);
+        }
 
         foreach ($orderSettleRows as $os) {
             $orderId = intval($os->orderId ?? 0);
@@ -281,6 +297,15 @@ class OrderClearClass extends BaseClass
             if ($orderId <= 0 || bccomp($mayClear, '0', 2) <= 0) {
                 continue;
             }
+            if ($paymentSettleOnly && bccomp($settleBudget, '0', 2) > 0) {
+                $budgetLeft = bcsub($settleBudget, $totalSettled, 2);
+                if (bccomp($budgetLeft, '0', 2) <= 0) {
+                    break;
+                }
+                if (bccomp($mayClear, $budgetLeft, 2) > 0) {
+                    $mayClear = $budgetLeft;
+                }
+            }
 
             $order = OrderClass::getById($orderId, true);
             if (empty($order) || ($order->debt ?? 0) == 0) {