Procuremenexport.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 外发采购 — 月度报表导出列表
  7. *
  8. * @icon fa fa-file-excel-o
  9. */
  10. class Procuremenexport extends Backend
  11. {
  12. protected $searchFields = 'CCYDH,CYJMC,CGYMC';
  13. public function index()
  14. {
  15. $this->relationSearch = false;
  16. $this->request->filter(['strip_tags', 'trim']);
  17. if ($this->request->isAjax()) {
  18. $ym = trim((string)$this->request->request('ym', date('Y-m')));
  19. if (!preg_match('/^\d{4}-\d{2}$/', $ym)) {
  20. $ym = date('Y-m');
  21. }
  22. try {
  23. $rows = $this->buildExportableOrderRows($ym);
  24. return json(['total' => count($rows), 'rows' => $rows, 'ym' => $ym]);
  25. } catch (\Throwable $e) {
  26. return json(['total' => 0, 'rows' => [], 'ym' => $ym, 'msg' => $e->getMessage()]);
  27. }
  28. }
  29. return $this->view->fetch();
  30. }
  31. /**
  32. * 已完结、已选定供应商、可按月份导出的订单(合并为一单一行)
  33. */
  34. public function exportable()
  35. {
  36. if (!$this->request->isAjax()) {
  37. $this->error(__('Invalid parameters'));
  38. }
  39. $ym = trim((string)$this->request->get('ym', date('Y-m')));
  40. if (!preg_match('/^\d{4}-\d{2}$/', $ym)) {
  41. $ym = date('Y-m');
  42. }
  43. try {
  44. $rows = $this->buildExportableOrderRows($ym);
  45. return json(['total' => count($rows), 'rows' => $rows, 'ym' => $ym]);
  46. } catch (\Throwable $e) {
  47. return json(['total' => 0, 'rows' => [], 'ym' => $ym, 'msg' => $e->getMessage()]);
  48. }
  49. }
  50. /**
  51. * @return array<int, array<string, mixed>>
  52. */
  53. protected function buildExportableOrderRows(string $ym): array
  54. {
  55. $pickedSet = $this->loadPickedScydgyIdSet();
  56. if ($pickedSet === []) {
  57. return [];
  58. }
  59. try {
  60. $query = Db::table('purchase_order');
  61. $query->whereRaw("(TRIM(CAST(`status` AS CHAR)) = '1' OR CAST(`status` AS UNSIGNED) = 1)");
  62. $query->where('scydgy_id', 'in', array_keys($pickedSet));
  63. $dbRows = $query
  64. ->field('id,scydgy_id,CCYDH,CYJMC,CGYMC,pick_time,createtime,dStamp')
  65. ->order('id', 'desc')
  66. ->select();
  67. } catch (\Throwable $e) {
  68. $dbRows = [];
  69. }
  70. if (!is_array($dbRows)) {
  71. $dbRows = [];
  72. }
  73. $sidList = [];
  74. foreach ($dbRows as $r) {
  75. if (!is_array($r)) {
  76. continue;
  77. }
  78. $sid = (int)($r['scydgy_id'] ?? 0);
  79. if ($sid > 0) {
  80. $sidList[$sid] = true;
  81. }
  82. }
  83. $amountMap = $this->loadPickedAmountSumByScydgyIds(array_keys($sidList));
  84. $pool = [];
  85. foreach ($dbRows as $r) {
  86. if (!is_array($r)) {
  87. continue;
  88. }
  89. $rowYm = $this->resolvePurchaseOrderRowYm($r);
  90. if ($rowYm !== $ym) {
  91. continue;
  92. }
  93. $sid = (int)($r['scydgy_id'] ?? 0);
  94. $doneText = $this->formatPurchaseOrderDoneTime($r);
  95. $pool[] = [
  96. 'id' => (int)($r['id'] ?? 0),
  97. 'scydgy_id' => $sid,
  98. 'CCYDH' => trim((string)($r['CCYDH'] ?? '')),
  99. 'CYJMC' => trim((string)($r['CYJMC'] ?? '')),
  100. 'CGYMC' => trim((string)($r['CGYMC'] ?? '')),
  101. 'ym' => $rowYm,
  102. 'createtime_text' => $doneText,
  103. 'detail_amount' => (float)($amountMap[$sid] ?? 0),
  104. ];
  105. }
  106. $merged = $this->collapseExportRowsByOrder($pool);
  107. foreach ($merged as &$row) {
  108. $amt = 0.0;
  109. $cnt = 0;
  110. if (!empty($row['_merge_rows']) && is_array($row['_merge_rows'])) {
  111. foreach ($row['_merge_rows'] as $mr) {
  112. if (!is_array($mr)) {
  113. continue;
  114. }
  115. $amt += (float)($mr['detail_amount'] ?? 0);
  116. $cnt++;
  117. if (trim((string)($row['ym'] ?? '')) === '') {
  118. $ymv = trim((string)($mr['ym'] ?? ''));
  119. if ($ymv !== '') {
  120. $row['ym'] = $ymv;
  121. }
  122. }
  123. }
  124. } else {
  125. $amt = (float)($row['detail_amount'] ?? 0);
  126. $cnt = 1;
  127. }
  128. $row['row_count'] = $cnt;
  129. $row['total_amount'] = round($amt, 2);
  130. $row['total_amount_text'] = number_format($amt, 2, '.', ',');
  131. unset($row['_merge_rows'], $row['detail_amount']);
  132. }
  133. unset($row);
  134. return $merged;
  135. }
  136. /**
  137. * @return array<int, true>
  138. */
  139. protected function loadPickedScydgyIdSet(): array
  140. {
  141. $set = [];
  142. try {
  143. $rows = Db::table('purchase_order_detail')->where('status', 1)->field('scydgy_id')->select();
  144. } catch (\Throwable $e) {
  145. $rows = [];
  146. }
  147. if (!is_array($rows)) {
  148. return $set;
  149. }
  150. foreach ($rows as $r) {
  151. if (!is_array($r)) {
  152. continue;
  153. }
  154. $sid = (int)($r['scydgy_id'] ?? 0);
  155. if ($sid > 0) {
  156. $set[$sid] = true;
  157. }
  158. }
  159. return $set;
  160. }
  161. /**
  162. * @param int[] $scydgyIds
  163. * @return array<int, float>
  164. */
  165. protected function loadPickedAmountSumByScydgyIds(array $scydgyIds): array
  166. {
  167. $out = [];
  168. $scydgyIds = array_values(array_unique(array_filter(array_map('intval', $scydgyIds))));
  169. if ($scydgyIds === []) {
  170. return $out;
  171. }
  172. try {
  173. $rows = Db::table('purchase_order_detail')
  174. ->where('scydgy_id', 'in', $scydgyIds)
  175. ->where('status', 1)
  176. ->field('scydgy_id,amount')
  177. ->select();
  178. } catch (\Throwable $e) {
  179. $rows = [];
  180. }
  181. if (!is_array($rows)) {
  182. return $out;
  183. }
  184. foreach ($rows as $r) {
  185. if (!is_array($r)) {
  186. continue;
  187. }
  188. $sid = (int)($r['scydgy_id'] ?? 0);
  189. $am = trim((string)($r['amount'] ?? ''));
  190. if ($sid <= 0 || $am === '' || $am === '0' || $am === '0.00') {
  191. continue;
  192. }
  193. if (!isset($out[$sid])) {
  194. $out[$sid] = 0.0;
  195. }
  196. $out[$sid] += (float)$am;
  197. }
  198. return $out;
  199. }
  200. /**
  201. * @param array<string, mixed> $row
  202. */
  203. protected function resolvePurchaseOrderRowYm(array $row): string
  204. {
  205. foreach (['pick_time', 'createtime', 'dStamp'] as $key) {
  206. $raw = $row[$key] ?? null;
  207. if ($raw === null || $raw === '') {
  208. continue;
  209. }
  210. if (is_numeric($raw) && (int)$raw > 946684800) {
  211. return date('Y-m', (int)$raw);
  212. }
  213. $t = trim((string)$raw);
  214. if ($t !== '' && preg_match('/^(\d{4}-\d{2})/', $t, $m)) {
  215. return $m[1];
  216. }
  217. }
  218. return '';
  219. }
  220. /**
  221. * @param array<string, mixed> $row
  222. */
  223. protected function formatPurchaseOrderDoneTime(array $row): string
  224. {
  225. foreach (['createtime', 'pick_time', 'dStamp'] as $key) {
  226. $raw = $row[$key] ?? null;
  227. if ($raw === null || $raw === '') {
  228. continue;
  229. }
  230. if (is_numeric($raw) && (int)$raw > 946684800) {
  231. return date('Y-m-d H:i:s', (int)$raw);
  232. }
  233. $t = trim((string)$raw);
  234. if ($t !== '' && stripos($t, '0000-00-00') !== 0) {
  235. return $t;
  236. }
  237. }
  238. return '';
  239. }
  240. /**
  241. * @param array<int, array<string, mixed>> $rows
  242. * @return array<int, array<string, mixed>>
  243. */
  244. protected function collapseExportRowsByOrder(array $rows): array
  245. {
  246. if ($rows === []) {
  247. return [];
  248. }
  249. $groups = [];
  250. foreach ($rows as $row) {
  251. if (!is_array($row)) {
  252. continue;
  253. }
  254. $dh = trim((string)($row['CCYDH'] ?? ''));
  255. $key = $dh !== '' ? $dh : ('_id_' . (int)($row['id'] ?? 0));
  256. if (!isset($groups[$key])) {
  257. $groups[$key] = [];
  258. }
  259. $groups[$key][] = $row;
  260. }
  261. $out = [];
  262. foreach ($groups as $groupRows) {
  263. if ($groupRows === []) {
  264. continue;
  265. }
  266. usort($groupRows, function ($a, $b) {
  267. return ((int)($a['id'] ?? 0)) <=> ((int)($b['id'] ?? 0));
  268. });
  269. $head = $groupRows[0];
  270. $gymcList = [];
  271. foreach ($groupRows as $r) {
  272. $g = trim((string)($r['CGYMC'] ?? ''));
  273. if ($g !== '' && !in_array($g, $gymcList, true)) {
  274. $gymcList[] = $g;
  275. }
  276. }
  277. $merged = $head;
  278. $merged['_merge_rows'] = $groupRows;
  279. if (count($groupRows) > 1) {
  280. $merged['CGYMC'] = implode('、', $gymcList);
  281. }
  282. $latestText = '';
  283. $latestTs = 0;
  284. foreach ($groupRows as $r) {
  285. $t = trim((string)($r['createtime_text'] ?? ''));
  286. $ts = $t !== '' ? (int)strtotime($t) : 0;
  287. if ($ts > $latestTs) {
  288. $latestTs = $ts;
  289. $latestText = $t;
  290. }
  291. }
  292. if ($latestText !== '') {
  293. $merged['createtime_text'] = $latestText;
  294. }
  295. $maxId = (int)($head['id'] ?? 0);
  296. foreach ($groupRows as $r) {
  297. $maxId = max($maxId, (int)($r['id'] ?? 0));
  298. }
  299. $merged['id'] = $maxId;
  300. $out[] = $merged;
  301. }
  302. usort($out, function ($a, $b) {
  303. return ((int)($b['id'] ?? 0)) <=> ((int)($a['id'] ?? 0));
  304. });
  305. return $out;
  306. }
  307. }