StatItemController.php 7.5 KB

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