shish před 3 roky
rodič
revize
7c32ca7801

+ 2 - 2
app-hd/controllers/RefundController.php

@@ -90,7 +90,7 @@ class RefundController extends BaseController
             if ($post['price'] <= 0) {
                 util::fail("退款金额不能小于0");
             }
-            if (bccomp($post['price'], $order['orderPrice'], 2) == 1) {
+            if (bccomp($post['price'], $order->orderPrice, 2) == 1) {
                 util::fail("退款金额超过订单金额");
             }
 
@@ -101,7 +101,7 @@ class RefundController extends BaseController
             $shopAdmin = $this->shopAdmin;
             $adminName = $shopAdmin['name'] ?? '';
             $post['shopAdminName'] = $adminName;
-            HdRefundService::addRefund($post,$order);
+            HdRefundService::addRefund($post, $order);
             $transaction->commit();
 
         } catch (\Exception $exception) {

+ 22 - 0
biz-ghs/shop/classes/ShopMoneyClass.php

@@ -55,6 +55,28 @@ class ShopMoneyClass extends BaseClass
         return $respond;
     }
 
+    public static function hdRefundSkOutAmount($data, $main)
+    {
+        $amount = $data['amount'] ?? 0;
+        if ($main->money < $amount) {
+            util::fail('现金不足' . $amount . '元');
+        }
+        $main->money = bcsub($main->money, $amount, 2);
+        $main->save();
+        $balance = $main->money ?? 0;
+        $data['balance'] = $balance;
+        $data['io'] = 0;
+        $orderSn = $data['orderSn'] ?? '';
+
+        $event = '客户的订单' . $orderSn . '申请退款,现金减少' . $amount . '元';
+        $capitalType = dict::getDict('capitalType', 'outMoney', 'id');
+        $data['capitalType'] = $capitalType;
+        $data['event'] = $event;
+
+        $respond = self::addData($data);
+        return $respond;
+    }
+
     //供货商给花店发起退款 ssh 20220923
     public static function ghsRefundHdOutAmount($data, $main)
     {

+ 49 - 0
biz-hd/custom/classes/CustomClass.php

@@ -66,6 +66,55 @@ class CustomClass extends BaseClass
         TotalDebtChangeClass::addChange($change);
     }
 
