WorkOrderProcess.php 68 KB

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