StatCgGhsController.php 14 KB

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