WorkOrderProcess.php 36 KB

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