shish пре 3 година
родитељ
комит
f7e7531f65

+ 55 - 2
app-hd/controllers/CgRefundController.php

@@ -19,10 +19,63 @@ class CgRefundController extends BaseController
     public function actionCreateRefund()
     {
         $post = Yii::$app->request->post();
+        $id = isset($post['id']) ? $post['id'] : 0;
+        $cg = PurchaseClass::getById($id, true);
+        if (empty($cg)) {
+            util::fail('没有找到采购单');
+        }
+        if ($cg->mainId != $this->mainId) {
+            util::fail('没有权限操作');
+        }
+        if ($cg->status != PurchaseClass::STATUS_COMPLETE) {
+            util::fail("完成的订单才能申请退货退款");
+        }
+
+        $connection = Yii::$app->db;
+        $transaction = $connection->beginTransaction();
+        try {
 
-        CgRefundService::createRefund();
+            //退款类型 1退货并退款 2 仅退款
+            $refundType = $post['refundType'] ?? 1;
+            if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
+                //花材列表结构
+                // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
+                $productJson = $post['product'] ?? '';
+                if (empty($productJson)) {
+                    util::fail('请选择要退款的花材');
+                }
+                $productList = json_decode($productJson, true);
+                if (!is_array($productList)) {
+                    util::fail('请选择要退款的花材哦!');
+                }
+                $post['product'] = $productList;
+            } else {
+                //仅退款花材直接设置为空
+                $post['product'] = [];
+            }
+            $post['price'] = $post['price'] ?? 0;
+            if ($post['price'] <= 0) {
+                util::fail("退款金额不能小于0");
+            }
+            if (bccomp($post['price'], $cg->realPrice, 2) == 1) {
+                util::fail("退款金额超过订单金额");
+            }
+            $post['shopId'] = $this->shopId;
+            $post['sjId'] = $this->sjId;
+            $post['shopAdminId'] = $this->shopAdminId;
+            $post['mainId'] = $this->mainId;
+            $shopAdmin = $this->shopAdmin;
+            $adminName = $shopAdmin['name'] ?? '';
+            $post['shopAdminName'] = $adminName;
+            $respond = CgRefundService::createOrder($post, $cg);
+            $transaction->commit();
+            util::success($respond);
+        } catch (\Exception $exception) {
+            $transaction->rollBack();
+            Yii::info("申请出错了,报错信息:" . $exception->getMessage());
+            util::fail('申请失败');
+        }
 
-        util::success($post);
     }
 
     //列表

+ 2 - 18
biz-ghs/order/services/RefundOrderService.php

@@ -34,9 +34,6 @@ class RefundOrderService extends BaseService
     //新增退款
     public static function addRefund($post, $order)
     {
-        $order->refund = OrderClass::REFUND_YES;
-        $order->save();
-
         $refundPrice = $post['price'] ?? 0;
         if ($refundPrice <= 0) {
             util::fail('请填写退款金额');
@@ -60,7 +57,7 @@ class RefundOrderService extends BaseService
         $postProduct = $post['product'] ?? '';
         if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
             //花材列表结构
-            // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
+            // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
             if (empty($postProduct)) {
                 util::fail('没有退款花材');
             }
@@ -87,20 +84,9 @@ class RefundOrderService extends BaseService
                 $itemCost = $productInfo[$id]['cost'] ?? 0;
                 $ratio = $productInfo[$id]['ratio'] ?? 0;
 
-                $currentOrderItem = $orderItemData[$id] ?? null;
-                if (empty($currentOrderItem)) {
+                if (isset($orderItemData[$id]) == false) {
                     util::fail('没有找到退款的花材');
                 }
-                $orderItemNum = $currentOrderItem->xhNum ?? 0;
-                $refundNum = $currentOrderItem->refundNum ?? 0;
-
-                $currentOrderItem->refundNum = bcadd($refundNum, $num);
-                $currentOrderItem->save();
-
-                $remainNum = bcsub($orderItemNum, $refundNum);
-                if ($num > $remainNum) {
-                    util::fail("{$name}超过可退数量");
-                }
 
                 $itemNum = $num;
                 if ($unitType == dict::getDict('unitType', 'small')) {
@@ -127,14 +113,12 @@ class RefundOrderService extends BaseService
             }
 
         }
-
         $data['customId'] = $order->customId ?? 0;
         $data['remark'] = $post['remark'] ?? '';
         $data['refundPrice'] = $refundPrice;
         $data['refundType'] = $post['refundType'] ?? 0;
         $data['status'] = 0;
         $refund = RefundOrderClass::add($data, true);
-
         return $refund;
     }
 }

+ 80 - 11
biz-hd/cg/services/CgRefundService.php

@@ -7,6 +7,8 @@ use biz\hb\classes\HbClass;
 use biz\shop\classes\ShopCapitalClass;
 use biz\shop\classes\ShopClass;
 use biz\wx\classes\WxMessageClass;
