StatItemController.php 8.4 KB

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