Просмотр исходного кода

邀请开店仅首次续费才有减免、佣金

ouyang 4 дней назад
Родитель
Сommit
02fabd3a3f

+ 42 - 9
app-hd/controllers/ApplyController.php

@@ -3,6 +3,7 @@
 namespace hd\controllers;
 
 use biz\shop\classes\ShopClass;
+use biz\renew\classes\RenewClass;
 use biz\shop\services\ShopAdminService;
 use biz\sj\classes\SjClass;
 use bizGhs\admin\classes\AdminClass;
@@ -81,8 +82,9 @@ class ApplyController extends BaseController
     {
         $get = Yii::$app->request->get();
         $inviteCode = trim($get['inviteCode'] ?? '');
-        $data = MainInviteClass::resolveRegisterFeePreview($inviteCode);
-        util::success($data);
+        $allowInviteBenefit = $this->canUseRegisterInviteBenefit();
+        $data = MainInviteClass::resolveRegisterFeePreview($inviteCode, $allowInviteBenefit);
+        util::success($this->appendInviteBenefitFlags($data));
     }
 
     /**
@@ -117,7 +119,7 @@ class ApplyController extends BaseController
                 $data['inviteCommissionAmount'] = round((float) ($apply['inviteCommissionAmount'] ?? 0), 2);
             }
         }
-        util::success($data);
+        util::success($this->appendInviteBenefitFlags($data));
     }
 
     /**
@@ -128,14 +130,23 @@ class ApplyController extends BaseController
         if ($this->adminId <= 0 || $this->shopId <= 0) {
             util::fail('请先登录并选择门店');
         }
-        $post = Yii::$app->request->post();
-        $inviteCode = trim((string) ($post['inviteCode'] ?? ''));
+        $allowInviteBenefit = $this->canUseRegisterInviteBenefit();
         $apply = $this->findRegisterApplyForAdmin($this->adminId, $this->shopId);
         if (empty($apply)) {
             util::fail('未找到注册申请记录');
         }
         $applyId = (int) ($apply['id'] ?? 0);
-        $fee = MainInviteClass::resolveRegisterFeeSubmit($inviteCode);
+        // 已有 xhRenew 付费成功:续费不再更新邀请减免
+        if (!$allowInviteBenefit) {
+            $preview = MainInviteClass::resolveRegisterFeePreview('', false);
+            util::success($this->appendInviteBenefitFlags(array_merge($preview, [
+                'applyId' => $applyId,
+            ])));
+            return;
+        }
+        $post = Yii::$app->request->post();
+        $inviteCode = trim((string) ($post['inviteCode'] ?? ''));
+        $fee = MainInviteClass::resolveRegisterFeeSubmit($inviteCode, true);
         if ($inviteCode !== '' && !empty($fee['inviteNotFound'])) {
             util::fail('邀请人不存在');
         }
@@ -146,13 +157,13 @@ class ApplyController extends BaseController
             'hdDiscountAmount' => $hdDiscountAmount,
             'inviteCommissionAmount' => $inviteCommissionAmount,
         ]);
-        $preview = MainInviteClass::resolveRegisterFeePreview($inviteCode);
-        util::success(array_merge($preview, [
+        $preview = MainInviteClass::resolveRegisterFeePreview($inviteCode, true);
+        util::success($this->appendInviteBenefitFlags(array_merge($preview, [
             'applyId' => $applyId,
             'inviteCode' => $inviteCode,
             'hdDiscountAmount' => $hdDiscountAmount,
             'inviteCommissionAmount' => $inviteCommissionAmount,
-        ]));
+        ])));
     }
 
     //邀请花店海报 ssh 20210610
@@ -455,4 +466,26 @@ class ApplyController extends BaseController
         return $apply;
     }
 
+    /**
+     * 是否仍可使用邀请码减免与佣金(无 xhRenew 付费成功记录时为 true)
+     */
+    private function canUseRegisterInviteBenefit(): bool
+    {
+        if ($this->mainId <= 0 && $this->shopId <= 0) {
+            return true;
+        }
+        return !RenewClass::hasPaidMemberRenew((int) $this->mainId, (int) $this->shopId);
+    }
+
+    /**
+     * 补充首次付费邀请权益标记
+     */
+    private function appendInviteBenefitFlags(array $data): array
+    {
+        $available = $this->canUseRegisterInviteBenefit();
+        $data['inviteBenefitAvailable'] = $available ? 1 : 0;
+        $data['hasPaidRegisterRenew'] = $available ? 0 : 1;
+        return $data;
+    }
+
 }

