StatItemController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. $deductMaps = [
  132. NextDayRefundClass::sumItemByProduct($this->mainId, $currentStartTime, $currentEndTime),
  133. NextDayRefundClass::sumMoneyOnlyAmountByProduct($this->mainId, $currentStartTime, $currentEndTime),
  134. ];
  135. foreach ($deductMaps as $deductMap) {
  136. foreach ($deductMap as $pid => $row) {
  137. if (!empty($staffId)) {
  138. $currentCgStaffId = $productInfo[$pid]['cgStaffId'] ?? 0;
  139. if ($staffId != $currentCgStaffId) {
  140. continue;
  141. }
  142. }
  143. if (!isset($stat[$pid])) {
  144. $stat[$pid] = [
  145. 'productId' => $pid,
  146. 'name' => $productInfo[$pid]['name'] ?? '',
  147. 'py' => $productInfo[$pid]['py'] ?? '',
  148. 'num' => '0.00',
  149. 'amount' => '0.00',
  150. 'gross' => '0.00',
  151. ];
  152. }
  153. $subNum = (string)($row['num'] ?? '0');
  154. $subAmt = (string)($row['amount'] ?? '0');
  155. $stat[$pid]['num'] = bcsub((string)$stat[$pid]['num'], $subNum, 2);
  156. $stat[$pid]['amount'] = bcsub((string)$stat[$pid]['amount'], $subAmt, 2);
  157. $stat[$pid]['gross'] = bcsub((string)$stat[$pid]['gross'], $subAmt, 2);
  158. $totalNum = bcsub((string)$totalNum, $subNum, 2);
  159. $totalAmount = bcsub((string)$totalAmount, $subAmt, 2);
  160. $totalMl = bcsub((string)$totalMl, $subAmt, 2);
  161. }
  162. }
  163. //增加毛利率
  164. foreach ($stat as $key => $val) {
  165. $profit = $val['gross'] ?? 0;
  166. $price = $val['amount'] ?? 0;
  167. if ($price > 0) {
  168. $mll = bcdiv($profit, $price, 3);
  169. $mll = bcmul($mll, 100);
  170. $mll = round($mll, 1);
  171. } else {
  172. $mll = 0;
  173. }
  174. $stat[$key]['mll'] = $mll;
  175. }
  176. $rank = 'num';
  177. if ($sort == 'gross') {
  178. $rank = 'gross';
  179. }
  180. if ($sort == 'amount') {
  181. $rank = 'amount';
  182. }
  183. if ($sort == 'mll') {
  184. $rank = 'mll';
  185. }
  186. $stat = arrayUtil::arraySort($stat, $rank);
  187. $export = $get['export'] ?? 0;
  188. if ($export == 1) {
  189. $mainId = $this->mainId;
  190. StatSaleClass::exportItemSale($stat, $mainId);
  191. }
  192. util::success(['list' => $stat, 'totalAmount' => floatval($totalAmount), 'totalNum' => floatval($totalNum), 'totalMl' => $totalMl,]);
  193. }
  194. //花材销量 ssh 20211021
  195. public function actionProfile()
  196. {
  197. //查看财务的权限
  198. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  199. if ($lookMoney == 0) {
  200. util::fail('无法查看');
  201. }
  202. $get = Yii::$app->request->get();
  203. $where = [];
  204. $where['shopId'] = $this->shopId;
  205. if (isset($get['shopId']) && !empty($get['shopId'])) {
  206. $where['shopId'] = $get['shopId'];
  207. }
  208. $searchTime = $get['searchTime'] ?? 'today';
  209. if (!empty($searchTime)) {
  210. $startTime = $get['startTime'] ?? '';
  211. $endTime = $get['endTime'] ?? '';
  212. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  213. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  214. }
  215. $list = StatItemClass::getAllByCondition($where, null, '*');
  216. if (empty($list)) {
  217. util::success(['list' => $list]);
  218. }
  219. $ids = array_column($list, 'itemId');
  220. $ids = array_unique(array_filter($ids));
  221. $productInfo = ProductClass::getByIds($ids, null, 'id');
  222. $stat = [];
  223. foreach ($list as $key => $val) {
  224. $itemId = $val['itemId'] ?? 0;
  225. $idsData[$itemId] = 0;
  226. $num = bcsub($val['num'], $val['refundNum'], 2);
  227. $amount = bcsub($val['amount'], $val['refundAmount'], 2);
  228. $name = $productInfo[$itemId]['name'] ?? '';
  229. $py = $productInfo[$itemId]['py'] ?? '';
  230. $stat[$itemId]['productId'] = $itemId;
  231. $stat[$itemId]['name'] = $name;
  232. $stat[$itemId]['py'] = $py;
  233. if (isset($stat[$itemId]['num'])) {
  234. $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
  235. } else {
  236. $stat[$itemId]['num'] = $num;
  237. }
  238. if (isset($stat[$itemId]['amount'])) {
  239. $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
  240. } else {
  241. $stat[$itemId]['amount'] = $amount;
  242. }
  243. }
  244. $stat = arrayUtil::arraySort($stat, 'num');
  245. util::success(['list' => $stat]);
  246. }
  247. }