Bladeren bron

Merge branch 'give'

shish 11 maanden geleden
bovenliggende
commit
293da1820e

+ 5 - 2
app-ghs/controllers/TestController.php

@@ -395,12 +395,15 @@ class TestController extends BaseController
             //['name' => '徐州贴心鲜花批发', 'merchantNo' => '82230305992005L', 'wx' => [770844003, 770842534], 'zfb' => '2088370485434992'],
             //['name' => '海沧好多花换店', 'merchantNo' => '8223930599200CN', 'wx' => [772684174, 772684211], 'zfb' => '2088370864383343'],
             //['name' => '九江伊朵鲜花批发', 'merchantNo' => '82242405992002A', 'wx' => [774681090, 774681246], 'zfb' => '2088470298005142'],
+            //['name' => '眉山花鲜谷', 'merchantNo' => '82266705992003C', 'wx' => [801669421, 801668546], 'zfb' => '2088870521514083'],
+            ['name' => '颍上四季花都', 'merchantNo' => '82237205992004C', 'wx' => [802165446, 802165923], 'zfb' => '2088870568169143'],
             //['name' => '徐州云之南鲜花', 'merchantNo' => '82230305992006C', 'wx' => [788161779, 788162410], 'zfb' => '2088670707622207'],
             //['name' => '济南伟伟鲜花批发', 'merchantNo' => '8224510599200EL', 'wx' => [789073494, 789074603], 'zfb' => '2088670850497224'],
             //['name' => '赣州紫荆鲜花批发', 'merchantNo' => '82242805992004J', 'wx' => [790052021, 790051859], 'zfb' => '2088670969143838'],
             ['name' => '东莞惠雅鲜花(莞城店)', 'merchantNo' => '8226020599200GJ', 'wx' => [796953190, 796976959], 'zfb' => '2088770940104912'],
-            ['name' => '沈阳市云花鲜花批发-专业化扫码', 'merchantNo' => '8222210599200BT', 'wx' => [796180053], 'zfb' => '2088770820458908'],
-            ['name' => '沈阳市云花鲜花批发-聚合收银台', 'merchantNo' => '8222210599200BS', 'wx' => [796157674], 'zfb' => '2088770815918545'],
+            ['name' => '东莞惠雅鲜花(南城店)', 'merchantNo' => '8226020599200JK', 'wx' => [802315656, 802315755], 'zfb' => '2088870583889365'],
+            //['name' => '沈阳市云花鲜花批发-专业化扫码', 'merchantNo' => '8222210599200BT', 'wx' => [796180053], 'zfb' => '2088770820458908'],
+            //['name' => '沈阳市云花鲜花批发-聚合收银台', 'merchantNo' => '8222210599200BS', 'wx' => [796157674], 'zfb' => '2088770815918545'],
             //['name' => '日照温暖小筑', 'merchantNo' => '82247705992002F', 'wx' => [781363841, 781362087], 'zfb' => '2088570557754874'],
             //['name' => '昆明建芳基地鲜花直发', 'merchantNo' => '8223970599200AH', 'wx' => [775915046, 775915051], 'zfb' => '2088470558755358'],
             //['name' => '日照月下鲜花批发', 'merchantNo' => '82247705992002D', 'wx' => [759404451, 759404055], 'zfb' => '2088170264295192'],

+ 42 - 0
app-hd/controllers/BalanceGiveChangeController.php

@@ -0,0 +1,42 @@
+<?php
+
+namespace hd\controllers;
+
+use bizHd\balance\classes\BalanceGiveChangeClass;
+use bizHd\custom\classes\CustomClass;
+use common\components\dateUtil;
+use common\components\util;
+use Yii;
+
+class BalanceGiveChangeController extends BaseController
+{
+
+    public function actionList()
+    {
+        $get = Yii::$app->request->get();
+        $where = [];
+        $where['mainId'] = $this->mainId;
+        $customId = $get['customId'] ?? 0;
+        if (!empty($customId)) {
+            $custom = CustomClass::getById($customId, true);
+            if (empty($custom)) {
+                util::fail('没有找到客户');
+            }
+            $shopId = $this->shopId;
+            if ($custom->shopId != $shopId) {
+                util::fail('没有找到客户信息');
+            }
+            $where['customId'] = $customId;
+        }
+        $searchTime = $get['searchTime'] ?? '';
+        if (!empty($searchTime)) {
+            $startTime = $get['startTime'] ?? '';
+            $endTime = $get['endTime'] ?? '';
+            $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
+            $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
+        }
+        $respond = BalanceGiveChangeClass::getChangeList($where);
+        util::success($respond);
+    }
+
+}

+ 42 - 0
app-hd/controllers/BalancePayChangeController.php

@@ -0,0 +1,42 @@
+<?php
+
+namespace hd\controllers;
+
+use bizHd\balance\classes\BalancePayChangeClass;
+use bizHd\custom\classes\CustomClass;
+use common\components\dateUtil;
+use common\components\util;
+use Yii;
+
+class BalancePayChangeController extends BaseController
+{
+
+    public function actionList()
+    {
+        $get = Yii::$app->request->get();
+        $where = [];
+        $where['mainId'] = $this->mainId;
+        $customId = $get['customId'] ?? 0;
+        if (!empty($customId)) {
+            $custom = CustomClass::getById($customId, true);
+            if (empty($custom)) {
+                util::fail('没有找到客户');
+            }
+            $shopId = $this->shopId;
+            if ($custom->shopId != $shopId) {
+                util::fail('没有找到客户信息');
+            }
+            $where['customId'] = $customId;
+        }
+        $searchTime = $get['searchTime'] ?? '';
+        if (!empty($searchTime)) {
+            $startTime = $get['startTime'] ?? '';
+            $endTime = $get['endTime'] ?? '';
+            $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
+            $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
+        }
+        $respond = BalancePayChangeClass::getChangeList($where);
+        util::success($respond);
+    }
+
+}

