shish hace 1 día
padre
commit
9dab77a082
Se han modificado 1 ficheros con 41 adiciones y 3 borrados
  1. 41 3
      biz-ghs/custom/classes/CustomClass.php

+ 41 - 3
biz-ghs/custom/classes/CustomClass.php

@@ -1260,29 +1260,44 @@ class CustomClass extends BaseClass
      * @param string|float $returnAmount 本次返充金额(正数)
      * @return string 返充后的客户最新余额
      */
+    /**
+     * 冲销单返充客户余额:双边镜像记账。
+     * xhGhsCustom(供货商侧的客户档案)与 xhGhs(花店侧对应该供货商的档案,通过 custom.ghsId 关联)
+     * 是同一笔往来账的两份存档,净 balance 必须同步增加,且各自记一条余额变动流水。
+     * 做法与 CustomClass::rechargeBalance() 中 rechargeType=1(售后返充)保持一致,
+     * 区别在于冲销单场景不生成 xhGhsRecharge/xhGhsCustomRecharge 充值单据,只做余额与流水。
+     */
     public static function forwardReturnBalance($custom, $forwardOrder, $returnAmount)
     {
-        AccountMoneyClass::ensureCustomMoneyReady($custom, true);
         if (bccomp($returnAmount, '0', 2) <= 0) {
             return $custom->balance ?? '0.00';
         }
+
+        // 通过 custom.ghsId 反查花店侧的供货商档案(xhGhs),并加锁,保证与 custom 一起原子更新
+        $ghsId = $custom->ghsId ?? 0;
+        $ghs = GhsClass::getLockById($ghsId);
+        if (empty($ghs)) {
+            util::fail('没有找到供货商信息,无法返充余额');
+        }
+
         $newBalance = bcadd($custom->balance ?? '0.00', $returnAmount, 2);
         $custom->balance = $newBalance;
         $custom->isDebt = bccomp($newBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
         $custom->save(false, ['balance', 'isDebt']);
 
         $forwardOrderSn = $forwardOrder->orderSn ?? '';
+        $event = "冲销单号:{$forwardOrderSn} 售后返充";
         $change = [
             'relateId' => $forwardOrder->id ?? 0,
             'customId' => $custom->id ?? 0,
             'customName' => $custom->name ?? '',
             'ptStyle' => dict::getDict('ptStyle', 'ghs'),
-            'capitalType' => dict::getDict('capitalType', 'saleRefund', 'id'),
+            'capitalType' => dict::getDict('capitalType', 'ghsHelpCustomRechargeReturn', 'id'),
             'amount' => $returnAmount,
             'balance' => $newBalance,
             'io' => 1,
             'payWay' => 0,
-            'event' => "冲销单号:{$forwardOrderSn} 退款",
+            'event' => $event,
             'sjId' => $forwardOrder->sjId ?? 0,
             'shopId' => $forwardOrder->shopId ?? 0,
             'mainId' => $forwardOrder->mainId ?? 0,
@@ -1295,6 +1310,29 @@ class CustomClass extends BaseClass
         ];
         CustomBalanceChangeClass::add($change, true);
 
+        // 花店侧 xhGhs 净 balance 同步增加 + 单独记一条流水,保持与 custom 两边一致
+        $newGhsBalance = bcadd($ghs->balance ?? '0.00', $returnAmount, 2);
+        $ghs->balance = $newGhsBalance;
+        $ghs->save(false, ['balance']);
+
+        GhsBalanceChangeClass::add([
+            'ghsId' => $ghs->id ?? 0,
+            'ghsName' => $ghs->name ?? '',
+            'relateId' => $forwardOrder->id ?? 0,
+            'ptStyle' => dict::getDict('ptStyle', 'ghs'),
+            'capitalType' => dict::getDict('capitalType', 'customAskGhsRechargeReturn', 'id'),
+            'amount' => $returnAmount,
+            'balance' => $newGhsBalance,
+            'io' => 1,
+            'side' => 0,
+            'payWay' => 0,
+            'fromType' => dict::getDict('fromType', 'shop'),
+            'event' => $event,
+            'sjId' => $custom->sjId ?? 0,
+            'shopId' => $custom->shopId ?? 0,
+            'mainId' => $custom->mainId ?? 0,
+        ], true);
+
         return $newBalance;
     }