StatItemController.php 9.6 KB

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