StatItemController.php 6.9 KB

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