WorkOrderProcess.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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'] = 1;
  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. 'process_code' => $code,
  377. 'process_name' => $params['process_name'],
  378. 'big_process' => $params['big_process'],
  379. 'standard_hour' => $params['standard_hour'],
  380. 'standard_score' => $params['standard_score'],
  381. 'coefficient' => $params['coefficient'],
  382. 'remark' => $params['remark'],
  383. 'sys_id' => $params['sys_id'],
  384. 'sys_rq' => date('Y-m-d H:i:s'),
  385. ];
  386. $result = db('工单_基础工艺资料')->insert($data);
  387. if ($result === false) {
  388. $this->error('新增失败');
  389. }
  390. $this->success('新增成功');
  391. }
  392. /**
  393. * 更新工单工艺资料
  394. * @param id 工艺ID
  395. * @param mod_id 修改人
  396. * @param process_name 工艺名称
  397. * @param big_process 大工艺
  398. * @param standard_hour 标准工时
  399. * @param standard_score 标准公分
  400. * @param coefficient 系数
  401. * @param remark 备注
  402. */
  403. public function UpdateProcess()
  404. {
  405. if (!$this->request->isPost()) {
  406. $this->error('请求方法错误');
  407. }
  408. $params = $this->request->post();
  409. if (empty($params['id'])) {
  410. $this->error('工艺ID不能为空');
  411. }
  412. if (empty($params['mod_id'])) {
  413. $this->error('修改人不能为空');
  414. }
  415. if (empty($params['process_name'])) {
  416. $this->error('工艺名称不能为空');
  417. }
  418. if (empty($params['big_process'])) {
  419. $this->error('大工艺不能为空');
  420. }
  421. if (empty($params['standard_hour'])) {
  422. $this->error('秒不能为空');
  423. }
  424. if (empty($params['standard_minutes'])) {
  425. $this->error('分钟不能为空');
  426. }
  427. if (empty($params['standard_score'])) {
  428. $this->error('定额分不能为空');
  429. }
  430. if (empty($params['money'])) {
  431. $this->error('金额不能为空');
  432. }
  433. if (!isset($params['coefficient']) || $params['coefficient'] === '') {
  434. $params['coefficient'] = 'C';
  435. }
  436. $process = Db::table('工单_基础工艺资料')
  437. ->where('id', intval($params['id']))
  438. ->whereNull('del_rq')
  439. ->find();
  440. if (empty($process)) {
  441. $this->error('工艺不存在');
  442. }
  443. $data = [
  444. 'part_code' => $params['part_code'],
  445. 'part_name' => $params['part_name'],
  446. 'standard_minutes' => $params['standard_minutes'],
  447. 'money' => $params['money'],
  448. 'remark' => $params['remark'],
  449. 'process_name' => $params['process_name'],
  450. 'big_process' => $params['big_process'],
  451. 'standard_hour' => $params['standard_hour'],
  452. 'standard_score' => $params['standard_score'],
  453. 'coefficient' => $params['coefficient'],
  454. 'mod_id' => $params['mod_id'],
  455. 'mod_rq' => date('Y-m-d H:i:s'),
  456. ];
  457. $fieldLabels = [
  458. 'part_code' => '部件编号',
  459. 'part_name' => '部件名称',
  460. 'process_code' => '工序编号',
  461. 'standard_minutes' => '分',
  462. 'money' => '金额',
  463. 'remark' => '备注',
  464. 'process_name' => '工序名称',
  465. 'big_process' => '大工序',
  466. 'standard_hour' => '秒',
  467. 'standard_score' => '定额分',
  468. 'coefficient' => '难度系数',
  469. ];
  470. $operTime = date('Y-m-d H:i:s');
  471. $logList = [];
  472. foreach ($fieldLabels as $field => $label) {
  473. $oldValue = isset($process[$field]) ? $process[$field] : '';
  474. $newValue = $data[$field];
  475. if ((string)$oldValue === (string)$newValue) {
  476. continue;
  477. }
  478. $logList[] = [
  479. 'order_no' => $process['work_order'],
  480. 'process_code' => $process['process_code'],
  481. 'change_field' => $label,
  482. 'old_value' => $oldValue === null ? '' : (string)$oldValue,
  483. 'new_value' => (string)$newValue,
  484. 'oper_type' => '修改工单工艺',
  485. 'oper_user_name' => $params['mod_id'],
  486. 'oper_time' => $operTime,
  487. ];
  488. }
  489. if (empty($logList)) {
  490. $this->error('未找到可更新的工艺或数据无变化');
  491. }
  492. $standardScoreChanged = (string)$process['standard_score'] !== (string)$params['standard_score'];
  493. if ($standardScoreChanged && (string)$process['status'] === '0') {
  494. $workOrder = Db::table('工单_基本资料')
  495. ->where('订单编号', $process['work_order'])
  496. ->where('Mod_rq', null)
  497. ->field('计划制造工分')
  498. ->find();
  499. if (empty($workOrder)) {
  500. $this->error('工单不存在');
  501. }
  502. $totalStandardScore = Db::table('工单_基础工艺资料')
  503. ->where('work_order', $process['work_order'])
  504. ->where('status', 0)
  505. ->whereNull('del_rq')
  506. ->sum('standard_score');
  507. $totalStandardScore = $totalStandardScore ? floatval($totalStandardScore) : 0;
  508. $newTotal = $totalStandardScore - floatval($process['standard_score']) + floatval($params['standard_score']);
  509. if ($newTotal > floatval($workOrder['计划制造工分'])) {
  510. $this->error('工序定额分和大于制造工分,请确认之后再修改');
  511. }
  512. }
  513. Db::startTrans();
  514. try {
  515. $updateResult = Db::table('工单_基础工艺资料')
  516. ->where('id', intval($params['id']))
  517. ->whereNull('del_rq')
  518. ->update($data);
  519. if ($updateResult === false) {
  520. throw new \Exception('更新工艺失败');
  521. }
  522. if ($updateResult === 0) {
  523. throw new \Exception('未找到可更新的工艺');
  524. }
  525. $logResult = Db::name('work_order_operation_log')->insertAll($logList);
  526. if ($logResult === false) {
  527. throw new \Exception('写入操作日志失败');
  528. }
  529. Db::commit();
  530. } catch (\Exception $e) {
  531. Db::rollback();
  532. $this->error('更新失败');
  533. }
  534. $this->success('更新成功');
  535. }
  536. /**
  537. * 工单工艺资料软删除
  538. * @param id 工艺ID
  539. */
  540. public function DeleteProcess()
  541. {
  542. if (!$this->request->isPost()) {
  543. $this->error('请求方法错误');
  544. }
  545. $params = $this->request->post();
  546. if (empty($params['id'])) {
  547. $this->error('工艺ID不能为空');
  548. }
  549. $ids = explode(',', $params['id']);
  550. $ids = array_filter(array_map('intval', $ids));
  551. if (empty($ids)) {
  552. $this->error('无效的工艺ID');
  553. }
  554. $result = db('工单_基础工艺资料')
  555. ->whereIn('id', $ids)
  556. ->whereNull('del_rq')
  557. ->update(['del_rq' => date('Y-m-d H:i:s')]);
  558. if ($result === false) {
  559. $this->error('删除失败');
  560. }
  561. if ($result === 0) {
  562. $this->error('未找到可删除的工艺');
  563. }
  564. $this->success('删除成功');
  565. }
  566. /**
  567. * 工单工艺复制
  568. * @param workorder 工单编号
  569. * @param product_code 产品编号
  570. * @param sys_id 操作人
  571. * @return array
  572. * @throws \think\db\exception\DataNotFoundException
  573. * @throws \think\db\exception\ModelNotFoundException
  574. * @throws \think\exception\DbException
  575. * @throws \think\exception\PDOException
  576. */
  577. public function workorderprocessCopy()
  578. {
  579. if (!$this->request->isGet()) {
  580. $this->error('请求方法错误');
  581. }
  582. $params = $this->request->param();
  583. if (empty($params['workorder'])) {
  584. $this->error('工单编号不能为空');
  585. }
  586. if (empty($params['product_code'])) {
  587. $this->error('产品编号不能为空');
  588. }
  589. if (empty($params['sys_id'])) {
  590. $this->error('操作人不能为空');
  591. }
  592. $now = date('Y-m-d H:i:s');
  593. $productParts = db('产品_部件资料')
  594. ->where('product_code', $params['product_code'])
  595. ->where('mod_rq', null)
  596. ->field('part_sort as part_code,part_name')
  597. ->select();
  598. if (empty($productParts)) {
  599. $this->error('该产品没有有效的部件');
  600. }
  601. $productProcesses = db('产品_工艺资料')
  602. ->where('product_code', $params['product_code'])
  603. ->field('part_sort as part_code,part_name,gy_sort as process_code,gy_name as process_name,
  604. big_process,standard_hour,standard_score,difficulty_coef as coefficient')
  605. ->select();
  606. if (empty($productProcesses)) {
  607. $this->error('该产品没有工艺');
  608. }
  609. $workOrderParts = [];
  610. //配置工单部件数据
  611. foreach ($productParts as $value) {
  612. $workOrderParts[] = [
  613. 'work_order' => $params['workorder'],
  614. 'part_code' => $value['part_code'],
  615. 'part_name' => $value['part_name'],
  616. 'part_type' => '',
  617. 'remark' => '',
  618. 'status' => 1,
  619. 'sys_id' => $params['sys_id'],
  620. 'sys_rq' => $now,
  621. ];
  622. }
  623. $workOrderProcesses = [];
  624. //配置工单工艺数据
  625. foreach ($productProcesses as $value) {
  626. $workOrderProcesses[] = [
  627. 'work_order' => $params['workorder'],
  628. 'part_code' => $value['part_code'],
  629. 'part_name' => $value['part_name'],
  630. 'process_code' => $value['process_code'],
  631. 'process_name' => $value['process_name'],
  632. 'big_process' => $value['big_process'],
  633. 'standard_hour' => $value['standard_hour'],
  634. 'standard_score' => $value['standard_score'],
  635. 'coefficient' => $value['coefficient'],
  636. 'remark' => '',
  637. 'sys_id' => $params['sys_id'],
  638. 'sys_rq' => $now,
  639. ];
  640. }
  641. Db::startTrans();
  642. try {
  643. //删除数据库现有的工单部件数据
  644. Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
  645. //删除数据库现有的工单工艺数据
  646. Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
  647. //插入工单部件数据
  648. $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
  649. if ($partInsertCount === false) {
  650. throw new \Exception('工单部件复制失败');
  651. }
  652. //插入工单工艺数据
  653. $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
  654. if ($processInsertCount === false) {
  655. throw new \Exception('工单工艺复制失败');
  656. }
  657. //提交事务
  658. Db::commit();
  659. } catch (\Exception $e) {
  660. Db::rollback();
  661. $this->error('复制失败');
  662. }
  663. $this->success('复制成功');
  664. }
  665. /**
  666. * 查询产品类型
  667. * @ApiMethod (GET)
  668. * @param string $workorder 工单编号
  669. * @return array
  670. * @throws \think\db\exception\DataNotFoundException
  671. * @throws \think\db\exception\ModelNotFoundException
  672. * @throws \think\exception\DbException
  673. */
  674. public function getproducttype()
  675. {
  676. if(!$this->request->isGet()){
  677. $this->error('请求方法错误');
  678. }
  679. $params = $this->request->get();
  680. if (empty($params['product'])) {
  681. $this->error('产品类型不能为空');
  682. }
  683. $list = db('产品_基本资料')
  684. ->where('product_type', $params['product'])
  685. ->where('status', 1)
  686. ->field('product_code as 产品编号,product_type as 产品类型,product_name as 产品名称')
  687. ->select();
  688. if(empty($list)){
  689. $this->error('该产品类型没有产品');
  690. }
  691. $this->success('成功', $list);
  692. }
  693. /**
  694. * 工单工艺排序
  695. * @param workorder 工单编号
  696. * @param part_code 部件编号
  697. * @param process_code 工艺编号
  698. * @param process_name 工艺名称
  699. * @param big_process 大工艺
  700. * @param standard_hour 标准工时
  701. * @param standard_score 标准公分
  702. * @param coefficient 系数
  703. * @param remark 备注
  704. */
  705. public function sortProcess()
  706. {
  707. if (!$this->request->isPost()) {
  708. $this->error('请求方法错误');
  709. }
  710. $params = $this->request->post();
  711. if (empty($params)) {
  712. $this->error('参数不能为空');
  713. }
  714. if (!is_array($params)) {
  715. $this->error('参数格式错误');
  716. }
  717. Db::startTrans();
  718. try {
  719. foreach ($params as $value) {
  720. if (empty($value['id'])) {
  721. throw new \Exception('缺少工艺ID');
  722. }
  723. if (!isset($value['process_code']) || $value['process_code'] === '') {
  724. throw new \Exception('缺少工艺编号');
  725. }
  726. $result = db('工单_基础工艺资料')
  727. ->where('id', intval($value['id']))
  728. ->whereNull('del_rq')
  729. ->update(['process_code' => intval($value['process_code'])]);
  730. if ($result === false) {
  731. throw new \Exception('排序失败');
  732. }
  733. }
  734. Db::commit();
  735. } catch (\Exception $e) {
  736. Db::rollback();
  737. $this->error($e->getMessage());
  738. }
  739. $this->success('排序成功');
  740. }
  741. /**
  742. * 工单工艺excel导入
  743. * @param workorder 工单编号
  744. *
  745. */
  746. public function importProcess()
  747. {
  748. if (!$this->request->isPost()) {
  749. $this->error('请求方法错误');
  750. }
  751. $params = $this->request->post();
  752. if (empty($params['workorder'])) {
  753. $this->error('工单编号不能为空');
  754. }
  755. if (empty($params['sys_id'])) {
  756. $this->error('操作人不能为空');
  757. }
  758. $file = $this->request->file('file');
  759. if (!$file) {
  760. $this->error('文件不能为空');
  761. }
  762. $uploadDir = ROOT_PATH . 'public' . DS . 'uploads';
  763. $info = $file->validate(['size' => 1024 * 1024 * 10, 'ext' => 'xlsx,xls,csv,txt'])
  764. ->move($uploadDir);
  765. if (!$info) {
  766. $this->error($file->getError());
  767. }
  768. $filePath = $uploadDir . DS . $info->getSaveName();
  769. // 定额表:上方为标题与元数据,第 5 行表头,第 6 行起为数据
  770. $data = $this->readExcel($filePath, 5, 6);
  771. if (empty($data)) {
  772. $this->error('文件内容为空');
  773. }
  774. $seenSeq = [];
  775. foreach ($data as $row) {
  776. $seq = isset($row['序号']) ? $row['序号'] : null;
  777. if ($seq === null || $seq === '') {
  778. continue;
  779. }
  780. $seqKey = is_scalar($seq) ? (string)$seq : $seq;
  781. if (isset($seenSeq[$seqKey])) {
  782. $this->error('工序序号重复,请重新调整之后再上传');
  783. }
  784. $seenSeq[$seqKey] = true;
  785. }
  786. $now = date('Y-m-d H:i:s');
  787. $partNameToCode = [];
  788. $nextPartCode = 0;
  789. foreach ($data as $row) {
  790. $partName = isset($row['部件名称']) ? $row['部件名称'] : '';
  791. if ($partName === '' || isset($partNameToCode[$partName])) {
  792. continue;
  793. }
  794. $partNameToCode[$partName] = ++$nextPartCode;
  795. }
  796. $workOrderParts = [];
  797. $i = 0;
  798. foreach ($partNameToCode as $partName => $partCode) {
  799. $workOrderParts[$i++] = [
  800. 'work_order' => $params['workorder'],
  801. 'part_name' => $partName,
  802. 'part_code' => $partCode,
  803. 'part_type' => '',
  804. 'remark' => '',
  805. 'status' => 1,
  806. 'sys_id' => $params['sys_id'],
  807. 'sys_rq' => $now,
  808. ];
  809. }
  810. Db::startTrans();
  811. try {
  812. //删除数据库现有的工单部件数据
  813. Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
  814. //插入工单部件数据
  815. $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
  816. if ($partInsertCount === false) {
  817. throw new \Exception('工单部件导入失败');
  818. }
  819. //提交事务
  820. Db::commit();
  821. } catch (\Exception $e) {
  822. Db::rollback();
  823. $this->error($e->getMessage());
  824. }
  825. $workOrderProcesses = [];
  826. foreach ($data as $value) {
  827. $name = isset($value['部件名称']) ? $value['部件名称'] : '';
  828. $partCode = ($name !== '' && isset($partNameToCode[$name])) ? $partNameToCode[$name] : '';
  829. $workOrderProcesses[] = [
  830. 'work_order' => $params['workorder'],
  831. 'part_code' => $partCode,
  832. 'part_name' => $name,
  833. 'process_code' => $value['序号'],
  834. 'process_name' => $value['工序名称'],
  835. 'big_process' => $value['生产工序'],
  836. 'standard_hour' => $value['秒'],
  837. 'standard_minutes' => $value['分'],
  838. 'standard_score' => $value['定额分'],
  839. 'money' => $value['金额'],
  840. 'coefficient' => $value['难度系数'],
  841. 'remark' => isset($value['备注']) ? $value['备注'] : '',
  842. 'sys_id' => $params['sys_id'],
  843. 'sys_rq' => $now,
  844. ];
  845. }
  846. Db::startTrans();
  847. try {
  848. //删除数据库现有的工单工艺数据
  849. Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
  850. //插入工单工艺数据
  851. $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
  852. if ($processInsertCount === false) {
  853. throw new \Exception('工单工艺导入失败');
  854. }
  855. //提交事务
  856. Db::commit();
  857. } catch (\Exception $e) {
  858. Db::rollback();
  859. $this->error($e->getMessage());
  860. }
  861. $this->success('导入成功');
  862. }
  863. /**
  864. * 读取 Excel/CSV(PhpSpreadsheet)
  865. *
  866. * @param string $filePath
  867. * @param int $headerRowNum 表头所在行(1 起计,如定额表为第 5 行)
  868. * @param int $dataStartRowNum 首条数据行(1 起计,须大于表头行,如第 6 行)
  869. * @return array 每行一条关联数组,键为表头单元格文本
  870. */
  871. public function readExcel($filePath, $headerRowNum = 5, $dataStartRowNum = 6)
  872. {
  873. if (!is_file($filePath) || !is_readable($filePath)) {
  874. return [];
  875. }
  876. if ($dataStartRowNum <= $headerRowNum) {
  877. return [];
  878. }
  879. $spreadsheet = IOFactory::load($filePath);
  880. $sheet = $spreadsheet->getActiveSheet();
  881. $rows = $sheet->toArray();
  882. if (empty($rows)) {
  883. return [];
  884. }
  885. $headerIdx = $headerRowNum - 1;
  886. $dataStartIdx = $dataStartRowNum - 1;
  887. if (!isset($rows[$headerIdx])) {
  888. return [];
  889. }
  890. $headers = array_map(function ($cell) {
  891. return is_string($cell) ? trim($cell) : $cell;
  892. }, $rows[$headerIdx]);
  893. $data = [];
  894. $rowCount = count($rows);
  895. for ($r = $dataStartIdx; $r < $rowCount; $r++) {
  896. $row = $rows[$r];
  897. $hasCell = false;
  898. foreach ($row as $cell) {
  899. if ($cell !== null && $cell !== '') {
  900. $hasCell = true;
  901. break;
  902. }
  903. }
  904. if (!$hasCell) {
  905. continue;
  906. }
  907. $assoc = [];
  908. foreach ($headers as $i => $key) {
  909. if ($key === '' || $key === null) {
  910. continue;
  911. }
  912. $assoc[$key] = array_key_exists($i, $row) ? $row[$i] : null;
  913. }
  914. $data[] = $assoc;
  915. }
  916. return $data;
  917. }
  918. /**
  919. * 获取工单信息和工单制造工分
  920. * @param workorder 工单编号
  921. * @return array
  922. * @throws \think\db\exception\DataNotFoundException
  923. * @throws \think\db\exception\ModelNotFoundException
  924. * @throws \think\exception\DbException
  925. */
  926. public function getWorkOrderInfo()
  927. {
  928. if (!$this->request->isGet()) {
  929. $this->error('请求方法错误');
  930. }
  931. $params = $this->request->param();
  932. if (empty($params['workorder'])) {
  933. $this->error('工单编号不能为空');
  934. }
  935. $where = [
  936. '订单编号' => $params['workorder'],
  937. 'Mod_rq' => null,
  938. ];
  939. $workOrderInfo = Db::table('工单_基本资料')
  940. ->where($where)
  941. ->field('订单编号,客户编号,生产款号,款式,落货日期,审核日期,接单日期,审核,订单数量,计划制造工分,Uniqid')
  942. ->find();
  943. if (empty($workOrderInfo)) {
  944. $this->error('工单不存在');
  945. }
  946. $this->success('成功', $workOrderInfo);
  947. }
  948. /**
  949. * 获取工单工艺信息
  950. * @param workorder 工单编号
  951. * @return array
  952. * @throws \think\db\exception\DataNotFoundException
  953. * @throws \think\db\exception\ModelNotFoundException
  954. * @throws \think\exception\DbException
  955. */
  956. public function getWorkOrderProcess()
  957. {
  958. if (!$this->request->isGet()) {
  959. $this->error('请求方法错误');
  960. }
  961. $params = $this->request->param();
  962. if (empty($params['workorder'])) {
  963. $this->error('工单编号不能为空');
  964. }
  965. $where = [
  966. 'work_order' => $params['workorder'],
  967. 'del_rq' => null,
  968. ];
  969. $workOrderProcess = Db::table('工单_基础工艺资料')
  970. ->where($where)
  971. ->field('id,part_code as 部件编号,part_name as 部件名称,process_code as 工序编号,process_name as 工序名称,
  972. big_process as 大工序,standard_hour as 秒,standard_minutes as 分,standard_score as 定额分,money as 金额,
  973. coefficient as 难度系数,remark as 备注,status as 状态')
  974. ->select();
  975. if (empty($workOrderProcess)) {
  976. $this->error('工单工艺不存在');
  977. }
  978. $this->success('成功', $workOrderProcess);
  979. }
  980. /**
  981. * 修改工单计划制造工分
  982. * @param id 工单Uniqid或订单编号
  983. * @param number 计划制造工分
  984. * @param sys_id 操作人
  985. */
  986. public function updateWorkOrderPlanManufacture()
  987. {
  988. if (!$this->request->isPost()) {
  989. $this->error('请求方法错误');
  990. }
  991. $params = $this->request->post();
  992. if (empty($params['id'])) {
  993. $this->error('工单ID不能为空');
  994. }
  995. if (!isset($params['number']) || $params['number'] === '') {
  996. $this->error('制造工分不能为空');
  997. }
  998. if (empty($params['sys_id'])) {
  999. $this->error('操作人不能为空');
  1000. }
  1001. $workOrder = Db::table('工单_基本资料')
  1002. ->where('Uniqid|订单编号', $params['id'])
  1003. ->where('Mod_rq', null)
  1004. ->field('Uniqid,订单编号,计划制造工分')
  1005. ->find();
  1006. if (empty($workOrder)) {
  1007. $this->error('工单不存在');
  1008. }
  1009. $oldValue = $workOrder['计划制造工分'];
  1010. $newValue = $params['number'];
  1011. if ((string)$oldValue === (string)$newValue) {
  1012. $this->error('计划制造工分未发生变化');
  1013. }
  1014. $totalStandardScore = Db::table('工单_基础工艺资料')
  1015. ->where('work_order', $workOrder['订单编号'])
  1016. ->where('status', 0)
  1017. ->whereNull('del_rq')
  1018. ->sum('standard_score');
  1019. $totalStandardScore = $totalStandardScore ? floatval($totalStandardScore) : 0;
  1020. if ($totalStandardScore > floatval($newValue)) {
  1021. $this->error('工序定额分和大于制造工分,请确认之后再修改');
  1022. }
  1023. $operTime = date('Y-m-d H:i:s');
  1024. $logData = [
  1025. 'order_no' => $workOrder['订单编号'],
  1026. 'change_field' => '计划制造工分',
  1027. 'old_value' => $oldValue === null ? '' : (string)$oldValue,
  1028. 'new_value' => (string)$newValue,
  1029. 'oper_type' => '修改工单计划制造工分',
  1030. 'oper_user_name' => $params['sys_id'],
  1031. 'oper_time' => $operTime,
  1032. ];
  1033. Db::startTrans();
  1034. try {
  1035. $updateResultSql = Db::table('工单_基本资料')
  1036. ->where('Uniqid', $workOrder['Uniqid'])
  1037. ->fetchSql(true)
  1038. ->update(['计划制造工分' => $newValue]);
  1039. $updateResult = Db::query($updateResultSql);
  1040. if ($updateResult === false) {
  1041. throw new \Exception('更新工单失败');
  1042. }
  1043. $logResult = Db::name('work_order_operation_log')->insert($logData);
  1044. if ($logResult === false) {
  1045. throw new \Exception('写入操作日志失败');
  1046. }
  1047. Db::commit();
  1048. } catch (\Exception $e) {
  1049. Db::rollback();
  1050. $this->error('修改失败');
  1051. }
  1052. $this->success('修改成功');
  1053. }
  1054. }