StatCgGhsController.php 9.5 KB

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