StatItemController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\stat\classes\StatItemClass;
  4. use bizGhs\forward\classes\ForwardClass;
  5. use bizGhs\order\classes\OrderClass;
  6. use bizGhs\order\classes\OrderItemClass;
  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. foreach ($list as $order) {
  71. $orderSn = $order['orderSn'] ?? '';
  72. $status = $order['status'] ?? 0;
  73. $book = $order['book'] ?? 0;
  74. $actPrice = $order['actPrice'] ?? 0;
  75. //ssh 冲销单功能:冲销单 actPrice 为负数,原有 actPrice<=0 会把它当"已全部退款"误跳过,
  76. //改成只有非冲销单才按原逻辑跳过,让冲销数量能正常从销量里扣除
  77. $forward = $order['forward'] ?? 0;
  78. if ($status == 5 || $status == 1 || ($book == 1 && $status == 2)) {
  79. continue;
  80. }
  81. if ($forward != 1 && $actPrice <= 0) {
  82. //如果全部退款的花材也不统计
  83. continue;
  84. }
  85. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  86. if (!empty($itemList)) {
  87. foreach ($itemList as $item) {
  88. $productId = $item->productId ?? 0;
  89. $name = $productInfo[$productId]['name'] ?? '';
  90. $py = $productInfo[$productId]['py'] ?? '';
  91. if (!empty($staffId)) {
  92. $currentCgStaffId = $productInfo[$productId]['cgStaffId'] ?? 0;
  93. if ($staffId != $currentCgStaffId) {
  94. continue;
  95. }
  96. }
  97. $num = bcsub($item['xhNum'], $item['refundNum'], 2);
  98. $unitPrice = $item['xhUnitPrice'] ?? 0;
  99. $unitCost = $item['cost'] ?? 0;
  100. $unitGross = bcsub($unitPrice, $unitCost, 2);
  101. $currentAmount = bcmul($unitPrice, $num, 2);
  102. $currentGross = bcmul($unitGross, $num, 2);
  103. //冲销单的数量字段($num)是正数,代表冲销掉的数量,需要反转符号去抵扣总销量;
  104. //但金额($currentAmount/$currentGross)已经用正数量×负单价算出了正确的负值,不能再用这个反转后的数量
  105. $qtyForTotal = ($forward == 1) ? bcmul($num, -1, 2) : $num;
  106. $totalNum = bcadd($totalNum, $qtyForTotal, 2);
  107. $totalAmount = bcadd($totalAmount, $currentAmount, 2);
  108. $totalMl = bcadd($totalMl, $currentGross, 2);
  109. if ($currentGross < 0) {
  110. //noticeUtil::push($item->id . ' 毛利是负的', '15280215347');
  111. }
  112. $stat[$productId]['productId'] = $productId;
  113. $stat[$productId]['name'] = $name;
  114. $stat[$productId]['py'] = $py;
  115. if (isset($stat[$productId]['num'])) {
  116. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $qtyForTotal, 2);
  117. } else {
  118. $stat[$productId]['num'] = $qtyForTotal;
  119. }
  120. if (isset($stat[$productId]['amount'])) {
  121. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentAmount, 2);
  122. } else {
  123. $stat[$productId]['amount'] = $currentAmount;
  124. }
  125. if (isset($stat[$productId]['gross'])) {
  126. $stat[$productId]['gross'] = bcadd($stat[$productId]['gross'], $currentGross, 2);
  127. } else {
  128. $stat[$productId]['gross'] = $currentGross;
  129. }
  130. }
  131. }
  132. }
  133. // 方案A:花材销量扣减成功冲销明细(数量/金额;毛利按金额同口径扣减)
  134. $fwdItemMap = ForwardClass::sumItemByProduct($this->mainId, $currentStartTime, $currentEndTime);
  135. foreach ($fwdItemMap as $fwdPid => $fwdRow) {
  136. if (!isset($stat[$fwdPid])) {
  137. continue;
  138. }
  139. $stat[$fwdPid]['num'] = bcsub((string)($stat[$fwdPid]['num'] ?? 0), (string)$fwdRow['num'], 2);
  140. $stat[$fwdPid]['amount'] = bcsub((string)($stat[$fwdPid]['amount'] ?? 0), (string)$fwdRow['amount'], 2);
  141. $stat[$fwdPid]['gross'] = bcsub((string)($stat[$fwdPid]['gross'] ?? 0), (string)$fwdRow['amount'], 2);
  142. $totalNum = bcsub((string)$totalNum, (string)$fwdRow['num'], 2);
  143. $totalAmount = bcsub((string)$totalAmount, (string)$fwdRow['amount'], 2);
  144. $totalMl = bcsub((string)$totalMl, (string)$fwdRow['amount'], 2);
  145. }
  146. //增加毛利率
  147. foreach ($stat as $key => $val) {
  148. $profit = $val['gross'] ?? 0;
  149. $price = $val['amount'] ?? 0;
  150. if ($price > 0) {
  151. $mll = bcdiv($profit, $price, 3);
  152. $mll = bcmul($mll, 100);
  153. $mll = round($mll, 1);
  154. } else {
  155. $mll = 0;
  156. }
  157. $stat[$key]['mll'] = $mll;
  158. }
  159. $rank = 'num';
  160. if ($sort == 'gross') {
  161. $rank = 'gross';
  162. }
  163. if ($sort == 'amount') {
  164. $rank = 'amount';
  165. }
  166. if ($sort == 'mll') {
  167. $rank = 'mll';
  168. }
  169. $stat = arrayUtil::arraySort($stat, $rank);
  170. $export = $get['export'] ?? 0;
  171. if ($export == 1) {
  172. $mainId = $this->mainId;
  173. StatSaleClass::exportItemSale($stat, $mainId);
  174. }
  175. util::success(['list' => $stat, 'totalAmount' => floatval($totalAmount), 'totalNum' => floatval($totalNum), 'totalMl' => $totalMl,]);
  176. }
  177. //花材销量 ssh 20211021
  178. public function actionProfile()
  179. {
  180. //查看财务的权限
  181. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  182. if ($lookMoney == 0) {
  183. util::fail('无法查看');
  184. }
  185. $get = Yii::$app->request->get();
  186. $where = [];
  187. $where['shopId'] = $this->shopId;
  188. if (isset($get['shopId']) && !empty($get['shopId'])) {
  189. $where['shopId'] = $get['shopId'];
  190. }
  191. $searchTime = $get['searchTime'] ?? 'today';
  192. if (!empty($searchTime)) {
  193. $startTime = $get['startTime'] ?? '';
  194. $endTime = $get['endTime'] ?? '';
  195. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  196. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  197. }
  198. $list = StatItemClass::getAllByCondition($where, null, '*');
  199. if (empty($list)) {
  200. util::success(['list' => $list]);
  201. }
  202. $ids = array_column($list, 'itemId');
  203. $ids = array_unique(array_filter($ids));
  204. $productInfo = ProductClass::getByIds($ids, null, 'id');
  205. $stat = [];
  206. foreach ($list as $key => $val) {
  207. $itemId = $val['itemId'] ?? 0;
  208. $idsData[$itemId] = 0;
  209. $num = bcsub($val['num'], $val['refundNum'], 2);
  210. $amount = bcsub($val['amount'], $val['refundAmount'], 2);
  211. $name = $productInfo[$itemId]['name'] ?? '';
  212. $py = $productInfo[$itemId]['py'] ?? '';
  213. $stat[$itemId]['productId'] = $itemId;
  214. $stat[$itemId]['name'] = $name;
  215. $stat[$itemId]['py'] = $py;
  216. if (isset($stat[$itemId]['num'])) {
  217. $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
  218. } else {
  219. $stat[$itemId]['num'] = $num;
  220. }
  221. if (isset($stat[$itemId]['amount'])) {
  222. $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
  223. } else {
  224. $stat[$itemId]['amount'] = $amount;
  225. }
  226. }
  227. $stat = arrayUtil::arraySort($stat, 'num');
  228. util::success(['list' => $stat]);
  229. }
  230. }