+ 1 - 1
app-hd/controllers/MemberWealController.php

@@ -122,7 +122,7 @@ class MemberWealController extends BaseController
         $shop = $this->shop;
         $rechargeWeal = $shop->rechargeWeal ?? 0;
         if ($rechargeWeal != 0) {
-            util::fail('已经设置过了,不能修改');
+            util::fail('已经设置过了');
         }
         $post = Yii::$app->request->post();
         $rechargeWeal = $post['rechargeWeal'] ?? 0;

+ 0 - 3
app-hd/controllers/OrderController.php

@@ -584,9 +584,6 @@ class OrderController extends BaseController
         try {
             $post['product'] = $productList;
 
-            //阿东开5支老是会出现5扎情况跟踪
-            ProductClass::adStockCheck($this->shop, $productList);
-
             //3秒内不允许重复开单
             $staffId = $this->shopAdminId ?? 0;
             $cacheKey = 'hd_create_order_' . $staffId . '_' . $customId;

+ 1 - 1
app-hd/controllers/StatController.php

@@ -84,7 +84,7 @@ class StatController extends BaseController
         $balance = $respond['balance'] ?? 0;
         $payCodeIncome = $respond['payCodeIncome'] ?? 0;
 
-        //由于损耗而造成少赚的钱
+        //由于损耗而造成的损失
         $wastageRespond = \bizGhs\stat\classes\StatSaleClass::wastage($mainId);
         $wastagePrice = $wastageRespond['totalPrice'] ?? 0;
         $wastagePrice = floatval($wastagePrice);

+ 8 - 1
biz-ghs/stat/classes/StatSaleClass.php

@@ -302,6 +302,7 @@ class StatSaleClass extends BaseClass
         //零售开单
         $payCodeIncome = 0;
         $lsAfterSale = 0;
+        $lsGiveBalance = 0;
         $lsIncome = 0;
         $lsSendCost = 0;
         $lsServiceFee = 0;
@@ -338,6 +339,10 @@ class StatSaleClass extends BaseClass
                     $payCodeIncome = bcadd($payCodeIncome, $actPrice, 2);
                     $payCodeIncome = floatval($payCodeIncome);
                 }
+                $giveBalance = $hdOrder['giveBalance'] ?? 0;
+                $giveBalanceTk = $hdOrder['giveBalanceTk'] ?? 0;
+                $remainGiveBalance = bcsub($giveBalance, $giveBalanceTk, 2);
+                $lsGiveBalance = bcadd($lsGiveBalance, $remainGiveBalance, 2);
             }
         }
 
@@ -388,6 +393,8 @@ class StatSaleClass extends BaseClass
             ['name' => '零售花材采购', 'id' => 'cg', 'amount' => floatval($lsCgAmount)],
             ['name' => '调拨入库', 'id' => 'allot', 'amount' => floatval($stockIn)],
             ['name' => '员工工资', 'id' => 'salary', 'amount' => floatval($salary)],
