Explorar el Código

退款完成待测试

shish hace 4 años
padre
commit
c5efa49988

+ 47 - 4
biz-ghs/order/services/OrderService.php

@@ -785,13 +785,56 @@ class OrderService extends BaseService
         $saleRefund = RefundOrderService::addRefund($post, $order);
         $saleRefundId = $saleRefund->id;
 
+
+        $purchaseId = $order->purchaseId;
+        $cg = PurchaseClass::getLockById($purchaseId);
+        if (empty($cg)) {
+            util::fail('没有找到采购订单');
+        }
+        $mainId = $cg->mainId ?? 0;
+
         //花材列表结构
         // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
+        //供应商的花材信息转换成零售店的花材信息
         $ghsProduct = $post['product'] ?? '';
-
-
-        //零售采购单退款
-        $cgRefund = CgRefundService::addRefund($post, $order);
+        $ghsProductIndex = array_column($ghsProduct, null, 'productId');
+        $ids = array_column($ghsProduct, 'productId');
+        $ghsProductInfo = ProductClass::getByIds($ids, null, 'itemId');
+        $ptItemIds = array_keys($ghsProductInfo);
+        $ptItemIds = array_unique(array_filter($ptItemIds));
+        $hdProductInfo = ProductClass::getAllByCondition(['mainId' => $mainId, 'itemId' => ['in', $ptItemIds]], null, '*', 'itemId');
+
+        $newProduct = [];
+        foreach ($hdProductInfo as $ptItemId => $hdInfo) {
+            $ghsItemId = $ghsProductInfo[$ptItemId]['id'] ?? 0;
+            $current = $ghsProductIndex[$ghsItemId] ?? [];
+            $hdItemId = $hdInfo['id'] ?? 0;
+            $cost = $hdInfo['cost'] ?? 0;
+            $name = $hdInfo['name'] ?? '';
+            $cover = $hdInfo['cover'] ?? '';
+            $ratio = $hdInfo['ratio'] ?? 20;
+            if (empty($current)) {
+                util::fail('没有找到花材');
+            }
+            $current['productId'] = $hdItemId;
+            $current['ptItemId'] = $ptItemId;
+            $current['cost'] = $cost;
+            $current['name'] = $name;
+            $current['cover'] = $cover;
+            $current['ratio'] = $ratio;
+            $newProduct[] = $current;
+        }
+
+        $refundType = $post['refundType'] ?? 0;
+        $price = $post['price'] ?? 0;
+        $remark = $post['remark'] ?? '';
+        $cgData = [
+            'product' => $newProduct,
+            'refundType' => $refundType,
+            'price' => $price,
+            'remark' => $remark,
+        ];
+        $cgRefund = CgRefundService::addRefund($cgData, $cg);
         $cgRefundId = $cgRefund->id;
 
         $saleRefund->cgRefundId = $cgRefundId;

+ 7 - 0
biz-ghs/stock/classes/StockRecordClass.php

@@ -118,6 +118,13 @@ class StockRecordClass extends BaseClass
         }
     }
 
