StatCgGhsController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\stat\classes\StatCgGhsClass;
  4. use bizGhs\cg\classes\CgPurchaseNextDayClass;
  5. use bizGhs\cg\classes\CgRefundClass;
  6. use bizGhs\ghs\classes\GhsClass;
  7. use bizGhs\order\classes\PurchaseOrderClass;
  8. use bizGhs\order\classes\PurchaseOrderItemClass;
  9. use common\components\arrayUtil;
  10. use common\components\dateUtil;
  11. use Yii;
  12. use common\components\util;
  13. class StatCgGhsController extends BaseController
  14. {
  15. /**
  16. * 供货业绩:入库日累加 actPrice(当天售后已减实付);
  17. * 跨天售后按通过日扣金额(可为负,笔数不减),对齐销售客户业绩口径。
  18. */
  19. public function actionProfile()
  20. {
  21. $get = Yii::$app->request->get();
  22. $mainId = $this->mainId;
  23. $export = $get['export'] ?? 0;
  24. $debt = isset($get['debt']) && is_numeric($get['debt']) ? $get['debt'] : 0;
  25. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  26. $startTime = $get['startTime'] ?? '';
  27. $endTime = $get['endTime'] ?? '';
  28. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  29. $start = $period['startTime'];
  30. $end = $period['endTime'];
  31. $ghsList = GhsClass::getAllByCondition(['ownMainId' => $mainId], null, '*', 'id');
  32. $currentStartDate = date('Y-m-d', strtotime($start));
  33. $currentEndDate = date('Y-m-d', strtotime($end));
  34. $currentStartTime = $currentStartDate . ' 00:00:00';
  35. $currentEndTime = $currentEndDate . ' 23:59:59';
  36. $where = ['mainId' => $mainId];
  37. if (!empty($debt)) {
  38. $where['debt'] = $debt;
  39. }
  40. // timeType=bill(默认记账 entryTime)/ send(发货 sendTime)
  41. $timeType = isset($get['timeType']) ? strval($get['timeType']) : 'bill';
  42. $cgTimeField = ($timeType === 'send') ? 'sendTime' : 'entryTime';
  43. $where[$cgTimeField] = ['between', [$currentStartTime, $currentEndTime]];
  44. // sendTime 不代表已入库,发货口径只统计已完成采购单
  45. if ($timeType === 'send') {
  46. $where['status'] = 4;
  47. }
  48. $orderList = PurchaseOrderClass::getAllByCondition($where, null, '*');
  49. $list = [];
  50. $totalActPrice = 0;
  51. $totalOrderPrice = 0;
  52. $totalCarton = 0;
  53. $totalPack = 0;
  54. $totalFreight = 0;
  55. $totalTkPrice = 0;
  56. //增加项
  57. $totalOnlyTkPrice = 0;
  58. $totalNum = 0;
  59. if (!empty($orderList)) {
  60. foreach ($orderList as $orderInfo) {
  61. $actPrice = $orderInfo['actPrice'] ?? 0;
  62. $orderPrice = $orderInfo['orderPrice'] ?? 0;
  63. $ghsId = $orderInfo['ghsId'] ?? 0;
  64. $status = $orderInfo['status'] ?? 0;
  65. $orderSn = $orderInfo['orderSn'] ?? '';
  66. $tkPrice = $orderInfo['tkPrice'] ?? 0;
  67. if ($status != 4) {
  68. continue;
  69. }
  70. $carton = 0;
  71. $pack = 0;
  72. $freight = 0;
  73. $orderItemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  74. if (!empty($orderItemList)) {
  75. foreach ($orderItemList as $orderItem) {
  76. $num = $orderItem['xhNum'] ?? 0;
  77. $unitPrice = $orderItem['xhUnitPrice'] ?? 0;
  78. $refundNum = $orderItem['refundNum'] ?? 0;
  79. $classId = $orderItem['classId'] ?? 0;
  80. if (getenv('YII_ENV') == 'production') {
  81. if ($mainId == 23390) {
  82. // 运费花材:入库日净量不含隔天退货
  83. $remainNum = CgPurchaseNextDayClass::payDayRemainNum(
  84. $num,
  85. $refundNum,
  86. $orderItem['nextRefundNum'] ?? 0
  87. );
  88. $amount = bcmul($remainNum, $unitPrice, 2);
  89. if ($classId == 73120) {
  90. $freight = bcadd($freight, $amount, 2);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. //增加项:仅退款(含当天);隔天仅退款金额在下方通过日一并扣 actPrice
  97. if ($tkPrice > 0) {
  98. $refundList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  99. if (!empty($refundList)) {
  100. foreach ($refundList as $refundInfo) {
  101. if (isset($refundInfo->refundType) && $refundInfo->refundType == 2) {
  102. // 隔天仅退款记在通过日,入库日不重复计入 onlyTk
  103. if (intval($refundInfo->sameDay ?? 1) === CgPurchaseNextDayClass::SAME_DAY_NO) {
  104. continue;
  105. }
  106. $refundPrice = $refundInfo->refundPrice ?? 0;
  107. $totalOnlyTkPrice = bcadd($totalOnlyTkPrice, $refundPrice, 2);
  108. }
  109. }
  110. }
  111. }
  112. $totalCarton = bcadd($totalCarton, $carton, 2);
  113. $totalPack = bcadd($totalPack, $pack, 2);
  114. $totalFreight = bcadd($totalFreight, $freight, 2);
  115. $totalTkPrice = bcadd($totalTkPrice, $tkPrice, 2);
  116. $totalActPrice = bcadd($totalActPrice, $actPrice, 2);
  117. $totalOrderPrice = bcadd($totalOrderPrice, $orderPrice, 2);
  118. $totalNum++;
  119. if (isset($list[$ghsId])) {
  120. $list[$ghsId]['actPrice'] = bcadd($list[$ghsId]['actPrice'], $actPrice, 2);
  121. $list[$ghsId]['orderPrice'] = bcadd($list[$ghsId]['orderPrice'], $orderPrice, 2);
  122. $list[$ghsId]['num'] = bcadd($list[$ghsId]['num'], 1);
  123. $list[$ghsId]['carton'] = bcadd($list[$ghsId]['carton'], $carton, 2);
  124. $list[$ghsId]['pack'] = bcadd($list[$ghsId]['pack'], $pack, 2);
  125. $list[$ghsId]['freight'] = bcadd($list[$ghsId]['freight'], $freight, 2);
  126. $list[$ghsId]['tkPrice'] = bcadd($list[$ghsId]['tkPrice'], $tkPrice, 2);
  127. } else {
  128. $name = $ghsList[$ghsId] && $ghsList[$ghsId]['name'] ? $ghsList[$ghsId]['name'] : '未命名';
  129. $mobile = $ghsList[$ghsId] && $ghsList[$ghsId]['mobile'] ? $ghsList[$ghsId]['mobile'] : '0';
  130. $list[$ghsId] = [
  131. 'id' => $ghsId,
  132. 'name' => $name,
  133. 'mobile' => $mobile,
  134. 'orderPrice' => $orderPrice,
  135. 'actPrice' => $actPrice,
  136. 'num' => 1,
  137. 'carton' => $carton,
  138. 'pack' => $pack,
  139. 'freight' => $freight,
  140. 'tkPrice' => $tkPrice,
  141. ];
  142. }
  143. }
  144. }
  145. // 跨天售后:按通过日扣供货商金额;无当日入库也建负行(笔数不减)
  146. $nextDayDeduct = CgPurchaseNextDayClass::sumAmountByMainAndTime($mainId, $currentStartTime, $currentEndTime);
  147. $nextMap = CgPurchaseNextDayClass::sumAmountGroupByGhs($mainId, $currentStartTime, $currentEndTime);
  148. foreach ($nextMap as $ghsId => $amt) {
  149. if (bccomp((string)$amt, '0', 2) <= 0) {
  150. continue;
  151. }
  152. if (!isset($list[$ghsId])) {
  153. $name = isset($ghsList[$ghsId]) && !empty($ghsList[$ghsId]['name']) ? $ghsList[$ghsId]['name'] : '未命名';
  154. $mobile = isset($ghsList[$ghsId]) && !empty($ghsList[$ghsId]['mobile']) ? $ghsList[$ghsId]['mobile'] : '0';
  155. $list[$ghsId] = [
  156. 'id' => $ghsId,
  157. 'name' => $name,
  158. 'mobile' => $mobile,
  159. 'orderPrice' => 0,
  160. 'actPrice' => '0.00',
  161. 'num' => 0,
  162. 'carton' => 0,
  163. 'pack' => 0,
  164. 'freight' => 0,
  165. 'tkPrice' => 0,
  166. ];
  167. }
  168. $list[$ghsId]['actPrice'] = bcsub((string)$list[$ghsId]['actPrice'], (string)$amt, 2);
  169. $list[$ghsId]['tkPrice'] = bcadd((string)($list[$ghsId]['tkPrice'] ?? 0), (string)$amt, 2);
  170. $totalActPrice = bcsub((string)$totalActPrice, (string)$amt, 2);
  171. $totalTkPrice = bcadd((string)$totalTkPrice, (string)$amt, 2);
  172. }
  173. // 隔天仅退款并入「仅退款」合计(与小程序供货业绩扣减口径一致)
  174. $totalOnlyTkPrice = bcadd(
  175. (string)$totalOnlyTkPrice,
  176. CgPurchaseNextDayClass::sumMoneyOnlyAmountByMainAndTime($mainId, $currentStartTime, $currentEndTime),
  177. 2
  178. );
  179. // 有入库单时补齐本主账号下全部供货商行(与旧逻辑一致)
  180. if (!empty($orderList)) {
  181. foreach ($ghsList as $key => $ghs) {
  182. $ghsId = $ghs['id'] ?? 0;
  183. if (isset($list[$ghsId]) == false) {
  184. $list[$ghsId] = [
  185. 'id' => $ghsId,
  186. 'name' => $ghs['name'],
  187. 'mobile' => $ghs['mobile'],
  188. 'actPrice' => 0,
  189. 'num' => 0,
  190. 'carton' => 0,
  191. 'pack' => 0,
  192. 'freight' => 0,
  193. 'tkPrice' => 0,
  194. 'orderPrice' => 0,
  195. ];
  196. }
  197. }
  198. }
  199. if (!empty($list)) {
  200. // 去掉订单数与金额都为 0 的行,避免全零供货商淹没隔天负金额行
  201. $list = array_values(array_filter($list, function ($row) {
  202. $num = bcadd((string)($row['num'] ?? '0'), '0', 2);
  203. $act = bcadd((string)($row['actPrice'] ?? '0'), '0', 2);
  204. return bccomp($num, '0', 2) != 0 || bccomp($act, '0', 2) != 0;
  205. }));
  206. }
  207. if ($export == 1) {
  208. if (empty($list)) {
  209. util::fail('没有数据需要导出');
  210. }
  211. ini_set('memory_limit', '2045M');
  212. set_time_limit(0);
  213. StatCgGhsClass::exportCgGhsYj($list, $mainId);
  214. util::stop();
  215. }
  216. util::success([
  217. 'list' => $list,
  218. 'totalActPrice' => $totalActPrice,
  219. 'totalOrderPrice' => $totalOrderPrice,
  220. 'totalCarton' => $totalCarton,
  221. 'totalNum' => $totalNum,
  222. 'totalPack' => $totalPack,
  223. 'totalFreight' => $totalFreight,
  224. 'totalTkPrice' => $totalTkPrice,
  225. //增加项
  226. 'totalOnlyTkPrice' => $totalOnlyTkPrice,
  227. 'nextDayDeduct' => $nextDayDeduct,
  228. ]);
  229. //[['amount','ghsId','ghsName','num','py']]
  230. }
  231. public function actionProfileOld()
  232. {
  233. $get = Yii::$app->request->get();
  234. $where = [];
  235. $where['mainId'] = $this->mainId;
  236. $searchTime = $get['searchTime'] ?? 'today';
  237. if (!empty($searchTime)) {
  238. $startTime = $get['startTime'] ?? '';
  239. $endTime = $get['endTime'] ?? '';
  240. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  241. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  242. }
  243. $list = StatCgGhsClass::getAllByCondition($where, null, '*');
  244. $ghsIds = [];
  245. $arr = [];
  246. if (!empty($list)) {
  247. foreach ($list as $ke => $item) {
  248. $ghsId = $item['ghsId'] ?? 0;
  249. $num = $item['num'] ?? 0;
  250. $refundNum = $item['refundNum'] ?? 0;
  251. $amount = $item['amount'] ?? 0;
  252. $refundAmount = $item['refundAmount'] ?? 0;
  253. $ghsIds[$ghsId] = 1;
  254. if (isset($arr[$ghsId]['num'])) {
  255. $arr[$ghsId]['num'] = bcadd($arr[$ghsId]['num'], $num, 2);
  256. } else {
  257. $arr[$ghsId]['num'] = $num;
  258. }
  259. if (isset($arr[$ghsId]['refundNum'])) {
  260. $arr[$ghsId]['refundNum'] = bcadd($arr[$ghsId]['refundNum'], $refundNum, 2);
  261. } else {
  262. $arr[$ghsId]['refundNum'] = $refundNum;
  263. }
  264. if (isset($arr[$ghsId]['amount'])) {
  265. $arr[$ghsId]['amount'] = bcadd($arr[$ghsId]['amount'], $amount, 2);
  266. } else {
  267. $arr[$ghsId]['amount'] = $amount;
  268. }
  269. if (isset($arr[$ghsId]['refundAmount'])) {
  270. $arr[$ghsId]['refundAmount'] = bcadd($arr[$ghsId]['refundAmount'], $refundAmount, 2);
  271. } else {
  272. $arr[$ghsId]['refundAmount'] = $refundAmount;
  273. }
  274. }
  275. }
  276. $ghsIds = array_keys($ghsIds);
  277. $ghsData = GhsClass::getByIds($ghsIds);
  278. $cg = [];
  279. foreach ($ghsData as $ghs) {
  280. $ghsId = $ghs['id'] ?? 0;
  281. $ghsName = $ghs['name'] ?? '';
  282. $ghsPy = $ghs['py'] ?? '';
  283. $num = $arr[$ghsId]['num'] ?? 0;
  284. $refundNum = $arr[$ghsId]['refundNum'] ?? 0;
  285. $amount = $arr[$ghsId]['amount'] ?? 0;
  286. $refundAmount = $arr[$ghsId]['refundAmount'] ?? 0;
  287. $lastAmount = bcsub($amount, $refundAmount, 2);
  288. $lastNum = bcsub($num, $refundNum, 2);
  289. $cg[] = [
  290. 'ghsId' => $ghsId,
  291. 'ghsName' => $ghsName,
  292. 'py' => $ghsPy,
  293. 'num' => $lastNum,
  294. 'amount' => $lastAmount,
  295. ];
  296. }
  297. $cg = arrayUtil::arraySort($cg, 'num');
  298. util::success(['list' => $cg]);
  299. }
  300. }