+            ['name' => '收款码重复收入', 'id' => 'salary', 'amount' => floatval($payCodeIncome)],
+            ['name' => '零售充值赠送', 'id' => 'salary', 'amount' => floatval($lsGiveBalance)],
         ];
         if (!empty($expendAmount)) {
             foreach ($expendAmount as $it) {
@@ -404,7 +411,7 @@ class StatSaleClass extends BaseClass
             $balance = bcsub($balance, $amount, 2);
         }
 
-        return ['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'totalOrderNum' => $totalOrderNum, 'payCodeIncome' => $payCodeIncome];
+        return ['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'totalOrderNum' => $totalOrderNum];
     }
 
     //按花材统计收入 ssh 20231111

+ 19 - 0
biz-hd/balance/classes/BalanceGiveChangeClass.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace bizHd\balance\classes;
+
+use Yii;
+use bizHd\base\classes\BaseClass;
+
+class BalanceGiveChangeClass extends BaseClass
+{
+
+    public static $baseFile = '\bizHd\balance\models\BalanceGiveChange';
+
+    public static function getChangeList($where)
+    {
+        $data = self::getList('*', $where, 'addTime DESC,id DESC');
+        return $data;
+    }
+
+}

+ 32 - 0
biz-hd/balance/classes/BalancePayChangeClass.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace bizHd\balance\classes;
+
+use Yii;
+use bizHd\base\classes\BaseClass;
+
+class BalancePayChangeClass extends BaseClass
+{
+
+    public static $baseFile = '\bizHd\balance\models\BalancePayChange';
+
+    public static function getChangeList($where)
+    {
+        $data = self::getList('*', $where, 'addTime DESC,id DESC');
+        if (!empty($data['list'])) {
+            foreach ($data['list'] as $k => $v) {
+                $settleAmount = $v['settleAmount'] ?? 0;
+                $amount = $v['amount'] ?? 0;
+                $io = $v['io'] ?? 0;
+                $becomeBalance = 0;
+                if ($io == 1) {
+                    $becomeBalance = bcsub($amount, $settleAmount, 2);
+                    $becomeBalance = floatval($becomeBalance);
+                }
+                $data['list'][$k]['becomeBalance'] = $becomeBalance;
+            }
+        }
+        return $data;
+    }
+
+}

+ 13 - 0
biz-hd/balance/models/BalanceGiveChange.php

@@ -0,0 +1,13 @@
+<?php
+namespace bizHd\balance\models;
+use bizHd\base\models\Base;
+
+class BalanceGiveChange extends Base
+{
+
+    public static function tableName()
+    {
+        return 'xhBalanceGiveChange';
+    }
+
+}

+ 13 - 0
biz-hd/balance/models/BalancePayChange.php

@@ -0,0 +1,13 @@
+<?php
+namespace bizHd\balance\models;
+use bizHd\base\models\Base;
+
+class BalancePayChange extends Base
+{
+
+    public static function tableName()
+    {
+        return 'xhBalancePayChange';
+    }
+
+}

+ 13 - 0
biz-hd/balance/services/BalanceGiveChangeService.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace bizHd\balance\services;
+
+use bizHd\base\services\BaseService;
+use Yii;
+
+class BalanceGiveChangeService extends BaseService
+{
+
+    public static $baseFile = '\bizHd\balance\classes\BalanceGiveChangeClass';
+
+}

+ 13 - 0
biz-hd/balance/services/BalancePayChangeService.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace bizHd\balance\services;
+
+use bizHd\base\services\BaseService;
+use Yii;
+
+class BalancePayChangeService extends BaseService
+{
+
+    public static $baseFile = '\bizHd\balance\classes\BalancePayChangeClass';
+
+}

+ 342 - 18
biz-hd/custom/classes/CustomClass.php

@@ -3,17 +3,16 @@
 namespace bizHd\custom\classes;
 
 use biz\wx\classes\WxMessageClass;
-use bizGhs\shop\classes\TotalDebtChangeClass;
 use bizHd\balance\classes\BalanceChangeClass;
+use bizHd\balance\classes\BalanceGiveChangeClass;
+use bizHd\balance\classes\BalancePayChangeClass;
 use bizHd\member\classes\MemberChangeClass;
 use bizHd\member\classes\MemberWealClass;
+use bizHd\order\classes\OrderClass;
 use bizHd\order\classes\SettleClass;
-use bizHd\shop\classes\MainClass;
 use bizHd\shop\classes\ShopClass;
-use bizMall\user\classes\UserClass;
 use common\components\dict;
 use common\components\imgUtil;
-use common\components\noticeUtil;
 use common\components\stringUtil;
 use common\components\util;
 use Yii;
@@ -85,7 +84,98 @@ class CustomClass extends BaseClass
             'mainId' => $mainId,
             'remark' => '',
         ];
-        return BalanceChangeClass::add($change, true);
+        BalanceChangeClass::add($change, true);
+        $order->balance = $actPrice;
+        $order->save();
+
+        // 多处需要同步修改 balance_pay_give
+        if ($hd->balancePay >= $actPrice) {
+            $payAmount = $actPrice;
+            $mustUseGiveAmount = 0;
+        } else {
+            $payAmount = $hd->balancePay;
+            $mustUseGiveAmount = bcsub($actPrice, $payAmount, 2);
+            if ($mustUseGiveAmount > $hd->balanceGive) {
+                util::fail('赠送余额异常,不够扣');
+            }
+        }
+
+        /************************* 充值余额 *****************************/
+        $customBalancePay = bcsub($custom->balancePay, $payAmount, 2);
+        $custom->balancePay = $customBalancePay;
+        $custom->save();
+        $hdBalancePay = bcsub($hd->balancePay, $payAmount, 2);
+        $hd->balancePay = $hdBalancePay;
+        $hd->save();
+        $payChange = [
+            'hdId' => $hdId,
+            'hdName' => $hdName,
+            'customId' => $customId,
+            'customName' => $customName,
+            'relateId' => $relateId,
+            'onlinePay' => 1,
+            'capitalType' => $capitalType,
+            'amount' => $payAmount,
+            'balance' => $customBalancePay,
+            'io' => 0,
+            'side' => $side,
+            'payWay' => $payWay,
+            'event' => $event,
+            'staffId' => $staffId,
+            'staffName' => $staffName,
+            'shopId' => $shopId,
+            'mainId' => $mainId,
+            'remark' => '',
+        ];
+        BalancePayChangeClass::add($payChange, true);
+        $order->payBalance = $payAmount;
+        $order->save();
+
+        /***************************** 赠送余额 ******************************/
+        //应用掉的赠送金额
+        if ($mustUseGiveAmount > 0) {
+            $customBalanceGive = bcsub($custom->balanceGive, $mustUseGiveAmount, 2);
+            if ($customBalanceGive < 0) {
+                util::fail('余额异常哈!请联系管理员,编号' . $hdId . ' ' . $custom->balanceGive);
+            }
+            $custom->balanceGive = $customBalanceGive;
+            $custom->save();
+            $hdBalanceGive = bcsub($hd->balanceGive, $mustUseGiveAmount, 2);
+            $hd->balanceGive = $hdBalanceGive;
+            $hd->save();
+            if (floatval($customBalanceGive) != floatval($hdBalanceGive)) {
+                util::fail('余额有问题呢!请联系管理员,编号' . $hdId);
+            }
+            $giveChange = [
+                'hdId' => $hdId,
+                'hdName' => $hdName,
+                'customId' => $customId,
+                'customName' => $customName,
+                'relateId' => $relateId,
+                'onlinePay' => 1,
+                'capitalType' => $capitalType,
+                'amount' => $mustUseGiveAmount,
+                'balance' => $customBalanceGive,
+                'io' => 0,
+                'side' => $side,
+                'payWay' => $payWay,
+                'event' => $event,
+                'staffId' => $staffId,
+                'staffName' => $staffName,
+                'shopId' => $shopId,
+                'mainId' => $mainId,
+                'remark' => '',
+            ];
+            BalanceGiveChangeClass::add($giveChange, true);
+            $order->giveBalance = $mustUseGiveAmount;
+            $order->save();
+        }
+
+        $addBalance = bcadd($hd->balancePay, $hd->balanceGive, 2);
+        if (floatval($addBalance) != floatval($custom->balance)) {
+            util::fail('账户余额有问题!请检查,编号' . $customId);
+        }
+
     }
 
     //会员等级变化,包括手动修改、自动降会员和充值升会员等 ssh 20250324
