Procuremenexport.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. * 已完结订单(与历史存证一致),按完结月份筛选后合并为一单一行
  52. *
  53. * @return array<int, array<string, mixed>>
  54. */
  55. protected function buildExportableOrderRows(string $ym): array
  56. {
  57. try {
  58. $query = Db::table('purchase_order');
  59. $query->whereRaw("(TRIM(CAST(`status` AS CHAR)) = '1' OR CAST(`status` AS UNSIGNED) = 1)");
  60. $dbRows = $query
  61. ->field('id,scydgy_id,CCYDH,CYJMC,CGYMC,pick_time,createtime,dStamp,updatetime')
  62. ->order('id', 'desc')
  63. ->select();
  64. } catch (\Throwable $e) {
  65. $dbRows = [];
  66. }
  67. if (!is_array($dbRows)) {
  68. $dbRows = [];
  69. }
  70. $sidList = [];
  71. foreach ($dbRows as $r) {
  72. if (!is_array($r)) {
  73. continue;
  74. }
  75. $sid = (int)($r['scydgy_id'] ?? 0);
  76. if ($sid > 0) {
  77. $sidList[$sid] = true;
  78. }
  79. }
  80. $amountMap = $this->loadDetailAmountSumByScydgyIds(array_keys($sidList));
  81. $pool = [];
  82. foreach ($dbRows as $r) {
  83. if (!is_array($r)) {
  84. continue;
  85. }
  86. $rowYm = $this->resolvePurchaseOrderRowYm($r);
  87. if ($rowYm !== $ym) {
  88. continue;
  89. }
  90. $sid = (int)($r['scydgy_id'] ?? 0);
  91. $doneText = $this->formatPurchaseOrderDoneTime($r);
  92. $pool[] = [
  93. 'id' => (int)($r['id'] ?? 0),
  94. 'scydgy_id' => $sid,
  95. 'CCYDH' => trim((string)($r['CCYDH'] ?? '')),
  96. 'CYJMC' => trim((string)($r['CYJMC'] ?? '')),
  97. 'CGYMC' => trim((string)($r['CGYMC'] ?? '')),
  98. 'ym' => $rowYm,
  99. 'createtime_text' => $doneText,
  100. 'detail_amount' => (float)($amountMap[$sid] ?? 0),
  101. ];
  102. }
  103. $merged = $this->collapseExportRowsByOrder($pool);
  104. foreach ($merged as &$row) {
  105. $amt = 0.0;
  106. $cnt = 0;
  107. if (!empty($row['_merge_rows']) && is_array($row['_merge_rows'])) {
  108. foreach ($row['_merge_rows'] as $mr) {
  109. if (!is_array($mr)) {
  110. continue;
  111. }
  112. $amt += (float)($mr['detail_amount'] ?? 0);
  113. $cnt++;
  114. if (trim((string)($row['ym'] ?? '')) === '') {
  115. $ymv = trim((string)($mr['ym'] ?? ''));
  116. if ($ymv !== '') {
  117. $row['ym'] = $ymv;
  118. }
  119. }
  120. }
  121. } else {
  122. $amt = (float)($row['detail_amount'] ?? 0);
  123. $cnt = 1;
  124. }
  125. $row['row_count'] = $cnt;
  126. $row['total_amount'] = round($amt, 2);
  127. $row['total_amount_text'] = number_format($amt, 2, '.', ',');
  128. unset($row['_merge_rows'], $row['detail_amount']);
  129. }
  130. unset($row);
  131. return $merged;
  132. }
  133. /**
  134. * 工序行金额合计:优先已选定明细 status=1,否则汇总有效明细上的加工金额
  135. *
  136. * @param int[] $scydgyIds
  137. * @return array<int, float>
  138. */
  139. protected function loadDetailAmountSumByScydgyIds(array $scydgyIds): array
  140. {
  141. $picked = $this->loadPickedAmountSumByScydgyIds($scydgyIds);
  142. $scydgyIds = array_values(array_unique(array_filter(array_map('intval', $scydgyIds))));
  143. $missing = [];
  144. foreach ($scydgyIds as $sid) {
  145. if ($sid > 0 && !isset($picked[$sid])) {
  146. $missing[] = $sid;
  147. }
  148. }
  149. if ($missing === []) {
  150. return $picked;
  151. }
  152. try {
  153. $rows = Db::table('purchase_order_detail')
  154. ->where('scydgy_id', 'in', $missing)
  155. ->where('status', '<>', 2)
  156. ->field('scydgy_id,amount')
  157. ->select();
  158. } catch (\Throwable $e) {
  159. $rows = [];
  160. }
  161. if (!is_array($rows)) {
  162. return $picked;
  163. }
  164. foreach ($rows as $r) {
  165. if (!is_array($r)) {
  166. continue;
  167. }
  168. $sid = (int)($r['scydgy_id'] ?? 0);
  169. $am = trim((string)($r['amount'] ?? ''));
  170. if ($sid <= 0 || $am === '' || $am === '0' || $am === '0.00') {
  171. continue;
  172. }
  173. if (!isset($picked[$sid])) {
  174. $picked[$sid] = 0.0;
  175. }
  176. $picked[$sid] += (float)$am;
  177. }
  178. return $picked;
  179. }
  180. /**
  181. * @param int[] $scydgyIds
  182. * @return array<int, float>
  183. */
  184. protected function loadPickedAmountSumByScydgyIds(array $scydgyIds): array
  185. {
  186. $out = [];
  187. $scydgyIds = array_values(array_unique(array_filter(array_map('intval', $scydgyIds))));
  188. if ($scydgyIds === []) {
  189. return $out;
  190. }
  191. try {
  192. $rows = Db::table('purchase_order_detail')
  193. ->where('scydgy_id', 'in', $scydgyIds)
  194. ->where('status', 1)
  195. ->field('scydgy_id,amount')
  196. ->select();
  197. } catch (\Throwable $e) {
  198. $rows = [];
  199. }
  200. if (!is_array($rows)) {
  201. return $out;
  202. }
  203. foreach ($rows as $r) {
  204. if (!is_array($r)) {
  205. continue;
  206. }
  207. $sid = (int)($r['scydgy_id'] ?? 0);
  208. $am = trim((string)($r['amount'] ?? ''));
  209. if ($sid <= 0 || $am === '' || $am === '0' || $am === '0.00') {
  210. continue;
  211. }
  212. if (!isset($out[$sid])) {
  213. $out[$sid] = 0.0;
  214. }
  215. $out[$sid] += (float)$am;
  216. }
  217. return $out;
  218. }
  219. /**
  220. * 归属月份:与历史存证「完结时间」一致(createtime / dStamp),不用下发时间 pick_time
  221. *
  222. * @param array<string, mixed> $row
  223. */
  224. protected function resolvePurchaseOrderRowYm(array $row): string
  225. {
  226. foreach (['createtime', 'dStamp', 'updatetime'] as $key) {
  227. $raw = $row[$key] ?? null;
  228. if ($raw === null || $raw === '') {
  229. continue;
  230. }
  231. if (is_numeric($raw) && (int)$raw > 946684800) {
  232. return date('Y-m', (int)$raw);
  233. }
  234. $t = trim((string)$raw);
  235. if ($t !== '' && preg_match('/^(\d{4}-\d{2})/', $t, $m)) {
  236. return $m[1];
  237. }
  238. }
  239. return '';
  240. }
  241. /**
  242. * @param array<string, mixed> $row
  243. */
  244. protected function formatPurchaseOrderDoneTime(array $row): string
  245. {
  246. foreach (['createtime', 'dStamp', 'updatetime'] as $key) {
  247. $raw = $row[$key] ?? null;
  248. if ($raw === null || $raw === '') {
  249. continue;
  250. }
  251. if (is_numeric($raw) && (int)$raw > 946684800) {
  252. return date('Y-m-d H:i:s', (int)$raw);
  253. }
  254. $t = trim((string)$raw);
  255. if ($t !== '' && stripos($t, '0000-00-00') !== 0) {
  256. return $t;
  257. }
  258. }
  259. return '';
  260. }
  261. /**
  262. * @param array<int, array<string, mixed>> $rows
  263. * @return array<int, array<string, mixed>>
  264. */
  265. protected function collapseExportRowsByOrder(array $rows): array
  266. {
  267. if ($rows === []) {
  268. return [];
  269. }
  270. $groups = [];
  271. foreach ($rows as $row) {
  272. if (!is_array($row)) {
  273. continue;
  274. }
  275. $dh = trim((string)($row['CCYDH'] ?? ''));
  276. $key = $dh !== '' ? $dh : ('_id_' . (int)($row['id'] ?? 0));
  277. if (!isset($groups[$key])) {
  278. $groups[$key] = [];
  279. }
  280. $groups[$key][] = $row;
  281. }
  282. $out = [];
  283. foreach ($groups as $groupRows) {
  284. if ($groupRows === []) {
  285. continue;
  286. }
  287. usort($groupRows, function ($a, $b) {
  288. return ((int)($a['id'] ?? 0)) <=> ((int)($b['id'] ?? 0));
  289. });
  290. $head = $groupRows[0];
  291. $gymcList = [];
  292. foreach ($groupRows as $r) {
  293. $g = trim((string)($r['CGYMC'] ?? ''));
  294. if ($g !== '' && !in_array($g, $gymcList, true)) {
  295. $gymcList[] = $g;
  296. }
  297. }
  298. $merged = $head;
  299. $merged['_merge_rows'] = $groupRows;
  300. if (count($groupRows) > 1) {
  301. $merged['CGYMC'] = implode('、', $gymcList);
  302. }
  303. $latestText = '';
  304. $latestTs = 0;
  305. foreach ($groupRows as $r) {
  306. $t = trim((string)($r['createtime_text'] ?? ''));
  307. $ts = $t !== '' ? (int)strtotime($t) : 0;
  308. if ($ts > $latestTs) {
  309. $latestTs = $ts;
  310. $latestText = $t;
  311. }
  312. }
  313. if ($latestText !== '') {
  314. $merged['createtime_text'] = $latestText;
  315. }
  316. $maxId = (int)($head['id'] ?? 0);
  317. foreach ($groupRows as $r) {
  318. $maxId = max($maxId, (int)($r['id'] ?? 0));
  319. }
  320. $merged['id'] = $maxId;
  321. $out[] = $merged;
  322. }
  323. usort($out, function ($a, $b) {
  324. return ((int)($b['id'] ?? 0)) <=> ((int)($a['id'] ?? 0));
  325. });
  326. return $out;
  327. }
  328. }