StatItemController.php 6.7 KB

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