StatItemController.php 6.1 KB

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