@@ -151,7 +241,9 @@ class CustomClass extends BaseClass
     }
 
     //客户采购欠款金额增加,如果有余额时,要进行欠款销账 ssh 20250715
-    public static function cgDebtAmountAdd($custom, $order)
+    //todo 这边开欠款单时,如果还有余额,会产生结账动作,结账动作也可以用赠送余额来结账,所以要分出赠送余额的占比
+    //todo 结账消余额,不是下单时消余额,不涉及售后时余额回滚问题,所以不需要设置 xhOrder 的 giveBalance payBalance balance等值
+    public static function skCgDebtAmountAdd($custom, $order)
     {
         $clearAmount = 0;
         $amount = $order->remainDebtPrice ?? 0;
@@ -167,14 +259,17 @@ class CustomClass extends BaseClass
         $hdId = $custom->hdId ?? 0;
         $hd = HdClass::getLockById($hdId);
         if (empty($hd)) {
-            util::fail('花店信息缺失,编号6331');
+            util::fail('花店信息缺失,编号6325');
         }
         $hdName = $hd->name ?? '';
+        //结账时用了多少赠送的余额
+        $settleUseGiveAmount = 0;
         if ($custom->balance > 0) {
-            if ($amount >= $custom->balance) {
+            if ($amount > $custom->balance) {
                 $clearAmount = $custom->balance;
+                $settleUseGiveAmount = $custom->balanceGive;
             } else {
-                $clearAmount = $amount;
+                util::fail('余额充足,请用余额支付');
             }
         }
         if ($clearAmount > 0) {
@@ -186,26 +281,28 @@ class CustomClass extends BaseClass
             $settleAmount = $ret['totalClearAmount'] ?? 0;
             $settleNo = $set->orderSn ?? '';
             $settleId = $set->id;
+            $newSettle = $ret['settle'];
+            //结账时用了多少赠送的余额
+            $newSettle->giveBalance = bcadd($newSettle->giveBalance, $settleUseGiveAmount, 2);
+            $newSettle->save();
         }
 
+        /************************ 总余额 *********************************/
         $customBalance = bcsub($custom->balance, $amount, 2);
         $custom->balance = $customBalance;
         if ($custom->balance < 0) {
             $custom->isDebt = 1;
         }
         $custom->save();
-
         $hdBalance = bcsub($hd->balance, $amount, 2);
         $hd->balance = $hdBalance;
         if ($hd->balance < 0) {
             $hd->debt = 2;
         }
         $hd->save();
-
         if (floatval($customBalance) != floatval($hdBalance)) {
             util::fail('余额有错,请联系管理员,编号' . $hdId);
         }
-
         $relateId = $order->id ?? 0;
         $customId = $custom->id ?? 0;
         $orderSn = $order->orderSn ?? '';
@@ -239,40 +336,190 @@ class CustomClass extends BaseClass
             'remark' => '',
         ];
         BalanceChangeClass::add($change, true);
+
+        $mustUseGiveAmount = 0;
+        // 多处需要同步修改 balance_pay_give
+        if ($hd->balancePay >= $amount) {
+            util::fail('请用余额支付哈');
+        } else {
+            // todo 如果【充值金额】小于订单欠款金额,则用全部【充值金额】消单,同时必须用上【赠送金额】,够抵,则【充值金额】还是正数,不够抵,则是负数
+            if ($hd->balanceGive <= 0) {
+                //没有【赠送金额】,则用【充值金额】来消全部的欠款金额,不够则变成负数
+                $payAmount = $amount;
+            } else {
+                //这个是【总余额】,包括【充值金额】和【赠送金额】
+                $currentAdd = bcadd($hd->balancePay, $hd->balanceGive, 2);
+                if ($currentAdd >= $amount) {
+                    util::fail('请使用余额支付哦');
+                } else {
+                    //【总余额】不够抵欠款金额,先用掉全部的【赠送金额】,其他的全部算在【充值金额】名下,因为充值金额也不够扣,会出现负数
+                    $mustUseGiveAmount = $hd->balanceGive;
+                    $payAmount = bcsub($amount, $mustUseGiveAmount, 2);
+                }
+            }
+        }
+
+        /************************* 充值余额 *****************************/
+        $customBalancePay = bcsub($custom->balancePay, $payAmount, 2);
+        $custom->balancePay = $customBalancePay;
+        $custom->save();
+        $hdBalancePay = bcsub($hd->balancePay, $payAmount, 2);
+        $hd->balancePay = $hdBalancePay;
+        $hd->save();
+        if (floatval($customBalancePay) != floatval($hdBalancePay)) {
+            util::fail('余额有错呢,请联系管理员,编号' . $hdId);
+        }
+        $relateId = $order->id ?? 0;
+        $customId = $custom->id ?? 0;
+        $orderSn = $order->orderSn ?? '';
+        $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
+        $event = '购买花材,单号:' . $orderSn;
+        $customName = $custom->name ?? '';
+        $onlinePay = 1;
+        $side = 0;
+        $payWay = 0;
+        $payChange = [
+            'hdId' => $hdId,
+            'hdName' => $hdName,
+            'customId' => $customId,
+            'customName' => $customName,
+            'relateId' => $relateId,
+            'onlinePay' => $onlinePay,
+            'capitalType' => $capitalType,
+            'amount' => $payAmount,
+            'balance' => $customBalancePay,
+            'io' => 0,
+            'side' => $side,
+            'payWay' => $payWay,
+            'event' => $event,
+            'staffId' => $staffId,
+            'staffName' => $staffName,
+            'shopId' => $shopId,
+            'mainId' => $order->mainId,
+            'settleId' => $settleId,
+            'settleNo' => $settleNo,
+            'settleAmount' => $settleAmount,
+            'remark' => '',
+        ];
+        BalancePayChangeClass::add($payChange, true);
+
+        /***************************** 赠送余额 ******************************/
+        //应用掉的赠送金额
+        if ($mustUseGiveAmount > 0) {
+            $customBalanceGive = bcsub($custom->balanceGive, $mustUseGiveAmount, 2);
+            if ($customBalanceGive < 0) {
+                util::fail('余额异常哈,请联系管理员,编号' . $hdId . ' ' . $custom->balanceGive);
+            }
+            $custom->balanceGive = $customBalanceGive;
+            $custom->save();
+            $hdBalanceGive = bcsub($hd->balanceGive, $mustUseGiveAmount, 2);
+            $hd->balanceGive = $hdBalanceGive;
+            $hd->save();
+            if (floatval($customBalanceGive) != floatval($hdBalanceGive)) {
+                util::fail('余额有问题呢,请联系管理员,编号' . $hdId);
+            }
+            $relateId = $order->id ?? 0;
+            $customId = $custom->id ?? 0;
+            $orderSn = $order->orderSn ?? '';
+            $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
+            $event = '购买花材,单号:' . $orderSn;
+            $customName = $custom->name ?? '';
+            $onlinePay = 1;
+            $side = 0;
+            $payWay = 0;
+            $giveChange = [
+                'hdId' => $hdId,
+                'hdName' => $hdName,
+                'customId' => $customId,
+                'customName' => $customName,
+                'relateId' => $relateId,
+                'onlinePay' => $onlinePay,
+                'capitalType' => $capitalType,
+                'amount' => $mustUseGiveAmount,
+                'balance' => $customBalanceGive,
+                'io' => 0,
+                'side' => $side,
+                'payWay' => $payWay,
+                'event' => $event,
+                'staffId' => $staffId,
+                'staffName' => $staffName,
+                'shopId' => $shopId,
+                'mainId' => $order->mainId,
+                'settleId' => $settleId,
+                'settleNo' => $settleNo,
+                'settleAmount' => $settleAmount,
+                'remark' => '',
+            ];
+            BalanceGiveChangeClass::add($giveChange, true);
+        }
+        $addBalance = bcadd($hd->balancePay, $hd->balanceGive, 2);
+        if (floatval($addBalance) != floatval($custom->balance)) {
+            util::fail('账户余额有问题,请检查,编号' . $customId);
+        }
     }
 
     //欠款和余额支付的售后 ssh 20250803
