StatCgGhsController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. $carton = 0;
  60. $pack = 0;
  61. $freight = 0;
  62. $orderItemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  63. if (!empty($orderItemList)) {
  64. foreach ($orderItemList as $orderItem) {
  65. $num = $orderItem['xhNum'] ?? 0;
  66. $unitPrice = $orderItem['xhUnitPrice'] ?? 0;
  67. $refundNum = $orderItem['refundNum'] ?? 0;
  68. $classId = $orderItem['classId'] ?? 0;
  69. if (getenv('YII_ENV') == 'production') {
  70. if ($mainId == 23390) {
  71. $remainNum = bcsub($num, $refundNum);
  72. $amount = bcmul($remainNum, $unitPrice, 2);
  73. if ($classId == 73120) {
  74. $freight = bcadd($freight, $amount, 2);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. //增加项
  81. if ($tkPrice > 0) {
  82. $refundList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  83. if (!empty($refundList)) {
  84. foreach ($refundList as $refundInfo) {
  85. if (isset($refundInfo->refundType) && $refundInfo->refundType == 2) {
  86. $refundPrice = $refundInfo->refundPrice ?? 0;
  87. $totalOnlyTkPrice = bcadd($totalOnlyTkPrice, $refundPrice, 2);
  88. }
  89. }
  90. }
  91. }
  92. $totalCarton = bcadd($totalCarton, $carton, 2);
  93. $totalPack = bcadd($totalPack, $pack, 2);
  94. $totalFreight = bcadd($totalFreight, $freight, 2);
  95. $totalTkPrice = bcadd($totalTkPrice, $tkPrice, 2);
  96. $totalActPrice = bcadd($totalActPrice, $actPrice, 2);
  97. $totalOrderPrice = bcadd($totalOrderPrice, $orderPrice, 2);
  98. $totalNum++;
  99. if (isset($list[$ghsId])) {
  100. $list[$ghsId]['actPrice'] = bcadd($list[$ghsId]['actPrice'], $actPrice, 2);
  101. $list[$ghsId]['orderPrice'] = bcadd($list[$ghsId]['orderPrice'], $orderPrice, 2);
  102. $list[$ghsId]['num'] = bcadd($list[$ghsId]['num'], 1);
  103. $list[$ghsId]['carton'] = bcadd($list[$ghsId]['carton'], $carton, 2);
  104. $list[$ghsId]['pack'] = bcadd($list[$ghsId]['pack'], $pack, 2);
  105. $list[$ghsId]['freight'] = bcadd($list[$ghsId]['freight'], $freight, 2);
  106. $list[$ghsId]['tkPrice'] = bcadd($list[$ghsId]['tkPrice'], $tkPrice, 2);
  107. } else {
  108. $name = $ghsList[$ghsId] && $ghsList[$ghsId]['name'] ? $ghsList[$ghsId]['name'] : '未命名';
  109. $mobile = $ghsList[$ghsId] && $ghsList[$ghsId]['mobile'] ? $ghsList[$ghsId]['mobile'] : '0';
  110. $list[$ghsId] = [
  111. 'id' => $ghsId,
  112. 'name' => $name,
  113. 'mobile' => $mobile,
  114. 'orderPrice' => $orderPrice,
  115. 'actPrice' => $actPrice,
  116. 'num' => 1,
  117. 'carton' => $carton,
  118. 'pack' => $pack,
  119. 'freight' => $freight,
  120. 'tkPrice' => $tkPrice,
  121. ];
  122. }
  123. }
  124. foreach ($ghsList as $key => $ghs) {
  125. $ghsId = $ghs['id'] ?? 0;
  126. if (isset($list[$ghsId]) == false) {
  127. $list[$ghsId] = [
  128. 'id' => $ghsId,
  129. 'name' => $ghs['name'],
  130. 'mobile' => $ghs['mobile'],
  131. 'actPrice' => 0,
  132. 'num' => 0,
  133. 'carton' => 0,
  134. 'pack' => 0,
  135. 'freight' => 0,
  136. 'tkPrice' => 0,
  137. 'orderPrice' => 0,
  138. ];
  139. }
  140. }
  141. $list = array_values($list);
  142. }
  143. if ($export == 1) {
  144. if (empty($list)) {
  145. util::fail('没有数据需要导出');
  146. }
  147. ini_set('memory_limit', '2045M');
  148. set_time_limit(0);
  149. StatCgGhsClass::exportCgGhsYj($list, $mainId);
  150. util::stop();
  151. }
  152. util::success([
  153. 'list' => $list,
  154. 'totalActPrice' => $totalActPrice,
  155. 'totalOrderPrice' => $totalOrderPrice,
  156. 'totalCarton' => $totalCarton,
  157. 'totalNum' => $totalNum,
  158. 'totalPack' => $totalPack,
  159. 'totalFreight' => $totalFreight,
  160. 'totalTkPrice' => $totalTkPrice,
  161. //增加项
  162. 'totalOnlyTkPrice' => $totalOnlyTkPrice,
  163. ]);
  164. //[['amount','ghsId','ghsName','num','py']]
  165. }
  166. public function actionProfileOld()
  167. {
  168. $get = Yii::$app->request->get();
  169. $where = [];
  170. $where['mainId'] = $this->mainId;
  171. $searchTime = $get['searchTime'] ?? 'today';
  172. if (!empty($searchTime)) {
  173. $startTime = $get['startTime'] ?? '';
  174. $endTime = $get['endTime'] ?? '';
  175. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  176. $where['time'] = ['between', [$period['startTime'], $period['endTime']]];
  177. }
  178. $list = StatCgGhsClass::getAllByCondition($where, null, '*');
  179. $ghsIds = [];
  180. $arr = [];
  181. if (!empty($list)) {
  182. foreach ($list as $ke => $item) {
  183. $ghsId = $item['ghsId'] ?? 0;
  184. $num = $item['num'] ?? 0;
  185. $refundNum = $item['refundNum'] ?? 0;
  186. $amount = $item['amount'] ?? 0;
  187. $refundAmount = $item['refundAmount'] ?? 0;
  188. $ghsIds[$ghsId] = 1;
  189. if (isset($arr[$ghsId]['num'])) {
  190. $arr[$ghsId]['num'] = bcadd($arr[$ghsId]['num'], $num, 2);
  191. } else {
  192. $arr[$ghsId]['num'] = $num;
  193. }
  194. if (isset($arr[$ghsId]['refundNum'])) {
  195. $arr[$ghsId]['refundNum'] = bcadd($arr[$ghsId]['refundNum'], $refundNum, 2);
  196. } else {
  197. $arr[$ghsId]['refundNum'] = $refundNum;
  198. }
  199. if (isset($arr[$ghsId]['amount'])) {
  200. $arr[$ghsId]['amount'] = bcadd($arr[$ghsId]['amount'], $amount, 2);
  201. } else {
  202. $arr[$ghsId]['amount'] = $amount;
  203. }
  204. if (isset($arr[$ghsId]['refundAmount'])) {
  205. $arr[$ghsId]['refundAmount'] = bcadd($arr[$ghsId]['refundAmount'], $refundAmount, 2);
  206. } else {
  207. $arr[$ghsId]['refundAmount'] = $refundAmount;
  208. }
  209. }
  210. }
  211. $ghsIds = array_keys($ghsIds);
  212. $ghsData = GhsClass::getByIds($ghsIds);
  213. $cg = [];
  214. foreach ($ghsData as $ghs) {
  215. $ghsId = $ghs['id'] ?? 0;
  216. $ghsName = $ghs['name'] ?? '';
  217. $ghsPy = $ghs['py'] ?? '';
  218. $num = $arr[$ghsId]['num'] ?? 0;
  219. $refundNum = $arr[$ghsId]['refundNum'] ?? 0;
  220. $amount = $arr[$ghsId]['amount'] ?? 0;
  221. $refundAmount = $arr[$ghsId]['refundAmount'] ?? 0;
  222. $lastAmount = bcsub($amount, $refundAmount, 2);
  223. $lastNum = bcsub($num, $refundNum, 2);
  224. $cg[] = [
  225. 'ghsId' => $ghsId,
  226. 'ghsName' => $ghsName,
  227. 'py' => $ghsPy,
  228. 'num' => $lastNum,
  229. 'amount' => $lastAmount,
  230. ];
  231. }
  232. $cg = arrayUtil::arraySort($cg, 'num');
  233. util::success(['list' => $cg]);
  234. }
  235. }