shish 3 rokov pred
rodič
commit
ae42a52219

+ 66 - 32
app-ghs/controllers/StatItemController.php

@@ -3,6 +3,8 @@
 namespace ghs\controllers;
 
 use biz\stat\classes\StatItemClass;
+use bizGhs\order\classes\OrderClass;
+use bizGhs\order\classes\OrderItemClass;
 use bizGhs\product\classes\ProductClass;
 use common\components\arrayUtil;
 use common\components\dateUtil;
@@ -15,46 +17,78 @@ class StatItemController extends BaseController
     //花材销量 ssh 20211021
     public function actionProfile()
     {
+        ini_set('memory_limit', '2045M');
+        set_time_limit(0);
+
         $get = Yii::$app->request->get();
+        $sort = $get['sort'] ?? 'num';
         $where = [];
         $where['mainId'] = $this->mainId;
-        $searchTime = $get['searchTime'] ?? 'today';
-        if (!empty($searchTime)) {
-            $startTime = $get['startTime'] ?? '';
-            $endTime = $get['endTime'] ?? '';
-            $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
-            $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
-        }
-        $list = StatItemClass::getAllByCondition($where, null, '*');
+        $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
+
+        $startTime = $get['startTime'] ?? '';
+        $endTime = $get['endTime'] ?? '';
+        $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
+
+        $start = $period['startTime'];
+        $end = $period['endTime'];
+
+        $currentStartDate = date('Y-m-d', strtotime($start));
+        $currentEndDate = date('Y-m-d', strtotime($end));
+        $currentStartTime = $currentStartDate . ' 00:00:00';
+        $currentEndTime = $currentEndDate . ' 23:59:59';
+        $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
+        $list = OrderClass::getAllByCondition($where, null, '*');
         if (empty($list)) {
-            util::success(['list' => $list]);
+            util::success(['list' => []]);
         }
-        $ids = array_column($list, 'itemId');
-        $ids = array_unique(array_filter($ids));
-        $productInfo = ProductClass::getByIds($ids, null, 'id');
+        $productInfo = ProductClass::getAllByCondition(['mainId' => $this->mainId], null, 'id,name,py', 'id');
         $stat = [];
-        foreach ($list as $key => $val) {
-            $itemId = $val['itemId'] ?? 0;
-            $idsData[$itemId] = 0;
-            $num = bcsub($val['num'], $val['refundNum'], 2);
-            $amount = bcsub($val['amount'], $val['refundAmount'], 2);
-            $name = $productInfo[$itemId]['name'] ?? '';
-            $py = $productInfo[$itemId]['py'] ?? '';
-            $stat[$itemId]['productId'] = $itemId;
-            $stat[$itemId]['name'] = $name;
-            $stat[$itemId]['py'] = $py;
-            if (isset($stat[$itemId]['num'])) {
-                $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
-            } else {
-                $stat[$itemId]['num'] = $num;
-            }
-            if (isset($stat[$itemId]['amount'])) {
-                $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
-            } else {
-                $stat[$itemId]['amount'] = $amount;
+        foreach ($list as $order) {
+            $orderSn = $order['orderSn'] ?? '';
+            $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
+            if (!empty($itemList)) {
+                foreach ($itemList as $item) {
+                    $productId = $item->productId ?? 0;
+                    $name = $productInfo[$productId]['name'] ?? '';
+                    $py = $productInfo[$productId]['py'] ?? '';
+
+                    $num = bcsub($item['xhNum'], $item['refundNum'], 2);
+                    $unitPrice = $item['xhUnitPrice'] ?? 0;
+                    $unitCost = $item['cost'] ?? 0;
+                    $unitGross = bcsub($unitPrice, $unitCost, 2);
+                    $currentAmount = bcmul($unitPrice, $num, 2);
+                    $currentGross = bcmul($unitGross, $num, 2);
+
+                    $stat[$productId]['productId'] = $productId;
+                    $stat[$productId]['name'] = $name;
+                    $stat[$productId]['py'] = $py;
+                    if (isset($stat[$productId]['num'])) {
+                        $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
+                    } else {
+                        $stat[$productId]['num'] = $num;
+                    }
+                    if (isset($stat[$productId]['amount'])) {
+                        $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentAmount, 2);
+                    } else {
+                        $stat[$productId]['amount'] = $currentAmount;
+                    }
+                    if (isset($stat[$productId]['gross'])) {
+                        $stat[$productId]['gross'] = bcadd($stat[$productId]['gross'], $currentGross, 2);
+                    } else {
+                        $stat[$productId]['gross'] = $currentAmount;
+                    }
+                }
             }
         }
-        $stat = arrayUtil::arraySort($stat, 'num');
+        $rank = 'num';
+        if ($sort == 'gross') {
+            $rank = 'gross';
+        }
+        if ($sort == 'amount') {
+            $rank = 'amount';
+        }
+        $stat = arrayUtil::arraySort($stat, $rank);
         util::success(['list' => $stat]);
     }
 

+ 1 - 0
console/controllers/ItemController.php

@@ -134,6 +134,7 @@ class ItemController extends Controller
         }
     }
 
+    //美天鲜花 ssh 20230223
     public function actionMt()
     {
         if (getenv('YII_ENV') == 'production') {