StatCgGhsController.php 9.4 KB

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