Browse Source

减余额功能

shish 2 months ago
parent
commit
080ea41f5b
2 changed files with 160 additions and 0 deletions
  1. 72 0
      app-ghs/controllers/CustomController.php
  2. 88 0
      biz-ghs/custom/classes/CustomClass.php

+ 72 - 0
app-ghs/controllers/CustomController.php

@@ -9,6 +9,7 @@ use biz\sj\classes\SjClass;
 use biz\wx\classes\WxMessageClass;
 use bizGhs\custom\classes\CustomClass;
 use bizGhs\custom\services\CustomService;
+use bizGhs\custom\services\GhsRechargeSettleService;
 use common\components\noticeUtil;
 use common\components\qrCodeUtil;
 use bizGhs\order\classes\OrderClass;
@@ -295,6 +296,77 @@ class CustomController extends BaseController
         }
     }
 
+    /**
+     * 手动减少客户净余额(财务权限,与充值销账同级)。
+     */
+    public function actionDeductAmount()
+    {
+        util::checkRepeatCommit($this->shopAdminId, 5);
+
+        $staff = $this->shopAdmin;
+        if (isset($staff->finance) == false || $staff->finance == 0) {
+            if ($this->shopId == 17118) {
+                if (!in_array($this->adminId, [17908])) {
+                    util::fail('你没有权限减少余额');
+                }
+            } elseif ($this->shopId == 72366) {
+                if (!in_array($this->adminId, [69792])) {
+                    util::fail('你没有权限减少余额');
+                }
+            } else {
+                util::fail('无法减少余额');
+            }
+        }
+
+        $post = Yii::$app->request->post();
+        $customId = intval($post['customId'] ?? 0);
+        $amount = $post['amount'] ?? 0;
+        $remark = $post['remark'] ?? '';
+        if (empty($customId)) {
+            util::fail('请选择客户');
+        }
+        if (empty($amount)) {
+            util::fail('请填写金额');
+        }
+        if ($amount < 0) {
+            util::fail('金额要大于0');
+        }
+
+        $cacheKey = 'help_custom_deduct_' . $customId;
+        $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
+        if (!empty($has)) {
+            util::fail('已操作,请15秒后再试');
+        }
+        Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 15, 'has']);
+
+        $pair = GhsRechargeSettleService::lockAccountPair(['id' => $customId]);
+        $custom = $pair['custom'];
+        $ghs = $pair['ghs'];
+        if (empty($custom)) {
+            util::fail('没有找到客户');
+        }
+        if ($custom->ownMainId != $this->mainId) {
+            util::fail('不是你的客户,无法操作');
+        }
+
+        $connection = Yii::$app->db;
+        $transaction = $connection->beginTransaction();
+        try {
+            $params = [
+                'remark' => $remark,
+                'staffId' => $staff->id ?? 0,
+                'staffName' => $staff->name ?? '',
+            ];
+            CustomClass::manualDeductBalance($this->shop, $custom, $ghs, $amount, $params);
+            $transaction->commit();
+            util::complete('减少成功');
+        } catch (\Exception $e) {
+            Yii::info('减少余额失败原因:' . $e->getMessage());
+            $transaction->rollBack();
+            util::fail($e->getMessage() ?: '减少失败');
+        }
+    }
+
     //生成货位号 ssh 20231215
     public function actionCreateSeatSn()
     {

+ 88 - 0
biz-ghs/custom/classes/CustomClass.php

@@ -716,6 +716,94 @@ class CustomClass extends BaseClass
         GhsBalanceChangeClass::add($gbData, true);
     }
 
+    /**
+     * 后台手动减少客户净余额(只减正余额部分,不增加待结)。
+     * 调用方须已通过 lockAccountPair 锁定 custom/ghs。
+     */
+    public static function manualDeductBalance($shop, $custom, $ghs, $amount, $params = [])
+    {
+        if (empty($custom)) {
+            util::fail('没有找到客户');
+        }
+        AccountMoneyClass::ensureCustomMoneyReady($custom, true);
+        if (!empty($ghs)) {
+            AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
+            self::assertPairBalanceEqual($custom, $ghs);
+        }
+
+        $amount = bcadd((string)$amount, '0', 2);
+        if (bccomp($amount, '0', 2) <= 0) {
+            util::fail('金额要大于0');
+        }
+        $beforeBalance = bcadd((string)($custom->balance ?? '0'), '0', 2);
+        if (bccomp($beforeBalance, '0', 2) <= 0) {
+            util::fail('没有可减余额');
+        }
+        if (bccomp($beforeBalance, $amount, 2) < 0) {
+            util::fail('余额不够扣');
+        }
+
+        $newBalance = bcsub($beforeBalance, $amount, 2);
+        self::savePairBalanceAfterDeduct($custom, $ghs, $newBalance);
+
+        $remark = $params['remark'] ?? '';
+        $staffId = intval($params['staffId'] ?? 0);
+        $staffName = $params['staffName'] ?? '';
+        $mainId = $shop->mainId ?? 0;
+        $shopId = $shop->id ?? 0;
+        $sjId = $shop->sjId ?? 0;
+        $customId = $custom->id ?? 0;
+        $customName = $custom->name ?? '';
+        $capitalType = dict::getDict('capitalType', 'deduct', 'id');
+        $fromType = dict::getDict('fromType', 'shop');
+        $event = '手动减少';
+
+        CustomBalanceChangeClass::add([
+            'customId' => $customId,
+            'customName' => $customName,
+            'relateId' => 0,
+            'onlinePay' => 1,
+            'ptStyle' => dict::getDict('ptStyle', 'ghs'),
+            'capitalType' => $capitalType,
+            'amount' => $amount,
+            'balance' => $newBalance,
+            'staffId' => $staffId,
+            'staffName' => $staffName,
+            'io' => 0,
+            'side' => 0,
+            'payWay' => 0,
+            'fromType' => $fromType,
+            'event' => $event,
+            'mainId' => $mainId,
+            'shopId' => $shopId,
+            'sjId' => $sjId,
+            'remark' => $remark,
+        ], true);
+
+        if (!empty($ghs)) {
+            $customShopId = $custom->shopId ?? 0;
+            $customShop = ShopClass::getById($customShopId, true);
+            GhsBalanceChangeClass::add([
+                'ghsId' => $ghs->id ?? 0,
+                'relateId' => 0,
+                'ptStyle' => dict::getDict('ptStyle', 'ghs'),
+                'capitalType' => $capitalType,
+                'amount' => $amount,
+                'balance' => $newBalance,
+                'io' => 0,
+                'side' => 0,
+                'onlinePay' => 1,
+                'payWay' => 0,
+                'fromType' => $fromType,
+                'event' => $event,
+                'sjId' => $customShop->sjId ?? 0,
+                'mainId' => $customShop->mainId ?? 0,
+                'shopId' => $customShopId,
+                'remark' => $remark,
+            ], true);
+        }
+    }
+
     /**
      * 开单余额支付:扣减净 balance 并记购买流水(须传入 payAfter 已 lockAccountPair 的 custom/ghs)。
      */