StatCgGhsController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\stat\classes\StatCgGhsClass;
  4. use bizGhs\cg\classes\CgRefundClass;
  5. use bizGhs\ghs\classes\GhsClass;
  6. use bizGhs\order\classes\PurchaseOrderClass;
  7. use bizGhs\order\classes\PurchaseOrderItemClass;
  8. use common\components\arrayUtil;
  9. use common\components\dateUtil;
  10. use Yii;
  11. use common\components\util;
  12. class StatCgGhsController extends BaseController
  13. {
  14. //采购列表 ssh 20211017
  15. public function actionProfile()
  16. {
  17. $get = Yii::$app->request->get();
  18. $mainId = $this->mainId;
  19. $export = $get['export'] ?? 0;
  20. $debt = isset($get['debt']) && is_numeric($get['debt']) ? $get['debt'] : 0;
  21. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  22. $startTime = $get['startTime'] ?? '';
  23. $endTime = $get['endTime'] ?? '';
  24. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  25. $start = $period['startTime'];
  26. $end = $period['endTime'];
  27. $ghsList = GhsClass::getAllByCondition(['ownMainId' => $mainId], null, '*', 'id');
  28. $currentStartDate = date('Y-m-d', strtotime($start));
  29. $currentEndDate = date('Y-m-d', strtotime($end));
  30. $currentStartTime = $currentStartDate . ' 00:00:00';
  31. $currentEndTime = $currentEndDate . ' 23:59:59';
  32. $where = ['mainId' => $mainId];
  33. if (!empty($debt)) {
  34. $where['debt'] = $debt;
  35. }
  36. $where['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  37. $orderList = PurchaseOrderClass::getAllByCondition($where, null, '*');
  38. $list = [];
  39. $totalActPrice = 0;
  40. $totalOrderPrice = 0;
  41. $totalCarton = 0;
  42. $totalPack = 0;
  43. $totalFreight = 0;
  44. $totalTkPrice = 0;
  45. //增加项
  46. $totalOnlyTkPrice = 0;
  47. $totalNum = 0;
  48. if (!empty($orderList)) {
  49. foreach ($orderList as $orderInfo) {
  50. $actPrice = $orderInfo['actPrice'] ?? 0;
  51. $orderPrice = $orderInfo['orderPrice'] ?? 0;
  52. $ghsId = $orderInfo['ghsId'] ?? 0;
  53. $status = $orderInfo['status'] ?? 0;
  54. $orderSn = $orderInfo['orderSn'] ?? '';
  55. $tkPrice = $orderInfo['tkPrice'] ?? 0;
  56. if ($status != 4) {
  57. continue;
  58. }
  59. if ($actPrice <= 0) {
  60. continue;
  61. }
  62. $carton = 0;
  63. $pack = 0;
  64. $freight = 0;
  65. $orderItemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  66. if (!empty($orderItemList)) {
  67. foreach ($orderItemList as $orderItem) {
  68. $num = $orderItem['xhNum'] ?? 0;
  69. $unitPrice = $orderItem['xhUnitPrice'] ?? 0;
  70. $refundNum = $orderItem['refundNum'] ?? 0;
  71. $classId = $orderItem['classId'] ?? 0;
  72. if (getenv('YII_ENV') == 'production') {
  73. if ($mainId == 23390) {
  74. $remainNum = bcsub($num, $refundNum);
  75. $amount = bcmul($remainNum, $unitPrice, 2);
  76. if ($classId == 73120) {
  77. $freight = bcadd($freight, $amount, 2);
  78. }
  79. }
  80. }
  81. }
  82. }
  83. //增加项
  84. if ($tkPrice > 0) {
  85. $refundList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  86. if (!empty($refundList)) {
  87. foreach ($refundList as $refundInfo) {
  88. if (isset($refundInfo->refundType) && $refundInfo->refundType == 2) {
  89. $refundPrice = $refundInfo->refundPrice ?? 0;
  90. $totalOnlyTkPrice = bcadd($totalOnlyTkPrice, $refundPrice, 2);
  91. }
  92. }
  93. }
  94. }
  95. $totalCarton = bcadd($totalCarton, $carton, 2);
  96. $totalPack = bcadd($totalPack, $pack, 2);
  97. $totalFreight = bcadd($totalFreight, $freight, 2);
  98. $totalTkPrice = bcadd($totalTkPrice, $tkPrice, 2);
  99. $totalActPrice = bcadd($totalActPrice, $actPrice, 2);
  100. $totalOrderPrice = bcadd($totalOrderPrice, $orderPrice, 2);
  101. $totalNum++;
  102. if (isset($list[$ghsId])) {
  103. $list[$ghsId]['actPrice'] = bcadd($list[$ghsId]['actPrice'], $actPrice, 2);
  104. $list[$ghsId]['orderPrice'] = bcadd($list[$ghsId]['orderPrice'], $orderPrice, 2);
  105. $list[$ghsId]['num'] = bcadd($list[$ghsId]['num'], 1);
  106. $list[$ghsId]['carton'] = bcadd($list[$ghsId]['carton'], $carton, 2);
  107. $list[$ghsId]['pack'] = bcadd($list[$ghsId]['pack'], $pack, 2);
  108. $list[$ghsId]['freight'] = bcadd($list[$ghsId]['freight'], $freight, 2);
  109. $list[$ghsId]['tkPrice'] = bcadd($list[$ghsId]['tkPrice'], $tkPrice, 2);
  110. } else {
  111. $name = $ghsList[$ghsId] && $ghsList[$ghsId]['name'] ? $ghsList[$ghsId]['name'] : '未命名';
  112. $mobile = $ghsList[$ghsId] && $ghsList[$ghsId]['mobile'] ? $ghsList[$ghsId]['mobile'] : '0';
  113. $list[$ghsId] = [
  114. 'id' => $ghsId,
  115. 'name' => $name,
  116. 'mobile' => $mobile,
  117. 'orderPrice' => $orderPrice,
  118. 'actPrice' => $actPrice,
  119. 'num' => 1,
  120. 'carton' => $carton,
  121. 'pack' => $pack,
  122. 'freight' => $freight,
  123. 'tkPrice' => $tkPrice,
  124. ];
  125. }
  126. }
  127. foreach ($ghsList as $key => $ghs) {
  128. $ghsId = $ghs['id'] ?? 0;
  129. if (isset($list[$ghsId]) == false) {
  130. $list[$ghsId] = [
  131. 'id' => $ghsId,
  132. 'name' => $ghs['name'],
  133. 'mobile' => $ghs['mobile'],
  134. 'actPrice' => 0,
  135. 'num' => 0,
  136. 'carton' => 0,
  137. 'pack' => 0,
  138. 'freight' => 0,
  139. 'tkPrice' => 0,
  140. 'orderPrice' => 0,
  141. ];
  142. }
  143. }
  144. $list = array_values($list);
  145. }
  146. if ($export == 1) {
  147. if (empty($list)) {
  148. util::fail('没有数据需要导出');
  149. }
  150. ini_set('memory_limit', '2045M');
  151. set_time_limit(0);
  152. StatCgGhsClass::exportCgGhsYj($list, $mainId);
  153. util::stop();
  154. }
  155. util::success([
  156. 'list' => $list,
  157. 'totalActPrice' => $totalActPrice,
  158. 'totalOrderPrice' => $totalOrderPrice,
  159. 'totalCarton' => $totalCarton,
  160. 'totalNum' => $totalNum,
  161. 'totalPack' => $totalPack,
  162. 'totalFreight' => $totalFreight,
  163. 'totalTkPrice' => $totalTkPrice,
  164. //增加项
  165. 'totalOnlyTkPrice' => $totalOnlyTkPrice,
  166. ]);
  167. //[['amount','ghsId','ghsName','num','py']]
  168. }
  169. public function actionProfileOld()
  170. {
  171. $get = Yii::$app->request->get();
  172. $where = [];
  173. $where['mainId'] = $this->mainId;
  174. $searchTime = $get['searchTime'] ?? 'today';
  175. if (!empty($searchTime)) {
  176. $startTime = $get['startTime'] ?? '';
  177. $endTime = $get['endTime'] ?? '';
  178. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  179. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  180. }
  181. $list = StatCgGhsClass::getAllByCondition($where, null, '*');
  182. $ghsIds = [];
  183. $arr = [];
  184. if (!empty($list)) {
  185. foreach ($list as $ke => $item) {
  186. $ghsId = $item['ghsId'] ?? 0;
  187. $num = $item['num'] ?? 0;
  188. $refundNum = $item['refundNum'] ?? 0;
  189. $amount = $item['amount'] ?? 0;
  190. $refundAmount = $item['refundAmount'] ?? 0;
  191. $ghsIds[$ghsId] = 1;
  192. if (isset($arr[$ghsId]['num'])) {
  193. $arr[$ghsId]['num'] = bcadd($arr[$ghsId]['num'], $num, 2);
  194. } else {
  195. $arr[$ghsId]['num'] = $num;
  196. }
  197. if (isset($arr[$ghsId]['refundNum'])) {
  198. $arr[$ghsId]['refundNum'] = bcadd($arr[$ghsId]['refundNum'], $refundNum, 2);
  199. } else {
  200. $arr[$ghsId]['refundNum'] = $refundNum;
  201. }
  202. if (isset($arr[$ghsId]['amount'])) {
  203. $arr[$ghsId]['amount'] = bcadd($arr[$ghsId]['amount'], $amount, 2);
  204. } else {
  205. $arr[$ghsId]['amount'] = $amount;
  206. }
  207. if (isset($arr[$ghsId]['refundAmount'])) {
  208. $arr[$ghsId]['refundAmount'] = bcadd($arr[$ghsId]['refundAmount'], $refundAmount, 2);
  209. } else {
  210. $arr[$ghsId]['refundAmount'] = $refundAmount;
  211. }
  212. }
  213. }
  214. $ghsIds = array_keys($ghsIds);
  215. $ghsData = GhsClass::getByIds($ghsIds);
  216. $cg = [];
  217. foreach ($ghsData as $ghs) {
  218. $ghsId = $ghs['id'] ?? 0;
  219. $ghsName = $ghs['name'] ?? '';
  220. $ghsPy = $ghs['py'] ?? '';
  221. $num = $arr[$ghsId]['num'] ?? 0;
  222. $refundNum = $arr[$ghsId]['refundNum'] ?? 0;
  223. $amount = $arr[$ghsId]['amount'] ?? 0;
  224. $refundAmount = $arr[$ghsId]['refundAmount'] ?? 0;
  225. $lastAmount = bcsub($amount, $refundAmount, 2);
  226. $lastNum = bcsub($num, $refundNum, 2);
  227. $cg[] = [
  228. 'ghsId' => $ghsId,
  229. 'ghsName' => $ghsName,
  230. 'py' => $ghsPy,
  231. 'num' => $lastNum,
  232. 'amount' => $lastAmount,
  233. ];
  234. }
  235. $cg = arrayUtil::arraySort($cg, 'num');
  236. util::success(['list' => $cg]);
  237. }
  238. }