+ 10 - 4
app-hd/controllers/RenewController.php

@@ -3,6 +3,7 @@
 namespace hd\controllers;
 
 use biz\set\classes\SetClass;
+use biz\renew\classes\RenewClass;
 use biz\sj\services\MerchantExtendService;
 use biz\sj\services\MerchantService;
 use bizHd\saas\classes\ApplyClass;
@@ -105,6 +106,8 @@ class RenewController extends BaseController
         $registerConfig = dict::getDict('hdRegisterConfig');
         $prePrice = round((float) ($registerConfig['price'] ?? 0), 2);
         $hdDiscountAmount = 0;
+        $attachApplyId = 0;
+        $allowInviteBenefit = !RenewClass::hasPaidMemberRenew((int) $this->mainId, (int) $this->shopId);
         if ($applyId > 0) {
             $apply = ApplyClass::getById($applyId);
             if (empty($apply)) {
@@ -113,7 +116,10 @@ class RenewController extends BaseController
             if ((int) ($apply['adminId'] ?? 0) !== (int) $this->adminId) {
                 util::fail('申请与当前账号不匹配');
             }
-            $hdDiscountAmount = round((float) ($apply['hdDiscountAmount'] ?? 0), 2);
+            if ($allowInviteBenefit) {
+                $hdDiscountAmount = round((float) ($apply['hdDiscountAmount'] ?? 0), 2);
+                $attachApplyId = $applyId;
+            }
         }
         $actPrice = round(max(0, $prePrice - $hdDiscountAmount), 2);
 
@@ -139,7 +145,7 @@ class RenewController extends BaseController
             'status' => 0,
             'type' => 1,
             'ptStyle' => dict::getDict('ptStyle', 'hd'),
-            'remark' => $applyId > 0 ? 'applyId=' . $applyId : '',
+            'remark' => $attachApplyId > 0 ? 'applyId=' . $attachApplyId : '',
             'beforeDeadline'=>$shop->beforeDeadline ?? '',
         ];
 
@@ -155,7 +161,7 @@ class RenewController extends BaseController
                 dict::getDict('payWay', 'wxPay'),
                 $orderSn,
                 $actPrice,
