shish 1 개월 전
부모
커밋
2815cfe48c

+ 107 - 3
biz-ghs/custom/classes/CustomBalanceChangeClass.php

@@ -2,6 +2,7 @@
 
 namespace bizGhs\custom\classes;
 
+use bizGhs\ghs\classes\GhsRechargeClass;
 use bizGhs\order\classes\OrderClearClass;
 use bizHd\purchase\classes\PurchaseClearClass;
 use common\components\dict;
@@ -63,12 +64,17 @@ class CustomBalanceChangeClass extends BaseClass
             return self::attachClearMetaFromClearId($row, $relateId);
         }
 
-        if (!self::isRechargeCapitalType($row['capitalType'] ?? 0)) {
+        if (!self::isRechargeCapitalType($row['capitalType'] ?? 0)
+            && !self::isGhsSideRechargeCapitalType($row['capitalType'] ?? 0)) {
             return $row;
         }
 
-        $recharge = CustomRechargeClass::getById($relateId, true);
+        $recharge = self::resolveCustomRechargeForBalanceRow($row);
         if (!empty($recharge)) {
+            $customId = intval(is_object($recharge) ? ($recharge->customId ?? 0) : ($recharge['customId'] ?? 0));
+            if ($customId > 0) {
+                $row['customId'] = $customId;
+            }
             $clearId = intval(is_object($recharge) ? ($recharge->clearId ?? 0) : ($recharge['clearId'] ?? 0));
             if ($clearId > 0) {
                 $row['clearId'] = $clearId;
@@ -78,10 +84,91 @@ class CustomBalanceChangeClass extends BaseClass
             }
         }
 
-        // 选订单销账:充值单未落库 clearId 时,按客户+金额+时间匹配已付结账单
+        // 花店 xhGhsBalanceChange 禁止按金额模糊匹配,避免串到其它结账单
+        if (self::isFloristGhsBalanceChangeRow($row)) {
+            return $row;
+        }
+
+        // 供货商侧 CustomBalanceChange:充值单未落库 clearId 时再按金额+时间匹配
         return self::fillClearAuditByPaidClearMatch($row, $recharge ?? null);
     }
 
