StatItemController.php 8.0 KB

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