Procuremenarchive.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\ProcuremenStatus;
  5. use app\common\library\ProcuremenTime;
  6. use think\Db;
  7. /**
  8. * 协助采购 — 历史存证档案查询(已完结工序 + 操作记录详情)
  9. *
  10. * @icon fa fa-archive
  11. */
  12. class Procuremenarchive extends Backend
  13. {
  14. protected $searchFields = 'CCYDH,CYJMC,CGYMC,pick_company_name';
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. $canArchiveAbandon = $this->canArchiveAbandon();
  19. $canDetails = $this->hasProcuremenSiblingPerm(['details']);
  20. $this->view->assign('canArchiveAbandon', $canArchiveAbandon);
  21. $this->assignconfig('procuremenAuth', [
  22. 'details' => $canDetails,
  23. 'auditabandon' => $canArchiveAbandon,
  24. 'archiveabandon' => $canArchiveAbandon,
  25. ]);
  26. }
  27. /**
  28. * 历史存证「重新下发」权限:与审批列表统一为 auditabandon
  29. */
  30. protected function canArchiveAbandon(): bool
  31. {
  32. if ($this->hasProcuremenSiblingPerm(['auditabandon', 'auditAbandon'])) {
  33. return true;
  34. }
  35. return $this->hasProcuremenSiblingPerm(['archiveabandon', 'archiveAbandon']);
  36. }
  37. /**
  38. * @param string[] $actions
  39. */
  40. protected function hasProcuremenSiblingPerm(array $actions): bool
  41. {
  42. foreach ($actions as $action) {
  43. $action = strtolower(trim((string)$action));
  44. if ($action === '') {
  45. continue;
  46. }
  47. if (strpos($action, 'procuremen/') !== 0) {
  48. $action = 'procuremen/' . $action;
  49. }
  50. if ($this->auth->check($action)) {
  51. return true;
  52. }
  53. }
  54. $suffixes = [];
  55. foreach ($actions as $action) {
  56. $action = strtolower(trim((string)$action));
  57. $action = preg_replace('#^procuremen/#', '', $action);
  58. if ($action !== '') {
  59. $suffixes[$action] = true;
  60. }
  61. }
  62. if ($suffixes === []) {
  63. return false;
  64. }
  65. try {
  66. foreach ($this->auth->getRuleList() as $rule) {
  67. $rule = strtolower((string)$rule);
  68. if (strpos($rule, 'procuremen/') !== 0) {
  69. continue;
  70. }
  71. foreach (array_keys($suffixes) as $suffix) {
  72. if ($rule === 'procuremen/' . $suffix || strpos($rule, 'procuremen/' . $suffix . '/') === 0) {
  73. return true;
  74. }
  75. }
  76. }
  77. } catch (\Throwable $e) {
  78. }
  79. return false;
  80. }
  81. /** purchase_order.status 为已完结(兼容 varchar / 数字 / 首尾空格) */
  82. protected function applyPurchaseOrderCompletedWhere($query): void
  83. {
  84. $query->whereRaw(ProcuremenStatus::sqlPoCompleted('status'));
  85. }
  86. public function index()
  87. {
  88. $this->relationSearch = false;
  89. $this->request->filter(['strip_tags', 'trim']);
  90. if ($this->request->isAjax()) {
  91. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  92. $limit = max(10, min(500, (int)$limit));
  93. $offset = max(0, (int)$offset);
  94. $applyFilters = function ($query) use ($where) {
  95. $this->applyPurchaseOrderCompletedWhere($query);
  96. if (!empty($where)) {
  97. $query->where($where);
  98. }
  99. };
  100. $sortField = preg_match('/^[a-zA-Z0-9_]+$/', (string)$sort) ? $sort : 'id';
  101. $orderDir = strtoupper((string)$order) === 'ASC' ? 'ASC' : 'DESC';
  102. $listQuery = Db::table('purchase_order');
  103. $applyFilters($listQuery);
  104. $rows = $listQuery
  105. ->field('id,scydgy_id,CCYDH,CYJMC,CGYMC,pick_company_name,createtime,dStamp,pick_time')
  106. ->order($sortField, $orderDir)
  107. ->select();
  108. if (!is_array($rows)) {
  109. $rows = [];
  110. }
  111. $sidList = [];
  112. foreach ($rows as $r) {
  113. if (!is_array($r)) {
  114. continue;
  115. }
  116. $sid = (int)($r['scydgy_id'] ?? 0);
  117. if ($sid !== 0) {
  118. $sidList[$sid] = true;
  119. }
  120. }
  121. $completeTsMap = ProcuremenTime::loadOperLogTimestampMap(
  122. array_keys($sidList),
  123. ['purchase_confirm', 'mark_complete', 'inbound_score']
  124. );
  125. $inboundMap = [];
  126. if ($sidList !== []) {
  127. try {
  128. $inRows = Db::table('purchase_order_inbound_score')
  129. ->where('scydgy_id', 'in', array_keys($sidList))
  130. ->field('scydgy_id,result')
  131. ->select();
  132. } catch (\Throwable $e) {
  133. $inRows = [];
  134. }
  135. if (is_array($inRows)) {
  136. foreach ($inRows as $ir) {
  137. if (!is_array($ir)) {
  138. continue;
  139. }
  140. $isid = (int)($ir['scydgy_id'] ?? 0);
  141. if ($isid !== 0) {
  142. $inboundMap[$isid] = trim((string)($ir['result'] ?? ''));
  143. }
  144. }
  145. }
  146. }
  147. $out = [];
  148. foreach ($rows as $r) {
  149. if (!is_array($r)) {
  150. continue;
  151. }
  152. $sid = (int)($r['scydgy_id'] ?? 0);
  153. $done = ProcuremenTime::resolveCompletedDone($r, $completeTsMap);
  154. $out[] = [
  155. 'id' => (int)($r['id'] ?? 0),
  156. 'scydgy_id' => $sid,
  157. 'CCYDH' => trim((string)($r['CCYDH'] ?? '')),
  158. 'CYJMC' => trim((string)($r['CYJMC'] ?? '')),
  159. 'CGYMC' => trim((string)($r['CGYMC'] ?? '')),
  160. 'pick_company_name' => trim((string)($r['pick_company_name'] ?? '')),
  161. 'inbound_result' => $inboundMap[$sid] ?? '',
  162. 'createtime' => $done['ts'],
  163. 'createtime_text' => $done['text'],
  164. ];
  165. }
  166. $merged = $this->collapseArchiveRowsByOrder($out);
  167. $nMerged = count($merged);
  168. if ($nMerged > 1 && $sortField === 'id') {
  169. usort($merged, function ($a, $b) use ($orderDir) {
  170. $cmp = ((int)($a['id'] ?? 0)) <=> ((int)($b['id'] ?? 0));
  171. if ($cmp === 0) {
  172. return strcmp((string)($a['CCYDH'] ?? ''), (string)($b['CCYDH'] ?? ''));
  173. }
  174. return $orderDir === 'ASC' ? $cmp : -$cmp;
  175. });
  176. } elseif ($nMerged > 1 && $sortField === 'createtime') {
  177. usort($merged, function ($a, $b) use ($orderDir) {
  178. $ta = (int)($a['createtime'] ?? 0);
  179. $tb = (int)($b['createtime'] ?? 0);
  180. if ($ta === $tb) {
  181. return ((int)($b['id'] ?? 0)) <=> ((int)($a['id'] ?? 0));
  182. }
  183. return $orderDir === 'ASC' ? ($ta <=> $tb) : ($tb <=> $ta);
  184. });
  185. }
  186. $pageRows = array_slice($merged, $offset, $limit);
  187. return json(['total' => $nMerged, 'rows' => $pageRows]);
  188. }
  189. return $this->view->fetch();
  190. }
  191. /**
  192. * 历史存证:同一订单号 CCYDH 合并为一行(与审批/确认列表一致)
  193. *
  194. * @param array<int, array<string, mixed>> $rows
  195. * @return array<int, array<string, mixed>>
  196. */
  197. protected function collapseArchiveRowsByOrder(array $rows): array
  198. {
  199. if ($rows === []) {
  200. return [];
  201. }
  202. $groups = [];
  203. foreach ($rows as $row) {
  204. if (!is_array($row)) {
  205. continue;
  206. }
  207. $dh = trim((string)($row['CCYDH'] ?? ''));
  208. $key = $dh !== '' ? $dh : ('_id_' . (int)($row['id'] ?? 0));
  209. if (!isset($groups[$key])) {
  210. $groups[$key] = [];
  211. }
  212. $groups[$key][] = $row;
  213. }
  214. $out = [];
  215. foreach ($groups as $groupRows) {
  216. if ($groupRows === []) {
  217. continue;
  218. }
  219. usort($groupRows, function ($a, $b) {
  220. return ((int)($a['id'] ?? 0)) <=> ((int)($b['id'] ?? 0));
  221. });
  222. $head = $groupRows[0];
  223. $gymcList = [];
  224. $supplierList = [];
  225. $inboundList = [];
  226. foreach ($groupRows as $r) {
  227. $g = trim((string)($r['CGYMC'] ?? ''));
  228. if ($g !== '' && !in_array($g, $gymcList, true)) {
  229. $gymcList[] = $g;
  230. }
  231. $sn = trim((string)($r['pick_company_name'] ?? ''));
  232. if ($sn !== '' && !in_array($sn, $supplierList, true)) {
  233. $supplierList[] = $sn;
  234. }
  235. $ir = trim((string)($r['inbound_result'] ?? ''));
  236. if ($ir !== '' && !in_array($ir, $inboundList, true)) {
  237. $inboundList[] = $ir;
  238. }
  239. }
  240. $merged = $head;
  241. if (count($groupRows) > 1) {
  242. $merged['CGYMC'] = implode('、', $gymcList);
  243. }
  244. $merged['pick_company_name'] = implode('、', $supplierList);
  245. $merged['inbound_result'] = $inboundList !== [] ? implode('、', $inboundList) : '';
  246. $merged['process_count'] = count($groupRows);
  247. $latestTs = 0;
  248. $latestText = '';
  249. foreach ($groupRows as $r) {
  250. $ts = (int)($r['createtime'] ?? 0);
  251. if ($ts <= 0 && !empty($r['createtime_text'])) {
  252. $ts = (int)strtotime((string)$r['createtime_text']);
  253. }
  254. if ($ts > $latestTs) {
  255. $latestTs = $ts;
  256. $latestText = trim((string)($r['createtime_text'] ?? ''));
  257. }
  258. }
  259. if ($latestTs > 0) {
  260. $merged['createtime'] = $latestTs;
  261. if ($latestText === '') {
  262. $latestText = date('Y-m-d H:i:s', $latestTs);
  263. }
  264. $merged['createtime_text'] = ProcuremenTime::formatDisplayDateTime($latestText);
  265. }
  266. $maxId = (int)($head['id'] ?? 0);
  267. foreach ($groupRows as $r) {
  268. $maxId = max($maxId, (int)($r['id'] ?? 0));
  269. }
  270. $merged['id'] = $maxId;
  271. $out[] = $merged;
  272. }
  273. return $out;
  274. }
  275. }