StatItemController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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'] = $currentAmount;
  70. }
  71. }
  72. }
  73. }
  74. //增加毛利率
  75. foreach ($stat as $key => $val) {
  76. $profit = $val['gross'] ?? 0;
  77. $price = $val['amount'] ?? 0;
  78. $mll = bcdiv($profit, $price, 3);
  79. $mll = bcmul($mll, 100);
  80. $mll = round($mll, 1);
  81. $stat[$key]['mll'] = $mll;
  82. }
  83. $rank = 'num';
  84. if ($sort == 'gross') {
  85. $rank = 'gross';
  86. }
  87. if ($sort == 'amount') {
  88. $rank = 'amount';
  89. }
  90. if ($sort == 'mll') {
  91. $rank = 'mll';
  92. }
  93. $stat = arrayUtil::arraySort($stat, $rank);
  94. util::success(['list' => $stat]);
  95. }
  96. //花材销量 ssh 20211021
  97. public function actionProfile()
  98. {
  99. $get = Yii::$app->request->get();
  100. $where = [];
  101. $where['shopId'] = $this->shopId;
  102. if (isset($get['shopId']) && !empty($get['shopId'])) {
  103. $where['shopId'] = $get['shopId'];
  104. }
  105. $searchTime = $get['searchTime'] ?? 'today';
  106. if (!empty($searchTime)) {
  107. $startTime = $get['startTime'] ?? '';
  108. $endTime = $get['endTime'] ?? '';
  109. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  110. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  111. }
  112. $list = StatItemClass::getAllByCondition($where, null, '*');
  113. if (empty($list)) {
  114. util::success(['list' => $list]);
  115. }
  116. $ids = array_column($list, 'itemId');
  117. $ids = array_unique(array_filter($ids));
  118. $productInfo = ProductClass::getByIds($ids, null, 'id');
  119. $stat = [];
  120. foreach ($list as $key => $val) {
  121. $itemId = $val['itemId'] ?? 0;
  122. $idsData[$itemId] = 0;
  123. $num = bcsub($val['num'], $val['refundNum'], 2);
  124. $amount = bcsub($val['amount'], $val['refundAmount'], 2);
  125. $name = $productInfo[$itemId]['name'] ?? '';
  126. $py = $productInfo[$itemId]['py'] ?? '';
  127. $stat[$itemId]['productId'] = $itemId;
  128. $stat[$itemId]['name'] = $name;
  129. $stat[$itemId]['py'] = $py;
  130. if (isset($stat[$itemId]['num'])) {
  131. $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
  132. } else {
  133. $stat[$itemId]['num'] = $num;
  134. }
  135. if (isset($stat[$itemId]['amount'])) {
  136. $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
  137. } else {
  138. $stat[$itemId]['amount'] = $amount;
  139. }
  140. }
  141. $stat = arrayUtil::arraySort($stat, 'num');
  142. util::success(['list' => $stat]);
  143. }
  144. }