+    //花店采购申请退货退款库存变动记录 ssh 20220330
+    public static function hdCgApplyRefundAddRecord($refundRecord)
+    {
+        $refundRecord['recordType'] = self::STOCK_RECORD_TYPE_REFUND;
+        self::add($refundRecord);
+    }
+
     //供应商给零售店退款退货增加库存变动记录 ssh 20220330
     public static function ghsRefundAddRecord($refundRecord)
     {

+ 49 - 1
biz-hd/cg/classes/CgRefundItemClass.php

@@ -2,22 +2,70 @@
 
 namespace bizHd\cg\classes;
 
+use bizGhs\stock\classes\StockRecordClass;
+use common\components\dict;
 use Yii;
 use bizGhs\base\classes\BaseClass;
 use bizGhs\order\traits\OrderTrait;
 use bizGhs\product\classes\ProductClass;
+
 class CgRefundItemClass extends BaseClass
 {
     use OrderTrait;
     public static $baseFile = '\bizHd\cg\models\CgRefundItem';
 
+    public static function addData($data)
+    {
+        //退货减库存
+        $productId = $data['productId'] ?? 0;
+        $unitType = $data['xhUnitType'] ?? 0;
+        $mainId = $data['mainId'] ?? 0;
+        $ptItemId = $data['itemId'] ?? 0;
+        $orderSn = $data['orderSn'] ?? '';
+        $sjId = $data['sjId'] ?? 0;
+        $shopId = $data['shopId'] ?? 0;
+        $ratio = $data['ratio'] ?? 20;
+
+        $bigNum = 0;
+        $smallNum = 0;
+        $num = $data['xhNum'] ?? 0;
+        if ($unitType == dict::getDict('unitType', 'big')) {
+            $bigNum = $num;
+            $totalNum = $num;
+        } else {
+            $smallNum = $num;
+            $totalNum = bcdiv($num, $ratio, 2);
+        }
+        $stockInfo = ProductClass::decreaseStock($productId, $bigNum, $smallNum);
+        $oldStock = $stockInfo['oldStock'] ?? 0;
+        $newStock = $stockInfo['newStock'] ?? 0;
+
+        $recordData = [];
+        $recordData['shopId'] = $data['shopId'];
+        $recordData['itemNum'] = $totalNum;
+        $recordData['sjId'] = $sjId;
+        $recordData['shopId'] = $shopId;
+        $recordData['mainId'] = $mainId;
+        $recordData['orderSn'] = $orderSn;
+        $recordData['itemId'] = $ptItemId;
+        $recordData['oldStock'] = $oldStock; //当时库存
+        $recordData['productId'] = $productId;
+        $recordData['newStock'] = $newStock;// 最新库存
+        StockRecordClass::hdCgApplyRefundAddRecord($recordData);
+
+        $data['itemStock'] = $oldStock;
+        $data['newStock'] = $newStock;
+        self::add($data);
+    }
+
     public static function batchAddOrderItem($data)
     {
         self::batchAdd($data);
     }
+
     public static function getOrderItemDetail($orderSn)
     {
-        return self::getAllByCondition(['orderSn'=>$orderSn],null,'*');
+        return self::getAllByCondition(['orderSn' => $orderSn], null, '*');
     }
 
 }

+ 50 - 41
biz-hd/cg/services/CgRefundService.php

@@ -28,14 +28,13 @@ class CgRefundService extends BaseService
     public static $baseFile = '\bizHd\cg\classes\CgRefundClass';
 
     //新增退款
-    public static function addRefund($post, $order)
+    public static function addRefund($post, $cg)
     {
-        $purchaseId = $order->purchaseId;
-        $cg = PurchaseClass::getLockById($purchaseId);
-        if (empty($cg)) {
-            util::fail('没有找到采购订单');
-        }
+
+        $cgId = $cg->id ?? 0;
         $mainId = $cg->mainId ?? 0;
+        $shopId = $cg->shopId ?? 0;
+        $sjId = $cg->sjId ?? 0;
         $payWay = $cg->payWay;
         $cg->refund = PurchaseClass::REFUND_YES;
         $cg->save();
@@ -47,14 +46,10 @@ class CgRefundService extends BaseService
             }
         }
         $refundOrderSn = orderSn::getCgRefundSn();
-        $post['cgId'] = $cg->id;
         $refundPrice = $post['price'] ?? 0;
         if (is_numeric($refundPrice) == false || $refundPrice < 0) {
             util::fail('退款金额有问题');
         }
