StatItemController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. foreach ($list as $order) {
  42. $orderSn = $order['orderSn'] ?? '';
  43. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  44. if (!empty($itemList)) {
  45. foreach ($itemList as $item) {
  46. $productId = $item->productId ?? 0;
  47. $name = $productInfo[$productId]['name'] ?? '';
  48. $py = $productInfo[$productId]['py'] ?? '';
  49. $num = bcsub($item['xhNum'], $item['refundNum'], 2);
  50. $unitPrice = $item['xhUnitPrice'] ?? 0;
  51. $unitCost = $item['cost'] ?? 0;
  52. $unitGross = bcsub($unitPrice, $unitCost, 2);
  53. $currentAmount = bcmul($unitPrice, $num, 2);
  54. $currentGross = bcmul($unitGross, $num, 2);
  55. $totalNum = bcadd($totalNum, $num, 2);
  56. $totalAmount = bcadd($totalAmount, $currentAmount, 2);
  57. $stat[$productId]['productId'] = $productId;
  58. $stat[$productId]['name'] = $name;
  59. $stat[$productId]['py'] = $py;
  60. if (isset($stat[$productId]['num'])) {
  61. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  62. } else {
  63. $stat[$productId]['num'] = $num;
  64. }
  65. if (isset($stat[$productId]['amount'])) {
  66. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentAmount, 2);
  67. } else {
  68. $stat[$productId]['amount'] = $currentAmount;
  69. }
  70. if (isset($stat[$productId]['gross'])) {
  71. $stat[$productId]['gross'] = bcadd($stat[$productId]['gross'], $currentGross, 2);
  72. } else {
  73. $stat[$productId]['gross'] = $currentGross;
  74. }
  75. }
  76. }
  77. }
  78. //增加毛利率
  79. foreach ($stat as $key => $val) {
  80. $profit = $val['gross'] ?? 0;
  81. $price = $val['amount'] ?? 0;
  82. if ($price > 0) {
  83. $mll = bcdiv($profit, $price, 3);
  84. $mll = bcmul($mll, 100);
  85. $mll = round($mll, 1);
  86. } else {
  87. $mll = 0;
  88. }
  89. $stat[$key]['mll'] = $mll;
  90. }
  91. $rank = 'num';
  92. if ($sort == 'gross') {
  93. $rank = 'gross';
  94. }
  95. if ($sort == 'amount') {
  96. $rank = 'amount';
  97. }
  98. if ($sort == 'mll') {
  99. $rank = 'mll';
  100. }
  101. $stat = arrayUtil::arraySort($stat, $rank);
  102. util::success(['list' => $stat, 'totalAmount' => floatval($totalAmount), 'totalNum' => floatval($totalNum),]);
  103. }
  104. //花材销量 ssh 20211021
  105. public function actionProfile()
  106. {
  107. $get = Yii::$app->request->get();
  108. $where = [];
  109. $where['shopId'] = $this->shopId;
  110. if (isset($get['shopId']) && !empty($get['shopId'])) {
  111. $where['shopId'] = $get['shopId'];
  112. }
  113. $searchTime = $get['searchTime'] ?? 'today';
  114. if (!empty($searchTime)) {
  115. $startTime = $get['startTime'] ?? '';
  116. $endTime = $get['endTime'] ?? '';
  117. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  118. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  119. }
  120. $list = StatItemClass::getAllByCondition($where, null, '*');
  121. if (empty($list)) {
  122. util::success(['list' => $list]);
  123. }
  124. $ids = array_column($list, 'itemId');
  125. $ids = array_unique(array_filter($ids));
  126. $productInfo = ProductClass::getByIds($ids, null, 'id');
  127. $stat = [];
  128. foreach ($list as $key => $val) {
  129. $itemId = $val['itemId'] ?? 0;
  130. $idsData[$itemId] = 0;
  131. $num = bcsub($val['num'], $val['refundNum'], 2);
  132. $amount = bcsub($val['amount'], $val['refundAmount'], 2);
  133. $name = $productInfo[$itemId]['name'] ?? '';
  134. $py = $productInfo[$itemId]['py'] ?? '';
  135. $stat[$itemId]['productId'] = $itemId;
  136. $stat[$itemId]['name'] = $name;
  137. $stat[$itemId]['py'] = $py;
  138. if (isset($stat[$itemId]['num'])) {
  139. $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
  140. } else {
  141. $stat[$itemId]['num'] = $num;
  142. }
  143. if (isset($stat[$itemId]['amount'])) {
  144. $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
  145. } else {
  146. $stat[$itemId]['amount'] = $amount;
  147. }
  148. }
  149. $stat = arrayUtil::arraySort($stat, 'num');
  150. util::success(['list' => $stat]);
  151. }
  152. }