StatItemController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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\product\classes\ProductClass;
  7. use bizGhs\shop\classes\ShopAdminClass;
  8. use bizGhs\stat\classes\StatSaleClass;
  9. use common\components\arrayUtil;
  10. use common\components\dateUtil;
  11. use common\components\noticeUtil;
  12. use Yii;
  13. use common\components\util;
  14. class StatItemController extends BaseController
  15. {
  16. //花材销量 ssh 20230223
  17. public function actionShow()
  18. {
  19. ini_set('memory_limit', '2045M');
  20. set_time_limit(0);
  21. //查看财务的权限
  22. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  23. $hasPower = 0;
  24. if (getenv('YII_ENV') == 'production') {
  25. //小向花卉
  26. if ($this->mainId == 23390) {
  27. if (in_array($this->adminId, [31105])) {
  28. $hasPower = 1;
  29. }
  30. }
  31. } else {
  32. if ($this->mainId == 644) {
  33. if (in_array($this->adminId, [1230])) {
  34. $hasPower = 1;
  35. }
  36. }
  37. }
  38. if ($lookMoney == 0 && $hasPower == 0) {
  39. //盛丰员工可以看花材销售数据
  40. if ($this->mainId != 44282) {
  41. util::fail('没有财务权限哦');
  42. }
  43. }
  44. $get = Yii::$app->request->get();
  45. $sort = $get['sort'] ?? 'num';
  46. $where = [];
  47. $where['mainId'] = $this->mainId;
  48. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  49. $startTime = $get['startTime'] ?? '';
  50. $endTime = $get['endTime'] ?? '';
  51. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  52. $start = $period['startTime'];
  53. $end = $period['endTime'];
  54. $currentStartDate = date('Y-m-d', strtotime($start));
  55. $currentEndDate = date('Y-m-d', strtotime($end));
  56. $currentStartTime = $currentStartDate . ' 00:00:00';
  57. $currentEndTime = $currentEndDate . ' 23:59:59';
  58. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  59. $staffId = $get['staffId'] ?? 0;
  60. $list = OrderClass::getAllByCondition($where, null, '*');
  61. if (empty($list)) {
  62. util::success(['list' => []]);
  63. }
  64. $productInfo = ProductClass::getAllByCondition(['mainId' => $this->mainId], null, 'id,name,py,cgStaffId', 'id');
  65. $stat = [];
  66. $totalAmount = 0;
  67. $totalNum = 0;
  68. $totalMl = 0;
  69. foreach ($list as $order) {
  70. $orderSn = $order['orderSn'] ?? '';
  71. $status = $order['status'] ?? 0;
  72. $book = $order['book'] ?? 0;
  73. $actPrice = $order['actPrice'] ?? 0;
  74. //ssh 冲销单功能:冲销单 actPrice 为负数,原有 actPrice<=0 会把它当"已全部退款"误跳过,
  75. //改成只有非冲销单才按原逻辑跳过,让冲销数量能正常从销量里扣除
  76. $forward = $order['forward'] ?? 0;
  77. if ($status == 5 || $status == 1 || ($book == 1 && $status == 2)) {
  78. continue;
  79. }
  80. if ($forward != 1 && $actPrice <= 0) {
  81. //如果全部退款的花材也不统计
  82. continue;
  83. }
  84. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  85. if (!empty($itemList)) {
  86. foreach ($itemList as $item) {
  87. $productId = $item->productId ?? 0;
  88. $name = $productInfo[$productId]['name'] ?? '';
  89. $py = $productInfo[$productId]['py'] ?? '';
  90. if (!empty($staffId)) {
  91. $currentCgStaffId = $productInfo[$productId]['cgStaffId'] ?? 0;
  92. if ($staffId != $currentCgStaffId) {
  93. continue;
  94. }
  95. }
  96. $num = bcsub($item['xhNum'], $item['refundNum'], 2);
  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. //冲销单的数量字段($num)是正数,代表冲销掉的数量,需要反转符号去抵扣总销量;
  103. //但金额($currentAmount/$currentGross)已经用正数量×负单价算出了正确的负值,不能再用这个反转后的数量
  104. $qtyForTotal = ($forward == 1) ? bcmul($num, -1, 2) : $num;
  105. $totalNum = bcadd($totalNum, $qtyForTotal, 2);
  106. $totalAmount = bcadd($totalAmount, $currentAmount, 2);
  107. $totalMl = bcadd($totalMl, $currentGross, 2);
  108. if ($currentGross < 0) {
  109. //noticeUtil::push($item->id . ' 毛利是负的', '15280215347');
  110. }
  111. $stat[$productId]['productId'] = $productId;
  112. $stat[$productId]['name'] = $name;
  113. $stat[$productId]['py'] = $py;
  114. if (isset($stat[$productId]['num'])) {
  115. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $qtyForTotal, 2);
  116. } else {
  117. $stat[$productId]['num'] = $qtyForTotal;
  118. }
  119. if (isset($stat[$productId]['amount'])) {
  120. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentAmount, 2);
  121. } else {
  122. $stat[$productId]['amount'] = $currentAmount;
  123. }
  124. if (isset($stat[$productId]['gross'])) {
  125. $stat[$productId]['gross'] = bcadd($stat[$productId]['gross'], $currentGross, 2);
  126. } else {
  127. $stat[$productId]['gross'] = $currentGross;
  128. }
  129. }
  130. }
  131. }
  132. //增加毛利率
  133. foreach ($stat as $key => $val) {
  134. $profit = $val['gross'] ?? 0;
  135. $price = $val['amount'] ?? 0;
  136. if ($price > 0) {
  137. $mll = bcdiv($profit, $price, 3);
  138. $mll = bcmul($mll, 100);
  139. $mll = round($mll, 1);
  140. } else {
  141. $mll = 0;
  142. }
  143. $stat[$key]['mll'] = $mll;
  144. }
  145. $rank = 'num';
  146. if ($sort == 'gross') {
  147. $rank = 'gross';
  148. }
  149. if ($sort == 'amount') {
  150. $rank = 'amount';
  151. }
  152. if ($sort == 'mll') {
  153. $rank = 'mll';
  154. }
  155. $stat = arrayUtil::arraySort($stat, $rank);
  156. $export = $get['export'] ?? 0;
  157. if ($export == 1) {
  158. $mainId = $this->mainId;
  159. StatSaleClass::exportItemSale($stat, $mainId);
  160. }
  161. util::success(['list' => $stat, 'totalAmount' => floatval($totalAmount), 'totalNum' => floatval($totalNum), 'totalMl' => $totalMl,]);
  162. }
  163. //花材销量 ssh 20211021
  164. public function actionProfile()
  165. {
  166. //查看财务的权限
  167. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  168. if ($lookMoney == 0) {
  169. util::fail('无法查看');
  170. }
  171. $get = Yii::$app->request->get();
  172. $where = [];
  173. $where['shopId'] = $this->shopId;
  174. if (isset($get['shopId']) && !empty($get['shopId'])) {
  175. $where['shopId'] = $get['shopId'];
  176. }
  177. $searchTime = $get['searchTime'] ?? 'today';
  178. if (!empty($searchTime)) {
  179. $startTime = $get['startTime'] ?? '';
  180. $endTime = $get['endTime'] ?? '';
  181. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  182. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  183. }
  184. $list = StatItemClass::getAllByCondition($where, null, '*');
  185. if (empty($list)) {
  186. util::success(['list' => $list]);
  187. }
  188. $ids = array_column($list, 'itemId');
  189. $ids = array_unique(array_filter($ids));
  190. $productInfo = ProductClass::getByIds($ids, null, 'id');
  191. $stat = [];
  192. foreach ($list as $key => $val) {
  193. $itemId = $val['itemId'] ?? 0;
  194. $idsData[$itemId] = 0;
  195. $num = bcsub($val['num'], $val['refundNum'], 2);
  196. $amount = bcsub($val['amount'], $val['refundAmount'], 2);
  197. $name = $productInfo[$itemId]['name'] ?? '';
  198. $py = $productInfo[$itemId]['py'] ?? '';
  199. $stat[$itemId]['productId'] = $itemId;
  200. $stat[$itemId]['name'] = $name;
  201. $stat[$itemId]['py'] = $py;
  202. if (isset($stat[$itemId]['num'])) {
  203. $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
  204. } else {
  205. $stat[$itemId]['num'] = $num;
  206. }
  207. if (isset($stat[$itemId]['amount'])) {
  208. $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
  209. } else {
  210. $stat[$itemId]['amount'] = $amount;
  211. }
  212. }
  213. $stat = arrayUtil::arraySort($stat, 'num');
  214. util::success(['list' => $stat]);
  215. }
  216. }