+use bizGhs\order\classes\OrderClass;
+use bizGhs\order\services\RefundOrderService;
 use bizGhs\product\classes\ProductClass;
 use bizGhs\stock\classes\StockRecordClass;
 use bizHd\base\services\BaseService;
@@ -28,24 +30,94 @@ class CgRefundService extends BaseService
 
     public static $baseFile = '\bizHd\cg\classes\CgRefundClass';
 
+    public static function createOrder($cgData, $cg)
+    {
+        $respond = self::addRefund($cgData, $cg);
+        $saleId = $cg->saleId ?? 0;
+        $cgProductList = $cgData['product'] ?? [];
+        if (empty($cgProductList)) {
+            util::fail('没有花材');
+        }
+        $cgIds = array_column($cgProductList, 'productId');
+        $cgProductInfo = ProductClass::getAllByCondition(['id' => ['in', $cgIds]], null, 'id,itemId');
+        if (empty($cgProductInfo)) {
+            util::fail('花材数据有问题');
+        }
+        $cgMap = [];
+        foreach ($cgProductInfo as $cc) {
+            $ccId = $cc['id'] ?? 0;
+            $ccPtItemId = $cc['itemId'] ?? 0;
+            $cgMap[$ccId] = $ccPtItemId;
+        }
+        $ptItemList = array_column($cgProductInfo, 'itemId');
+
+        $order = OrderClass::getById($saleId, true);
+        if (empty($order)) {
+            util::fail('没有订单信息');
+        }
+        $hdRemark = $cgData['hdRemark'] ?? '';
+        $price = $cgData['price'] ?? 0;
+        $refundType = $cgData['refundType'] ?? 1;
+        $imgList = $cgData['imgList'] ?? '';
+        $sjId = $order->sjId ?? 0;
+        $shopId = $order->shopId ?? 0;
+        $mainId = $order->mainId ?? 0;
+
+        $saleProductInfo = ProductClass::getAllByCondition(['mainId' => $mainId, 'itemId' => ['in', $ptItemList]], null, 'id,itemId');
+        if (empty($saleProductInfo)) {
+            util::fail('花材信息有问题');
+        }
+        $saleMap = [];
+        foreach ($saleProductInfo as $ss) {
+            $ssId = $ss['id'] ?? 0;
+            $ssPtItemId = $ss['itemId'] ?? 0;
+            $saleMap[$ssPtItemId] = $ssId;
+        }
+
+        $newProductList = [];
+        foreach ($cgProductList as $cg) {
+            if (isset($cg['productId']) == false) {
+                util::fail('花材有问题呢');
+            }
+            $productId = $cg['productId'];
+            if (isset($cgMap[$productId]) == false) {
+                util::fail('花材有问题哈');
+            }
+            $ptItemId = $cgMap[$productId];
+            if (isset($saleMap[$ptItemId]) == false) {
+                util::fail('花材有问题哦');
+            }
+            $newProductId = $saleMap[$ptItemId];
+            $cg['productId'] = $newProductId;
+            $newProductList[] = $cg;
+        }
+
+        $orderData = [
+            'hdRemark' => $hdRemark,
+            'price' => $price,
+            'refundType' => $refundType,
+            'imgList' => $imgList,
+            'sjId' => $sjId,
+            'shopId' => $shopId,
+            'mainId' => $mainId,
+            'product' => $newProductList,
+        ];
+        RefundOrderService::addRefund($orderData, $order);
+        return $respond;
+    }
+
     //新增退款
     public static function addRefund($post, $cg)
     {
-        $cgId = $cg->id ?? 0;
         $mainId = $cg->mainId ?? 0;
         $shopId = $cg->shopId ?? 0;
         $sjId = $cg->sjId ?? 0;
-        $payWay = $cg->payWay;
-        $onlinePay = $cg->onlinePay ?? 1;
         $cgOrderSn = $cg->orderSn ?? '';
-        $cg->refund = PurchaseClass::REFUND_YES;
-        $cg->save();
 
-        $transaction_id = $cg->thirdNo ?? '';
         $refundOrderSn = orderSn::getCgRefundSn();
         $refundPrice = $post['price'] ?? 0;
         if (is_numeric($refundPrice) == false || $refundPrice < 0) {
-            util::fail('退款金额有问题');
+            util::fail('请填写退款金额');
         }
         $shop = ShopClass::getLockById($shopId);
         if (empty($shop)) {
@@ -100,12 +172,9 @@ class CgRefundService extends BaseService
                 $productId = $info['productId'] ?? 0;
                 $ptItemId = $info['ptItemId'] ?? 0;
 
-                $currentCgItem = $cgItemInfo[$productId] ?? null;
-                if (empty($currentCgItem)) {
+                if (isset($cgItemInfo[$productId]) == false) {
                     util::fail('没有找到花材哦!');
                 }
-                $currentCgItem->refundNum = bcadd($currentCgItem->refundNum, $num);
-                $currentCgItem->save();
 
                 $refundData = [];
                 $refundData['orderSn'] = $refundOrderSn;