-        $post['refundPrice'] = $refundPrice;
-
-        $shopId = $cg->shopId ?? 0;
         $shop = ShopClass::getLockById($shopId);
         if (empty($shop)) {
             util::fail('没有找到门店');
@@ -63,9 +58,9 @@ class CgRefundService extends BaseService
         $data = [];
         $data['status'] = CgRefundClass::STATUS_COMPLETE;
         $data['orderSn'] = $refundOrderSn;
-        $data['shopId'] = $cg->shopId;
+        $data['shopId'] = $shopId;
         $data['mainId'] = $mainId;
-        $data['sjId'] = $cg->sjId;
+        $data['sjId'] = $sjId;
         $data['shopAdminId'] = $cg->shopAdminId;
         $data['shopAdminName'] = $cg->shopAdminName;
         $data['cgId'] = $cg->id;
@@ -74,41 +69,55 @@ class CgRefundService extends BaseService
         $data['refundPrice'] = $refundPrice;
         $data['remark'] = $post['remark'];
 
-        $ghsItemInfo = $post['product'] ?? '';
-
-        $data['bigNum'] = $sumNum['bigNum'] ?? 0;// 总的大单位数量 总共多少扎
-        $data['smallNum'] = $sumNum['smallNum'] ?? 0; //总的小单位数量 总共多少支
-
-        //写入订单表
+        //花材列表结构
+        // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
+        $productList = $post['product'] ?? '';
+        $bigNum = 0;
+        $smallNum = 0;
+        foreach ($productList as $current) {
+            $unitType = $current['unitType'] ?? dict::getDict('unitType', 'big');
+            $num = $current['num'] ?? 0;
+            if ($unitType == dict::getDict('unitType', 'big')) {
+                $bigNum = bcadd($bigNum, $num);
+            } else {
+                $smallNum = bcadd($smallNum, $num);
+            }
+        }
+        $data['bigNum'] = $bigNum;
+        $data['smallNum'] = $smallNum;
         $cgRefund = CgRefundClass::add($data, true);
 
         if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
-
-
-            foreach ($ghsItemInfo as $k => $v) {
-                $ghsItemInfo[$k]['productId'] = $v['hdProductId'];
+            if (empty($productList)) {
+                util::fail('没有找到花材哦!');
             }
-            //退货,减库存
-            foreach ($ghsItemInfo as $k => $v) {
-                $stockInfo = ProductClass::decreaseStock($v['hdProductId'], $v['bigNum'], $v['smallNum']);
-                $ghsItemInfo[$k]['oldStock'] = $stockInfo['oldStock'];
-                $ghsItemInfo[$k]['newStock'] = $stockInfo['newStock'];
+            foreach ($productList as $info) {
+                $unitType = $info['unitType'] ?? 0;
+                $num = $info['num'] ?? 0;
+                $unitPrice = $info['unitPrice'] ?? 0;
+
+                $refundData = [];
+                $productId = $info['productId'] ?? 0;
+                $ptItemId = $info['ptItemId'] ?? 0;
+                $refundData['orderSn'] = $refundOrderSn;
+                $refundData['itemId'] = $ptItemId;
+                $refundData['productId'] = $productId;
+                $refundData['name'] = $info['name'] ?? '';
+                $refundData['cover'] = $info['cover'] ?? '';
+                $refundData['itemCost'] = $info['cost'] ?? 0;
+                $refundData['ratio'] = $info['ratio'] ?? 0;
+                $refundData['mainId'] = $mainId;
+                $refundData['shopId'] = $shopId;
+                $refundData['sjId'] = $sjId;
+                $refundData['xhNum'] = $num;
+                $refundData['xhUnitName'] = 0;
+                $refundData['xhUnitType'] = $unitType;
+                $refundData['xhUnitPrice'] = $unitPrice;
+                $refundData['xhPrice'] = 0;
+                CgRefundItemClass::addData($refundData);
             }
-
-            $cgOrderSn = $cg->orderSn ?? '';
-            $cgItemList = PurchaseItemClass::getAllByCondition(['orderSn' => $cgOrderSn], null, '*', 'productId');
-
-            $batchData = CgRefundClass::groupOrderItem($ghsItemInfo, $refundOrderSn, $cgItemList);
-            CgRefundItemClass::batchAddOrderItem($batchData);
-
-            $orderData = CgRefundClass::getOrderDetail($refundOrderSn, $cg->shopId);
-            $orderData['shopId'] = $data['shopId'];
-            //写流水
-            StockRecordClass::addRecordByOrder($orderData, StockRecordClass::STOCK_RECORD_TYPE_REFUND);
-
         }
 
-
         //门店总收入增加
         $currentTotalIncome = bcadd($shop->totalIncome, $refundPrice, 2);
         $shop->totalIncome = $currentTotalIncome;
@@ -162,7 +171,7 @@ class CgRefundService extends BaseService
 
             if (isset($return['return_code']) == false || $return['return_code'] != 'SUCCESS') {
                 $msg = $return['return_msg'] ?? '';
-                Yii::info("退款失败 cgId:{$purchaseId} msg:{$msg}");
+                Yii::info("退款失败 cgId:{$cgId} msg:{$msg}");
                 util::fail('退款失败');
             }
             $wxRefundId = $return['refund_id'] ?? '';