+    /**
+     * 供货商帮充:CustomBalanceChange.relateId=CustomRecharge.id;
+     * 花店线上/帮充的 GhsBalanceChange.relateId=GhsRecharge.id(capitalType 常为 66 线上充)。
+     */
+    protected static function resolveCustomRechargeForBalanceRow(array $row)
+    {
+        $relateId = intval($row['relateId'] ?? 0);
+        if ($relateId <= 0) {
+            return null;
+        }
+        $capitalType = intval($row['capitalType'] ?? 0);
+
+        if (self::isFloristGhsBalanceChangeRow($row) || self::isGhsSideRechargeCapitalType($capitalType)) {
+            $ghsRecharge = GhsRechargeClass::getById($relateId, true);
+            if (!empty($ghsRecharge)) {
+                return self::findCustomRechargeByGhsRecharge($ghsRecharge);
+            }
+        }
+        if (self::isRechargeCapitalType($capitalType)) {
+            $customRecharge = CustomRechargeClass::getById($relateId, true);
+            if (!empty($customRecharge)) {
+                $linkedGhsRechargeId = intval(is_object($customRecharge)
+                    ? ($customRecharge->ghsRechargeId ?? 0)
+                    : ($customRecharge['ghsRechargeId'] ?? 0));
+                if ($linkedGhsRechargeId <= 0 || $linkedGhsRechargeId !== $relateId) {
+                    return $customRecharge;
+                }
+            }
+            $ghsRecharge = GhsRechargeClass::getById($relateId, true);
+            if (!empty($ghsRecharge)) {
+                return self::findCustomRechargeByGhsRecharge($ghsRecharge);
+            }
+            return $customRecharge ?? null;
+        }
+        return null;
+    }
+
+    /** 花店端 xhGhsBalanceChange:有 ghsId、无 customId */
+    protected static function isFloristGhsBalanceChangeRow(array $row)
+    {
+        return intval($row['ghsId'] ?? 0) > 0 && intval($row['customId'] ?? 0) <= 0;
+    }
+
+    protected static function findCustomRechargeByGhsRecharge($ghsRecharge)
+    {
+        if (empty($ghsRecharge)) {
+            return null;
+        }
+        $ghsRechargeId = intval(is_object($ghsRecharge) ? ($ghsRecharge->id ?? 0) : ($ghsRecharge['id'] ?? 0));
+        if ($ghsRechargeId > 0) {
+            $probe = new \bizGhs\custom\models\CustomRecharge();
+            if (method_exists($probe, 'hasAttribute') && $probe->hasAttribute('ghsRechargeId')) {
+                $linked = CustomRechargeClass::getByCondition(['ghsRechargeId' => $ghsRechargeId], true, null, 'id DESC');
+                if (!empty($linked)) {
+                    return $linked;
+                }
+            }
+        }
+        $customId = intval(is_object($ghsRecharge) ? ($ghsRecharge->customId ?? 0) : ($ghsRecharge['customId'] ?? 0));
+        $ghsId = intval(is_object($ghsRecharge) ? ($ghsRecharge->ghsId ?? 0) : ($ghsRecharge['ghsId'] ?? 0));
+        $amount = bcadd((string)(is_object($ghsRecharge) ? ($ghsRecharge->amount ?? 0) : ($ghsRecharge['amount'] ?? 0)), '0', 2);
+        if ($customId <= 0 || $ghsId <= 0 || bccomp($amount, '0', 2) <= 0) {
+            return null;
+        }
+        $where = [
+            'customId' => $customId,
+            'ghsId' => $ghsId,
+            'amount' => $amount,
+        ];
+        $payTime = is_object($ghsRecharge) ? ($ghsRecharge->payTime ?? '') : ($ghsRecharge['payTime'] ?? '');
+        if (!empty($payTime) && $payTime !== '0000-00-00 00:00:00') {
+            $where['payTime'] = $payTime;
+        }
+        return CustomRechargeClass::getByCondition($where, true, null, 'id DESC');
+    }
+
     /** 写入流水/充值单时带上结账单(有字段才写,对照 hd 充值即带 settleId) */
     public static function mergeClearAuditIntoRowData(array $data, array $audit)
     {
@@ -141,6 +228,12 @@ class CustomBalanceChangeClass extends BaseClass
     protected static function fillClearAuditByPaidClearMatch(array $row, $recharge = null)
     {
         $customId = intval($row['customId'] ?? 0);
+        if ($customId <= 0 && !empty($recharge)) {
+            $customId = intval(is_object($recharge) ? ($recharge->customId ?? 0) : ($recharge['customId'] ?? 0));
+            if ($customId > 0) {
+                $row['customId'] = $customId;
+            }
+        }
         if ($customId <= 0) {
             return $row;
         }
@@ -211,6 +304,17 @@ class CustomBalanceChangeClass extends BaseClass
         return in_array($capitalType, $ids, true);
     }
 
+    /** 花店侧 xhGhsBalanceChange:relateId 为 GhsRecharge.id */
+    protected static function isGhsSideRechargeCapitalType($capitalType)
+    {
+        $capitalType = intval($capitalType);
+        $ids = [
+            dict::getDict('capitalType', 'customAskGhsRecharge', 'id'),
+            dict::getDict('capitalType', 'customAskGhsRechargeReturn', 'id'),
+        ];
+        return in_array($capitalType, $ids, true);
+    }
+
     /** 结账类入账(relateId=结账单 id) */
     protected static function isClearIncomeCapitalType($capitalType)
     {

+ 7 - 0
biz-ghs/custom/services/GhsRechargeSettleService.php

@@ -363,7 +363,14 @@ class GhsRechargeSettleService
 
         if (!empty($ghsRecharge)) {
             $gRelateId = $ghsRecharge->id ?? 0;
+            $ghsRowId = is_object($ghs) ? intval($ghs->id ?? 0) : 0;
             self::patchBalanceChangeRow(GhsBalanceChangeClass::class, $gRelateId, $clearId, $clearSn, $clearAmount, [], $finalGhsBalance);
+            if ($ghsRowId > 0) {
+                self::patchBalanceChangeRow(GhsBalanceChangeClass::class, 0, $clearId, $clearSn, $clearAmount, [
+                    'ghsId' => $ghsRowId,
+                    'amount' => self::money($ghsRecharge->amount ?? 0),
+                ], $finalGhsBalance);
+            }
             if ($finalGhsBalance !== null) {
                 $ghsRecharge->balance = $finalGhsBalance;
                 $ghsRecharge->save(false, ['balance']);

+ 60 - 1
biz-hd/balance/classes/BalanceChangeClass.php

@@ -2,6 +2,9 @@
 
 namespace bizHd\balance\classes;
 
+use bizHd\order\classes\SettleClass;
+use bizHd\recharge\classes\RechargeClass;
+use common\components\dict;
 use Yii;
 use bizHd\base\classes\BaseClass;
 
@@ -15,6 +18,8 @@ class BalanceChangeClass extends BaseClass
         $data = self::getList('*', $where, 'addTime DESC,id DESC');
         if (!empty($data['list'])) {
             foreach ($data['list'] as $k => $v) {
+                $v = is_array($v) ? $v : (array)$v;
+                $v = self::fillSettleAuditFromRecharge($v);
                 $settleAmount = $v['settleAmount'] ?? 0;
                 $amount = $v['amount'] ?? 0;
                 $io = $v['io'] ?? 0;
@@ -23,10 +28,64 @@ class BalanceChangeClass extends BaseClass
                     $becomeBalance = bcsub($amount, $settleAmount, 2);
                     $becomeBalance = floatval($becomeBalance);
                 }
-                $data['list'][$k]['becomeBalance'] = $becomeBalance;
+                $v['becomeBalance'] = $becomeBalance;
+                $data['list'][$k] = $v;
             }
         }
         return $data;
     }
 
+    /**
+     * 零售会员充值流水补全结账单(对照 ghs CustomBalanceChangeClass::fillClearAuditFromRecharge)
+     */
+    public static function fillSettleAuditFromRecharge($row)
+    {
+        if (empty($row) || !is_array($row)) {
+            return $row;
+        }
+        if (intval($row['settleId'] ?? 0) > 0) {
+            return self::normalizeSettleAuditRow($row);
+        }
+        if (intval($row['io'] ?? 0) !== 1) {
+            return $row;
+        }
+        $relateId = intval($row['relateId'] ?? 0);
+        if ($relateId <= 0 || !self::isRechargeCapitalType($row['capitalType'] ?? 0)) {
+            return $row;
+        }
+        $recharge = RechargeClass::getById($relateId, true);
+        if (!empty($recharge)) {
+            $settleId = intval(is_object($recharge) ? ($recharge->settleId ?? 0) : ($recharge['settleId'] ?? 0));
+            if ($settleId > 0) {
+                $row['settleId'] = $settleId;
+                $settle = SettleClass::getById($settleId, true);
+                if (!empty($settle)) {
+                    $row['settleNo'] = is_object($settle) ? ($settle->orderSn ?? '') : ($settle['orderSn'] ?? '');
+                    $row['settleAmount'] = is_object($settle) ? ($settle->actPrice ?? 0) : ($settle['actPrice'] ?? 0);
+                }
+                return self::normalizeSettleAuditRow($row);
+            }
+        }
+        return $row;
+    }
+
+    protected static function normalizeSettleAuditRow(array $row)
+    {
+        $settleId = intval($row['settleId'] ?? 0);
+        if ($settleId > 0 && (empty($row['settleNo']) || bccomp((string)($row['settleAmount'] ?? 0), '0', 2) <= 0)) {
+            $settle = SettleClass::getById($settleId, true);
+            if (!empty($settle)) {
+                $row['settleNo'] = is_object($settle) ? ($settle->orderSn ?? '') : ($settle['orderSn'] ?? '');
+                $row['settleAmount'] = is_object($settle) ? ($settle->actPrice ?? 0) : ($settle['actPrice'] ?? 0);
+            }
+        }
+        return $row;
+    }
+
+    protected static function isRechargeCapitalType($capitalType)
+    {
+        $capitalType = intval($capitalType);
+        return $capitalType === intval(dict::getDict('capitalType', 'xhRecharge', 'id'));
+    }
+
 }