shish 5 سال پیش
والد
کامیت
4c29db0d36

+ 1 - 2
app-ghs/controllers/RefundController.php

@@ -15,7 +15,6 @@ class RefundController extends BaseController
     //列表
     public function actionList()
     {
-        $get = Yii::$app->request->get();
         $where = [];
         $where['shopId'] = $this->shopId;
         $list = RefundOrderClass::getOrderList($where);
@@ -113,7 +112,7 @@ class RefundController extends BaseController
             $transaction->commit();
         } catch (\Exception $exception) {
             $transaction->rollBack();
-            Yii::info("退款操作报错:" . $exception->getMessage());
+            Yii::info("退款出错了,报错信息:" . $exception->getMessage());
             util::fail('操作失败');
         }
         util::complete();

+ 0 - 3
biz-ghs/order/services/OrderService.php

@@ -595,9 +595,6 @@ class OrderService extends BaseService
         $cgRefund->saleRefundId = $saleRefundId;
         $cgRefund->save();
 
-        $order->refund = OrderClass::REFUND_YES;
-        $order->save();
-
         return true;
     }
 

+ 107 - 85
biz-ghs/order/services/RefundOrderService.php

@@ -7,6 +7,7 @@ use biz\shop\classes\ShopClass;
 use biz\stat\classes\StatOutClass;
 use bizGhs\base\services\BaseService;
 use bizGhs\custom\classes\CustomClass;
+use bizGhs\order\classes\OrderClass;
 use bizGhs\order\classes\RefundOrderClass;
 use bizGhs\order\classes\RefundOrderItemClass;
 use bizGhs\stock\classes\StockRecordClass;
