StatCgGhsController.php 13 KB

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