Kaynağa Gözat

退款流程

shish 5 yıl önce
ebeveyn
işleme
b3d9ba28a9

+ 47 - 13
biz-ghs/order/services/RefundOrderService.php

@@ -2,10 +2,13 @@
 
 namespace bizGhs\order\services;
 
+use biz\shop\classes\ShopClass;
 use bizGhs\base\services\BaseService;
+use bizGhs\custom\classes\CustomClass;
 use bizGhs\order\classes\RefundOrderClass;
 use bizGhs\order\classes\RefundOrderItemClass;
 use bizGhs\stock\classes\StockRecordClass;
+use common\components\dict;
 use common\components\orderSn;
 use common\components\util;
 use bizGhs\product\classes\ProductClass;
@@ -17,13 +20,13 @@ class RefundOrderService extends BaseService
     public static $baseFile = '\bizGhs\order\classes\RefundOrderClass';
 
     //新增退款
-    public static function addRefund($post, $saleOrder)
+    public static function addRefund($post, $order)
     {
         $data = [];
         $data['status'] = RefundOrderClass::STATUS_COMPLETE;
         $orderSn = orderSn::getGhsRefundSn();
         $data['orderSn'] = $orderSn;
-        $data['relateOrderSn'] = $saleOrder->orderSn ?? '';
+        $data['relateOrderSn'] = $order->orderSn ?? '';
         $data['sjId'] = $post['sjId'];
         $data['shopId'] = $post['shopId'];
         $data['shopAdminId'] = $post['shopAdminId'] ?? 0;
@@ -39,12 +42,17 @@ class RefundOrderService extends BaseService
         $data['bigNum'] = $sumNum['bigNum'] ?? 0;// 总的大单位数量 总共多少扎
         $data['smallNum'] = $sumNum['smallNum'] ?? 0; //总的小单位数量 总共多少支
         $data['remark'] = $post['remark'] ?? '';
-        $data['refundPrice'] = $post['price'] ?? 0;
+        $refundPrice = $post['price'] ?? 0;
+        if (is_numeric($refundPrice) == false || $refundPrice < 0) {
+            util::fail('退款金额有问题');
+        }
+        $data['refundPrice'] = $refundPrice;
         $data['refundType'] = $post['refundType'] ?? 0;
 
         try {
+
             //写入订单表
-            $order = RefundOrderClass::add($data, true);
+            $refund = RefundOrderClass::add($data, true);
             if ($data['refundType'] == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
                 //退货,加库存
                 foreach ($ghsItemInfo as $k => $v) {
@@ -58,28 +66,54 @@ class RefundOrderService extends BaseService
                 $batchData = RefundOrderClass::groupOrderItem($ghsItemInfo, $orderSn);
                 RefundOrderItemClass::batchAddOrderItem($batchData);
             }
-            $orderData = RefundOrderClass::getOrderDetail($orderSn, $order->shopId);
+            $orderData = RefundOrderClass::getOrderDetail($orderSn, $refund->shopId);
             $orderData['shopId'] = $data['shopId'];
             if ($data['refundType'] == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
                 //写流水
                 StockRecordClass::addRecordByOrder($orderData, StockRecordClass::STOCK_RECORD_TYPE_REFUND);
             }
 
-            $payWay = 0;
+            $payWay = $order->payWay;
+            $shopId = $order->shopId;
+            $shop = ShopClass::getLockById($shopId);
+            if (empty($shop)) {
+                util::fail('没有找到门店');
+            }
 
             if ($payWay == dict::getDict('payWay', 'wxPay')) {
-                //无操作
+                //门店余额减少
+                ShopClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
             } elseif ($payWay == dict::getDict('payWay', 'debtPay')) {
-                //供应商应收款减少
+
+                //门店应收款减少
+                $currentMayGathering = bcsub($shop->mayGathering, $refundPrice, 2);
+                if ($currentMayGathering < 0) {
+                    util::fail('应付款有问题');
+                }
+                $shop->mayGathering = $currentMayGathering;
+                $shop->save();
+
+                //门店客户欠款减少
+                $customId = $order->customId;
+                $custom = CustomClass::getLockById($customId);
+                if (empty($custom)) {
+                    util::fail('没有找到客户');
+                }
+                $currentDebtAmount = bcsub($custom->debtAmount, $refundPrice, 2);
+                if ($currentDebtAmount < 0) {
+                    util::fail('客户欠款有问题');
+                }
+                $custom->debtAmount = $currentDebtAmount;
+                $custom->save();
+
             } elseif ($payWay == dict::getDict('payWay', 'balancePay')) {
-                //供应商余额减少
+                //门店余额减少
+                ShopClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
             } else {
-
+                util::fail('支付方式没有找到');
             }
 
-            //引起供应商支出增加
-
-            return $order;
+            return $refund;
         } catch (\Exception $exception) {
             util::fail("操作失败" . $exception->getMessage());
         }

+ 18 - 67
biz-hd/cg/services/CgRefundService.php

@@ -2,6 +2,7 @@
 
 namespace bizHd\cg\services;
 
+use biz\ghs\classes\GhsClass;
 use biz\pt\classes\PtAssetClass;
 use biz\pt\classes\PtYeChangeClass;
 use biz\shop\classes\ShopCapitalClass;
@@ -55,9 +56,6 @@ class CgRefundService extends BaseService
         $shop->totalIncome = $currentTotalIncome;
         $shop->save();
 
-        $ptStyle = $shop->ptStyle;
-        $sjId = $shop->sjId;
-        $shopId = $shop->id;
         $payWay = $cg->payWay;
         $currentType = dict::getDict('capitalType', 'cgRefund', 'id');
 
@@ -105,10 +103,10 @@ class CgRefundService extends BaseService
 
         } elseif ($payWay == dict::getDict('payWay', 'debtPay')) {
 
-            //客户应付款减少
+            //门店应付总款减少
             $clearId = $cg->clearId ?? 0;
             if (!empty($clearId)) {
-                util::fail('订单已经结,无法再发起退款');
+                util::fail('订单已经结,无法再发起退款');
             }
             $currentMayPay = bcsub($shop->mayPay, $refundPrice, 2);
             if ($currentMayPay < 0) {
@@ -117,70 +115,23 @@ class CgRefundService extends BaseService
             $shop->mayPay = $currentMayPay;
             $shop->save();
 
-        } elseif ($payWay == dict::getDict('payWay', 'balancePay')) {
-
-            //客户余额增加
-            $currentBalance = bcadd($shop->balance, $refundPrice, 2);
-            $shop->balance = $currentBalance;
-            $shop->save();
-            $currentTxBalance = $shop->txBalance;
-
-            $id = $cgRefund->id;
-            $change = [
-                'relateId' => $id,
-                'capitalType' => $currentType,
-                'amount' => $refundPrice,
-                'balance' => $currentBalance,
-                'txBalance' => $currentTxBalance,
-                'io' => 1,
-                'payWay' => 0,
-                'event' => "采购退款",
-                'sjId' => $sjId,
-                'shopId' => $shopId,
-                'tx' => dict::getDict('yeTx', 'canNot'),
-                'ptStyle' => $ptStyle,
-            ];
-            ShopYeChangeClass::addChange($change, true);
-
-            $asset = null;
-            if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
-                $asset = PtAssetClass::getHdBalance();
-            } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
-                $asset = PtAssetClass::getGhsBalance();
-            } else {
-                util::fail('没有找到平台');
+            //门店应付供应商款减少
+            $ghsId = $cg->ghsId;
+            $ghs = GhsClass::getLockById($ghsId);
+            if (empty($ghs)) {
+                util::fail('没有找到供应商');
             }
-            $currentPtBalance = bcadd($asset->amount, $refundPrice, 2);
-            $asset->amount = $currentPtBalance;
-            $asset->save();
-
-            $txAsset = null;
-            if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
-                $txAsset = PtAssetClass::getHdTxBalance();
-            } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
-                $txAsset = PtAssetClass::getGhsTxBalance();
-            } else {
-                util::fail('没有找到平台');
+            $currentDebtAmount = bcsub($ghs->debtAmount, $refundPrice, 2);
+            if ($currentDebtAmount < 0) {
+                util::fail('供应商欠款有问题!');
             }
-            $currentPtTxBalance = $txAsset->amount;
-
-            $change = [
-                'relateId' => $id,
-                'capitalType' => $currentType,
-                'amount' => $refundPrice,
-                'balance' => $currentPtBalance,
-                'txBalance' => $currentPtTxBalance,
-                'io' => 1,
-                'payWay' => 0,
-                'event' => "【{$shop->merchantName} {$shop->shopName}】的采购被退款",
-                'sjId' => $sjId,
-                'shopId' => $shopId,
-                'tx' => dict::getDict('yeTx', 'canNot'),
-                'ptStyle' => $ptStyle,
-                'shopName' => $shop->shopName,
-                'sjName' => $shop->merchantName,
-            ];
-            PtYeChangeClass::addChange($change, true);
+            $ghs->debtAmount = $currentDebtAmount;
+            $ghs->save();
+
+        } elseif ($payWay == dict::getDict('payWay', 'balancePay')) {
+
+            //门店余额增加
+            ShopClass::cgRefundAddBalance($shop, $refundPrice, $cgRefund);
 
         } else {
             util::fail('支付方式没有找到哦!');

+ 105 - 1
biz/pt/classes/PtAssetClass.php

@@ -163,7 +163,7 @@ class PtAssetClass extends BaseClass
         PtYeChangeClass::addChange($change, true);
     }
 