-    public static function refundDebtAmountReduce($custom, $amount, $refund)
+    public static function skRefundDebtAmountReduce($custom, $amount, $refund, $payWay)
     {
         $hdId = $custom->hdId ?? 0;
         $hd = HdClass::getLockById($hdId);
         if (empty($hd)) {
-            util::fail('花店信息缺失,编号520');
+            util::fail('花店信息缺失,编号5362');
         }
-
         $customBalance = bcadd($custom->balance, $amount, 2);
         $custom->balance = $customBalance;
         if ($custom->balance < 0) {
             $custom->isDebt = 1;
         }
         $custom->save();
-
         $hdBalance = bcadd($hd->balance, $amount, 2);
         $hd->balance = $hdBalance;
         if ($hd->balance < 0) {
             $hd->debt = 2;
         }
         $hd->save();
-
         if (floatval($customBalance) !== floatval($hdBalance)) {
             util::fail('余额错误,请联系管理员,编号' . $hdId);
         }
-
         $relateId = $refund->id ?? 0;
         $customId = $custom->id ?? 0;
         $refundSn = $refund->refundSn ?? '';
         $orderSn = $refund->orderSn ?? '';
         $orderId = $refund->orderId ?? 0;
+        $order = OrderClass::getLockById($orderId);
+        if (empty($order)) {
+            util::fail('没有找到订单哈!');
+        }
+
+        // 多处需要同步修改 balance_pay_give
+        $mustUseGiveAmount = 0;
+        $buyAmount = 0;
+        if ($payWay == 3) {
+            //如果是欠款单售后,没有退款到赠送余额的情况
+            $buyAmount = $amount;
+        } elseif ($payWay == 2) {
+            //这个是总消费余额中充值余额部分
+            $payBalance = $order->payBalance ?? 0;
+            //这个是累计已经退还的余额
+            $balanceTk = $order->balanceTk ?? 0;
+            $currentBalanceTk = bcadd($balanceTk, $amount, 2);
+            //当前累计退还的余额如果超过了充值余额的话,说明要退还赠送的余额了
+            if ($currentBalanceTk > $payBalance) {
+                $pastAmount = bcsub($currentBalanceTk, $payBalance, 2);
+                if ($pastAmount > $amount) {
+                    $mustUseGiveAmount = $amount;
+                } else {
+                    $buyAmount = bcsub($amount, $pastAmount, 2);
+                    $mustUseGiveAmount = bcsub($amount, $buyAmount, 2);
+                }
+            } else {
+                //累计退还的余额,还没超过充值余额,还是退充值余额部分
+                $buyAmount = $amount;
+            }
+        } else {
+            util::fail('无效的支付方式!编号69880');
+        }
+
         $staffId = $refund->shopAdminId ?? 0;
         $staffName = $refund->shopAdminName ?? '';
         $capitalType = dict::getDict('capitalType', 'hdRefund', 'id');
@@ -303,6 +550,83 @@ class CustomClass extends BaseClass
             'remark' => '',
         ];
         BalanceChangeClass::add($change, true);