-                'capitalType=' . dict::getDict('capitalType', 'xhRenew', 'id') . '&applyId=' . $applyId,
+                'capitalType=' . dict::getDict('capitalType', 'xhRenew', 'id') . '&applyId=' . $attachApplyId,
                 ''
             );
             util::success([
@@ -174,7 +180,7 @@ class RenewController extends BaseController
         }
 
         $capitalType = dict::getDict('capitalType', 'xhRenew', 'id');
-        $attach = 'capitalType=' . $capitalType . '&applyId=' . $applyId . '&sjId=' . $this->sjId;
+        $attach = 'capitalType=' . $capitalType . '&applyId=' . $attachApplyId . '&sjId=' . $this->sjId;
 
         $wx = Yii::getAlias('@vendor/weixin');
         require_once $wx . '/lib/WxPay.Api.php';

+ 11 - 3
biz-hd/shop/classes/MainInviteClass.php

@@ -41,7 +41,7 @@ class MainInviteClass extends BaseClass
     /**
      * 花店注册页:会员价与邀请减免(dict + 可选邀请码)
      */
-    public static function resolveRegisterFeePreview($inviteCode = '')
+    public static function resolveRegisterFeePreview($inviteCode = '', $allowInviteBenefit = true)
     {
         $config = dict::getDict('hdRegisterConfig');
         $price = round((float)($config['price'] ?? 0), 2);
@@ -58,6 +58,10 @@ class MainInviteClass extends BaseClass
             'payAmount' => $price,
         ];
 
+        if (!$allowInviteBenefit) {
+            return $out;
+        }
+
         $code = trim((string)$inviteCode);
         if ($code === '') {
             return $out;
@@ -92,9 +96,9 @@ class MainInviteClass extends BaseClass
     /**
      * 注册提交:费用 + 邀请人 mainId + 佣金快照
      */
-    public static function resolveRegisterFeeSubmit($inviteCode = '')
+    public static function resolveRegisterFeeSubmit($inviteCode = '', $allowInviteBenefit = true)
     {
-        $preview = self::resolveRegisterFeePreview($inviteCode);
+        $preview = self::resolveRegisterFeePreview($inviteCode, $allowInviteBenefit);
         $config = dict::getDict('hdRegisterConfig');
         $configComm = round((float) ($config['commissionAmount'] ?? 0), 2);
 
@@ -103,6 +107,10 @@ class MainInviteClass extends BaseClass
         $preview['inviterMainId'] = 0;
         $preview['inviteCommissionAmount'] = 0;
 
+        if (!$allowInviteBenefit) {
+            return $preview;
+        }
+
         if (!empty($preview['inviteNotFound'])) {
             return $preview;
         }

+ 44 - 1
biz/renew/classes/RenewClass.php

@@ -7,6 +7,7 @@ use biz\shop\classes\ShopClass;
 use biz\sj\classes\SjClass;
 use bizHd\saas\classes\ApplyClass;
 use bizHd\shop\classes\MainInviteClass;
+use common\components\dict;
 use common\components\noticeUtil;
 use common\components\util;
 use Yii;
@@ -22,6 +23,48 @@ class RenewClass extends BaseClass
         return $data;
     }
 
+    /**
+     * 是否已有 xhRenew 付费成功记录(同一 main/shop 仅首次付费享受邀请减免与佣金)
+     */
+    public static function hasPaidMemberRenew(int $mainId, int $shopId = 0): bool
+    {
+        $ptStyle = dict::getDict('ptStyle', 'hd');
+        if ($mainId > 0) {
+            $row = self::getByCondition(['mainId' => $mainId, 'status' => 1, 'ptStyle' => $ptStyle], false);
+            if (!empty($row)) {
+                return true;
+            }
+        }
+        if ($shopId > 0) {
+            $row = self::getByCondition(['shopId' => $shopId, 'status' => 1, 'ptStyle' => $ptStyle], false);
+            return !empty($row);
+        }
+        return false;
+    }
+
+    /**
+     * 除当前订单外是否已有付费成功的 xhRenew(回调写佣金时用)
+     */
+    public static function hasOtherPaidMemberRenew(int $mainId, string $excludeOrderSn = ''): bool
+    {
+        if ($mainId <= 0) {
+            return false;
+        }
+        $ptStyle = dict::getDict('ptStyle', 'hd');
+        $list = self::getRenewList(['mainId' => $mainId, 'status' => 1, 'ptStyle' => $ptStyle]);
+        if (empty($list)) {
+            return false;
+        }
+        foreach ($list as $item) {
+            $sn = is_array($item) ? (string) ($item['orderSn'] ?? '') : (string) ($item->orderSn ?? '');
+            if ($excludeOrderSn !== '' && $sn === $excludeOrderSn) {
+                continue;
+            }
+            return true;
+        }
+        return false;
+    }
+
     public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId)
     {
         $renew = self::getByCondition(['orderSn' => $orderSn], true);
@@ -84,7 +127,7 @@ class RenewClass extends BaseClass
         $renew->status = 1;
         $renew->save();
 
-        if ($isRegisterMember) {
+        if ($isRegisterMember && !self::hasOtherPaidMemberRenew((int) $mainId, (string) $orderSn)) {
             $apply = ApplyClass::getById($applyId);
             if (!empty($apply)) {
                 MainInviteClass::grantInviterCommission(