StatItemController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. // timeType=bill(默认记账 payTime)/ send(发货 sendTime)
  60. $timeType = isset($get['timeType']) ? strval($get['timeType']) : 'bill';
  61. $saleTimeField = ($timeType === 'send') ? 'sendTime' : 'payTime';
  62. $where[$saleTimeField] = ['between', [$currentStartTime, $currentEndTime]];
  63. // sendTime 不代表成交,发货口径只统计已确认付款(xhGhsOrder.payStatus=1)
  64. if ($timeType === 'send') {
  65. $where['payStatus'] = 1;
  66. }
  67. // includeNextDay=1(默认)计入跨天售后通过日扣减;=0 不含售后历史
  68. $includeNextDay = !isset($get['includeNextDay']) || intval($get['includeNextDay']) === 1;
  69. $staffId = $get['staffId'] ?? 0;
  70. $list = OrderClass::getAllByCondition($where, null, '*');
  71. $productInfo = ProductClass::getAllByCondition(['mainId' => $this->mainId], null, 'id,name,py,cgStaffId', 'id');
  72. $stat = [];
  73. $totalAmount = 0;
  74. $totalNum = 0;
  75. $totalMl = 0;
  76. if (!empty($list)) {
  77. foreach ($list as $order) {
  78. $orderSn = $order['orderSn'] ?? '';
  79. $status = $order['status'] ?? 0;
  80. $book = $order['book'] ?? 0;
  81. $actPrice = $order['actPrice'] ?? 0;
  82. if ($status == 5 || $status == 1 || ($book == 1 && $status == 2)) {
  83. continue;
  84. }
  85. if ($actPrice <= 0) {
  86. //如果全部退款的花材也不统计
  87. continue;
  88. }
  89. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  90. if (!empty($itemList)) {
  91. foreach ($itemList as $item) {
  92. $productId = $item->productId ?? 0;
  93. $name = $productInfo[$productId]['name'] ?? '';
  94. $py = $productInfo[$productId]['py'] ?? '';
  95. if (!empty($staffId)) {
  96. $currentCgStaffId = $productInfo[$productId]['cgStaffId'] ?? 0;
  97. if ($staffId != $currentCgStaffId) {
  98. continue;
  99. }
  100. }
  101. $num = NextDayRefundClass::payDayRemainNum(
  102. $item['xhNum'] ?? 0,
  103. $item['refundNum'] ?? 0,
  104. $item['nextRefundNum'] ?? 0
  105. );
  106. $unitPrice = $item['xhUnitPrice'] ?? 0;
  107. $unitCost = $item['cost'] ?? 0;
  108. $unitGross = bcsub($unitPrice, $unitCost, 2);
  109. $currentAmount = bcmul($unitPrice, $num, 2);
  110. $currentGross = bcmul($unitGross, $num, 2);
  111. $totalNum = bcadd($totalNum, $num, 2);
  112. $totalAmount = bcadd($totalAmount, $currentAmount, 2);
  113. $totalMl = bcadd($totalMl, $currentGross, 2);
  114. if ($currentGross < 0) {
  115. //noticeUtil::push($item->id . ' 毛利是负的', '15280215347');
  116. }
  117. $stat[$productId]['productId'] = $productId;
  118. $stat[$productId]['name'] = $name;
  119. $stat[$productId]['py'] = $py;
  120. if (isset($stat[$productId]['num'])) {
  121. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  122. } else {
  123. $stat[$productId]['num'] = $num;
  124. }
  125. if (isset($stat[$productId]['amount'])) {
  126. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentAmount, 2);
  127. } else {
  128. $stat[$productId]['amount'] = $currentAmount;
  129. }
  130. if (isset($stat[$productId]['gross'])) {
  131. $stat[$productId]['gross'] = bcadd($stat[$productId]['gross'], $currentGross, 2);
  132. } else {
  133. $stat[$productId]['gross'] = $currentGross;
  134. }
  135. }
  136. }
  137. }
  138. }
  139. // 通过日:退货扣数量+金额;仅退款只扣金额;无当日销量的花材也建负行
  140. $nextDayDeduct = '0.00';
  141. if ($includeNextDay) {
  142. $deductMaps = [
  143. NextDayRefundClass::sumItemByProduct($this->mainId, $currentStartTime, $currentEndTime),
  144. NextDayRefundClass::sumMoneyOnlyAmountByProduct($this->mainId, $currentStartTime, $currentEndTime),
  145. ];
  146. foreach ($deductMaps as $deductMap) {
  147. foreach ($deductMap as $pid => $row) {
  148. if (!empty($staffId)) {
  149. $currentCgStaffId = $productInfo[$pid]['cgStaffId'] ?? 0;
  150. if ($staffId != $currentCgStaffId) {
  151. continue;
  152. }
  153. }
  154. if (!isset($stat[$pid])) {
  155. $stat[$pid] = [
  156. 'productId' => $pid,
  157. 'name' => $productInfo[$pid]['name'] ?? '',
  158. 'py' => $productInfo[$pid]['py'] ?? '',
  159. 'num' => '0.00',
  160. 'amount' => '0.00',
  161. 'gross' => '0.00',
  162. ];
  163. }
  164. $subNum = (string)($row['num'] ?? '0');
  165. $subAmt = (string)($row['amount'] ?? '0');
  166. $stat[$pid]['num'] = bcsub((string)$stat[$pid]['num'], $subNum, 2);
  167. $stat[$pid]['amount'] = bcsub((string)$stat[$pid]['amount'], $subAmt, 2);
  168. $stat[$pid]['gross'] = bcsub((string)$stat[$pid]['gross'], $subAmt, 2);
  169. $totalNum = bcsub((string)$totalNum, $subNum, 2);
  170. $totalAmount = bcsub((string)$totalAmount, $subAmt, 2);
  171. $totalMl = bcsub((string)$totalMl, $subAmt, 2);
  172. $nextDayDeduct = bcadd($nextDayDeduct, $subAmt, 2);
  173. }
  174. }
  175. }
  176. //增加毛利率(金额为负时不计算毛利率,避免除零/误导)
  177. foreach ($stat as $key => $val) {
  178. $profit = $val['gross'] ?? 0;
  179. $price = $val['amount'] ?? 0;
  180. if (bccomp((string)$price, '0', 2) > 0) {
  181. $mll = bcdiv($profit, $price, 3);
  182. $mll = bcmul($mll, 100);
  183. $mll = round($mll, 1);
  184. } else {
  185. $mll = 0;
  186. }
  187. $stat[$key]['mll'] = $mll;
  188. }
  189. $rank = 'num';
  190. if ($sort == 'gross') {
  191. $rank = 'gross';
  192. }
  193. if ($sort == 'amount') {
  194. $rank = 'amount';
  195. }
  196. if ($sort == 'mll') {
  197. $rank = 'mll';
  198. }
  199. $stat = array_values(arrayUtil::arraySort($stat, $rank));
  200. $export = $get['export'] ?? 0;
  201. if ($export == 1) {
  202. $mainId = $this->mainId;
  203. StatSaleClass::exportItemSale($stat, $mainId);
  204. }
  205. util::success([
  206. 'list' => $stat,
  207. 'totalAmount' => floatval($totalAmount),
  208. 'totalNum' => floatval($totalNum),
  209. 'totalMl' => floatval($totalMl),
  210. 'nextDayDeduct' => floatval($nextDayDeduct),
  211. ]);
  212. }
  213. //花材销量 ssh 20211021
  214. public function actionProfile()
  215. {
  216. //查看财务的权限
  217. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  218. if ($lookMoney == 0) {
  219. util::fail('无法查看');
  220. }
  221. $get = Yii::$app->request->get();
  222. $where = [];
  223. $where['shopId'] = $this->shopId;
  224. if (isset($get['shopId']) && !empty($get['shopId'])) {
  225. $where['shopId'] = $get['shopId'];
  226. }
  227. $searchTime = $get['searchTime'] ?? 'today';
  228. if (!empty($searchTime)) {
  229. $startTime = $get['startTime'] ?? '';
  230. $endTime = $get['endTime'] ?? '';
  231. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  232. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  233. }
  234. $list = StatItemClass::getAllByCondition($where, null, '*');
  235. if (empty($list)) {
  236. util::success(['list' => $list]);
  237. }
  238. $ids = array_column($list, 'itemId');
  239. $ids = array_unique(array_filter($ids));
  240. $productInfo = ProductClass::getByIds($ids, null, 'id');
  241. $stat = [];
  242. foreach ($list as $key => $val) {
  243. $itemId = $val['itemId'] ?? 0;
  244. $idsData[$itemId] = 0;
  245. $num = bcsub($val['num'], $val['refundNum'], 2);
  246. $amount = bcsub($val['amount'], $val['refundAmount'], 2);
  247. $name = $productInfo[$itemId]['name'] ?? '';
  248. $py = $productInfo[$itemId]['py'] ?? '';
  249. $stat[$itemId]['productId'] = $itemId;
  250. $stat[$itemId]['name'] = $name;
  251. $stat[$itemId]['py'] = $py;
  252. if (isset($stat[$itemId]['num'])) {
  253. $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
  254. } else {
  255. $stat[$itemId]['num'] = $num;
  256. }
  257. if (isset($stat[$itemId]['amount'])) {
  258. $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
  259. } else {
  260. $stat[$itemId]['amount'] = $amount;
  261. }
  262. }
  263. $stat = arrayUtil::arraySort($stat, 'num');
  264. util::success(['list' => $stat]);
  265. }
  266. }