-    //平台余额增加 shish 20210519
+    //平台余额减少 shish 20210519
     public static function reduceBalance($shop, $amount, $order, $capitalType, $tx)
     {
         $ptStyle = $shop->ptStyle;
@@ -218,6 +218,63 @@ class PtAssetClass extends BaseClass
         PtYeChangeClass::addChange($change, true);
     }
 
+    public static function saleRefundReduceBalance($shop, $refund, $refundPrice)
+    {
+        $ptStyle = $shop->ptStyle;
+        $asset = null;
+        if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
+            $asset = self::getHdBalance();
+        } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
+            $asset = self::getGhsBalance();
+        } else {
+            util::fail('没有找到平台');
+        }
+        if (bccomp($refundPrice, $asset->amount, 2) == 1) {
+            util::fail('平台余额不足');
+        }
+        $currentAmount = bcsub($asset->amount, $refundPrice, 2);
+        $asset->amount = $currentAmount;
+        $asset->save();
+
+        $txAsset = null;
+        if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
+            $txAsset = self::getHdTxBalance();
+        } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
+            $txAsset = self::getGhsTxBalance();
+        } else {
+            util::fail('没有找到平台');
+        }
+        $tx = dict::getDict('yeTx', 'can');
+        $currentTxBalance = $txAsset->amount;
+        if (!empty($currentTxBalance)) {
+            $reduceTxBalance = $currentTxBalance > $refundPrice ? $refundPrice : $currentTxBalance;
+            $currentTxBalance = bcsub($currentTxBalance, $reduceTxBalance, 2);
+            $txAsset->amount = $currentTxBalance;
+            $txAsset->save();
+        }
+
+        $id = $refund->id;
+        $currentType = dict::getDict('capitalType', 'saleRefund');
+        $change = [
+            'relateId' => $id,
+            'capitalType' => $currentType,
+            'amount' => $refundPrice,
+            'balance' => $currentAmount,
+            'txBalance' => $currentTxBalance,
+            'io' => 0,
+            'payWay' => 0,
+            'event' => "【{$shop->merchantName} {$shop->shopName}】 的销售单发起退款",
+            'sjId' => $shop->sjId,
+            'shopId' => $shop->shopId,
+            'tx' => $tx,
+            'ptStyle' => $ptStyle,
+            'shopName' => $shop->shopName,
+            'sjName' => $shop->merchantName,
+        ];
+        PtYeChangeClass::addChange($change, true);
+    }
+
+
     //零售平台余额对象 shish 20210519
     public static function getHdBalance()
     {
@@ -290,4 +347,51 @@ class PtAssetClass extends BaseClass
         return $asset;
     }
 