+    //散客申请退款,零售门店欠款金额减少
+    public static function refundDebtAmountReduce($custom, $amount, $refund)
+    {
+        $debtAmount = $custom->debtAmount ?? 0.00;
+        $remain = bcsub($debtAmount, $amount, 2);
+        if ($remain < 0) {
+            noticeUtil::push("零售客户欠款总金额:{$custom->debtAmount} 本次结账金额:{$amount} 客户ID:{$custom->id} 超出了", '15280215347');
+            util::fail('操作失败,结账后欠款金额会出现负数');
+        }
+        $custom->isDebt = $remain <= 0 ? 0 : 1;
+        $custom->debtAmount = $remain;
+        $custom->save();
+        $relateId = $refund->id ?? 0;
+        $customId = $custom->id ?? 0;
+        $capitalType = dict::getDict('capitalType', 'hdRefund', 'id');
+        $orderSn = $refund->orderSn ?? '';
+
+        $change = [
+            'relateId' => $relateId,
+            'customId' => $customId,
+            'ptStyle' => dict::getDict('ptStyle', 'hd'),
+            'capitalType' => $capitalType,
+            'amount' => $amount,
+            'balance' => $remain,
+            'io' => 0,
+            'payWay' => 0,
+            'event' => "客户申请退款,退款单号:{$orderSn}",
+            'sjId' => $refund->sjId ?? 0,
+            'shopId' => $refund->shopId ?? 0,
+            'mainId' => $refund->mainId ?? 0,
+        ];
+        CustomDebtRecordClass::addChange($change);
+
+        $mainId = $clear->mainId ?? 0;
+        $main = MainClass::getLockById($mainId);
+        if (empty($main)) {
+            util::fail('没有main信息');
+        }
+        $debt = $main->debt ?? 0;
+        $totalRemain = bcsub($debt, $amount, 2);
+        $main->debt = $totalRemain;
+        $main->save();
+
+        $event = $custom->name . "申请退款,退款单号:{$orderSn}";
+        $change['balance'] = $totalRemain;
+        $change['event'] = $event;
+        TotalDebtChangeClass::addChange($change);
+    }
+
     //客户结账欠款金额减少 ssh 20220908
     public static function clearDebtAmountReduce($custom, $amount, $clear)
     {

+ 69 - 40
biz-hd/refund/services/HdRefundService.php

@@ -8,6 +8,7 @@ use biz\shop\classes\ShopCapitalClass;
 use biz\stat\classes\StatOutClass;
 use biz\stat\classes\StatRefundClass;
 use bizHd\base\services\BaseService;
+use bizHd\custom\classes\CustomClass;
 use bizHd\goods\classes\GoodsClass;
 use bizHd\merchant\classes\ShopClass;
 use bizHd\order\classes\OrderClass;
@@ -16,6 +17,7 @@ use bizHd\order\classes\OrderItemClass;
 use bizHd\refund\classes\HdRefundClass;
 use bizHd\refund\classes\HdRefundGoodsClass;
 use bizHd\refund\classes\HdRefundItemClass;
+use bizGhs\shop\classes\ShopMoneyClass;
 use bizHd\wx\classes\WxOpenClass;
 use common\components\dict;
 use common\components\orderSn;
@@ -39,13 +41,13 @@ class HdRefundService extends BaseService
         $order->refundPrice = $refundPrice;
         $currentTkPrice = bcadd($order->tkPrice, $refundPrice, 2);
         if ($currentTkPrice > $order->mainPay) {
-            util::fail('退款金额超过支付金额');
+            util::fail('可退款金额不足');
         }
         $order->tkPrice = $currentTkPrice;
         $order->refund = OrderClass::REFUND_YES;
         $currentActPrice = bcsub($order->actPrice, $refundPrice, 2);
         if ($currentActPrice < 0) {
-            util::fail('退款金额有问题!');
+            util::fail('可退款金额不足!');
         }
         $order->actPrice = $currentActPrice;
         $order->realPrice = $currentActPrice;
@@ -193,6 +195,7 @@ class HdRefundService extends BaseService
 
         $payWay = $order->payWay;
         $shopId = $order->shopId;
+        $onlinePay = $order->onlinePay ?? 1;
         $main = MainClass::getLockById($mainId);
         $shop = ShopClass::getLockById($shopId);
         if (empty($main)) {
@@ -229,54 +232,80 @@ class HdRefundService extends BaseService
         //退款统计
         StatRefundClass::replace($main, $shop, $payWay, $refundPrice);
 
-        //退款流程
-        if ($payWay == dict::getDict('payWay', 'wxPay')) {
+        if ($onlinePay == dict::getDict('onlinePay', 'yes')) {
+            //退款流程
+            if ($payWay == dict::getDict('payWay', 'wxPay')) {
 
-            //门店余额减少
-            \biz\shop\classes\ShopClass::hdSaleRefundReduceBalance($main, $shop, $refund, $refundPrice);
+                //门店余额减少
+                \biz\shop\classes\ShopClass::hdSaleRefundReduceBalance($main, $shop, $refund, $refundPrice);
 
-            //微信收款的原路退回
-            ini_set('date.timezone', 'Asia/Shanghai');
-            $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
-            require_once($wx . '/lib/WxPay.Api.php');
-            require_once($wx . '/example/WxPay.Config.php');
+                //微信收款的原路退回
+                ini_set('date.timezone', 'Asia/Shanghai');
+                $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
+                require_once($wx . '/lib/WxPay.Api.php');
+                require_once($wx . '/example/WxPay.Config.php');
 
-            $totalFee = $order->mainPay * 100;
-            $refundFee = $refundPrice * 100;
+                $totalFee = $order->mainPay * 100;
+                $refundFee = $refundPrice * 100;
 
-            $merchantExt = WxOpenClass::getMallWxInfo();
-            $mid = $merchantExt['wxPayMerchantId'] ?? '';
-            $appId = $merchantExt['miniAppId'] ?? '';
-            $key = $merchantExt['wxPayKey'] ?? '';
+                $merchantExt = WxOpenClass::getMallWxInfo();
+                $mid = $merchantExt['wxPayMerchantId'] ?? '';
+                $appId = $merchantExt['miniAppId'] ?? '';
+                $key = $merchantExt['wxPayKey'] ?? '';
 
-            $input = new \WxPayRefund();
-            $input->SetTransaction_id($transaction_id);
-            $input->SetTotal_fee($totalFee);
-            $input->SetRefund_fee($refundFee);
-            Yii::info('totalFee:' . $totalFee . ' refundFee:' . $refundFee);
-            $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-refund-callback/');
+                $input = new \WxPayRefund();
+                $input->SetTransaction_id($transaction_id);
+                $input->SetTotal_fee($totalFee);
+                $input->SetRefund_fee($refundFee);
+                Yii::info('totalFee:' . $totalFee . ' refundFee:' . $refundFee);
+                $input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-refund-callback/');
 
-            $config = new \WxPayConfig();
-            $config->SetAppId($appId);
-            $config->SetMerchantId($mid);
-            $config->SetKey($key);
+                $config = new \WxPayConfig();
+                $config->SetAppId($appId);
+                $config->SetMerchantId($mid);
+                $config->SetKey($key);
 
-            $input->SetOut_refund_no($refundSn);
-            $input->SetOp_user_id($mid);
-            $return = \WxPayApi::refund($config, $input);
-            if (isset($return['return_code']) == false || $return['return_code'] != 'SUCCESS' || isset($return['result_code']) == false || $return['result_code'] != 'SUCCESS') {
-                $str = '退款失败:';
-                foreach ($return as $rKey => $text) {
-                    $str .= $rKey . ':' . $text . " | ";
+                $input->SetOut_refund_no($refundSn);
+                $input->SetOp_user_id($mid);
+                $return = \WxPayApi::refund($config, $input);
+                if (isset($return['return_code']) == false || $return['return_code'] != 'SUCCESS' || isset($return['result_code']) == false || $return['result_code'] != 'SUCCESS') {
+                    $str = '退款失败:';
+                    foreach ($return as $rKey => $text) {
+                        $str .= $rKey . ':' . $text . " | ";
+                    }
+                    Yii::info($str);
+                    util::fail('退款失败');
                 }
-                Yii::info($str);
-                util::fail('退款失败');
+                $wxRefundId = $return['refund_id'] ?? '';
+                $refund->thirdRefundNo = $wxRefundId;
+                $refund->save();
+            } else {
+                util::fail('此支付方式暂不支持退款');
             }
-            $wxRefundId = $return['refund_id'] ?? '';
-            $refund->thirdRefundNo = $wxRefundId;
-            $refund->save();
         } else {
-            util::fail('暂时只支持微信支付订单退款');
+            if ($payWay == dict::getDict('payWay', 'debtPay')) {
+
+                $settleId = $order->settleId;
+                if (!empty($settleId)) {
+                    util::fail('订单已结账,无法再发起退款');
+                }
+
+                //客户欠款金额减少
+                $customId = $order->customId ?? 0;
+                $custom = CustomClass::getLockById($customId);
+                if (empty($custom)) {
+                    util::fail('没有找到客户');
+                }
+                CustomClass::refundDebtAmountReduce($custom, $refundPrice, $refund);
+            } elseif ($payWay == dict::getDict('payWay', 'cash')) {
+                $moneyData = [
+                    'shopId' => $shopId,
+                    'amount' => $refundPrice,
+                    'orderSn' => $order->orderSn,
+                    'mainId' => $mainId,
+                ];
+                ShopMoneyClass::hdRefundSkOutAmount($moneyData, $main);
+            }
         }
         return $refund;
     }

+ 2 - 0
common/components/dict.php

@@ -242,6 +242,7 @@ class dict
             'expendRegister' => ['id' => 57, 'name' => 'expendRegister'],
             //花店销售结账单
             'hdXsClear' => ['id' => 58, 'name' => 'hdXsClear'],
+            'hdRefund' => ['id' => 59, 'name' => 'hdRefund'],
         ],
         "capitalTypeList" => [//流水类型的对应链接,后台收支明细查看时跳转的链接
             0 => ['link' => '/capital/order-detail', 'name' => '网店', 'orderLink' => '/order/detail', 'id' => 0,],
@@ -273,6 +274,7 @@ class dict
             56 => ['link' => '', 'name' => '取出现金', 'id' => 56,],
             57 => ['link' => '', 'name' => '支出登记', 'id' => 57,],
             58 => ['link' => '', 'name' => '结算', 'id' => 58,],
+            59 => ['link' => '', 'name' => '退货退款', 'id' => 59,],
         ],
 
         //用户来源