Procuremenarchive.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. '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,pick_company_name,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. 'pick_company_name' => trim((string)($r['pick_company_name'] ?? '')),
  138. 'createtime' => $done['ts'],
  139. 'createtime_text' => $done['text'],
  140. ];
  141. }
  142. $merged = $this->collapseArchiveRowsByOrder($out);
  143. $nMerged = count($merged);
  144. if ($nMerged > 1 && $sortField === 'id') {
  145. usort($merged, function ($a, $b) use ($orderDir) {
  146. $cmp = ((int)($a['id'] ?? 0)) <=> ((int)($b['id'] ?? 0));
  147. if ($cmp === 0) {
  148. return strcmp((string)($a['CCYDH'] ?? ''), (string)($b['CCYDH'] ?? ''));
  149. }
  150. return $orderDir === 'ASC' ? $cmp : -$cmp;
  151. });
  152. } elseif ($nMerged > 1 && $sortField === 'createtime') {
  153. usort($merged, function ($a, $b) use ($orderDir) {
  154. $ta = (int)($a['createtime'] ?? 0);
  155. $tb = (int)($b['createtime'] ?? 0);
  156. if ($ta === $tb) {
  157. return ((int)($b['id'] ?? 0)) <=> ((int)($a['id'] ?? 0));
  158. }
  159. return $orderDir === 'ASC' ? ($ta <=> $tb) : ($tb <=> $ta);
  160. });
  161. }
  162. $pageRows = array_slice($merged, $offset, $limit);
  163. return json(['total' => $nMerged, 'rows' => $pageRows]);
  164. }
  165. return $this->view->fetch();
  166. }
  167. /**
  168. * 历史存证:同一订单号 CCYDH 合并为一行(与审批/确认列表一致)
  169. *
  170. * @param array<int, array<string, mixed>> $rows
  171. * @return array<int, array<string, mixed>>
  172. */
  173. protected function collapseArchiveRowsByOrder(array $rows): array
  174. {
  175. if ($rows === []) {
  176. return [];
  177. }
  178. $groups = [];
  179. foreach ($rows as $row) {
  180. if (!is_array($row)) {
  181. continue;
  182. }
  183. $dh = trim((string)($row['CCYDH'] ?? ''));
  184. $key = $dh !== '' ? $dh : ('_id_' . (int)($row['id'] ?? 0));
  185. if (!isset($groups[$key])) {
  186. $groups[$key] = [];
  187. }
  188. $groups[$key][] = $row;
  189. }
  190. $out = [];
  191. foreach ($groups as $groupRows) {
  192. if ($groupRows === []) {
  193. continue;
  194. }
  195. usort($groupRows, function ($a, $b) {
  196. return ((int)($a['id'] ?? 0)) <=> ((int)($b['id'] ?? 0));
  197. });
  198. $head = $groupRows[0];
  199. $gymcList = [];
  200. $supplierList = [];
  201. foreach ($groupRows as $r) {
  202. $g = trim((string)($r['CGYMC'] ?? ''));
  203. if ($g !== '' && !in_array($g, $gymcList, true)) {
  204. $gymcList[] = $g;
  205. }
  206. $sn = trim((string)($r['pick_company_name'] ?? ''));
  207. if ($sn !== '' && !in_array($sn, $supplierList, true)) {
  208. $supplierList[] = $sn;
  209. }
  210. }
  211. $merged = $head;
  212. if (count($groupRows) > 1) {
  213. $merged['CGYMC'] = implode('、', $gymcList);
  214. }
  215. $merged['pick_company_name'] = implode('、', $supplierList);
  216. $merged['process_count'] = count($groupRows);
  217. $latestTs = 0;
  218. $latestText = '';
  219. foreach ($groupRows as $r) {
  220. $ts = (int)($r['createtime'] ?? 0);
  221. if ($ts <= 0 && !empty($r['createtime_text'])) {
  222. $ts = (int)strtotime((string)$r['createtime_text']);
  223. }
  224. if ($ts > $latestTs) {
  225. $latestTs = $ts;
  226. $latestText = trim((string)($r['createtime_text'] ?? ''));
  227. }
  228. }
  229. if ($latestTs > 0) {
  230. $merged['createtime'] = $latestTs;
  231. if ($latestText === '') {
  232. $latestText = date('Y-m-d H:i:s', $latestTs);
  233. }
  234. $merged['createtime_text'] = ProcuremenTime::formatDisplayDateTime($latestText);
  235. }
  236. $maxId = (int)($head['id'] ?? 0);
  237. foreach ($groupRows as $r) {
  238. $maxId = max($maxId, (int)($r['id'] ?? 0));
  239. }
  240. $merged['id'] = $maxId;
  241. $out[] = $merged;
  242. }
  243. return $out;
  244. }
  245. }