StatItemController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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\order\classes\NextDayRefundClass;
  7. use bizGhs\product\classes\ProductClass;
  8. use bizGhs\shop\classes\ShopAdminClass;
  9. use bizGhs\stat\classes\StatSaleClass;
  10. use common\components\arrayUtil;
  11. use common\components\dateUtil;
  12. use common\components\noticeUtil;
  13. use Yii;
  14. use common\components\util;
  15. class StatItemController extends BaseController
  16. {
  17. //花材销量 ssh 20230223
  18. public function actionShow()
  19. {
  20. ini_set('memory_limit', '2045M');
  21. set_time_limit(0);
  22. //查看财务的权限
  23. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  24. $hasPower = 0;
  25. if (getenv('YII_ENV') == 'production') {
  26. //小向花卉
  27. if ($this->mainId == 23390) {
  28. if (in_array($this->adminId, [31105])) {
  29. $hasPower = 1;
  30. }
  31. }
  32. } else {
  33. if ($this->mainId == 644) {
  34. if (in_array($this->adminId, [1230])) {
  35. $hasPower = 1;
  36. }
  37. }
  38. }
  39. if ($lookMoney == 0 && $hasPower == 0) {
  40. //盛丰员工可以看花材销售数据
  41. if ($this->mainId != 44282) {
  42. util::fail('没有财务权限哦');
  43. }
  44. }
  45. $get = Yii::$app->request->get();
  46. $sort = $get['sort'] ?? 'num';
  47. $where = [];
  48. $where['mainId'] = $this->mainId;
  49. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  50. $startTime = $get['startTime'] ?? '';
  51. $endTime = $get['endTime'] ?? '';
  52. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  53. $start = $period['startTime'];
  54. $end = $period['endTime'];
  55. $currentStartDate = date('Y-m-d', strtotime($start));
  56. $currentEndDate = date('Y-m-d', strtotime($end));
  57. $currentStartTime = $currentStartDate . ' 00:00:00';
  58. $currentEndTime = $currentEndDate . ' 23:59:59';
  59. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  60. $staffId = $get['staffId'] ?? 0;
  61. $list = OrderClass::getAllByCondition($where, null, '*');
  62. $productInfo = ProductClass::getAllByCondition(['mainId' => $this->mainId], null, 'id,name,py,cgStaffId', 'id');
  63. $stat = [];
  64. $totalAmount = 0;
  65. $totalNum = 0;
  66. $totalMl = 0;
  67. if (!empty($list)) {
  68. foreach ($list as $order) {
  69. $orderSn = $order['orderSn'] ?? '';
  70. $status = $order['status'] ?? 0;
  71. $book = $order['book'] ?? 0;
  72. $actPrice = $order['actPrice'] ?? 0;
  73. if ($status == 5 || $status == 1 || ($book == 1 && $status == 2)) {
  74. continue;
  75. }
  76. if ($actPrice <= 0) {
  77. //如果全部退款的花材也不统计
  78. continue;
  79. }
  80. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  81. if (!empty($itemList)) {
  82. foreach ($itemList as $item) {
  83. $productId = $item->productId ?? 0;
  84. $name = $productInfo[$productId]['name'] ?? '';
  85. $py = $productInfo[$productId]['py'] ?? '';
  86. if (!empty($staffId)) {
  87. $currentCgStaffId = $productInfo[$productId]['cgStaffId'] ?? 0;
  88. if ($staffId != $currentCgStaffId) {
  89. continue;
  90. }
  91. }
  92. $num = NextDayRefundClass::payDayRemainNum(
  93. $item['xhNum'] ?? 0,
  94. $item['refundNum'] ?? 0,
  95. $item['nextRefundNum'] ?? 0
  96. );
  97. $unitPrice = $item['xhUnitPrice'] ?? 0;
  98. $unitCost = $item['cost'] ?? 0;
  99. $unitGross = bcsub($unitPrice, $unitCost, 2);
  100. $currentAmount = bcmul($unitPrice, $num, 2);
  101. $currentGross = bcmul($unitGross, $num, 2);
  102. $totalNum = bcadd($totalNum, $num, 2);
  103. $totalAmount = bcadd($totalAmount, $currentAmount, 2);
  104. $totalMl = bcadd($totalMl, $currentGross, 2);
  105. if ($currentGross < 0) {
  106. //noticeUtil::push($item->id . ' 毛利是负的', '15280215347');
  107. }
  108. $stat[$productId]['productId'] = $productId;
  109. $stat[$productId]['name'] = $name;
  110. $stat[$productId]['py'] = $py;
  111. if (isset($stat[$productId]['num'])) {
  112. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  113. } else {
  114. $stat[$productId]['num'] = $num;
  115. }
  116. if (isset($stat[$productId]['amount'])) {
  117. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentAmount, 2);
  118. } else {
  119. $stat[$productId]['amount'] = $currentAmount;
  120. }
  121. if (isset($stat[$productId]['gross'])) {
  122. $stat[$productId]['gross'] = bcadd($stat[$productId]['gross'], $currentGross, 2);
  123. } else {
  124. $stat[$productId]['gross'] = $currentGross;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. // 通过日:退货扣数量+金额;仅退款只扣金额;无当日销量的花材也建负行
  131. $nextDayDeduct = '0.00';
  132. $deductMaps = [
  133. NextDayRefundClass::sumItemByProduct($this->mainId, $currentStartTime, $currentEndTime),
  134. NextDayRefundClass::sumMoneyOnlyAmountByProduct($this->mainId, $currentStartTime, $currentEndTime),
  135. ];
  136. foreach ($deductMaps as $deductMap) {
  137. foreach ($deductMap as $pid => $row) {
  138. if (!empty($staffId)) {
  139. $currentCgStaffId = $productInfo[$pid]['cgStaffId'] ?? 0;
  140. if ($staffId != $currentCgStaffId) {
  141. continue;
  142. }
  143. }
  144. if (!isset($stat[$pid])) {
  145. $stat[$pid] = [
  146. 'productId' => $pid,
  147. 'name' => $productInfo[$pid]['name'] ?? '',
  148. 'py' => $productInfo[$pid]['py'] ?? '',
  149. 'num' => '0.00',
  150. 'amount' => '0.00',
  151. 'gross' => '0.00',
  152. ];
  153. }
  154. $subNum = (string)($row['num'] ?? '0');
  155. $subAmt = (string)($row['amount'] ?? '0');
  156. $stat[$pid]['num'] = bcsub((string)$stat[$pid]['num'], $subNum, 2);
  157. $stat[$pid]['amount'] = bcsub((string)$stat[$pid]['amount'], $subAmt, 2);
  158. $stat[$pid]['gross'] = bcsub((string)$stat[$pid]['gross'], $subAmt, 2);
  159. $totalNum = bcsub((string)$totalNum, $subNum, 2);
  160. $totalAmount = bcsub((string)$totalAmount, $subAmt, 2);
  161. $totalMl = bcsub((string)$totalMl, $subAmt, 2);
  162. $nextDayDeduct = bcadd($nextDayDeduct, $subAmt, 2);
  163. }
  164. }
  165. //增加毛利率(金额为负时不计算毛利率,避免除零/误导)
  166. foreach ($stat as $key => $val) {
  167. $profit = $val['gross'] ?? 0;
  168. $price = $val['amount'] ?? 0;
  169. if (bccomp((string)$price, '0', 2) > 0) {
  170. $mll = bcdiv($profit, $price, 3);
  171. $mll = bcmul($mll, 100);
  172. $mll = round($mll, 1);
  173. } else {
  174. $mll = 0;
  175. }
  176. $stat[$key]['mll'] = $mll;
  177. }
  178. $rank = 'num';
  179. if ($sort == 'gross') {
  180. $rank = 'gross';
  181. }
  182. if ($sort == 'amount') {
  183. $rank = 'amount';
  184. }
  185. if ($sort == 'mll') {
  186. $rank = 'mll';
  187. }
  188. $stat = array_values(arrayUtil::arraySort($stat, $rank));
  189. $export = $get['export'] ?? 0;
  190. if ($export == 1) {
  191. $mainId = $this->mainId;
  192. StatSaleClass::exportItemSale($stat, $mainId);
  193. }
  194. util::success([
  195. 'list' => $stat,
  196. 'totalAmount' => floatval($totalAmount),
  197. 'totalNum' => floatval($totalNum),
  198. 'totalMl' => floatval($totalMl),
  199. 'nextDayDeduct' => floatval($nextDayDeduct),
  200. ]);
  201. }
  202. //花材销量 ssh 20211021
  203. public function actionProfile()
  204. {
  205. //查看财务的权限
  206. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  207. if ($lookMoney == 0) {
  208. util::fail('无法查看');
  209. }
  210. $get = Yii::$app->request->get();
  211. $where = [];
  212. $where['shopId'] = $this->shopId;
  213. if (isset($get['shopId']) && !empty($get['shopId'])) {
  214. $where['shopId'] = $get['shopId'];
  215. }
  216. $searchTime = $get['searchTime'] ?? 'today';
  217. if (!empty($searchTime)) {
  218. $startTime = $get['startTime'] ?? '';
  219. $endTime = $get['endTime'] ?? '';
  220. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  221. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  222. }
  223. $list = StatItemClass::getAllByCondition($where, null, '*');
  224. if (empty($list)) {
  225. util::success(['list' => $list]);
  226. }
  227. $ids = array_column($list, 'itemId');
  228. $ids = array_unique(array_filter($ids));
  229. $productInfo = ProductClass::getByIds($ids, null, 'id');
  230. $stat = [];
  231. foreach ($list as $key => $val) {
  232. $itemId = $val['itemId'] ?? 0;
  233. $idsData[$itemId] = 0;
  234. $num = bcsub($val['num'], $val['refundNum'], 2);
  235. $amount = bcsub($val['amount'], $val['refundAmount'], 2);
  236. $name = $productInfo[$itemId]['name'] ?? '';
  237. $py = $productInfo[$itemId]['py'] ?? '';
  238. $stat[$itemId]['productId'] = $itemId;
  239. $stat[$itemId]['name'] = $name;
  240. $stat[$itemId]['py'] = $py;
  241. if (isset($stat[$itemId]['num'])) {
  242. $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
  243. } else {
  244. $stat[$itemId]['num'] = $num;
  245. }
  246. if (isset($stat[$itemId]['amount'])) {
  247. $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
  248. } else {
  249. $stat[$itemId]['amount'] = $amount;
  250. }
  251. }
  252. $stat = arrayUtil::arraySort($stat, 'num');
  253. util::success(['list' => $stat]);
  254. }
  255. }