Procuremenarchive.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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';
  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. 'archiveabandon' => $canArchiveAbandon,
  24. ]);
  25. }
  26. /**
  27. * 历史存证「重新下发」权限(规则名在 procuremen 控制器下)
  28. */
  29. protected function canArchiveAbandon(): bool
  30. {
  31. if ($this->hasProcuremenSiblingPerm(['archiveabandon', 'archiveAbandon'])) {
  32. return true;
  33. }
  34. return $this->hasProcuremenSiblingPerm(['auditabandon', 'auditAbandon']);
  35. }
  36. /**
  37. * @param string[] $actions
  38. */
  39. protected function hasProcuremenSiblingPerm(array $actions): bool
  40. {
  41. foreach ($actions as $action) {
  42. $action = strtolower(trim((string)$action));
  43. if ($action === '') {
  44. continue;
  45. }
  46. if (strpos($action, 'procuremen/') !== 0) {
  47. $action = 'procuremen/' . $action;
  48. }
  49. if ($this->auth->check($action)) {
  50. return true;
  51. }
  52. }
  53. $suffixes = [];
  54. foreach ($actions as $action) {
  55. $action = strtolower(trim((string)$action));
  56. $action = preg_replace('#^procuremen/#', '', $action);
  57. if ($action !== '') {
  58. $suffixes[$action] = true;
  59. }
  60. }
  61. if ($suffixes === []) {
  62. return false;
  63. }
  64. try {
  65. foreach ($this->auth->getRuleList() as $rule) {
  66. $rule = strtolower((string)$rule);
  67. if (strpos($rule, 'procuremen/') !== 0) {
  68. continue;
  69. }
  70. foreach (array_keys($suffixes) as $suffix) {
  71. if ($rule === 'procuremen/' . $suffix || strpos($rule, 'procuremen/' . $suffix . '/') === 0) {
  72. return true;
  73. }
  74. }
  75. }
  76. } catch (\Throwable $e) {
  77. }
  78. return false;
  79. }
  80. /** purchase_order.status 为已完结(兼容 varchar / 数字 / 首尾空格) */
  81. protected function applyPurchaseOrderCompletedWhere($query): void
  82. {
  83. $query->whereRaw(ProcuremenStatus::sqlPoCompleted('status'));
  84. }
  85. public function index()
  86. {
  87. $this->relationSearch = false;
  88. $this->request->filter(['strip_tags', 'trim']);
  89. if ($this->request->isAjax()) {
  90. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  91. $limit = max(10, min(500, (int)$limit));
  92. $offset = max(0, (int)$offset);
  93. $applyFilters = function ($query) use ($where) {
  94. $this->applyPurchaseOrderCompletedWhere($query);
  95. if (!empty($where)) {
  96. $query->where($where);
  97. }
  98. };
  99. $sortField = preg_match('/^[a-zA-Z0-9_]+$/', (string)$sort) ? $sort : 'id';
  100. $orderDir = strtoupper((string)$order) === 'ASC' ? 'ASC' : 'DESC';
  101. $listQuery = Db::table('purchase_order');
  102. $applyFilters($listQuery);
  103. $rows = $listQuery
  104. ->field('id,scydgy_id,CCYDH,CYJMC,CGYMC,createtime,dStamp,pick_time')
  105. ->order($sortField, $orderDir)
  106. ->select();
  107. if (!is_array($rows)) {
  108. $rows = [];
  109. }
  110. $sidList = [];
  111. foreach ($rows as $r) {
  112. if (!is_array($r)) {
  113. continue;
  114. }
  115. $sid = (int)($r['scydgy_id'] ?? 0);
  116. if ($sid !== 0) {
  117. $sidList[$sid] = true;
  118. }
  119. }
  120. $completeTsMap = ProcuremenTime::loadOperLogTimestampMap(
  121. array_keys($sidList),
  122. ['purchase_confirm', 'mark_complete']
  123. );
  124. $out = [];
  125. foreach ($rows as $r) {
  126. if (!is_array($r)) {
  127. continue;
  128. }
  129. $sid = (int)($r['scydgy_id'] ?? 0);
  130. $done = ProcuremenTime::resolveCompletedDone($r, $completeTsMap);
  131. $out[] = [
  132. 'id' => (int)($r['id'] ?? 0),
  133. 'scydgy_id' => $sid,
  134. 'CCYDH' => trim((string)($r['CCYDH'] ?? '')),
  135. 'CYJMC' => trim((string)($r['CYJMC'] ?? '')),
  136. 'CGYMC' => trim((string)($r['CGYMC'] ?? '')),
  137. 'createtime' => $done['ts'],
  138. 'createtime_text' => $done['text'],
  139. ];
  140. }
  141. $merged = $this->collapseArchiveRowsByOrder($out);
  142. $nMerged = count($merged);
  143. if ($nMerged > 1 && $sortField === 'id') {
  144. usort($merged, function ($a, $b) use ($orderDir) {
  145. $cmp = ((int)($a['id'] ?? 0)) <=> ((int)($b['id'] ?? 0));
  146. if ($cmp === 0) {
  147. return strcmp((string)($a['CCYDH'] ?? ''), (string)($b['CCYDH'] ?? ''));
  148. }
  149. return $orderDir === 'ASC' ? $cmp : -$cmp;
  150. });
  151. } elseif ($nMerged > 1 && $sortField === 'createtime') {
  152. usort($merged, function ($a, $b) use ($orderDir) {
  153. $ta = (int)($a['createtime'] ?? 0);
  154. $tb = (int)($b['createtime'] ?? 0);
  155. if ($ta === $tb) {
  156. return ((int)($b['id'] ?? 0)) <=> ((int)($a['id'] ?? 0));
  157. }
  158. return $orderDir === 'ASC' ? ($ta <=> $tb) : ($tb <=> $ta);
  159. });
  160. }
  161. $pageRows = array_slice($merged, $offset, $limit);
  162. return json(['total' => $nMerged, 'rows' => $pageRows]);
  163. }
  164. return $this->view->fetch();
  165. }
  166. /**
  167. * 历史存证:同一订单号 CCYDH 合并为一行(与审批/确认列表一致)
  168. *
  169. * @param array<int, array<string, mixed>> $rows
  170. * @return array<int, array<string, mixed>>
  171. */
  172. protected function collapseArchiveRowsByOrder(array $rows): array
  173. {
  174. if ($rows === []) {
  175. return [];
  176. }
  177. $groups = [];
  178. foreach ($rows as $row) {
  179. if (!is_array($row)) {
  180. continue;
  181. }
  182. $dh = trim((string)($row['CCYDH'] ?? ''));
  183. $key = $dh !== '' ? $dh : ('_id_' . (int)($row['id'] ?? 0));
  184. if (!isset($groups[$key])) {
  185. $groups[$key] = [];
  186. }
  187. $groups[$key][] = $row;
  188. }
  189. $out = [];
  190. foreach ($groups as $groupRows) {
  191. if ($groupRows === []) {
  192. continue;
  193. }
  194. usort($groupRows, function ($a, $b) {
  195. return ((int)($a['id'] ?? 0)) <=> ((int)($b['id'] ?? 0));
  196. });
  197. $head = $groupRows[0];
  198. $gymcList = [];
  199. foreach ($groupRows as $r) {
  200. $g = trim((string)($r['CGYMC'] ?? ''));
  201. if ($g !== '' && !in_array($g, $gymcList, true)) {
  202. $gymcList[] = $g;
  203. }
  204. }
  205. $merged = $head;
  206. if (count($groupRows) > 1) {
  207. $merged['CGYMC'] = implode('、', $gymcList);
  208. }
  209. $merged['process_count'] = count($groupRows);
  210. $latestTs = 0;
  211. $latestText = '';
  212. foreach ($groupRows as $r) {
  213. $ts = (int)($r['createtime'] ?? 0);
  214. if ($ts <= 0 && !empty($r['createtime_text'])) {
  215. $ts = (int)strtotime((string)$r['createtime_text']);
  216. }
  217. if ($ts > $latestTs) {
  218. $latestTs = $ts;
  219. $latestText = trim((string)($r['createtime_text'] ?? ''));
  220. }
  221. }
  222. if ($latestTs > 0) {
  223. $merged['createtime'] = $latestTs;
  224. if ($latestText === '') {
  225. $latestText = date('Y-m-d H:i:s', $latestTs);
  226. }
  227. $merged['createtime_text'] = ProcuremenTime::formatDisplayDateTime($latestText);
  228. }
  229. $maxId = (int)($head['id'] ?? 0);
  230. foreach ($groupRows as $r) {
  231. $maxId = max($maxId, (int)($r['id'] ?? 0));
  232. }
  233. $merged['id'] = $maxId;
  234. $out[] = $merged;
  235. }
  236. return $out;
  237. }
  238. }