StatItemController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\stat\classes\StatItemClass;
  4. use bizGhs\order\classes\OrderClass;
  5. use bizGhs\order\classes\OrderItemClass;
  6. use bizGhs\product\classes\ProductClass;
  7. use common\components\arrayUtil;
  8. use common\components\dateUtil;
  9. use Yii;
  10. use common\components\util;
  11. class StatItemController extends BaseController
  12. {
  13. //花材销量 ssh 20230223
  14. public function actionShow()
  15. {
  16. ini_set('memory_limit', '2045M');
  17. set_time_limit(0);
  18. $get = Yii::$app->request->get();
  19. $sort = $get['sort'] ?? 'num';
  20. $where = [];
  21. $where['mainId'] = $this->mainId;
  22. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  23. $startTime = $get['startTime'] ?? '';
  24. $endTime = $get['endTime'] ?? '';
  25. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  26. $start = $period['startTime'];
  27. $end = $period['endTime'];
  28. $currentStartDate = date('Y-m-d', strtotime($start));
  29. $currentEndDate = date('Y-m-d', strtotime($end));
  30. $currentStartTime = $currentStartDate . ' 00:00:00';
  31. $currentEndTime = $currentEndDate . ' 23:59:59';
  32. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  33. $list = OrderClass::getAllByCondition($where, null, '*');
  34. if (empty($list)) {
  35. util::success(['list' => []]);
  36. }
  37. $productInfo = ProductClass::getAllByCondition(['mainId' => $this->mainId], null, 'id,name,py', 'id');
  38. $stat = [];
  39. $totalAmount = 0;
  40. $totalNum = 0;
  41. $totalMl = 0;
  42. foreach ($list as $order) {
  43. $orderSn = $order['orderSn'] ?? '';
  44. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  45. if (!empty($itemList)) {
  46. foreach ($itemList as $item) {
  47. $productId = $item->productId ?? 0;
  48. $name = $productInfo[$productId]['name'] ?? '';
  49. $py = $productInfo[$productId]['py'] ?? '';
  50. $num = bcsub($item['xhNum'], $item['refundNum'], 2);
  51. $unitPrice = $item['xhUnitPrice'] ?? 0;
  52. $unitCost = $item['cost'] ?? 0;
  53. $unitGross = bcsub($unitPrice, $unitCost, 2);
  54. $currentAmount = bcmul($unitPrice, $num, 2);
  55. $currentGross = bcmul($unitGross, $num, 2);
  56. $totalNum = bcadd($totalNum, $num, 2);
  57. $totalAmount = bcadd($totalAmount, $currentAmount, 2);
  58. $totalMl = bcadd($totalMl, $currentGross, 2);
  59. $stat[$productId]['productId'] = $productId;
  60. $stat[$productId]['name'] = $name;
  61. $stat[$productId]['py'] = $py;
  62. if (isset($stat[$productId]['num'])) {
  63. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  64. } else {
  65. $stat[$productId]['num'] = $num;
  66. }
  67. if (isset($stat[$productId]['amount'])) {
  68. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentAmount, 2);
  69. } else {
  70. $stat[$productId]['amount'] = $currentAmount;
  71. }
  72. if (isset($stat[$productId]['gross'])) {
  73. $stat[$productId]['gross'] = bcadd($stat[$productId]['gross'], $currentGross, 2);
  74. } else {
  75. $stat[$productId]['gross'] = $currentGross;
  76. }
  77. }
  78. }
  79. }
  80. //增加毛利率
  81. foreach ($stat as $key => $val) {
  82. $profit = $val['gross'] ?? 0;
  83. $price = $val['amount'] ?? 0;
  84. if ($price > 0) {
  85. $mll = bcdiv($profit, $price, 3);
  86. $mll = bcmul($mll, 100);
  87. $mll = round($mll, 1);
  88. } else {
  89. $mll = 0;
  90. }
  91. $stat[$key]['mll'] = $mll;
  92. }
  93. $rank = 'num';
  94. if ($sort == 'gross') {
  95. $rank = 'gross';
  96. }
  97. if ($sort == 'amount') {
  98. $rank = 'amount';
  99. }
  100. if ($sort == 'mll') {
  101. $rank = 'mll';
  102. }
  103. $stat = arrayUtil::arraySort($stat, $rank);
  104. util::success(['list' => $stat, 'totalAmount' => floatval($totalAmount), 'totalNum' => floatval($totalNum), 'totalMl' => $totalMl,]);
  105. }
  106. //花材销量 ssh 20211021
  107. public function actionProfile()
  108. {
  109. $get = Yii::$app->request->get();
  110. $where = [];
  111. $where['shopId'] = $this->shopId;
  112. if (isset($get['shopId']) && !empty($get['shopId'])) {
  113. $where['shopId'] = $get['shopId'];
  114. }
  115. $searchTime = $get['searchTime'] ?? 'today';
  116. if (!empty($searchTime)) {
  117. $startTime = $get['startTime'] ?? '';
  118. $endTime = $get['endTime'] ?? '';
  119. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  120. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  121. }
  122. $list = StatItemClass::getAllByCondition($where, null, '*');
  123. if (empty($list)) {
  124. util::success(['list' => $list]);
  125. }
  126. $ids = array_column($list, 'itemId');
  127. $ids = array_unique(array_filter($ids));
  128. $productInfo = ProductClass::getByIds($ids, null, 'id');
  129. $stat = [];
  130. foreach ($list as $key => $val) {
  131. $itemId = $val['itemId'] ?? 0;
  132. $idsData[$itemId] = 0;
  133. $num = bcsub($val['num'], $val['refundNum'], 2);
  134. $amount = bcsub($val['amount'], $val['refundAmount'], 2);
  135. $name = $productInfo[$itemId]['name'] ?? '';
  136. $py = $productInfo[$itemId]['py'] ?? '';
  137. $stat[$itemId]['productId'] = $itemId;
  138. $stat[$itemId]['name'] = $name;
  139. $stat[$itemId]['py'] = $py;
  140. if (isset($stat[$itemId]['num'])) {
  141. $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
  142. } else {
  143. $stat[$itemId]['num'] = $num;
  144. }
  145. if (isset($stat[$itemId]['amount'])) {
  146. $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
  147. } else {
  148. $stat[$itemId]['amount'] = $amount;
  149. }
  150. }
  151. $stat = arrayUtil::arraySort($stat, 'num');
  152. util::success(['list' => $stat]);
  153. }
  154. }