StatItemController.php 12 KB

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