+        $order->balanceTk = bcadd($order->balanceTk, $amount, 2);
+        if ($order->balanceTk < 0) {
+            util::fail('不能小于0,编号88672');
+        }
+        $order->save();
+
+        if ($buyAmount > 0) {
+            $customBalancePay = bcadd($custom->balancePay, $buyAmount, 2);
+            $custom->balancePay = $customBalancePay;
+            $custom->save();
+            $hdBalancePay = bcadd($hd->balancePay, $buyAmount, 2);
+            $hd->balancePay = $hdBalancePay;
+            $hd->save();
+            $payChange = [
+                'customId' => $customId,
+                'customName' => $customName,
+                'relateId' => $relateId,
+                'relateSn' => $refundSn,
+                'onlinePay' => $onlinePay,
+                'capitalType' => $capitalType,
+                'amount' => $buyAmount,
+                'balance' => $hdBalancePay,
+                'io' => 1,
+                'side' => $side,
+                'payWay' => $payWay,
+                'event' => $event,
+                'staffId' => $staffId,
+                'staffName' => $staffName,
+                'shopId' => $refund->shopId,
+                'mainId' => $refund->mainId,
+                'refundOrderId' => $orderId,
+                'refundOrderSn' => $orderSn,
+                'remark' => '',
+            ];
+            BalancePayChangeClass::add($payChange, true);
+            $order->payBalanceTk = bcadd($order->payBalanceTk, $buyAmount, 2);
+            if ($order->payBalanceTk < 0) {
+                util::fail('不能小于0,编号88673');
+            }
+            $order->save();
+        }
+
+        if ($mustUseGiveAmount > 0) {
+            $customBalanceGive = bcadd($custom->balanceGive, $mustUseGiveAmount, 2);
+            $custom->balanceGive = $customBalanceGive;
+            $custom->save();
+            $hdBalanceGive = bcadd($hd->balanceGive, $mustUseGiveAmount, 2);
+            $hd->balanceGive = $hdBalanceGive;
+            $hd->save();
+            $giveChange = [
+                'customId' => $customId,
+                'customName' => $customName,
+                'relateId' => $relateId,
+                'relateSn' => $refundSn,
+                'onlinePay' => $onlinePay,
+                'capitalType' => $capitalType,
+                'amount' => $mustUseGiveAmount,
+                'balance' => $hdBalanceGive,
+                'io' => 1,
+                'side' => $side,
+                'payWay' => $payWay,
+                'event' => $event,
+                'staffId' => $staffId,
+                'staffName' => $staffName,
+                'shopId' => $refund->shopId,
+                'mainId' => $refund->mainId,
+                'refundOrderId' => $orderId,
+                'refundOrderSn' => $orderSn,
+                'remark' => '',
+            ];
+            BalanceGiveChangeClass::add($giveChange, true);
+            $order->giveBalanceTk = bcadd($order->giveBalanceTk, $mustUseGiveAmount, 2);
+            if ($order->giveBalanceTk < 0) {
+                util::fail('不能小于0,编号88675');
+            }
+            $order->save();
+        }
     }
 
     //添加客户 ssh 2021.2.28

+ 1 - 1
biz-hd/order/classes/OrderClass.php

@@ -543,7 +543,7 @@ class OrderClass extends BaseClass
         //有欠款时
         if ($payWay == dict::getDict('payWay', 'debtPay')) {
             if (isset($order->remainDebtPrice) && $order->remainDebtPrice > 0) {
-                CustomClass::cgDebtAmountAdd($myCustom, $order);
+                CustomClass::skCgDebtAmountAdd($myCustom, $order);
             }
         }
 

+ 1 - 1
biz-hd/order/classes/SettleClass.php

@@ -178,7 +178,7 @@ class SettleClass extends BaseClass
                 $order->save();
             }
         }
