StatItemController.php 7.1 KB

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