StatItemController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 bizHd\order\classes\OrderClass as HdOrderClass;
  11. use bizHd\order\classes\OrderItemClass as HdOrderItemClass;
  12. use bizHd\refund\classes\HdNextDayRefundClass;
  13. use common\components\arrayUtil;
  14. use common\components\dateUtil;
  15. use common\components\noticeUtil;
  16. use Yii;
  17. use common\components\util;
  18. class StatItemController extends BaseController
  19. {
  20. //花材销量 ssh 20230223
  21. public function actionShow()
  22. {
  23. ini_set('memory_limit', '2045M');
  24. set_time_limit(0);
  25. //查看财务的权限
  26. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  27. $hasPower = 0;
  28. if (getenv('YII_ENV') == 'production') {
  29. //小向花卉
  30. if ($this->mainId == 23390) {
  31. if (in_array($this->adminId, [31105])) {
  32. $hasPower = 1;
  33. }
  34. }
  35. } else {
  36. if ($this->mainId == 644) {
  37. if (in_array($this->adminId, [1230])) {
  38. $hasPower = 1;
  39. }
  40. }
  41. }
  42. if ($lookMoney == 0 && $hasPower == 0) {
  43. //盛丰员工可以看花材销售数据
  44. if ($this->mainId != 44282) {
  45. util::fail('没有财务权限哦');
  46. }
  47. }
  48. $get = Yii::$app->request->get();
  49. $sort = $get['sort'] ?? 'num';
  50. $where = [];
  51. $where['mainId'] = $this->mainId;
  52. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  53. $startTime = $get['startTime'] ?? '';
  54. $endTime = $get['endTime'] ?? '';
  55. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  56. $start = $period['startTime'];
  57. $end = $period['endTime'];
  58. $currentStartDate = date('Y-m-d', strtotime($start));
  59. $currentEndDate = date('Y-m-d', strtotime($end));
  60. $currentStartTime = $currentStartDate . ' 00:00:00';
  61. $currentEndTime = $currentEndDate . ' 23:59:59';
  62. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  63. $staffId = $get['staffId'] ?? 0;
  64. $list = OrderClass::getAllByCondition($where, null, '*');
  65. $productInfo = ProductClass::getAllByCondition(['mainId' => $this->mainId], null, 'id,name,py,cgStaffId,cost,avCost', 'id');
  66. $stat = [];
  67. $totalAmount = 0;
  68. $totalNum = 0;
  69. $totalMl = 0;
  70. if (!empty($list)) {
  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. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  84. if (!empty($itemList)) {
  85. foreach ($itemList as $item) {
  86. $productId = $item->productId ?? 0;
  87. $name = $productInfo[$productId]['name'] ?? '';
  88. $py = $productInfo[$productId]['py'] ?? '';
  89. if (!empty($staffId)) {
  90. $currentCgStaffId = $productInfo[$productId]['cgStaffId'] ?? 0;
  91. if ($staffId != $currentCgStaffId) {
  92. continue;
  93. }
  94. }
  95. $num = NextDayRefundClass::payDayRemainNum(
  96. $item['xhNum'] ?? 0,
  97. $item['refundNum'] ?? 0,
  98. $item['nextRefundNum'] ?? 0
  99. );
  100. $unitPrice = $item['xhUnitPrice'] ?? 0;
  101. $unitCost = $item['cost'] ?? 0;
  102. $unitGross = bcsub($unitPrice, $unitCost, 2);
  103. $currentAmount = bcmul($unitPrice, $num, 2);
  104. $currentGross = bcmul($unitGross, $num, 2);
  105. $totalNum = bcadd($totalNum, $num, 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'], $num, 2);
  116. } else {
  117. $stat[$productId]['num'] = $num;
  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. // 零售开单花材:与批发共用库存花材 id(xhOrderItem.itemId = Product.id)
  134. $hdWhere = [
  135. 'mainId' => $this->mainId,
  136. 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]],
  137. 'payTime' => ['between', [$currentStartTime, $currentEndTime]],
  138. ];
  139. $hdList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  140. if (!empty($hdList)) {
  141. foreach ($hdList as $hdOrder) {
  142. if (intval($hdOrder['forward'] ?? 0) === 1) {
  143. continue;
  144. }
  145. $mainPay = bcsub(
  146. (string)($hdOrder['mainPay'] ?? '0'),
  147. (string)($hdOrder['tkPrice'] ?? '0'),
  148. 2
  149. );
  150. $cash = bcadd((string)($hdOrder['cash'] ?? '0'), '0', 2);
  151. if (bccomp(bcadd($mainPay, $cash, 2), '0', 2) <= 0) {
  152. continue;
  153. }
  154. $orderSn = $hdOrder['orderSn'] ?? '';
  155. $itemList = HdOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  156. if (empty($itemList)) {
  157. continue;
  158. }
  159. foreach ($itemList as $item) {
  160. $productId = intval($item->itemId ?? 0);
  161. if ($productId <= 0) {
  162. continue;
  163. }
  164. $name = $productInfo[$productId]['name'] ?? '';
  165. $py = $productInfo[$productId]['py'] ?? '';
  166. if (!empty($staffId)) {
  167. $currentCgStaffId = $productInfo[$productId]['cgStaffId'] ?? 0;
  168. if ($staffId != $currentCgStaffId) {
  169. continue;
  170. }
  171. }
  172. $num = HdNextDayRefundClass::payDayRemainNum(
  173. $item->num ?? 0,
  174. $item->refundNum ?? 0,
  175. $item->nextRefundNum ?? 0
  176. );
  177. if (bccomp((string)$num, '0', 2) <= 0) {
  178. continue;
  179. }
  180. $unitPrice = $item->unitPrice ?? 0;
  181. $unitCost = $item->cost ?? null;
  182. if ($unitCost === null || $unitCost === '') {
  183. $unitCost = $productInfo[$productId]['avCost']
  184. ?? ($productInfo[$productId]['cost'] ?? 0);
  185. }
  186. $unitGross = bcsub((string)$unitPrice, (string)$unitCost, 2);
  187. $currentAmount = bcmul((string)$unitPrice, (string)$num, 2);
  188. $currentGross = bcmul($unitGross, (string)$num, 2);
  189. $totalNum = bcadd($totalNum, $num, 2);
  190. $totalAmount = bcadd($totalAmount, $currentAmount, 2);
  191. $totalMl = bcadd($totalMl, $currentGross, 2);
  192. $stat[$productId]['productId'] = $productId;
  193. $stat[$productId]['name'] = $name;
  194. $stat[$productId]['py'] = $py;
  195. if (isset($stat[$productId]['num'])) {
  196. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  197. } else {
  198. $stat[$productId]['num'] = $num;
  199. }
  200. if (isset($stat[$productId]['amount'])) {
  201. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentAmount, 2);
  202. } else {
  203. $stat[$productId]['amount'] = $currentAmount;
  204. }
  205. if (isset($stat[$productId]['gross'])) {
  206. $stat[$productId]['gross'] = bcadd($stat[$productId]['gross'], $currentGross, 2);
  207. } else {
  208. $stat[$productId]['gross'] = $currentGross;
  209. }
  210. }
  211. }
  212. }
  213. // 通过日:退货扣数量+金额;仅退款只扣金额;无当日销量的花材也建负行(批发+零售)
  214. $deductMaps = [
  215. NextDayRefundClass::sumItemByProduct($this->mainId, $currentStartTime, $currentEndTime),
  216. NextDayRefundClass::sumMoneyOnlyAmountByProduct($this->mainId, $currentStartTime, $currentEndTime),
  217. HdNextDayRefundClass::sumItemByProduct($this->mainId, $currentStartTime, $currentEndTime),
  218. HdNextDayRefundClass::sumMoneyOnlyAmountByProduct($this->mainId, $currentStartTime, $currentEndTime),
  219. ];
  220. foreach ($deductMaps as $deductMap) {
  221. foreach ($deductMap as $pid => $row) {
  222. if (!empty($staffId)) {
  223. $currentCgStaffId = $productInfo[$pid]['cgStaffId'] ?? 0;
  224. if ($staffId != $currentCgStaffId) {
  225. continue;
  226. }
  227. }
  228. if (!isset($stat[$pid])) {
  229. $stat[$pid] = [
  230. 'productId' => $pid,
  231. 'name' => $productInfo[$pid]['name'] ?? '',
  232. 'py' => $productInfo[$pid]['py'] ?? '',
  233. 'num' => '0.00',
  234. 'amount' => '0.00',
  235. 'gross' => '0.00',
  236. ];
  237. }
  238. $subNum = (string)($row['num'] ?? '0');
  239. $subAmt = (string)($row['amount'] ?? '0');
  240. $stat[$pid]['num'] = bcsub((string)$stat[$pid]['num'], $subNum, 2);
  241. $stat[$pid]['amount'] = bcsub((string)$stat[$pid]['amount'], $subAmt, 2);
  242. $stat[$pid]['gross'] = bcsub((string)$stat[$pid]['gross'], $subAmt, 2);
  243. $totalNum = bcsub((string)$totalNum, $subNum, 2);
  244. $totalAmount = bcsub((string)$totalAmount, $subAmt, 2);
  245. $totalMl = bcsub((string)$totalMl, $subAmt, 2);
  246. }
  247. }
  248. //增加毛利率
  249. foreach ($stat as $key => $val) {
  250. $profit = $val['gross'] ?? 0;
  251. $price = $val['amount'] ?? 0;
  252. if ($price > 0) {
  253. $mll = bcdiv($profit, $price, 3);
  254. $mll = bcmul($mll, 100);
  255. $mll = round($mll, 1);
  256. } else {
  257. $mll = 0;
  258. }
  259. $stat[$key]['mll'] = $mll;
  260. }
  261. $rank = 'num';
  262. if ($sort == 'gross') {
  263. $rank = 'gross';
  264. }
  265. if ($sort == 'amount') {
  266. $rank = 'amount';
  267. }
  268. if ($sort == 'mll') {
  269. $rank = 'mll';
  270. }
  271. $stat = arrayUtil::arraySort($stat, $rank);
  272. $export = $get['export'] ?? 0;
  273. if ($export == 1) {
  274. $mainId = $this->mainId;
  275. StatSaleClass::exportItemSale($stat, $mainId);
  276. }
  277. util::success(['list' => $stat, 'totalAmount' => floatval($totalAmount), 'totalNum' => floatval($totalNum), 'totalMl' => $totalMl,]);
  278. }
  279. //花材销量 ssh 20211021
  280. public function actionProfile()
  281. {
  282. //查看财务的权限
  283. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  284. if ($lookMoney == 0) {
  285. util::fail('无法查看');
  286. }
  287. $get = Yii::$app->request->get();
  288. $where = [];
  289. $where['shopId'] = $this->shopId;
  290. if (isset($get['shopId']) && !empty($get['shopId'])) {
  291. $where['shopId'] = $get['shopId'];
  292. }
  293. $searchTime = $get['searchTime'] ?? 'today';
  294. if (!empty($searchTime)) {
  295. $startTime = $get['startTime'] ?? '';
  296. $endTime = $get['endTime'] ?? '';
  297. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  298. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  299. }
  300. $list = StatItemClass::getAllByCondition($where, null, '*');
  301. if (empty($list)) {
  302. util::success(['list' => $list]);
  303. }
  304. $ids = array_column($list, 'itemId');
  305. $ids = array_unique(array_filter($ids));
  306. $productInfo = ProductClass::getByIds($ids, null, 'id');
  307. $stat = [];
  308. foreach ($list as $key => $val) {
  309. $itemId = $val['itemId'] ?? 0;
  310. $idsData[$itemId] = 0;
  311. $num = bcsub($val['num'], $val['refundNum'], 2);
  312. $amount = bcsub($val['amount'], $val['refundAmount'], 2);
  313. $name = $productInfo[$itemId]['name'] ?? '';
  314. $py = $productInfo[$itemId]['py'] ?? '';
  315. $stat[$itemId]['productId'] = $itemId;
  316. $stat[$itemId]['name'] = $name;
  317. $stat[$itemId]['py'] = $py;
  318. if (isset($stat[$itemId]['num'])) {
  319. $stat[$itemId]['num'] = bcadd($stat[$itemId]['num'], $num, 2);
  320. } else {
  321. $stat[$itemId]['num'] = $num;
  322. }
  323. if (isset($stat[$itemId]['amount'])) {
  324. $stat[$itemId]['amount'] = bcadd($stat[$itemId]['amount'], $amount, 2);
  325. } else {
  326. $stat[$itemId]['amount'] = $amount;
  327. }
  328. }
  329. $stat = arrayUtil::arraySort($stat, 'num');
  330. util::success(['list' => $stat]);
  331. }
  332. }