StatItemController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. $status = $order['status'] ?? 0;
  52. $book = $order['book'] ?? 0;
  53. if ($status == 5 || $status == 1 || ($book == 1 && $status == 2)) {
  54. continue;
  55. }
  56. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  57. if (!empty($itemList)) {
  58. foreach ($itemList as $item) {
  59. $productId = $item->productId ?? 0;
  60. $name = $productInfo[$productId]['name'] ?? '';
  61. $py = $productInfo[$productId]['py'] ?? '';
  62. $num = bcsub($item['xhNum'], $item['refundNum'], 2);
  63. $unitPrice = $item['xhUnitPrice'] ?? 0;
  64. $unitCost = $item['cost'] ?? 0;
  65. $unitGross = bcsub($unitPrice, $unitCost, 2);
  66. $currentAmount = bcmul($unitPrice, $num, 2);
  67. $currentGross = bcmul($unitGross, $num, 2);
  68. $totalNum = bcadd($totalNum, $num, 2);
  69. $totalAmount = bcadd($totalAmount, $currentAmount, 2);
  70. $totalMl = bcadd($totalMl, $currentGross, 2);
  71. if ($currentGross < 0) {
  72. noticeUtil::push($item->id . ' 毛利是负的', '15280215347');
  73. }
  74. $stat[$productId]['productId'] = $productId;
  75. $stat[$productId]['name'] = $name;
  76. $stat[$productId]['py'] = $py;
  77. if (isset($stat[$productId]['num'])) {
  78. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  79. } else {
  80. $stat[$productId]['num'] = $num;
  81. }
  82. if (isset($stat[$productId]['amount'])) {
  83. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentAmount, 2);
  84. } else {
  85. $stat[$productId]['amount'] = $currentAmount;
  86. }
  87. if (isset($stat[$productId]['gross'])) {
  88. $stat[$productId]['gross'] = bcadd($stat[$productId]['gross'], $currentGross, 2);
  89. } else {
  90. $stat[$productId]['gross'] = $currentGross;
  91. }
  92. }
  93. }
  94. }
  95. //增加毛利率
  96. foreach ($stat as $key => $val) {
  97. $profit = $val['gross'] ?? 0;
  98. $price = $val['amount'] ?? 0;
  99. if ($price > 0) {
  100. $mll = bcdiv($profit, $price, 3);
  101. $mll = bcmul($mll, 100);
  102. $mll = round($mll, 1);
  103. } else {
  104. $mll = 0;
  105. }
  106. $stat[$key]['mll'] = $mll;
  107. }
  108. $rank = 'num';
  109. if ($sort == 'gross') {
  110. $rank = 'gross';
  111. }
  112. if ($sort == 'amount') {
  113. $rank = 'amount';
  114. }
  115. if ($sort == 'mll') {
  116. $rank = 'mll';
  117. }
  118. $stat = arrayUtil::arraySort($stat, $rank);
  119. util::success(['list' => $stat, 'totalAmount' => floatval($totalAmount), 'totalNum' => floatval($totalNum), 'totalMl' => $totalMl,]);
  120. }
  121. //花材销量 ssh 20211021
  122. public function actionProfile()
  123. {
  124. //查看财务的权限
  125. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  126. if ($lookMoney == 0) {
  127. util::fail('无法查看');
  128. }
  129. $get = Yii::$app->request->get();
  130. $where = [];
  131. $where['shopId'] = $this->shopId;
  132. if (isset($get['shopId']) && !empty($get['shopId'])) {
  133. $where['shopId'] = $get['shopId'];
  134. }
  135. $searchTime = $get['searchTime'] ?? 'today';
  136. if (!empty($searchTime)) {
  137. $startTime = $get['startTime'] ?? '';
  138. $endTime = $get['endTime'] ?? '';
  139. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  140. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  141. }
  142. $list = StatItemClass::getAllByCondition($where, null, '*');
  143. if (empty($list)) {
  144. util::success(['list' => $list]);
  145. }
  146. $ids = array_column($list, 'itemId');
  147. $ids = array_unique(array_filter($ids));
  148. $productInfo = ProductClass::getByIds($ids, null, 'id');
  149. $stat = [];
  150. foreach ($list as $key => $val) {
  151. $itemId = $val['itemId'] ?? 0;
  152. $idsData[$itemId] = 0;
  153. $num = bcsub($val['num'], $val['refundNum'], 2);
  154. $amount = bcsub($val['amount'], $val['refundAmount'], 2);
  155. $name = $productInfo[$itemId]['name'] ?? '';
  156. $py = $productInfo[$itemId]['py'] ?? '';
  157. $stat[$itemId]['productId'] = $itemId;
  158. $stat[$itemId]['name'] = $name;
  159. $stat[$itemId]['py'] = $py;
  160. if (isset($stat[$itemId]['num'])) {
  161. $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
  162. } else {
  163. $stat[$itemId]['num'] = $num;
  164. }
  165. if (isset($stat[$itemId]['amount'])) {
  166. $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
  167. } else {
  168. $stat[$itemId]['amount'] = $amount;
  169. }
  170. }
  171. $stat = arrayUtil::arraySort($stat, 'num');
  172. util::success(['list' => $stat]);
  173. }
  174. }