WorkOrderProcess.php 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use PhpOffice\PhpSpreadsheet\IOFactory;
  5. use think\Db;
  6. /**
  7. * 工单工艺管理
  8. *
  9. */
  10. class WorkOrderProcess extends Api
  11. {
  12. protected $noNeedLogin = ['*'];
  13. protected $noNeedRight = ['*'];
  14. /**
  15. * 工单大工序列表
  16. * @param workorder 工单编号
  17. */
  18. public function MajorprocessList()
  19. {
  20. if (!$this->request->isGet()) {
  21. $this->error('请求方法错误');
  22. }
  23. $params = $this->request->param();
  24. if (empty($params['workorder'])) {
  25. $this->error('工单编号不能为空');
  26. }
  27. $list = db('工单_工艺资料')
  28. ->where('订单编号', $params['workorder'])
  29. ->field('工序编号,工序名称,工序备注')
  30. ->distinct('工序名称')
  31. ->select();
  32. if(empty($list)){
  33. $this->error('该工单没有大工序');
  34. }
  35. $this->success('成功', $list);
  36. }
  37. /**
  38. * 工单部件列表
  39. * @param workorder 工单编号
  40. */
  41. public function PartList()
  42. {
  43. if (!$this->request->isGet()) {
  44. $this->error('请求方法错误');
  45. }
  46. $params = $this->request->param();
  47. if (empty($params['workorder'])) {
  48. $this->error('工单编号不能为空');
  49. }
  50. $list = db('工单_部件资料')
  51. ->where('work_order', $params['workorder'])
  52. ->where('del_rq', null)
  53. ->field('part_code as 部件编号,part_name as 部件名称,remark as 部件备注,part_type as 部件类型,status as 状态,
  54. sys_id as 操作人,sys_rq as 操作时间,mod_rq as 修改时间,mod_id as 修改人,id as 部件ID')
  55. ->orderRaw('CAST(part_code AS UNSIGNED)')
  56. ->select();
  57. if(empty($list)){
  58. // $this->error('该工单没有部件,请先添加部件');
  59. }
  60. $this->success('成功', $list);
  61. }
  62. /**
  63. * 新增工单部件
  64. * @param workorder 工单编号
  65. * @param part_code 部件编号
  66. * @param part_name 部件名称
  67. * @param remark 部件备注
  68. * @param part_type 部件类型
  69. * @param sys_id 操作人
  70. */
  71. public function AddPart()
  72. {
  73. if (!$this->request->isPost()) {
  74. $this->error('请求方法错误');
  75. }
  76. $params = $this->request->post();
  77. if (empty($params['workorder'])) {
  78. $this->error('工单编号不能为空');
  79. }
  80. if (empty($params['part_name'])) {
  81. $this->error('部件名称不能为空');
  82. }
  83. // 检查是否传入了part_code
  84. if (!empty($params['part_code'])) {
  85. $code = $params['part_code'];
  86. // 检查是否存在冲突
  87. $exists = db('工单_部件资料')
  88. ->where('work_order', $params['workorder'])
  89. ->where('part_code', '>=', $code)
  90. ->count();
  91. if ($exists > 0) {
  92. // 如果存在冲突,将后续编号自动加1
  93. db('工单_部件资料')
  94. ->where('work_order', $params['workorder'])
  95. ->where('part_code', '>=', $code)
  96. ->setInc('part_code');
  97. }
  98. } else {
  99. // 如果没有传入part_code,按照原来的逻辑生成
  100. $code = db('工单_部件资料')
  101. ->where('work_order', $params['workorder'])
  102. ->max('part_code');
  103. $code = $code ? $code + 1 : 1;
  104. }
  105. $data = [
  106. 'work_order' => $params['workorder'],
  107. 'part_code' => $code,
  108. 'part_name' => $params['part_name'],
  109. 'remark' => $params['remark'],
  110. 'part_type' => $params['part_type'],
  111. 'status' => 1,
  112. 'sys_id' => $params['sys_id'],
  113. 'sys_rq' => date('Y-m-d H:i:s'),
  114. ];
  115. $result = db('工单_部件资料')->insert($data);
  116. if (!$result) {
  117. $this->error('新增失败');
  118. }
  119. $this->success('新增成功');
  120. }
  121. /**
  122. * 修改部件状态
  123. * @param id 部件ID
  124. * @param status 状态
  125. */
  126. public function UpdatePartStatus()
  127. {
  128. if (!$this->request->isPost()) {
  129. $this->error('请求方法错误');
  130. }
  131. $params = $this->request->post();
  132. if (empty($params['id'])) {
  133. $this->error('部件ID不能为空');
  134. }
  135. if (!isset($params['status'])) {
  136. $this->error('状态不能为空');
  137. }
  138. $result = db('工单_部件资料')
  139. ->where('id', $params['id'])
  140. ->update(['status' => $params['status']]);
  141. if (!$result) {
  142. $this->error('修改失败');
  143. }
  144. $this->success('修改成功');
  145. }
  146. /**
  147. * 修改部件信息
  148. * @param workorder 工单编号
  149. * @param part_code 部件编号
  150. * @param part_name 部件名称
  151. * @param remark 部件备注
  152. * @param part_type 部件类型
  153. */
  154. public function UpdatePartInfo()
  155. {
  156. if (!$this->request->isPost()) {
  157. $this->error('请求方法错误');
  158. }
  159. $params = $this->request->post();
  160. if (empty($params['part_name'])) {
  161. $this->error('部件名称不能为空');
  162. }
  163. if (empty($params['remark'])) {
  164. $this->error('部件备注不能为空');
  165. }
  166. if (empty($params['part_type'])) {
  167. $this->error('部件类型不能为空');
  168. }
  169. if(empty($params['mod_id'])){
  170. $this->error('修改人不能为空');
  171. }
  172. if(empty($params['id'])){
  173. $this->error('部件ID不能为空');
  174. }
  175. $result = db('工单_部件资料')
  176. ->where('id', $params['id'])
  177. ->update([
  178. 'part_name' => $params['part_name'],
  179. 'remark' => $params['remark'],
  180. 'part_type' => $params['part_type'],
  181. 'mod_id' => $params['mod_id'],
  182. 'mod_rq' => date('Y-m-d H:i:s'),
  183. ]);
  184. if (!$result) {
  185. $this->error('修改失败');
  186. }
  187. $this->success('修改成功');
  188. }
  189. /** 软删除部件信息
  190. * @param id 部件ID
  191. */
  192. public function DeletePart()
  193. {
  194. if (!$this->request->isPost()) {
  195. $this->error('请求方法错误');
  196. }
  197. $params = $this->request->post();
  198. if (empty($params['id'])) {
  199. $this->error('部件ID不能为空');
  200. }
  201. $ids = explode(',', $params['id']);
  202. $ids = array_filter(array_map('intval', $ids));
  203. if (empty($ids)) {
  204. $this->error('无效的部件ID');
  205. }
  206. $result = db('工单_部件资料')
  207. ->whereIn('id', $ids)
  208. ->whereNull('del_rq')
  209. ->update(['del_rq' => date('Y-m-d H:i:s')]);
  210. if ($result === false) {
  211. $this->error('删除失败');
  212. }
  213. if ($result === 0) {
  214. $this->error('未找到可删除的部件');
  215. }
  216. $this->success('删除成功');
  217. }
  218. /**
  219. * 工单工艺列表
  220. * @param workorder 工单编号
  221. */
  222. public function GetProcessList()
  223. {
  224. if (!$this->request->isGet()) {
  225. $this->error('请求方法错误');
  226. }
  227. $params = $this->request->param();
  228. if (empty($params['workorder'])) {
  229. $this->error('工单编号不能为空');
  230. }
  231. $list = db('工单_基础工艺资料')
  232. ->alias('a')
  233. ->join('工单_部件资料 b', 'a.part_code = b.part_code and a.work_order = b.work_order', 'LEFT')
  234. ->where('a.del_rq', null)
  235. ->where('a.work_order', $params['workorder'])
  236. ->where('b.del_rq', null)
  237. ->field('a.id,a.part_code as 部件编号,
  238. IFNULL(b.part_name, "") as 部件名称,
  239. a.process_code as 工艺编号,
  240. a.process_name as 工艺名称,
  241. a.big_process as 大工艺,
  242. a.standard_hour as 标准工时,
  243. a.standard_score as 标准公分,
  244. a.remark as 备注,
  245. a.coefficient as 系数,
  246. a.sys_id as 系统人,
  247. a.sys_rq as 系统时间,
  248. a.mod_id as 修改人,
  249. a.mod_rq as 修改时间,
  250. a.status as 状态')
  251. ->orderRaw('CAST(a.process_code AS UNSIGNED)')
  252. ->select();
  253. $this->success('成功', $list);
  254. }
  255. /**
  256. * 工单工艺列表
  257. * @param workorder 工单编号
  258. */
  259. // public function GetProcessList()
  260. // {
  261. // if (!$this->request->isGet()) {
  262. // $this->error('请求方法错误');
  263. // }
  264. // $params = $this->request->param();
  265. // if (empty($params['workorder'])) {
  266. // $this->error('工单编号不能为空');
  267. // }
  268. // $list = db('工单_基础工艺资料')
  269. // ->alias('a')
  270. // ->join('工单_部件资料 b', 'a.part_code = b.part_code and a.work_order = b.work_order')
  271. // ->where('a.del_rq', null)
  272. // ->where('a.work_order', $params['workorder'])
  273. // ->where('b.del_rq', null)
  274. // ->field('a.id,a.part_code as 部件编号,b.part_name as 部件名称,a.process_code as 序号,
  275. // a.process_name as 工序名称,a.big_process as 大工艺,a.standard_hour as 秒,a.standard_minutes as 分钟,
  276. // a.standard_score as 标准公分,a.money as 金额,a.remark as 备注,a.coefficient as 难度系数,a.sys_id as 操作人员,
  277. // a.sys_rq as 系统时间,a.mod_id as 修改人,a.mod_rq as 修改时间')
  278. // ->order('process_code')
  279. // ->select();
  280. // $this->success('成功', $list);
  281. // }
  282. /**
  283. * 获取部件列表
  284. * @param workorder 工单编号
  285. */
  286. public function getpartlist()
  287. {
  288. if(!$this->request->isGet()){
  289. $this->error('请求方法错误');
  290. }
  291. $params = $this->request->get();
  292. if (empty($params['workorder'])) {
  293. $this->error('工单编号不能为空');
  294. }
  295. $list = db('工单_部件资料')
  296. ->where('work_order', $params['workorder'])
  297. ->where('del_rq', null)
  298. ->field('part_code as 部件编号,part_name as 部件名称')
  299. ->order('part_code')
  300. ->select();
  301. if(empty($list)){
  302. $this->error('该工单没有部件,请先添加部件');
  303. }
  304. $this->success('成功', $list);
  305. }
  306. /**
  307. * 新增工单工艺资料
  308. * @param workorder 工单编号
  309. * @param part_code 部件编号
  310. * @param process_code 工艺编号
  311. * @param process_name 工艺名称
  312. * @param big_process 大工艺
  313. * @param standard_hour 标准工时
  314. * @param standard_score 标准公分
  315. * @param sys_id 系统人
  316. * @param coefficient 系数
  317. * @param remark 备注
  318. */
  319. public function AddProcess()
  320. {
  321. if (!$this->request->isPost()) {
  322. $this->error('请求方法错误');
  323. }
  324. $params = $this->request->post();
  325. if (empty($params['workorder'])) {
  326. $this->error('工单编号不能为空');
  327. }
  328. if (empty($params['part_code'])) {
  329. $this->error('部件编号不能为空');
  330. }
  331. if (empty($params['process_name'])) {
  332. $this->error('工艺名称不能为空');
  333. }
  334. if (empty($params['big_process'])) {
  335. $this->error('大工艺不能为空');
  336. }
  337. if (empty($params['sys_id'])) {
  338. $this->error('系统人不能为空');
  339. }
  340. if (!empty($params['process_code'])) {
  341. $code = intval($params['process_code']);
  342. $exists = db('工单_基础工艺资料')
  343. ->where('work_order', $params['workorder'])
  344. ->where('part_code', $params['part_code'])
  345. ->where('process_code', $code)
  346. ->whereNull('del_rq')
  347. ->count();
  348. if ($exists > 0) {
  349. $this->error('工序编号已存在');
  350. }
  351. } else {
  352. $code = db('工单_基础工艺资料')
  353. ->where('work_order', $params['workorder'])
  354. ->where('part_code', $params['part_code'])
  355. ->whereNull('del_rq')
  356. ->max('process_code');
  357. $code = $code ? $code + 1 : 1;
  358. }
  359. $data = [
  360. 'work_order' => $params['workorder'],
  361. 'part_code' => $params['part_code'],
  362. 'part_name' => $params['part_name'],
  363. 'process_code' => $code,
  364. 'process_name' => $params['process_name'],
  365. 'big_process' => $params['big_process'],
  366. 'standard_hour' => $params['standard_hour'],
  367. 'standard_score' => $params['standard_score'],
  368. 'coefficient' => $params['coefficient'],
  369. 'remark' => $params['remark'],
  370. 'sys_id' => $params['sys_id'],
  371. 'sys_rq' => date('Y-m-d H:i:s'),
  372. ];
  373. $result = db('工单_基础工艺资料')->insert($data);
  374. if ($result === false) {
  375. $this->error('新增失败');
  376. }
  377. $this->success('新增成功');
  378. }
  379. /**
  380. * 更新工单工艺资料
  381. * @param id 工艺ID
  382. * @param mod_id 修改人
  383. * @param process_name 工艺名称
  384. * @param big_process 大工艺
  385. * @param standard_hour 标准工时
  386. * @param standard_score 标准公分
  387. * @param coefficient 系数
  388. * @param remark 备注
  389. */
  390. public function UpdateProcess()
  391. {
  392. if (!$this->request->isPost()) {
  393. $this->error('请求方法错误');
  394. }
  395. $params = $this->request->post();
  396. if (empty($params['id'])) {
  397. $this->error('工艺ID不能为空');
  398. }
  399. if (empty($params['mod_id'])) {
  400. $this->error('修改人不能为空');
  401. }
  402. if (empty($params['process_name'])) {
  403. $this->error('工艺名称不能为空');
  404. }
  405. if (empty($params['big_process'])) {
  406. $this->error('大工艺不能为空');
  407. }
  408. if (!isset($params['coefficient']) || $params['coefficient'] === '') {
  409. $params['coefficient'] = 'C';
  410. }
  411. $process = Db::table('工单_基础工艺资料')
  412. ->where('id', intval($params['id']))
  413. ->whereNull('del_rq')
  414. ->find();
  415. if (empty($process)) {
  416. $this->error('工艺不存在');
  417. }
  418. $data = [
  419. 'part_code' => $params['part_code'],
  420. 'part_name' => $params['part_name'],
  421. 'standard_minutes' => $params['standard_minutes'],
  422. 'money' => $params['money'],
  423. 'remark' => $params['remark'],
  424. 'process_name' => $params['process_name'],
  425. 'big_process' => $params['big_process'],
  426. 'standard_hour' => $params['standard_hour'],
  427. 'standard_score' => $params['standard_score'],
  428. 'coefficient' => $params['coefficient'],
  429. 'mod_id' => $params['mod_id'],
  430. 'mod_rq' => date('Y-m-d H:i:s'),
  431. ];
  432. $fieldLabels = [
  433. 'part_code' => '部件编号',
  434. 'part_name' => '部件名称',
  435. 'standard_minutes' => '分',
  436. 'money' => '金额',
  437. 'remark' => '备注',
  438. 'process_name' => '工序名称',
  439. 'big_process' => '大工序',
  440. 'standard_hour' => '秒',
  441. 'standard_score' => '定额分',
  442. 'coefficient' => '难度系数',
  443. ];
  444. $operTime = date('Y-m-d H:i:s');
  445. $logList = [];
  446. foreach ($fieldLabels as $field => $label) {
  447. $oldValue = isset($process[$field]) ? $process[$field] : '';
  448. $newValue = $data[$field];
  449. if ((string)$oldValue === (string)$newValue) {
  450. continue;
  451. }
  452. $logList[] = [
  453. 'order_no' => $process['work_order'],
  454. 'process_code' => $params['process_code'],
  455. 'change_field' => $label,
  456. 'old_value' => $oldValue === null ? '' : (string)$oldValue,
  457. 'new_value' => (string)$newValue,
  458. 'oper_type' => '修改工单工艺',
  459. 'oper_user_name' => $params['mod_id'],
  460. 'oper_time' => $operTime,
  461. ];
  462. }
  463. if (empty($logList)) {
  464. $this->error('未找到可更新的工艺或数据无变化');
  465. }
  466. $standardScoreChanged = (string)$process['standard_score'] !== (string)$params['standard_score'];
  467. if ($standardScoreChanged && (string)$process['status'] === '0') {
  468. $workOrder = Db::table('工单_基本资料')
  469. ->where('订单编号', $process['work_order'])
  470. ->where('Mod_rq', null)
  471. ->field('计划制造工分')
  472. ->find();
  473. if (empty($workOrder)) {
  474. $this->error('工单不存在');
  475. }
  476. $totalStandardScore = Db::table('工单_基础工艺资料')
  477. ->where('work_order', $process['work_order'])
  478. ->where('status', 0)
  479. ->whereNull('del_rq')
  480. ->sum('standard_score');
  481. $totalStandardScore = $totalStandardScore ? floatval($totalStandardScore) : 0;
  482. $newTotal = $totalStandardScore - floatval($process['standard_score']) + floatval($params['standard_score']);
  483. if ($newTotal > floatval($workOrder['计划制造工分'])) {
  484. $this->error('工序定额分和大于制造工分,请确认之后再修改');
  485. }
  486. }
  487. Db::startTrans();
  488. try {
  489. if (!empty($params['part_code'])) {
  490. $partExists = Db::table('工单_部件资料')
  491. ->where('work_order', $process['work_order'])
  492. ->where('part_code', $params['part_code'])
  493. ->whereNull('del_rq')
  494. ->find();
  495. if (empty($partExists)) {
  496. $partData = [
  497. 'work_order' => $process['work_order'],
  498. 'part_code' => $params['part_code'],
  499. 'part_name' => $params['part_name'],
  500. 'remark' => '',
  501. 'part_type' => $params['big_process'],
  502. 'status' => 1,
  503. 'sys_id' => $params['mod_id'],
  504. 'sys_rq' => date('Y-m-d H:i:s'),
  505. ];
  506. $partInsertResult = Db::table('工单_部件资料')->insert($partData);
  507. if ($partInsertResult === false) {
  508. $dbError = Db::getError();
  509. $detail = is_array($dbError) && !empty($dbError) ? json_encode($dbError, JSON_UNESCAPED_UNICODE) : '';
  510. throw new \Exception('新增部件资料失败' . ($detail !== '' ? ':' . $detail : ''));
  511. }
  512. }
  513. }
  514. $updateResult = Db::table('工单_基础工艺资料')
  515. ->where('id', intval($params['id']))
  516. ->whereNull('del_rq')
  517. ->update($data);
  518. if ($updateResult === false) {
  519. $dbError = Db::getError();
  520. $detail = is_array($dbError) && !empty($dbError) ? json_encode($dbError, JSON_UNESCAPED_UNICODE) : '';
  521. throw new \Exception('更新工艺失败' . ($detail !== '' ? ':' . $detail : ''));
  522. }
  523. if ($updateResult === 0) {
  524. throw new \Exception('未找到可更新的工艺');
  525. }
  526. $logResult = Db::name('work_order_operation_log')->insertAll($logList);
  527. if ($logResult === false || $logResult === 0) {
  528. $dbError = Db::getError();
  529. $detail = is_array($dbError) && !empty($dbError) ? json_encode($dbError, JSON_UNESCAPED_UNICODE) : '';
  530. throw new \Exception('写入操作日志失败' . ($detail !== '' ? ':' . $detail : ''));
  531. }
  532. Db::commit();
  533. } catch (\think\exception\HttpResponseException $e) {
  534. throw $e;
  535. } catch (\Exception $e) {
  536. Db::rollback();
  537. $msg = trim($e->getMessage());
  538. $this->error('更新失败:' . ($msg !== '' ? $msg : '未知错误'));
  539. }
  540. $this->success('更新成功');
  541. }
  542. /**
  543. * 工单工艺资料软删除
  544. * @param id 工艺ID
  545. */
  546. public function DeleteProcess()
  547. {
  548. if (!$this->request->isPost()) {
  549. $this->error('请求方法错误');
  550. }
  551. $params = $this->request->post();
  552. if (empty($params['id'])) {
  553. $this->error('工艺ID不能为空');
  554. }
  555. $ids = explode(',', $params['id']);
  556. $ids = array_filter(array_map('intval', $ids));
  557. if (empty($ids)) {
  558. $this->error('无效的工艺ID');
  559. }
  560. $result = db('工单_基础工艺资料')
  561. ->whereIn('id', $ids)
  562. ->whereNull('del_rq')
  563. ->update(['del_rq' => date('Y-m-d H:i:s')]);
  564. if ($result === false) {
  565. $this->error('删除失败');
  566. }
  567. if ($result === 0) {
  568. $this->error('未找到可删除的工艺');
  569. }
  570. $this->success('删除成功');
  571. }
  572. /**
  573. * 工单工艺复制
  574. * @param workorder 工单编号
  575. * @param product_code 产品编号
  576. * @param sys_id 操作人
  577. * @return array
  578. * @throws \think\db\exception\DataNotFoundException
  579. * @throws \think\db\exception\ModelNotFoundException
  580. * @throws \think\exception\DbException
  581. * @throws \think\exception\PDOException
  582. */
  583. public function workorderprocessCopy()
  584. {
  585. if (!$this->request->isGet()) {
  586. $this->error('请求方法错误');
  587. }
  588. $params = $this->request->param();
  589. if (empty($params['workorder'])) {
  590. $this->error('工单编号不能为空');
  591. }
  592. if (empty($params['product_code'])) {
  593. $this->error('产品编号不能为空');
  594. }
  595. if (empty($params['sys_id'])) {
  596. $this->error('操作人不能为空');
  597. }
  598. $now = date('Y-m-d H:i:s');
  599. $productParts = db('产品_部件资料')
  600. ->where('product_code', $params['product_code'])
  601. ->where('mod_rq', null)
  602. ->field('part_sort as part_code,part_name')
  603. ->select();
  604. if (empty($productParts)) {
  605. $this->error('该产品没有有效的部件');
  606. }
  607. $productProcesses = db('产品_工艺资料')
  608. ->where('product_code', $params['product_code'])
  609. ->field('part_sort as part_code,part_name,gy_sort as process_code,gy_name as process_name,
  610. big_process,standard_hour,standard_score,difficulty_coef as coefficient')
  611. ->select();
  612. if (empty($productProcesses)) {
  613. $this->error('该产品没有工艺');
  614. }
  615. $workOrderParts = [];
  616. //配置工单部件数据
  617. foreach ($productParts as $value) {
  618. $workOrderParts[] = [
  619. 'work_order' => $params['workorder'],
  620. 'part_code' => $value['part_code'],
  621. 'part_name' => $value['part_name'],
  622. 'part_type' => '',
  623. 'remark' => '',
  624. 'status' => 1,
  625. 'sys_id' => $params['sys_id'],
  626. 'sys_rq' => $now,
  627. ];
  628. }
  629. $workOrderProcesses = [];
  630. //配置工单工艺数据
  631. foreach ($productProcesses as $value) {
  632. $workOrderProcesses[] = [
  633. 'work_order' => $params['workorder'],
  634. 'part_code' => $value['part_code'],
  635. 'part_name' => $value['part_name'],
  636. 'process_code' => $value['process_code'],
  637. 'process_name' => $value['process_name'],
  638. 'big_process' => $value['big_process'],
  639. 'standard_hour' => $value['standard_hour'],
  640. 'standard_score' => $value['standard_score'],
  641. 'coefficient' => $value['coefficient'],
  642. 'remark' => '',
  643. 'sys_id' => $params['sys_id'],
  644. 'sys_rq' => $now,
  645. ];
  646. }
  647. Db::startTrans();
  648. try {
  649. //删除数据库现有的工单部件数据
  650. Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
  651. //删除数据库现有的工单工艺数据
  652. Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
  653. //插入工单部件数据
  654. $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
  655. if ($partInsertCount === false) {
  656. throw new \Exception('工单部件复制失败');
  657. }
  658. //插入工单工艺数据
  659. $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
  660. if ($processInsertCount === false) {
  661. throw new \Exception('工单工艺复制失败');
  662. }
  663. //提交事务
  664. Db::commit();
  665. } catch (\Exception $e) {
  666. Db::rollback();
  667. $this->error('复制失败');
  668. }
  669. $this->success('复制成功');
  670. }
  671. /**
  672. * 查询产品类型
  673. * @ApiMethod (GET)
  674. * @param string $workorder 工单编号
  675. * @return array
  676. * @throws \think\db\exception\DataNotFoundException
  677. * @throws \think\db\exception\ModelNotFoundException
  678. * @throws \think\exception\DbException
  679. */
  680. public function getproducttype()
  681. {
  682. if(!$this->request->isGet()){
  683. $this->error('请求方法错误');
  684. }
  685. $params = $this->request->get();
  686. if (empty($params['product'])) {
  687. $this->error('产品类型不能为空');
  688. }
  689. $list = db('产品_基本资料')
  690. ->where('product_type', $params['product'])
  691. ->where('status', 1)
  692. ->field('product_code as 产品编号,product_type as 产品类型,product_name as 产品名称')
  693. ->select();
  694. if(empty($list)){
  695. $this->error('该产品类型没有产品');
  696. }
  697. $this->success('成功', $list);
  698. }
  699. /**
  700. * 工单工艺排序
  701. * @param workorder 工单编号
  702. * @param part_code 部件编号
  703. * @param process_code 工艺编号
  704. * @param process_name 工艺名称
  705. * @param big_process 大工艺
  706. * @param standard_hour 标准工时
  707. * @param standard_score 标准公分
  708. * @param coefficient 系数
  709. * @param remark 备注
  710. */
  711. public function sortProcess()
  712. {
  713. if (!$this->request->isPost()) {
  714. $this->error('请求方法错误');
  715. }
  716. $params = $this->request->post();
  717. if (empty($params)) {
  718. $this->error('参数不能为空');
  719. }
  720. if (!is_array($params)) {
  721. $this->error('参数格式错误');
  722. }
  723. Db::startTrans();
  724. try {
  725. foreach ($params as $value) {
  726. if (empty($value['id'])) {
  727. throw new \Exception('缺少工艺ID');
  728. }
  729. if (!isset($value['process_code']) || $value['process_code'] === '') {
  730. throw new \Exception('缺少工艺编号');
  731. }
  732. $result = db('工单_基础工艺资料')
  733. ->where('id', intval($value['id']))
  734. ->whereNull('del_rq')
  735. ->update(['process_code' => intval($value['process_code'])]);
  736. if ($result === false) {
  737. throw new \Exception('排序失败');
  738. }
  739. }
  740. Db::commit();
  741. } catch (\Exception $e) {
  742. Db::rollback();
  743. $this->error($e->getMessage());
  744. }
  745. $this->success('排序成功');
  746. }
  747. /**
  748. * 工单工艺excel导入
  749. * @param workorder 工单编号
  750. *
  751. */
  752. public function importProcess()
  753. {
  754. if (!$this->request->isPost()) {
  755. $this->error('请求方法错误');
  756. }
  757. $params = $this->request->post();
  758. if (empty($params['workorder'])) {
  759. $this->error('工单编号不能为空');
  760. }
  761. if (empty($params['sys_id'])) {
  762. $this->error('操作人不能为空');
  763. }
  764. $file = $this->request->file('file');
  765. if (!$file) {
  766. $this->error('文件不能为空');
  767. }
  768. $uploadDir = ROOT_PATH . 'public' . DS . 'uploads';
  769. $info = $file->validate(['size' => 1024 * 1024 * 10, 'ext' => 'xlsx,xls,csv,txt'])
  770. ->move($uploadDir);
  771. if (!$info) {
  772. $this->error($file->getError());
  773. }
  774. $filePath = $uploadDir . DS . $info->getSaveName();
  775. // 定额表:上方为标题与元数据,第 5 行表头,第 6 行起为数据
  776. $data = $this->readExcel($filePath, 5, 6);
  777. if (empty($data)) {
  778. $this->error('文件内容为空');
  779. }
  780. $seenSeq = [];
  781. foreach ($data as $row) {
  782. $seq = isset($row['序号']) ? $row['序号'] : null;
  783. if ($seq === null || $seq === '') {
  784. continue;
  785. }
  786. $seqKey = is_scalar($seq) ? (string)$seq : $seq;
  787. if (isset($seenSeq[$seqKey])) {
  788. $this->error('工序序号重复,请重新调整之后再上传');
  789. }
  790. $seenSeq[$seqKey] = true;
  791. }
  792. $now = date('Y-m-d H:i:s');
  793. $partMap = [];
  794. $nextPartCode = 0;
  795. foreach ($data as $row) {
  796. $partName = isset($row['部件名称']) ? trim((string)$row['部件名称']) : '';
  797. if ($partName === '') {
  798. continue;
  799. }
  800. $partType = isset($row['生产工序']) ? trim((string)$row['生产工序']) : '';
  801. if (!isset($partMap[$partName])) {
  802. $partMap[$partName] = [
  803. 'part_code' => ++$nextPartCode,
  804. 'part_type' => $partType,
  805. ];
  806. } elseif ($partType !== '' && $partMap[$partName]['part_type'] === '') {
  807. $partMap[$partName]['part_type'] = $partType;
  808. }
  809. }
  810. $workOrderParts = [];
  811. foreach ($partMap as $partName => $partInfo) {
  812. $workOrderParts[] = [
  813. 'work_order' => $params['workorder'],
  814. 'part_name' => $partName,
  815. 'part_code' => $partInfo['part_code'],
  816. 'part_type' => $partInfo['part_type'],
  817. 'remark' => '',
  818. 'status' => 1,
  819. 'sys_id' => $params['sys_id'],
  820. 'sys_rq' => $now,
  821. ];
  822. }
  823. $workOrderProcesses = [];
  824. foreach ($data as $value) {
  825. $seq = isset($value['序号']) ? $value['序号'] : null;
  826. if ($seq === null || $seq === '') {
  827. continue;
  828. }
  829. $name = isset($value['部件名称']) ? trim((string)$value['部件名称']) : '';
  830. $partCode = ($name !== '' && isset($partMap[$name])) ? $partMap[$name]['part_code'] : '';
  831. $workOrderProcesses[] = [
  832. 'work_order' => $params['workorder'],
  833. 'part_code' => $partCode,
  834. 'part_name' => $name,
  835. 'process_code' => $seq,
  836. 'process_name' => $value['工序名称'],
  837. 'big_process' => $value['生产工序'],
  838. 'standard_hour' => $value['秒'],
  839. 'standard_minutes' => $value['分'],
  840. 'standard_score' => $value['定额分'],
  841. 'money' => $value['金额'],
  842. 'coefficient' => $value['难度系数'],
  843. 'remark' => isset($value['备注']) ? $value['备注'] : '',
  844. 'sys_id' => $params['sys_id'],
  845. 'sys_rq' => $now,
  846. ];
  847. }
  848. Db::startTrans();
  849. try {
  850. Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
  851. if (!empty($workOrderParts)) {
  852. $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
  853. if ($partInsertCount === false || $partInsertCount === 0) {
  854. throw new \Exception('工单部件导入失败');
  855. }
  856. }
  857. Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
  858. if (empty($workOrderProcesses)) {
  859. throw new \Exception('没有可导入的工序数据');
  860. }
  861. $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
  862. if ($processInsertCount === false || $processInsertCount === 0) {
  863. throw new \Exception('工单工艺导入失败');
  864. }
  865. Db::commit();
  866. } catch (\think\exception\HttpResponseException $e) {
  867. throw $e;
  868. } catch (\Exception $e) {
  869. Db::rollback();
  870. $this->error('导入失败:' . $e->getMessage());
  871. }
  872. $this->success('导入成功', [
  873. 'part_count' => count($workOrderParts),
  874. 'process_count' => count($workOrderProcesses),
  875. ]);
  876. }
  877. /**
  878. * 读取 Excel/CSV(PhpSpreadsheet)
  879. *
  880. * @param string $filePath
  881. * @param int $headerRowNum 表头所在行(1 起计,如定额表为第 5 行)
  882. * @param int $dataStartRowNum 首条数据行(1 起计,须大于表头行,如第 6 行)
  883. * @return array 每行一条关联数组,键为表头单元格文本
  884. */
  885. public function readExcel($filePath, $headerRowNum = 5, $dataStartRowNum = 6)
  886. {
  887. if (!is_file($filePath) || !is_readable($filePath)) {
  888. return [];
  889. }
  890. if ($dataStartRowNum <= $headerRowNum) {
  891. return [];
  892. }
  893. $spreadsheet = IOFactory::load($filePath);
  894. $sheet = $spreadsheet->getActiveSheet();
  895. $rows = $sheet->toArray();
  896. if (empty($rows)) {
  897. return [];
  898. }
  899. $headerIdx = $headerRowNum - 1;
  900. $dataStartIdx = $dataStartRowNum - 1;
  901. if (!isset($rows[$headerIdx])) {
  902. return [];
  903. }
  904. $headers = array_map(function ($cell) {
  905. return is_string($cell) ? trim($cell) : $cell;
  906. }, $rows[$headerIdx]);
  907. $data = [];
  908. $rowCount = count($rows);
  909. for ($r = $dataStartIdx; $r < $rowCount; $r++) {
  910. $row = $rows[$r];
  911. $hasCell = false;
  912. foreach ($row as $cell) {
  913. if ($cell !== null && $cell !== '') {
  914. $hasCell = true;
  915. break;
  916. }
  917. }
  918. if (!$hasCell) {
  919. continue;
  920. }
  921. $assoc = [];
  922. foreach ($headers as $i => $key) {
  923. if ($key === '' || $key === null) {
  924. continue;
  925. }
  926. $assoc[$key] = array_key_exists($i, $row) ? $row[$i] : null;
  927. }
  928. $data[] = $assoc;
  929. }
  930. return $data;
  931. }
  932. /**
  933. * 获取工单工艺信息
  934. * @param workorder 工单编号
  935. * @return array
  936. * @throws \think\db\exception\DataNotFoundException
  937. * @throws \think\db\exception\ModelNotFoundException
  938. * @throws \think\exception\DbException
  939. */
  940. public function getWorkOrderProcess()
  941. {
  942. if (!$this->request->isGet()) {
  943. $this->error('请求方法错误');
  944. }
  945. $params = $this->request->param();
  946. if (empty($params['workorder'])) {
  947. $this->error('工单编号不能为空');
  948. }
  949. $where = [
  950. 'work_order' => $params['workorder'],
  951. 'del_rq' => null,
  952. ];
  953. $workOrderProcess = Db::table('工单_基础工艺资料')
  954. ->where($where)
  955. ->field('id,part_code as 部件编号,part_name as 部件名称,process_code as 工序编号,process_name as 工序名称,
  956. big_process as 大工序,standard_hour as 秒,standard_minutes as 分,standard_score as 定额分,money as 金额,
  957. coefficient as 难度系数,remark as 备注,status as 状态,pid')
  958. ->orderRaw('CAST(process_code AS UNSIGNED)')
  959. ->select();
  960. if (empty($workOrderProcess)) {
  961. $this->error('工单工艺不存在');
  962. }
  963. $pidList = [];
  964. foreach ($workOrderProcess as $row) {
  965. if (!empty($row['pid'])) {
  966. $pidList[(int)$row['pid']] = true;
  967. }
  968. }
  969. $parentCodeMap = [];
  970. if (!empty($pidList)) {
  971. $parentCodeMap = Db::table('工单_基础工艺资料')
  972. ->whereIn('id', array_keys($pidList))
  973. ->column('process_code', 'id');
  974. }
  975. foreach ($workOrderProcess as &$row) {
  976. $row['母工序编号'] = !empty($row['pid']) && isset($parentCodeMap[(int)$row['pid']])
  977. ? $parentCodeMap[(int)$row['pid']]
  978. : '';
  979. }
  980. unset($row);
  981. $this->success('成功', $workOrderProcess);
  982. }
  983. /**
  984. * 修改工单计划制造工分
  985. * @param id 工单Uniqid或订单编号
  986. * @param number 计划制造工分
  987. * @param sys_id 操作人
  988. */
  989. public function updateWorkOrderPlanManufacture()
  990. {
  991. if (!$this->request->isPost()) {
  992. $this->error('请求方法错误');
  993. }
  994. $params = $this->request->post();
  995. if (empty($params['id'])) {
  996. $this->error('工单ID不能为空');
  997. }
  998. if (!isset($params['number']) || $params['number'] === '') {
  999. $this->error('制造工分不能为空');
  1000. }
  1001. if (empty($params['sys_id'])) {
  1002. $this->error('操作人不能为空');
  1003. }
  1004. $workOrder = Db::table('工单_基本资料')
  1005. ->where('Uniqid|订单编号', $params['id'])
  1006. ->where('Mod_rq', null)
  1007. ->field('Uniqid,订单编号,计划制造工分')
  1008. ->find();
  1009. if (empty($workOrder)) {
  1010. $this->error('工单不存在');
  1011. }
  1012. $oldValue = $workOrder['计划制造工分'];
  1013. $newValue = $params['number'];
  1014. if ((string)$oldValue === (string)$newValue) {
  1015. $this->error('计划制造工分未发生变化');
  1016. }
  1017. $totalStandardScore = Db::table('工单_基础工艺资料')
  1018. ->where('work_order', $workOrder['订单编号'])
  1019. ->where('status', 0)
  1020. ->whereNull('del_rq')
  1021. ->sum('standard_score');
  1022. $totalStandardScore = $totalStandardScore ? floatval($totalStandardScore) : 0;
  1023. if ($totalStandardScore > floatval($newValue)) {
  1024. $this->error('工序定额分和大于制造工分,请确认之后再修改');
  1025. }
  1026. $operTime = date('Y-m-d H:i:s');
  1027. $logData = [
  1028. 'order_no' => $workOrder['订单编号'],
  1029. 'change_field' => '计划制造工分',
  1030. 'old_value' => $oldValue === null ? '' : (string)$oldValue,
  1031. 'new_value' => (string)$newValue,
  1032. 'oper_type' => '修改工单计划制造工分',
  1033. 'oper_user_name' => $params['sys_id'],
  1034. 'oper_time' => $operTime,
  1035. ];
  1036. Db::startTrans();
  1037. try {
  1038. $updateResultSql = Db::table('工单_基本资料')
  1039. ->where('Uniqid', $workOrder['Uniqid'])
  1040. ->fetchSql(true)
  1041. ->update(['计划制造工分' => $newValue]);
  1042. $updateResult = Db::query($updateResultSql);
  1043. if ($updateResult === false) {
  1044. throw new \Exception('更新工单失败');
  1045. }
  1046. $logResult = Db::name('work_order_operation_log')->insert($logData);
  1047. if ($logResult === false) {
  1048. throw new \Exception('写入操作日志失败');
  1049. }
  1050. Db::commit();
  1051. } catch (\Exception $e) {
  1052. Db::rollback();
  1053. $this->error('修改失败');
  1054. }
  1055. $this->success('修改成功');
  1056. }
  1057. /**
  1058. * 拆分工序时判断工序是否存在报工
  1059. * @param workorder 工单编号
  1060. * @param process_code 工艺编号
  1061. * @return array
  1062. * @throws \think\db\exception\DataNotFoundException
  1063. * @throws \think\db\exception\ModelNotFoundException
  1064. * @throws \think\exception\DbException
  1065. */
  1066. public function checkProcessReport()
  1067. {
  1068. if (!$this->request->isPost()) {
  1069. $this->error('请求方法错误');
  1070. }
  1071. $params = $this->request->post();
  1072. if (empty($params['workorder'])) {
  1073. $this->error('工单编号不能为空');
  1074. }
  1075. if (empty($params['process_code'])) {
  1076. $this->error('工艺编号不能为空');
  1077. }
  1078. $process = db('设备_工分计酬')
  1079. ->where('work_order', $params['workorder'])
  1080. ->where('process_code', $params['process_code'])
  1081. ->find();
  1082. if (!empty($process)) {
  1083. $this->error('');
  1084. }
  1085. $this->success('工序不存在报工,可以拆分');
  1086. }
  1087. /**
  1088. * 确认拆分工序:number>0 时母工序置为已拆分并生成子工序(子工序 pid 为母工序 id);
  1089. * number=0 时恢复母工序 status=0 并软删除其子工序
  1090. * @param id 工艺ID
  1091. * @param number 拆分数量(0 表示取消拆分)
  1092. * @param sys_id 操作人员
  1093. * @param sys_rq 操作时间(可选,默认当前时间)
  1094. */
  1095. public function confirmProcessSplit()
  1096. {
  1097. if (!$this->request->isPost()) {
  1098. $this->error('请求方法错误');
  1099. }
  1100. $params = $this->request->post();
  1101. if (empty($params['id'])) {
  1102. $this->error('工艺ID不能为空');
  1103. }
  1104. if (!isset($params['number']) || $params['number'] === '' || $params['number'] === null) {
  1105. $this->error('拆分工序数量不能为空');
  1106. }
  1107. if (empty($params['sys_id'])) {
  1108. $this->error('操作人不能为空');
  1109. }
  1110. $splitCount = intval($params['number']);
  1111. $operTime = !empty($params['sys_rq']) ? $params['sys_rq'] : date('Y-m-d H:i:s');
  1112. $processId = intval($params['id']);
  1113. $process = Db::table('工单_基础工艺资料')
  1114. ->where('id', $processId)
  1115. ->whereNull('del_rq')
  1116. ->find();
  1117. if (empty($process)) {
  1118. $this->error('工序不存在');
  1119. }
  1120. $workorder = $process['work_order'];
  1121. if ($splitCount === 0) {
  1122. $children = Db::table('工单_基础工艺资料')
  1123. ->where('pid', $processId)
  1124. ->whereNull('del_rq')
  1125. ->field('id,process_code')
  1126. ->select();
  1127. $logList = [
  1128. [
  1129. 'order_no' => $workorder,
  1130. 'process_code' => $process['process_code'],
  1131. 'change_field' => '状态',
  1132. 'old_value' => '1',
  1133. 'new_value' => '0',
  1134. 'oper_type' => '取消拆分工序',
  1135. 'oper_user_name' => $params['sys_id'],
  1136. 'oper_time' => $operTime,
  1137. ],
  1138. ];
  1139. foreach ($children as $child) {
  1140. $logList[] = [
  1141. 'order_no' => $workorder,
  1142. 'process_code' => $child['process_code'],
  1143. 'change_field' => '工序拆分',
  1144. 'old_value' => (string)$process['process_code'],
  1145. 'new_value' => '删除子工序',
  1146. 'oper_type' => '取消拆分工序',
  1147. 'oper_user_name' => $params['sys_id'],
  1148. 'oper_time' => $operTime,
  1149. ];
  1150. }
  1151. Db::startTrans();
  1152. try {
  1153. if (!empty($children)) {
  1154. $deleteResult = Db::table('工单_基础工艺资料')
  1155. ->where('pid', $processId)
  1156. ->whereNull('del_rq')
  1157. ->update(['del_rq' => $operTime]);
  1158. if ($deleteResult === false) {
  1159. throw new \Exception('删除子工序失败');
  1160. }
  1161. }
  1162. $updateResult = Db::table('工单_基础工艺资料')
  1163. ->where('id', $processId)
  1164. ->whereNull('del_rq')
  1165. ->update(['status' => 0]);
  1166. if ($updateResult === false) {
  1167. throw new \Exception('恢复母工序状态失败');
  1168. }
  1169. if (!empty($logList)) {
  1170. $logResult = Db::name('work_order_operation_log')->insertAll($logList);
  1171. if ($logResult === false || $logResult === 0) {
  1172. throw new \Exception('写入操作日志失败');
  1173. }
  1174. }
  1175. Db::commit();
  1176. } catch (\think\exception\HttpResponseException $e) {
  1177. throw $e;
  1178. } catch (\Exception $e) {
  1179. Db::rollback();
  1180. $this->error('取消拆分失败:' . $e->getMessage());
  1181. }
  1182. $this->success('取消拆分工序成功', ['count' => count($children)]);
  1183. return;
  1184. }
  1185. if ($splitCount < 1) {
  1186. $this->error('拆分工序数量必须大于0');
  1187. }
  1188. if (!in_array($process['status'], [0, '0'], true)) {
  1189. $this->error('工序不存在或已确认拆分');
  1190. }
  1191. $processCodes = Db::table('工单_基础工艺资料')
  1192. ->where('work_order', $workorder)
  1193. ->whereNull('del_rq')
  1194. ->column('process_code');
  1195. $maxNumeric = 0;
  1196. foreach ($processCodes as $code) {
  1197. if (preg_match('/^(\d+)/', (string)$code, $match)) {
  1198. $num = intval($match[1]);
  1199. if ($num > $maxNumeric) {
  1200. $maxNumeric = $num;
  1201. }
  1202. }
  1203. }
  1204. $baseCode = $maxNumeric + 1;
  1205. $splitFields = ['standard_hour', 'standard_minutes', 'standard_score', 'money'];
  1206. $splitValues = [];
  1207. foreach ($splitFields as $field) {
  1208. $splitValues[$field] = $this->splitNumericValue($process[$field], $splitCount);
  1209. }
  1210. $insertList = [];
  1211. for ($i = 1; $i <= $splitCount; $i++) {
  1212. $newProcessCode = $baseCode + $i - 1;
  1213. $exists = Db::table('工单_基础工艺资料')
  1214. ->where('work_order', $workorder)
  1215. ->where('process_code', $newProcessCode)
  1216. ->whereNull('del_rq')
  1217. ->count();
  1218. if ($exists > 0) {
  1219. $this->error('工序编号已存在:' . $newProcessCode);
  1220. }
  1221. $insertList[] = [
  1222. 'work_order' => $workorder,
  1223. 'part_code' => $process['part_code'],
  1224. 'part_name' => isset($process['part_name']) ? $process['part_name'] : '',
  1225. 'process_code' => $newProcessCode,
  1226. 'process_name' => $process['process_name'] . '-' . $i,
  1227. 'big_process' => $process['big_process'],
  1228. 'standard_hour' => $splitValues['standard_hour'][$i - 1],
  1229. 'standard_minutes' => $splitValues['standard_minutes'][$i - 1],
  1230. 'standard_score' => $splitValues['standard_score'][$i - 1],
  1231. 'money' => $splitValues['money'][$i - 1],
  1232. 'coefficient' => isset($process['coefficient']) && $process['coefficient'] !== '' && $process['coefficient'] !== null
  1233. ? $process['coefficient'] : 'C',
  1234. 'remark' => isset($process['remark']) && $process['remark'] !== null ? $process['remark'] : '',
  1235. 'status' => 0,
  1236. 'pid' => $processId,
  1237. 'sys_id' => $params['sys_id'],
  1238. 'sys_rq' => $operTime,
  1239. ];
  1240. }
  1241. $logList = [
  1242. [
  1243. 'order_no' => $workorder,
  1244. 'process_code' => $process['process_code'],
  1245. 'change_field' => '状态',
  1246. 'old_value' => '0',
  1247. 'new_value' => '1',
  1248. 'oper_type' => '确认拆分工序',
  1249. 'oper_user_name' => $params['sys_id'],
  1250. 'oper_time' => $operTime,
  1251. ],
  1252. ];
  1253. foreach ($insertList as $row) {
  1254. $logList[] = [
  1255. 'order_no' => $workorder,
  1256. 'process_code' => $row['process_code'],
  1257. 'change_field' => '工序拆分',
  1258. 'old_value' => (string)$process['process_code'],
  1259. 'new_value' => sprintf(
  1260. '秒:%s,分:%s,定额分:%s,金额:%s',
  1261. $row['standard_hour'],
  1262. $row['standard_minutes'],
  1263. $row['standard_score'],
  1264. $row['money']
  1265. ),
  1266. 'oper_type' => '确认拆分工序',
  1267. 'oper_user_name' => $params['sys_id'],
  1268. 'oper_time' => $operTime,
  1269. ];
  1270. }
  1271. Db::startTrans();
  1272. try {
  1273. $updateResult = Db::table('工单_基础工艺资料')
  1274. ->where('id', $process['id'])
  1275. ->where('status', 'in', [0, '0'])
  1276. ->update(['status' => 1]);
  1277. if ($updateResult === false) {
  1278. throw new \Exception('更新母工序状态失败');
  1279. }
  1280. if ($updateResult === 0) {
  1281. throw new \Exception('母工序状态已变更,请刷新后重试');
  1282. }
  1283. $insertResult = Db::name('工单_基础工艺资料')->insertAll($insertList);
  1284. if ($insertResult === false || $insertResult === 0) {
  1285. throw new \Exception('拆分工序新增失败');
  1286. }
  1287. $logResult = Db::name('work_order_operation_log')->insertAll($logList);
  1288. if ($logResult === false || $logResult === 0) {
  1289. throw new \Exception('写入操作日志失败,请确认日志表已包含 process_code 字段');
  1290. }
  1291. Db::commit();
  1292. } catch (\think\exception\HttpResponseException $e) {
  1293. throw $e;
  1294. } catch (\Exception $e) {
  1295. Db::rollback();
  1296. $this->error('修改失败:' . $e->getMessage());
  1297. }
  1298. $this->success('确认拆分工序成功', ['count' => $splitCount]);
  1299. }
  1300. /**
  1301. * 按拆分数量分配数值:不能整除时第一条为剩余后的最大值,其余为向下取整的平均值
  1302. * @param mixed $total
  1303. * @param int $count
  1304. * @return array
  1305. */
  1306. private function splitNumericValue($total, $count)
  1307. {
  1308. $count = intval($count);
  1309. if ($count <= 0) {
  1310. return [];
  1311. }
  1312. $total = floatval($total);
  1313. $avg = floor($total / $count * 100) / 100;
  1314. $first = round($total - $avg * ($count - 1), 2);
  1315. $values = [$first];
  1316. for ($i = 1; $i < $count; $i++) {
  1317. $values[] = $avg;
  1318. }
  1319. return $values;
  1320. }
  1321. /**
  1322. * 根据款号查询工单信息
  1323. * @param search 搜索内容
  1324. * @return array
  1325. * @throws \think\db\exception\DataNotFoundException
  1326. * @throws \think\db\exception\ModelNotFoundException
  1327. * @throws \think\exception\DbException
  1328. */
  1329. public function getWorkOrderInfoByStyle()
  1330. {
  1331. if (!$this->request->isGet()) {
  1332. $this->error('请求方法错误');
  1333. }
  1334. $params = $this->request->param();
  1335. if (empty($params['search'])) {
  1336. $this->error('搜索内容不能为空');
  1337. }
  1338. $where = [
  1339. 'a.订单编号|a.生产款号|a.款式' => ['like', '%' . $params['search'] . '%'],
  1340. 'a.Mod_rq' => null,
  1341. ];
  1342. $workOrder = db('工单_基本资料')->alias('a')
  1343. ->join('工单_基础工艺资料 b', 'a.订单编号 = b.work_order')
  1344. ->field('a.订单编号,a.生产款号,a.款式,a.客户编号')
  1345. ->where($where)
  1346. ->group('a.订单编号')
  1347. ->select();
  1348. if (empty($workOrder)) {
  1349. $this->error('未找到工单信息');
  1350. }
  1351. $this->success('成功', $workOrder);
  1352. }
  1353. /**
  1354. * 复制目标工单的部件与工艺到当前工单
  1355. * @param target_workorder 目标工单编号
  1356. * @param current_workorder 当前工单编号
  1357. * @param sys_id 操作人
  1358. */
  1359. public function copyProcessToCurrentWorkOrder()
  1360. {
  1361. if (!$this->request->isPost()) {
  1362. $this->error('请求方法错误');
  1363. }
  1364. $params = $this->request->post();
  1365. if (empty($params['target_workorder'])) {
  1366. $this->error('目标工单编号不能为空');
  1367. }
  1368. if (empty($params['current_workorder'])) {
  1369. $this->error('当前工单编号不能为空');
  1370. }
  1371. if (empty($params['sys_id'])) {
  1372. $this->error('操作人不能为空');
  1373. }
  1374. if ($params['target_workorder'] === $params['current_workorder']) {
  1375. $this->error('目标工单与当前工单不能相同');
  1376. }
  1377. if (!$this->workOrderExists($params['target_workorder'])) {
  1378. $this->error('目标工单不存在');
  1379. }
  1380. if (!$this->workOrderExists($params['current_workorder'])) {
  1381. $this->error('当前工单不存在');
  1382. }
  1383. $targetWorkOrderInfo = $this->getWorkOrderInfo($params['target_workorder']);
  1384. if (empty($targetWorkOrderInfo['partInfo']) && empty($targetWorkOrderInfo['processInfo'])) {
  1385. $this->error('目标工单没有可复制的部件或工艺');
  1386. }
  1387. $currentWorkOrderInfo = $this->getWorkOrderInfo($params['current_workorder']);
  1388. $oldPartCount = count($currentWorkOrderInfo['partInfo']);
  1389. $oldProcessCount = count($currentWorkOrderInfo['processInfo']);
  1390. $now = date('Y-m-d H:i:s');
  1391. $partInsertList = $this->buildWorkOrderPartCopyRows(
  1392. $targetWorkOrderInfo['partInfo'],
  1393. $params['current_workorder'],
  1394. $params['sys_id'],
  1395. $now
  1396. );
  1397. $processInsertList = $this->buildWorkOrderProcessCopyRows(
  1398. $targetWorkOrderInfo['processInfo'],
  1399. $params['current_workorder'],
  1400. $params['sys_id'],
  1401. $now
  1402. );
  1403. $logData = [
  1404. 'order_no' => $params['current_workorder'],
  1405. 'change_field' => $params['target_workorder'],
  1406. 'old_value' => sprintf('部件%d条,工艺%d条', $oldPartCount, $oldProcessCount),
  1407. 'new_value' => sprintf('部件%d条,工艺%d条', count($partInsertList), count($processInsertList)),
  1408. 'oper_type' => '复制工单工艺',
  1409. 'oper_user_name' => $params['sys_id'],
  1410. 'oper_time' => $now,
  1411. ];
  1412. Db::startTrans();
  1413. try {
  1414. if ($oldPartCount > 0 || $oldProcessCount > 0) {
  1415. $partDeleteResult = Db::table('工单_部件资料')
  1416. ->where('work_order', $params['current_workorder'])
  1417. ->whereNull('del_rq')
  1418. ->update(['del_rq' => $now]);
  1419. if ($partDeleteResult === false) {
  1420. throw new \Exception('清除当前工单部件失败');
  1421. }
  1422. $processDeleteResult = Db::table('工单_基础工艺资料')
  1423. ->where('work_order', $params['current_workorder'])
  1424. ->whereNull('del_rq')
  1425. ->update(['del_rq' => $now]);
  1426. if ($processDeleteResult === false) {
  1427. throw new \Exception('清除当前工单工艺失败');
  1428. }
  1429. }
  1430. if (!empty($partInsertList)) {
  1431. $partInsertResult = Db::name('工单_部件资料')->insertAll($partInsertList);
  1432. if ($partInsertResult === false || $partInsertResult === 0) {
  1433. throw new \Exception('复制部件失败');
  1434. }
  1435. }
  1436. if (!empty($processInsertList)) {
  1437. $processInsertResult = Db::name('工单_基础工艺资料')->insertAll($processInsertList);
  1438. if ($processInsertResult === false || $processInsertResult === 0) {
  1439. throw new \Exception('复制工艺失败');
  1440. }
  1441. }
  1442. $logResult = Db::name('work_order_operation_log')->insert($logData);
  1443. if ($logResult === false || $logResult === 0) {
  1444. throw new \Exception('写入操作日志失败');
  1445. }
  1446. Db::commit();
  1447. } catch (\think\exception\HttpResponseException $e) {
  1448. throw $e;
  1449. } catch (\Exception $e) {
  1450. Db::rollback();
  1451. $this->error('复制失败:' . $e->getMessage());
  1452. }
  1453. $this->success('复制成功', [
  1454. 'part_count' => count($partInsertList),
  1455. 'process_count' => count($processInsertList),
  1456. ]);
  1457. }
  1458. /**
  1459. * 判断工单是否存在
  1460. * @param string $workorder
  1461. * @return bool
  1462. */
  1463. private function workOrderExists($workorder)
  1464. {
  1465. return Db::table('工单_基本资料')
  1466. ->where('订单编号', $workorder)
  1467. ->whereNull('Mod_rq')
  1468. ->count() > 0;
  1469. }
  1470. /**
  1471. * 查询工单部件与工艺(未删除、工艺仅 status=0)
  1472. * @param string $workorder
  1473. * @return array
  1474. */
  1475. private function getWorkOrderInfo($workorder)
  1476. {
  1477. $partInfo = db('工单_部件资料')
  1478. ->where('work_order', $workorder)
  1479. ->whereNull('del_rq')
  1480. ->field('part_code,part_name,remark,part_type,status')
  1481. ->order('part_code')
  1482. ->select();
  1483. $processInfo = db('工单_基础工艺资料')
  1484. ->where('work_order', $workorder)
  1485. ->whereNull('del_rq')
  1486. ->where('status', 0)
  1487. ->field('part_code,part_name,process_code,process_name,standard_hour,standard_minutes,
  1488. standard_score,money,coefficient,remark,big_process')
  1489. ->order('process_code')
  1490. ->select();
  1491. return [
  1492. 'partInfo' => $partInfo ?: [],
  1493. 'processInfo' => $processInfo ?: [],
  1494. ];
  1495. }
  1496. /**
  1497. * 组装待复制的部件数据
  1498. */
  1499. private function buildWorkOrderPartCopyRows($partList, $workOrder, $sysId, $operTime)
  1500. {
  1501. $rows = [];
  1502. foreach ($partList as $part) {
  1503. $rows[] = [
  1504. 'work_order' => $workOrder,
  1505. 'part_code' => $part['part_code'],
  1506. 'part_name' => $part['part_name'],
  1507. 'remark' => isset($part['remark']) ? $part['remark'] : '',
  1508. 'part_type' => isset($part['part_type']) ? $part['part_type'] : '',
  1509. 'status' => 0,
  1510. 'sys_id' => $sysId,
  1511. 'sys_rq' => $operTime,
  1512. ];
  1513. }
  1514. return $rows;
  1515. }
  1516. /**
  1517. * 组装待复制的工艺数据
  1518. */
  1519. private function buildWorkOrderProcessCopyRows($processList, $workOrder, $sysId, $operTime)
  1520. {
  1521. $rows = [];
  1522. foreach ($processList as $process) {
  1523. $rows[] = [
  1524. 'work_order' => $workOrder,
  1525. 'part_code' => $process['part_code'],
  1526. 'part_name' => isset($process['part_name']) ? $process['part_name'] : '',
  1527. 'process_code' => $process['process_code'],
  1528. 'process_name' => $process['process_name'],
  1529. 'big_process' => $process['big_process'],
  1530. 'standard_hour' => $process['standard_hour'],
  1531. 'standard_minutes' => $process['standard_minutes'],
  1532. 'standard_score' => $process['standard_score'],
  1533. 'money' => $process['money'],
  1534. 'coefficient' => $process['coefficient'],
  1535. 'remark' => isset($process['remark']) ? $process['remark'] : '',
  1536. 'status' => 0,
  1537. 'pid' => 0,
  1538. 'sys_id' => $sysId,
  1539. 'sys_rq' => $operTime,
  1540. ];
  1541. }
  1542. return $rows;
  1543. }
  1544. /**
  1545. * 批量新增工序资料(请求体为 JSON 数组,每条含 work_order 等字段)
  1546. * @param processList 工序信息
  1547. * @return array
  1548. * @throws \think\db\exception\DataNotFoundException
  1549. * @throws \think\db\exception\ModelNotFoundException
  1550. * @throws \think\exception\DbException
  1551. **/
  1552. public function batchAddProcess()
  1553. {
  1554. if (!$this->request->isPost()) {
  1555. $this->error('请求方法错误');
  1556. }
  1557. $processList = $this->parseBatchProcessList();
  1558. if (empty($processList)) {
  1559. $this->error('工艺数据不能为空');
  1560. }
  1561. $requiredFields = [
  1562. 'work_order' => '工单编号',
  1563. 'part_code' => '部件编号',
  1564. 'part_name' => '部件名称',
  1565. 'process_code' => '工序编号',
  1566. 'process_name' => '工序名称',
  1567. 'big_process' => '大工序',
  1568. 'standard_hour' => '秒',
  1569. 'standard_score' => '定额分',
  1570. 'coefficient' => '难度系数',
  1571. 'standard_minutes' => '分',
  1572. 'money' => '金额',
  1573. 'remark' => '备注',
  1574. 'sys_id' => '操作人员',
  1575. ];
  1576. $workorder = '';
  1577. $now = date('Y-m-d H:i:s');
  1578. $insertList = [];
  1579. $processCodeMap = [];
  1580. $newBatchStatus0Sum = 0;
  1581. foreach ($processList as $index => $process) {
  1582. $rowNo = $index + 1;
  1583. if (!is_array($process)) {
  1584. $this->error('第' . $rowNo . '条工艺数据格式错误');
  1585. }
  1586. // foreach ($requiredFields as $field => $label) {
  1587. // if (!isset($process[$field]) || $process[$field] === '' || $process[$field] === null) {
  1588. // $this->error('第' . $rowNo . '条' . $label . '不能为空');
  1589. // }
  1590. // }
  1591. $itemWorkOrder = trim($process['work_order']);
  1592. if ($workorder === '') {
  1593. $workorder = $itemWorkOrder;
  1594. } elseif ($itemWorkOrder !== $workorder) {
  1595. $this->error('第' . $rowNo . '条工单编号与其它数据不一致');
  1596. }
  1597. $processCode = (string)$process['process_code'];
  1598. if (isset($processCodeMap[$processCode])) {
  1599. $this->error('批量数据中存在重复的工序编号:' . $processCode);
  1600. }
  1601. $processCodeMap[$processCode] = true;
  1602. $status = isset($process['status']) ? $process['status'] : 0;
  1603. if (in_array($status, [0, '0'], true)) {
  1604. $newBatchStatus0Sum += floatval($process['standard_score']);
  1605. }
  1606. $insertList[] = [
  1607. 'work_order' => $workorder,
  1608. 'part_code' => $process['part_code'],
  1609. 'part_name' => $process['part_name'],
  1610. 'process_code' => $processCode,
  1611. 'process_name' => $process['process_name'],
  1612. 'big_process' => $process['big_process'],
  1613. 'standard_hour' => $process['standard_hour'],
  1614. 'standard_minutes' => $process['standard_minutes'],
  1615. 'standard_score' => $process['standard_score'],
  1616. 'coefficient' => $process['coefficient'],
  1617. 'money' => $process['money'],
  1618. 'remark' => $process['remark'],
  1619. 'status' => $status,
  1620. 'sys_id' => $process['sys_id'],
  1621. 'sys_rq' => $now,
  1622. ];
  1623. }
  1624. $existingCodes = Db::table('工单_基础工艺资料')
  1625. ->where('work_order', $workorder)
  1626. ->whereIn('process_code', array_keys($processCodeMap))
  1627. ->whereNull('del_rq')
  1628. ->column('process_code');
  1629. if (!empty($existingCodes)) {
  1630. $this->error('工序编号已存在:' . $existingCodes[0]);
  1631. }
  1632. $workOrder = Db::table('工单_基本资料')
  1633. ->where('订单编号', $workorder)
  1634. ->where('Mod_rq', null)
  1635. ->field('订单编号,计划制造工分')
  1636. ->find();
  1637. if (empty($workOrder)) {
  1638. $this->error('工单不存在');
  1639. }
  1640. $existingTotal = Db::table('工单_基础工艺资料')
  1641. ->where('work_order', $workorder)
  1642. ->where('status', 0)
  1643. ->whereNull('del_rq')
  1644. ->sum('standard_score');
  1645. $existingTotal = $existingTotal ? floatval($existingTotal) : 0;
  1646. if ($existingTotal + $newBatchStatus0Sum > floatval($workOrder['计划制造工分'])) {
  1647. $this->error('工序定额分和大于制造工分,请确认之后再修改');
  1648. }
  1649. Db::startTrans();
  1650. try {
  1651. $insertResult = Db::name('工单_基础工艺资料')->insertAll($insertList);
  1652. if ($insertResult === false || $insertResult === 0) {
  1653. throw new \Exception('批量新增工序失败');
  1654. }
  1655. Db::commit();
  1656. } catch (\think\exception\HttpResponseException $e) {
  1657. throw $e;
  1658. } catch (\Exception $e) {
  1659. Db::rollback();
  1660. $this->error('批量新增工序资料失败');
  1661. }
  1662. $this->success('批量新增工序资料成功', ['count' => count($insertList)]);
  1663. }
  1664. /**
  1665. * 解析批量工序请求体(form 数组 / JSON 数组 / 单字段 JSON 字符串)
  1666. * @return array
  1667. */
  1668. private function parseBatchProcessList()
  1669. {
  1670. $params = $this->request->post();
  1671. if (isset($params[0]) && is_array($params[0])) {
  1672. return $params;
  1673. }
  1674. $decoded = json_decode($this->request->getInput(), true);
  1675. if (is_array($decoded) && isset($decoded[0]) && is_array($decoded[0])) {
  1676. return $decoded;
  1677. }
  1678. $jsonStr = is_string($params) ? $params : (count($params) === 1 ? reset($params) : null);
  1679. if (is_string($jsonStr)) {
  1680. $decoded = json_decode($jsonStr, true);
  1681. return is_array($decoded) ? $decoded : [];
  1682. }
  1683. return [];
  1684. }
  1685. /**
  1686. * 工序产量核查:按工单查询报工汇总(工单信息 / 工序列表 / 人员报工明细)
  1687. * @param workorder 工单编号
  1688. * @param process 工序(可选,按big_process过滤)
  1689. * @return array
  1690. */
  1691. public function checkProcessProduction()
  1692. {
  1693. if (!$this->request->isGet()) {
  1694. $this->error('请求方法错误');
  1695. }
  1696. $params = $this->request->param();
  1697. if (empty($params['workorder'])) {
  1698. $this->error('工单编号不能为空');
  1699. }
  1700. $workorder = $params['workorder'];
  1701. $processFilter = $params['process'] ?? '';
  1702. // 1. 查询工单基本信息
  1703. $workOrderInfo = Db::name('工单_基本资料')
  1704. ->where('订单编号', $workorder)
  1705. ->whereNull('Mod_rq')
  1706. ->field('订单编号, 生产款号, 款式, 订单数量')
  1707. ->find();
  1708. if (empty($workOrderInfo)) {
  1709. $this->error('工单不存在');
  1710. }
  1711. // 2. 查询工单基础工艺资料(所有工序)
  1712. $processQuery = Db::name('工单_基础工艺资料')
  1713. ->where('work_order', $workorder)
  1714. ->whereNull('del_rq');
  1715. // 如果传了工序参数,按 big_process 过滤
  1716. if (!empty($processFilter)) {
  1717. $processQuery->where('big_process', $processFilter);
  1718. }
  1719. $processList = $processQuery
  1720. ->field('process_code as 工序号, process_name as 工序名称, part_name as 部件名称')
  1721. ->select();
  1722. if (empty($processList)) {
  1723. $this->error('该工单暂无工艺资料');
  1724. }
  1725. // 按工序号数字排序
  1726. usort($processList, function ($a, $b) {
  1727. return intval($a['工序号']) - intval($b['工序号']);
  1728. });
  1729. // 3. 查询设备_工分计酬中的报工数据,补齐staff_no字段
  1730. $reportList = Db::name('设备_工分计酬')->alias('a')
  1731. ->where('a.work_order', $workorder)
  1732. ->whereNull('a.del_rq')
  1733. ->field('a.process_code as 工序号, a.process_name as 工序名称, a.staff_no as 员工号, a.staff_name as 员工姓名,
  1734. a.sys_id as 小组,
  1735. SUM(a.number) as 数量, MIN(a.date) as 开工日期, MAX(a.date) as 完工日期,
  1736. SUM(a.standard_score) as 工分, SUM(a.production_score) as 工时, SUM(a.salary) as 工资')
  1737. ->group('a.process_code, a.process_name, a.staff_no, a.staff_name')
  1738. ->select();
  1739. // 按工序号、员工号数字排序
  1740. if (!empty($reportList)) {
  1741. usort($reportList, function ($a, $b) {
  1742. $codeCmp = intval($a['工序号']) - intval($b['工序号']);
  1743. if ($codeCmp !== 0) return $codeCmp;
  1744. return intval($a['员工号'] ?? 0) - intval($b['员工号'] ?? 0);
  1745. });
  1746. }
  1747. // 4. 组装返回数据
  1748. $manufactureQty = $workOrderInfo['订单数量'] ?? 0;
  1749. $data = [
  1750. '订单编号' => $workOrderInfo['订单编号'],
  1751. '生产款号' => $workOrderInfo['生产款号'],
  1752. '款式' => $workOrderInfo['款式'],
  1753. '订单数量' => $manufactureQty,
  1754. '工序列表' => [],
  1755. ];
  1756. // 5. 构建工序映射【核心修复:仅使用工序号匹配,不再受工序名称不一致影响】
  1757. $processMap = [];
  1758. foreach ($processList as $process) {
  1759. $processCode = trim((string)$process['工序号']);
  1760. if (!isset($processMap[$processCode])) {
  1761. $processMap[$processCode] = [
  1762. '工序号' => $process['工序号'],
  1763. '工序名称' => $process['工序名称'],
  1764. '部件名称' => $process['部件名称'] ?? '',
  1765. '制造数量' => $manufactureQty,
  1766. '累计完成数' => 0,
  1767. '未完成数' => floatval($manufactureQty),
  1768. 'staffs' => [],
  1769. ];
  1770. }
  1771. }
  1772. // 6. 将报工数据合并到对应工序
  1773. if (!empty($reportList)) {
  1774. foreach ($reportList as $report) {
  1775. $processCode = trim((string)$report['工序号']);
  1776. if (isset($processMap[$processCode])) {
  1777. $startDate = !empty($report['开工日期']) ? date('Y-m-d', strtotime($report['开工日期'])) : '';
  1778. $endDate = !empty($report['完工日期']) ? date('Y-m-d', strtotime($report['完工日期'])) : '';
  1779. $processMap[$processCode]['staffs'][] = [
  1780. '员工姓名' => $report['员工姓名'],
  1781. '小组' => $report['小组'],
  1782. '数量' => floatval($report['数量']),
  1783. '开工日期' => $startDate,
  1784. '完工日期' => $endDate,
  1785. '工分' => round(floatval($report['工分']), 4),
  1786. '工时' => round(floatval($report['工时']), 4),
  1787. '工资' => round(floatval($report['工资']), 4),
  1788. ];
  1789. }
  1790. }
  1791. }
  1792. // 7. 计算每道工序累计完成数、未完成数
  1793. foreach ($processMap as $processCode => $process) {
  1794. $completedQty = 0;
  1795. foreach ($process['staffs'] as $staff) {
  1796. $completedQty += floatval($staff['数量']);
  1797. }
  1798. $processMap[$processCode]['累计完成数'] = $completedQty;
  1799. $processMap[$processCode]['未完成数'] = floatval($manufactureQty) - $completedQty;
  1800. }
  1801. $resultList = array_values($processMap);
  1802. // 最终按工序号数字排序
  1803. usort($resultList, function ($a, $b) {
  1804. return intval($a['工序号']) - intval($b['工序号']);
  1805. });
  1806. $data['工序列表'] = $resultList;
  1807. $this->success('成功', $data);
  1808. }
  1809. }