@@ -24,6 +25,9 @@ class RefundOrderService extends BaseService
     //新增退款
     public static function addRefund($post, $order)
     {
+        $order->refund = OrderClass::REFUND_YES;
+        $order->save();
+
         $data = [];
         $data['status'] = RefundOrderClass::STATUS_COMPLETE;
         $orderSn = orderSn::getGhsRefundSn();
@@ -51,101 +55,119 @@ class RefundOrderService extends BaseService
         $data['refundPrice'] = $refundPrice;
         $data['refundType'] = $post['refundType'] ?? 0;
 
-        try {
-
-            //写入订单表
-            $refund = RefundOrderClass::add($data, true);
-            if ($data['refundType'] == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
-                //退货,加库存
-                foreach ($ghsItemInfo as $k => $v) {
-                    $stockInfo = ProductClass::addStock($v['productId'], $v['bigNum'], $v['smallNum']);
-                    $ghsItemInfo[$k]['oldStock'] = $stockInfo['oldStock'];
-                    $ghsItemInfo[$k]['newStock'] = $stockInfo['newStock'];
-                }
-            }
-            //写入详情表
-            if ($ghsItemInfo) {
-                $batchData = RefundOrderClass::groupOrderItem($ghsItemInfo, $orderSn);
-                RefundOrderItemClass::batchAddOrderItem($batchData);
+        //写入订单表
+        $refund = RefundOrderClass::add($data, true);
+        if ($data['refundType'] == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
+            //退货,加库存
+            foreach ($ghsItemInfo as $k => $v) {
+                $stockInfo = ProductClass::addStock($v['productId'], $v['bigNum'], $v['smallNum']);
+                $ghsItemInfo[$k]['oldStock'] = $stockInfo['oldStock'];
+                $ghsItemInfo[$k]['newStock'] = $stockInfo['newStock'];
             }
-            $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);
+        }
+        //写入详情表
+        if ($ghsItemInfo) {
+            $batchData = RefundOrderClass::groupOrderItem($ghsItemInfo, $orderSn);
+            RefundOrderItemClass::batchAddOrderItem($batchData);
+        }
+        $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 = $order->payWay;
+        $shopId = $order->shopId;
+        $shop = ShopClass::getLockById($shopId);
+        if (empty($shop)) {
+            util::fail('没有找到门店');
+        }
+        //总支出增加
+        $currentTotalExpend = bcadd($shop->totalExpend, $refundPrice, 2);
+        $shop->totalExpend = $currentTotalExpend;
+        $shop->save();
+
+        //支出流水
+        $thisType = dict::getDict('capitalType', 'saleRefund', 'id');
+        $sjId = $order->sjId ?? 0;
+        $event = '销售退款';
+        $capitalData = [
+            'capitalType' => $thisType,
+            'io' => 0,
+            'totalExpend' => $currentTotalExpend,
+            'payWay' => 0,
+            'amount' => $refundPrice,
+            'sjId' => $sjId,
+            'shopId' => $shopId,
+            'event' => $event,
+        ];
+        ShopCapitalClass::addCapital($capitalData);
+        //当天和当月支出统计
+        StatOutClass::updateOrInsert($shop, $refundPrice);
+
+        if ($payWay == dict::getDict('payWay', 'wxPay')) {
+            //门店余额减少
+            ShopClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
+        } elseif ($payWay == dict::getDict('payWay', 'debtPay')) {
+
+            $clearId = $order->clearId;
+            if (empty($clearId)) {
+                util::fail('订单已月结,无法再发起退款');
             }
 
-            $payWay = $order->payWay;
-            $shopId = $order->shopId;
-            $shop = ShopClass::getLockById($shopId);
-            if (empty($shop)) {
-                util::fail('没有找到门店');
+            //门店应收款减少
+            $currentMayGathering = bcsub($shop->mayGathering, $refundPrice, 2);
+            if ($currentMayGathering < 0) {
+                util::fail('应付款有问题');
             }
-            //总支出增加
-            $currentTotalExpend = bcadd($shop->totalExpend, $refundPrice, 2);
-            $shop->totalExpend = $currentTotalExpend;
+            $shop->mayGathering = $currentMayGathering;
             $shop->save();
 
-            //支出流水
-            $thisType = dict::getDict('capitalType', 'saleRefund', 'id');
-            $sjId = $order->sjId ?? 0;
-            $event = '销售退款';
-            $capitalData = [
-                'capitalType' => $thisType,
-                'io' => 0,
-                'totalExpend' => $currentTotalExpend,
-                'payWay' => 0,
-                'amount' => $refundPrice,
-                'sjId' => $sjId,
-                'shopId' => $shopId,
-                'event' => $event,
-            ];
-            ShopCapitalClass::addCapital($capitalData);
-            //当天和当月支出统计
-            StatOutClass::updateOrInsert($shop, $refundPrice);
-
-            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();
-                if ($currentDebtAmount == 0) {
-                    $shop->mayGatheringNum -= 1;
-                    $shop->save();
-                }
-
-            } elseif ($payWay == dict::getDict('payWay', 'balancePay')) {
-                //门店余额减少
-                ShopClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
-            } else {
-                util::fail('支付方式没有找到');
+            //减掉退款金额
+            $currentRefund = bcadd($order->refundPrice, $refundPrice, 2);
+            $order->refundPrice = $currentRefund;
+            $currentActPrice = bcsub($order->actPrice, $refundPrice, 2);
+            if ($currentActPrice < 0) {
+                util::fail('退款金额有问题!');
+            }
+            $order->actPrice = $currentActPrice;
+            $currentRealPrice = bcsub($order->realPrice, $refundPrice, 2);
+            if ($currentRealPrice < 0) {
+                util::fail('退款金额有问题,请排查');
+            }
+            $order->realPrice = $currentRealPrice;
+            if ($currentRealPrice == 0) {
+                $order->debt = OrderClass::DEBT_NO;
             }
+            $order->save();
 
-            return $refund;
+            //门店客户欠款减少
+            $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();
+            if ($currentDebtAmount == 0) {
+                $shop->mayGatheringNum -= 1;
+                $shop->save();
+            }
 
-        } catch (\Exception $exception) {
-            util::fail("操作失败" . $exception->getMessage());
+        } elseif ($payWay == dict::getDict('payWay', 'balancePay')) {
+            //门店余额减少
+            ShopClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
+        } else {
+            util::fail('支付方式没有找到');
         }
+
+        return $refund;
+
     }
 
 }

+ 23 - 2
biz-hd/cg/services/CgRefundService.php

@@ -31,6 +31,9 @@ class CgRefundService extends BaseService
         if (empty($cg)) {
             util::fail('没有找到采购订单');
         }
+        $cg->refund = PurchaseClass::REFUND_YES;
+        $cg->save();
+
         $transaction_id = $cg->thirdNo ?? '';
         if (empty($transaction_id)) {
             util::fail('第三方支付订单号没有找到');
@@ -146,11 +149,30 @@ class CgRefundService extends BaseService
 
         } elseif ($payWay == dict::getDict('payWay', 'debtPay')) {
 
-            //门店应付总款减少
             $clearId = $cg->clearId ?? 0;
             if (!empty($clearId)) {
                 util::fail('订单已经月结,无法再发起退款');
             }
+
+            //减掉退款金额
+            $currentRefund = bcadd($cg->refundPrice, $refundPrice, 2);
+            $cg->refundPrice = $currentRefund;
+            $currentActPrice = bcsub($cg->actPrice, $refundPrice, 2);
+            if ($currentActPrice < 0) {
+                util::fail('退款金额有问题!');
+            }
+            $cg->actPrice = $currentActPrice;
+            $currentRealPrice = bcsub($cg->realPrice, $refundPrice, 2);
+            if ($currentRealPrice < 0) {
+                util::fail('退款金额有问题,请排查');
+            }
+            if ($currentRealPrice == 0) {
+                $cg->debt = PurchaseClass::DEBT_NO;
+            }
+            $cg->realPrice = $currentRealPrice;
+            $cg->save();
+
+            //门店应付总款减少
             $currentMayPay = bcsub($shop->mayPay, $refundPrice, 2);
             if ($currentMayPay < 0) {
                 util::fail('应付款有问题');
@@ -206,7 +228,6 @@ class CgRefundService extends BaseService
 
     }
 
-
     //供货商productId 转 hdProductId [{productId:1,...}]
     public static function toggleProductList($ghsItemInfo, $hdShopId, $hdSjId)
     {

+ 4 - 0
biz-hd/purchase/classes/PurchaseClass.php

@@ -44,6 +44,10 @@ class PurchaseClass extends BaseClass
     //零售采购生成
     const ORDER_TYPE_HD_ADD = 2;
 
+    const REFUND_NO = 1;
+    const REFUND_YES = 2;
+
+
     //添加采购 ssh 2021.1.18
     public static function addPurchase($data)
     {