Bladeren bron

退款花材控制

shish 4 jaren geleden
bovenliggende
commit
76cd50ba98

+ 5 - 8
biz-ghs/order/services/RefundOrderService.php

@@ -93,6 +93,11 @@ class RefundOrderService extends BaseService
 
             $batchData = RefundOrderClass::groupOrderItem($ghsItemInfo, $orderSn, $orderItemData);
             RefundOrderItemClass::batchAddOrderItem($batchData);
+
+            //花材销量统计变动
+            foreach($batchData as $refundInfo){
+                StatItemClass::refundReplace($refundInfo);
+            }
         }
         $orderData = RefundOrderClass::getOrderDetail($orderSn, $refund->shopId);
         $orderData['shopId'] = $data['shopId'];
@@ -103,14 +108,6 @@ class RefundOrderService extends BaseService
             StockRecordClass::addRecordByOrder($orderData, StockRecordClass::STOCK_RECORD_TYPE_REFUND);
         }
 
-        //花材销量统计变动
-        $refundItemList = RefundOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
-        if (!empty($refundItemList)) {
-            foreach ($refundItemList as $refundItemInfo) {
-                StatItemClass::replace($refundItemInfo, 1);
-            }
-        }
-
         $payWay = $order->payWay;
         $shopId = $order->shopId;
         $shop = ShopClass::getLockById($shopId);

+ 36 - 18
biz/stat/classes/StatItemClass.php

@@ -12,7 +12,7 @@ class StatItemClass extends BaseClass
     public static $baseFile = '\biz\stat\models\StatItem';
 
     //每日花材统计 shish 2021012
-    public static function replace($item, $isRefund, $date = null)
+    public static function replace($item, $date = null)
     {
         $sjId = $item->sjId ?? 0;
         $shopId = $item->shopId ?? 0;
@@ -25,26 +25,44 @@ class StatItemClass extends BaseClass
         $id = $stat->id;
         $unique = self::getLockById($id);
 
-        if ($isRefund == 1) {
-            $num = $item->itemNum ?? 0;
-            $unitePrice = $item->itemPrice ?? 0;
-            $price = bcmul($unitePrice, $num, 2);
-
-            $currentRefundNum = bcadd($unique->refundNum, $num, 2);
-            $currentRefundAmount = bcadd($unique->refundAmount, $price, 2);
-            $unique->refundNum = $currentRefundNum;
-            $unique->refundAmount = $currentRefundAmount;
-        } else {
-            $amount = $item->price ?? 0;
-            $num = $item->num ?? 0;
-            $currentNum = bcadd($unique->num, $num, 2);
-            $unique->num = $currentNum;
-            $currentAmount = bcadd($unique->amount, $amount, 2);
-            $unique->amount = $currentAmount;
+
+        $amount = $item->price ?? 0;
+        $num = $item->num ?? 0;
+        $currentNum = bcadd($unique->num, $num, 2);
+        $unique->num = $currentNum;
+        $currentAmount = bcadd($unique->amount, $amount, 2);
+        $unique->amount = $currentAmount;
+
+
+        $unique->save();
+        StatItemMonthClass::replace($item, $time);
+    }
+
+    //退款花材
+    public static function refundReplace($item, $date = null)
+    {
+        $sjId = $item['sjId'] ?? 0;
+        $shopId = $item['shopId'] ?? 0;
+        $productId = $item['productId'] ?? 0;
+        $time = $date == null ? date('Ymd') : $date;
+        $stat = self::getByCondition(['sjId' => $sjId, 'shopId' => $shopId, 'itemId' => $productId, 'time' => $time], true);
+        if (empty($stat)) {
+            $stat = self::add(['shopId' => $shopId, 'sjId' => $sjId, 'itemId' => $productId, 'time' => $time], true);
         }
+        $id = $stat->id;
+        $unique = self::getLockById($id);
+
+        $num = $item['itemNum'] ?? 0;
+        $unitePrice = $item['itemPrice'] ?? 0;
+        $price = bcmul($unitePrice, $num, 2);
 
+        $currentRefundNum = bcadd($unique->refundNum, $num, 2);
+        $currentRefundAmount = bcadd($unique->refundAmount, $price, 2);
+        $unique->refundNum = $currentRefundNum;
+        $unique->refundAmount = $currentRefundAmount;
         $unique->save();
-        StatItemMonthClass::replace($item, $isRefund, $time);
+
+        StatItemMonthClass::refundReplace($item, $time);
     }
 
 }

+ 41 - 9
biz/stat/classes/StatItemMonthClass.php

@@ -12,7 +12,7 @@ class StatItemMonthClass extends BaseClass
     public static $baseFile = '\biz\stat\models\StatItemMonth';
 
     //每月花材统计 shish 20211012
-    public static function replace($item, $isRefund, $date = null)
+    public static function replace($item, $date = null)
     {
         if ($date == null) {
             $time = date('Ym');
@@ -34,18 +34,50 @@ class StatItemMonthClass extends BaseClass
         $id = $stat->id;
         $unique = self::getLockById($id);
 
-        if ($isRefund == 1) {
+        $amount = $item->price ?? 0;
+        $num = $item->num ?? 0;
 
-        } else {
-            $amount = $item->price ?? 0;
-            $num = $item->num ?? 0;
+        $currentNum = bcadd($unique->num, $num, 2);
+        $unique->num = $currentNum;
+
+        $currentAmount = bcadd($unique->amount, $amount, 2);
+        $unique->amount = $currentAmount;
 
-            $currentNum = bcadd($unique->num, $num, 2);
-            $unique->num = $currentNum;
+        $unique->save();
+    }
 
-            $currentAmount = bcadd($unique->amount, $amount, 2);
-            $unique->amount = $currentAmount;
+    //退款花材
+    public static function refundReplace($item, $date = null)
+    {
+        if ($date == null) {
+            $time = date('Ym');
+            $year = date('Y');
+            $month = date('m');
+        } else {
+            $timestamp = strtotime($date);
+            $time = date('Ym', $timestamp);
+            $year = date('Y', $timestamp);
+            $month = date('m', $timestamp);
+        }
+        $sjId = $item['sjId'] ?? 0;
+        $shopId = $item['shopId'] ?? 0;
+        $productId = $item['productId'] ?? 0;
+        $stat = self::getByCondition(['sjId' => $sjId, 'shopId' => $shopId, 'itemId' => $productId, 'time' => $time], true);
+        if (empty($stat)) {
+            $stat = self::add(['shopId' => $shopId, 'sjId' => $sjId, 'itemId' => $productId, 'time' => $time, 'year' => $year, 'month' => $month], true);
         }
+        $id = $stat->id;
+        $unique = self::getLockById($id);
+
+        $num = $item['itemNum'] ?? 0;
+        $unitePrice = $item['itemPrice'] ?? 0;
+        $price = bcmul($unitePrice, $num, 2);
+
+        $currentRefundNum = bcadd($unique->refundNum, $num, 2);
+        $currentRefundAmount = bcadd($unique->refundAmount, $price, 2);
+        $unique->refundNum = $currentRefundNum;
+        $unique->refundAmount = $currentRefundAmount;
+
         $unique->save();
     }