-        return ['totalClearAmount' => $totalClearAmount];
+        return ['totalClearAmount' => $totalClearAmount, 'settle' => $set];
     }
 
     public static function addData($data)

+ 98 - 20
biz-hd/recharge/classes/RechargeClass.php

@@ -5,6 +5,8 @@ namespace bizHd\recharge\classes;
 use biz\shop\classes\ShopClass;
 use bizGhs\shop\classes\ShopMoneyClass;
 use bizHd\balance\classes\BalanceChangeClass;
+use bizHd\balance\classes\BalanceGiveChangeClass;
+use bizHd\balance\classes\BalancePayChangeClass;
 use bizHd\custom\classes\CustomClass;
 use bizHd\custom\classes\HdClass;
 use bizHd\member\classes\MemberWealClass;
@@ -144,7 +146,6 @@ class RechargeClass extends BaseClass
         $custom = CustomClass::getLockById($customId);
         if (empty($custom)) {
             $msg = '充值回调失败,没有找到客户';
-            Yii::info($msg);
             util::fail($msg);
         }
         $oldLevel = $custom->member ?? 0;
@@ -152,13 +153,12 @@ class RechargeClass extends BaseClass
         $hd = HdClass::getLockById($hdId);
         if (empty($hd)) {
             $msg = '充值回调失败,没有找到花店';
-            Yii::info($msg);
             util::fail($msg);
         }
 
         $shop = ShopClass::getById($shopId, true);
         $rechargeWeal = $shop->rechargeWeal ?? 0;
-        $addGiveAmount = 0;
+        $balanceGive = 0;
 
         //如果客户有欠款、结账充值、售后充值、售后付款充值,不参与优惠
         if ($custom->balance >= 0 && $recharge->settleId == 0 && $recharge->shOrderId == 0 && $recharge->refundOrderId == 0) {
@@ -195,7 +195,7 @@ class RechargeClass extends BaseClass
                                 $recharge->giveAmount = $thisGive;
                                 $recharge->save();
                                 //增加赠送的金额
-                                $addGiveAmount = $thisGive;
+                                $balanceGive = $thisGive;
                             }
                         }
                     }
@@ -203,7 +203,7 @@ class RechargeClass extends BaseClass
             }
         }
 
-        $getBalance = $amount;
+        $balancePay = $amount;
         if ($onlinePay == 2) {
             $event = $payWay == 0 ? '线上微信 充值' : '线上支付宝 充值';
         } else {
@@ -211,21 +211,42 @@ class RechargeClass extends BaseClass
             $channel = $payWayName[$payWay] ?? '微信';
             $event = "线下{$channel} 充值";
         }
