StatItemController.php 8.6 KB

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