+    //零售采购退款
+    public static function cgRefundAddBalance($shop, $refundPrice, $cgRefund, $currentType, $tx)
+    {
+        $ptStyle = $shop->ptStyle;
+        $asset = null;
+        if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
+            $asset = PtAssetClass::getHdBalance();
+        } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
+            $asset = PtAssetClass::getGhsBalance();
+        } else {
+            util::fail('没有找到平台');
+        }
+        $currentPtBalance = bcadd($asset->amount, $refundPrice, 2);
+        $asset->amount = $currentPtBalance;
+        $asset->save();
+
+        $txAsset = null;
+        if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
+            $txAsset = PtAssetClass::getHdTxBalance();
+        } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
+            $txAsset = PtAssetClass::getGhsTxBalance();
+        } else {
+            util::fail('没有找到平台');
+        }
+        $currentPtTxBalance = $txAsset->amount;
+        $id = $cgRefund->id;
+        $sjId = $shop->sjId;
+        $shopId = $shop->id;
+        $change = [
+            'relateId' => $id,
+            'capitalType' => $currentType,
+            'amount' => $refundPrice,
+            'balance' => $currentPtBalance,
+            'txBalance' => $currentPtTxBalance,
+            'io' => 1,
+            'payWay' => 0,
+            'event' => "【{$shop->merchantName} {$shop->shopName}】的采购被退款",
+            'sjId' => $sjId,
+            'shopId' => $shopId,
+            'tx' => dict::getDict('yeTx', 'canNot'),
+            'ptStyle' => $ptStyle,
+            'shopName' => $shop->shopName,
+            'sjName' => $shop->merchantName,
+        ];
+        PtYeChangeClass::addChange($change, true);
+    }
+
 }