-
-        //赠送的金额
-        if ($addGiveAmount > 0) {
-            $event .= floatval($getBalance) . "(送" . floatval($addGiveAmount) . ")";
-            $getBalance = bcadd($getBalance, $addGiveAmount, 2);
+        $event .= floatval($balancePay);
+        //赠送的余额
+        if ($balanceGive > 0) {
+            $event .= "(送" . floatval($balanceGive) . ")";
+            $custom->balanceGive = bcadd($custom->balanceGive, $balanceGive, 2);
+            $hd->balanceGive = bcadd($hd->balanceGive, $balanceGive, 2);
         }
-        $custom->balance = bcadd($custom->balance, $getBalance, 2);
+        //充值的余额
+        $custom->balancePay = bcadd($custom->balancePay, $balancePay, 2);
+        $hd->balancePay = bcadd($hd->balancePay, $balancePay, 2);
+        //总余额
+        $totalBalance = bcadd($balancePay, $balanceGive, 2);
+        $custom->balance = bcadd($custom->balance, $totalBalance, 2);
+        $hd->balance = bcadd($hd->balance, $totalBalance, 2);
         $custom->save();
-        $hd->balance = bcadd($hd->balance, $getBalance, 2);
         $hd->save();
+
         if ($hd->balance != $custom->balance) {
             $msg = '充值金额不一致。编号:' . floatval($hd->balance) . ' / ' . floatval($custom->balance);
             Yii::info($msg);
             util::fail($msg);
         }
+        if ($hd->balancePay != $custom->balancePay) {
+            $msg = '充值金额有异常。编号:' . floatval($hd->balancePay) . ' / ' . floatval($custom->balancePay);
+            Yii::info($msg);
+            util::fail($msg);
+        }
+        if ($hd->balanceGive != $custom->balanceGive) {
+            $msg = '充值金额异常。编号:' . floatval($hd->balanceGive) . ' / ' . floatval($custom->balanceGive);
+            Yii::info($msg);
+            util::fail($msg);
+        }
+        $addBalance = bcadd($hd->balanceGive, $hd->balancePay, 2);
+        if (floatval($hd->balance) != floatval($addBalance)) {
+            util::fail('充值金额有问题哦,编号:' . $customId);
+        }
 
         $settleId = $recharge->settleId;
         $settleAmount = 0;
@@ -251,9 +272,9 @@ class RechargeClass extends BaseClass
                     //销订单全部的欠款!!!
                     $remainDebtPrice = $order->remainDebtPrice ?? 0;
                     $currentTotalDebtAmount = bcadd($lastRemainDebtAmount, $remainDebtPrice, 2);
-                    if ($currentTotalDebtAmount > $getBalance) {
+                    if ($currentTotalDebtAmount > $totalBalance) {
                         if (!$hasOver) {
-                            $lastClearOrderAmount = bcsub($getBalance, $lastRemainDebtAmount, 2);
+                            $lastClearOrderAmount = bcsub($totalBalance, $lastRemainDebtAmount, 2);
                             if ($lastClearOrderAmount > 0) {
                                 //销订单部分的欠款!!!
                                 $lastClearOrderId = $orderId;
@@ -281,14 +302,19 @@ class RechargeClass extends BaseClass
             }
         }
         if ($settleId > 0) {
-            if (floatval($getBalance) != floatval($realClearAmount)) {
+            if (floatval($totalBalance) != floatval($realClearAmount) && $hd->balance < 0) {
+                $rechargeId = $recharge->id;
+                noticeUtil::push("注意,充值单,结账金额不一致:getBalance:{$totalBalance} totalAmount:{$realClearAmount},recharge:{$rechargeId}", '15280215347');
+                util::fail('充值和结账金额异常哈');
+            }
+            if (floatval($totalBalance) != floatval($settleAmount) && $hd->balance < 0) {
                 $rechargeId = $recharge->id;
-                noticeUtil::push("注意,充值单,结账单,和实际清掉欠款金额不一致:getBalance:{$getBalance} totalAmount:{$realClearAmount},recharge:{$rechargeId}", '15280215347');
-                util::fail('充值结账金额异常哈');
+                noticeUtil::push("注意,充值单,结账单不一致:getBalance:{$totalBalance} settleAmount:{$settleAmount},settleId:{$settleId} recharge:{$rechargeId}", '15280215347');
+                util::fail('充值和结账金额异常呢');
             }
-            if (floatval($getBalance) != floatval($settleAmount)) {
+            if (floatval($realClearAmount) != floatval($settleAmount)) {
                 $rechargeId = $recharge->id;
-                noticeUtil::push("注意,充值单,结账单,和实际清掉欠款金额不一致:getBalance:{$getBalance} settleAmount:{$settleAmount},settleId:{$settleId} recharge:{$rechargeId}", '15280215347');
+                noticeUtil::push("注意,结账金额有问题,编号 settleId:{$settleId} recharge:{$rechargeId}", '15280215347');
                 util::fail('充值结账金额异常呢');
             }
         }
@@ -300,7 +326,7 @@ class RechargeClass extends BaseClass
             'relateId' => $relateId,
             'onlinePay' => $onlinePay,
             'capitalType' => $capitalType,
-            'amount' => $getBalance,
+            'amount' => $totalBalance,
             'balance' => $hd->balance,
             'io' => 1,
             'side' => $side,
@@ -320,6 +346,58 @@ class RechargeClass extends BaseClass
             'hdName' => $hdName,
         ];
         $returnChange = BalanceChangeClass::add($change, true);
+
+        $payChange = [
+            'customId' => $customId,
+            'customName' => $customName,
+            'relateId' => $relateId,
+            'onlinePay' => $onlinePay,
+            'capitalType' => $capitalType,
+            'amount' => $balancePay,
+            'balance' => $hd->balancePay,
+            'io' => 1,
+            'side' => $side,
+            'payWay' => $payWay,
+            'event' => $event,
+            'staffId' => $staffId,
+            'staffName' => $staffName,
+            'shopId' => $shopId,
+            'mainId' => $mainId,
+            'settleId' => $settleId,
+            'settleNo' => $settleNo,
+            'settleAmount' => $settleAmount,
+            'remark' => $remark,
+            'shOrderId' => $shOrderId,
+            'shOrderSn' => $shOrderSn,
+            'hdId' => $hdId,
+            'hdName' => $hdName,
+        ];
+        BalancePayChangeClass::add($payChange, true);
+
+        if ($balanceGive > 0) {
+            $givChange = [
+                'customId' => $customId,
+                'customName' => $customName,
+                'relateId' => $relateId,
+                'onlinePay' => $onlinePay,
+                'capitalType' => $capitalType,
+                'amount' => $balanceGive,
+                'balance' => $hd->balanceGive,
+                'io' => 1,
+                'side' => $side,
+                'payWay' => $payWay,
+                'event' => $event,
+                'staffId' => $staffId,
+                'staffName' => $staffName,
+                'shopId' => $shopId,
+                'mainId' => $mainId,
+                'remark' => $remark,
+                'hdId' => $hdId,
+                'hdName' => $hdName,
+            ];
+            BalanceGiveChangeClass::add($givChange, true);
+        }
+
         if ($recharge->onlinePay == 2) {
             //如果线上充值,门店余额增加
             $shop = ShopClass::getLockById($shopId);

+ 2 - 2
biz-hd/refund/services/HdRefundService.php

@@ -296,7 +296,7 @@ class HdRefundService extends BaseService
                     $order->debt = 0;
                 }
                 $order->save();
-                CustomClass::refundDebtAmountReduce($custom, $refundPrice, $refund);
+                CustomClass::skRefundDebtAmountReduce($custom, $refundPrice, $refund,$payWay);
             }
             if ($payWay == dict::getDict('payWay', 'cash')) {
                 //如果现金支付
@@ -310,7 +310,7 @@ class HdRefundService extends BaseService
             }
             if ($payWay == dict::getDict('payWay', 'balancePay')) {
                 //如果是余额支付,客户的余额要增加
-                CustomClass::refundDebtAmountReduce($custom, $refundPrice, $refund);
+                CustomClass::skRefundDebtAmountReduce($custom, $refundPrice, $refund,$payWay);
             }
         }
         return $refund;