StatItemController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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', '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. print_r($productInfo);die;
  85. echo $staffId.' '.$currentCgStaffId;die;
  86. if ($staffId != $currentCgStaffId) {
  87. continue;
  88. }
  89. }
  90. $num = bcsub($item['xhNum'], $item['refundNum'], 2);
  91. $unitPrice = $item['xhUnitPrice'] ?? 0;
  92. $unitCost = $item['cost'] ?? 0;
  93. $unitGross = bcsub($unitPrice, $unitCost, 2);
  94. $currentAmount = bcmul($unitPrice, $num, 2);
  95. $currentGross = bcmul($unitGross, $num, 2);
  96. $totalNum = bcadd($totalNum, $num, 2);
  97. $totalAmount = bcadd($totalAmount, $currentAmount, 2);
  98. $totalMl = bcadd($totalMl, $currentGross, 2);
  99. if ($currentGross < 0) {
  100. //noticeUtil::push($item->id . ' 毛利是负的', '15280215347');
  101. }
  102. $stat[$productId]['productId'] = $productId;
  103. $stat[$productId]['name'] = $name;
  104. $stat[$productId]['py'] = $py;
  105. if (isset($stat[$productId]['num'])) {
  106. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  107. } else {
  108. $stat[$productId]['num'] = $num;
  109. }
  110. if (isset($stat[$productId]['amount'])) {
  111. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentAmount, 2);
  112. } else {
  113. $stat[$productId]['amount'] = $currentAmount;
  114. }
  115. if (isset($stat[$productId]['gross'])) {
  116. $stat[$productId]['gross'] = bcadd($stat[$productId]['gross'], $currentGross, 2);
  117. } else {
  118. $stat[$productId]['gross'] = $currentGross;
  119. }
  120. }
  121. }
  122. }
  123. //增加毛利率
  124. foreach ($stat as $key => $val) {
  125. $profit = $val['gross'] ?? 0;
  126. $price = $val['amount'] ?? 0;
  127. if ($price > 0) {
  128. $mll = bcdiv($profit, $price, 3);
  129. $mll = bcmul($mll, 100);
  130. $mll = round($mll, 1);
  131. } else {
  132. $mll = 0;
  133. }
  134. $stat[$key]['mll'] = $mll;
  135. }
  136. $rank = 'num';
  137. if ($sort == 'gross') {
  138. $rank = 'gross';
  139. }
  140. if ($sort == 'amount') {
  141. $rank = 'amount';
  142. }
  143. if ($sort == 'mll') {
  144. $rank = 'mll';
  145. }
  146. $stat = arrayUtil::arraySort($stat, $rank);
  147. $export = $get['export'] ?? 0;
  148. if ($export == 1) {
  149. $mainId = $this->mainId;
  150. StatSaleClass::exportItemSale($stat, $mainId);
  151. }
  152. util::success(['list' => $stat, 'totalAmount' => floatval($totalAmount), 'totalNum' => floatval($totalNum), 'totalMl' => $totalMl,]);
  153. }
  154. //花材销量 ssh 20211021
  155. public function actionProfile()
  156. {
  157. //查看财务的权限
  158. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  159. if ($lookMoney == 0) {
  160. util::fail('无法查看');
  161. }
  162. $get = Yii::$app->request->get();
  163. $where = [];
  164. $where['shopId'] = $this->shopId;
  165. if (isset($get['shopId']) && !empty($get['shopId'])) {
  166. $where['shopId'] = $get['shopId'];
  167. }
  168. $searchTime = $get['searchTime'] ?? 'today';
  169. if (!empty($searchTime)) {
  170. $startTime = $get['startTime'] ?? '';
  171. $endTime = $get['endTime'] ?? '';
  172. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  173. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  174. }
  175. $list = StatItemClass::getAllByCondition($where, null, '*');
  176. if (empty($list)) {
  177. util::success(['list' => $list]);
  178. }
  179. $ids = array_column($list, 'itemId');
  180. $ids = array_unique(array_filter($ids));
  181. $productInfo = ProductClass::getByIds($ids, null, 'id');
  182. $stat = [];
  183. foreach ($list as $key => $val) {
  184. $itemId = $val['itemId'] ?? 0;
  185. $idsData[$itemId] = 0;
  186. $num = bcsub($val['num'], $val['refundNum'], 2);
  187. $amount = bcsub($val['amount'], $val['refundAmount'], 2);
  188. $name = $productInfo[$itemId]['name'] ?? '';
  189. $py = $productInfo[$itemId]['py'] ?? '';
  190. $stat[$itemId]['productId'] = $itemId;
  191. $stat[$itemId]['name'] = $name;
  192. $stat[$itemId]['py'] = $py;
  193. if (isset($stat[$itemId]['num'])) {
  194. $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
  195. } else {
  196. $stat[$itemId]['num'] = $num;
  197. }
  198. if (isset($stat[$itemId]['amount'])) {
  199. $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
  200. } else {
  201. $stat[$itemId]['amount'] = $amount;
  202. }
  203. }
  204. $stat = arrayUtil::arraySort($stat, 'num');
  205. util::success(['list' => $stat]);
  206. }
  207. }