+ 77 - 0
biz/shop/classes/ShopClass.php

@@ -375,6 +375,83 @@ class ShopClass extends BaseClass
 
     }
 
+    //零售采购退款
+    public static function cgRefundAddBalance($shop, $refundPrice, $cgRefund)
+    {
+        //客户余额增加
+        $currentBalance = bcadd($shop->balance, $refundPrice, 2);
+        $shop->balance = $currentBalance;
+        $shop->save();
+        $currentTxBalance = $shop->txBalance;
+        $currentType = dict::getDict('capitalType', 'cgRefund', 'id');
+        $id = $cgRefund->id;
+        $ptStyle = $shop->ptStyle;
+        $sjId = $shop->sjId;
+        $shopId = $shop->id;
+        $tx = dict::getDict('yeTx', 'canNot');
+        $change = [
+            'relateId' => $id,
+            'capitalType' => $currentType,
+            'amount' => $refundPrice,
+            'balance' => $currentBalance,
+            'txBalance' => $currentTxBalance,
+            'io' => 1,
+            'payWay' => 0,
+            'event' => "采购退款",
+            'sjId' => $sjId,
+            'shopId' => $shopId,
+            'tx' => $tx,
+            'ptStyle' => $ptStyle,
+        ];
+        ShopYeChangeClass::addChange($change, true);
+
+        PtAssetClass::cgRefundAddBalance($shop, $refundPrice, $cgRefund, $currentType, $tx);
+    }
+
+    //销售单退款减少余额 ssh 2021.2.21
+    public static function saleRefundReduceBalance($shop, $refund, $refundPrice)
+    {
+        //门店余额减少
+        $currentBalance = bcsub($shop->balance, $refundPrice, 2);
+        if ($currentBalance < 0) {
+            util::fail('余额不足,请先充值');
+        }
+        $shop->balance = $currentBalance;
+        $currentTx = $shop->txBalance;
+        if ($currentTx > 0) {
+            if ($refundPrice > $currentTx) {
+                $currentTx = 0;
+            } else {
+                $currentTx = bcsub($currentTx, $refundPrice, 2);
+            }
+        }
+        $shop->txBalance = $currentTx;
+        $shop->save();
+
+        $currentType = dict::getDict('capitalType', 'saleRefund');
+        $id = $refund->id;
+        $event = "采购退款给客户";
+        $tx = dict::getDict('yeTx', 'can');
+        $change = [
+            'relateId' => $id,
+            'capitalType' => $currentType,
+            'amount' => $refundPrice,
+            'balance' => $currentBalance,
+            'txBalance' => $currentTx,
+            'io' => 1,
+            'payWay' => 0,
+            'event' => $event,
+            'sjId' => $shop->sjId,
+            'shopId' => $shop->shopId,
+            'tx' => $tx,
+            'ptStyle' => $shop->ptStyle,
+        ];
+        ShopYeChangeClass::addChange($change, true);
+
+        //平台余额增加
+        PtAssetClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
+    }
+
     //获取商家所有的门店 ssh 2021.2.26
     public static function getSjShopList($sjId)
     {