WorkOrderProcess.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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 (!isset($params['coefficient']) || $params['coefficient'] === '') {
  422. $params['coefficient'] = 'C';
  423. }
  424. $process = Db::table('工单_基础工艺资料')
  425. ->where('id', intval($params['id']))
  426. ->whereNull('del_rq')
  427. ->find();
  428. if (empty($process)) {
  429. $this->error('工艺不存在');
  430. }
  431. $data = [
  432. 'part_code' => $params['part_code'],
  433. 'part_name' => $params['part_name'],
  434. 'standard_minutes' => $params['standard_minutes'],
  435. 'money' => $params['money'],
  436. 'remark' => $params['remark'],
  437. 'process_name' => $params['process_name'],
  438. 'big_process' => $params['big_process'],
  439. 'standard_hour' => $params['standard_hour'],
  440. 'standard_score' => $params['standard_score'],
  441. 'coefficient' => $params['coefficient'],
  442. 'mod_id' => $params['mod_id'],
  443. 'mod_rq' => date('Y-m-d H:i:s'),
  444. ];
  445. $fieldLabels = [
  446. 'part_code' => '部件编号',
  447. 'part_name' => '部件名称',
  448. 'standard_minutes' => '分',
  449. 'money' => '金额',
  450. 'remark' => '备注',
  451. 'process_name' => '工序名称',
  452. 'big_process' => '大工序',
  453. 'standard_hour' => '秒',
  454. 'standard_score' => '定额分',
  455. 'coefficient' => '难度系数',
  456. ];
  457. $operTime = date('Y-m-d H:i:s');
  458. $logList = [];
  459. foreach ($fieldLabels as $field => $label) {
  460. $oldValue = isset($process[$field]) ? $process[$field] : '';
  461. $newValue = $data[$field];
  462. if ((string)$oldValue === (string)$newValue) {
  463. continue;
  464. }
  465. $logList[] = [
  466. 'order_no' => $process['work_order'],
  467. 'process_code' => $params['process_code'],
  468. 'change_field' => $label,
  469. 'old_value' => $oldValue === null ? '' : (string)$oldValue,
  470. 'new_value' => (string)$newValue,
  471. 'oper_type' => '修改工单工艺',
  472. 'oper_user_name' => $params['mod_id'],
  473. 'oper_time' => $operTime,
  474. ];
  475. }
  476. if (empty($logList)) {
  477. $this->error('未找到可更新的工艺或数据无变化');
  478. }
  479. $standardScoreChanged = (string)$process['standard_score'] !== (string)$params['standard_score'];
  480. if ($standardScoreChanged && (string)$process['status'] === '0') {
  481. $workOrder = Db::table('工单_基本资料')
  482. ->where('订单编号', $process['work_order'])
  483. ->where('Mod_rq', null)
  484. ->field('计划制造工分')
  485. ->find();
  486. if (empty($workOrder)) {
  487. $this->error('工单不存在');
  488. }
  489. $totalStandardScore = Db::table('工单_基础工艺资料')
  490. ->where('work_order', $process['work_order'])
  491. ->where('status', 0)
  492. ->whereNull('del_rq')
  493. ->sum('standard_score');
  494. $totalStandardScore = $totalStandardScore ? floatval($totalStandardScore) : 0;
  495. $newTotal = $totalStandardScore - floatval($process['standard_score']) + floatval($params['standard_score']);
  496. if ($newTotal > floatval($workOrder['计划制造工分'])) {
  497. $this->error('工序定额分和大于制造工分,请确认之后再修改');
  498. }
  499. }
  500. Db::startTrans();
  501. try {
  502. $updateResult = Db::table('工单_基础工艺资料')
  503. ->where('id', intval($params['id']))
  504. ->whereNull('del_rq')
  505. ->update($data);
  506. if ($updateResult === false) {
  507. $dbError = Db::getError();
  508. $detail = is_array($dbError) && !empty($dbError) ? json_encode($dbError, JSON_UNESCAPED_UNICODE) : '';
  509. throw new \Exception('更新工艺失败' . ($detail !== '' ? ':' . $detail : ''));
  510. }
  511. if ($updateResult === 0) {
  512. throw new \Exception('未找到可更新的工艺');
  513. }
  514. $logResult = Db::name('work_order_operation_log')->insertAll($logList);
  515. if ($logResult === false || $logResult === 0) {
  516. $dbError = Db::getError();
  517. $detail = is_array($dbError) && !empty($dbError) ? json_encode($dbError, JSON_UNESCAPED_UNICODE) : '';
  518. throw new \Exception('写入操作日志失败' . ($detail !== '' ? ':' . $detail : ''));
  519. }
  520. Db::commit();
  521. } catch (\think\exception\HttpResponseException $e) {
  522. throw $e;
  523. } catch (\Exception $e) {
  524. Db::rollback();
  525. $msg = trim($e->getMessage());
  526. $this->error('更新失败:' . ($msg !== '' ? $msg : '未知错误'));
  527. }
  528. $this->success('更新成功');
  529. }
  530. /**
  531. * 工单工艺资料软删除
  532. * @param id 工艺ID
  533. */
  534. public function DeleteProcess()
  535. {
  536. if (!$this->request->isPost()) {
  537. $this->error('请求方法错误');
  538. }
  539. $params = $this->request->post();
  540. if (empty($params['id'])) {
  541. $this->error('工艺ID不能为空');
  542. }
  543. $ids = explode(',', $params['id']);
  544. $ids = array_filter(array_map('intval', $ids));
  545. if (empty($ids)) {
  546. $this->error('无效的工艺ID');
  547. }
  548. $result = db('工单_基础工艺资料')
  549. ->whereIn('id', $ids)
  550. ->whereNull('del_rq')
  551. ->update(['del_rq' => date('Y-m-d H:i:s')]);
  552. if ($result === false) {
  553. $this->error('删除失败');
  554. }
  555. if ($result === 0) {
  556. $this->error('未找到可删除的工艺');
  557. }
  558. $this->success('删除成功');
  559. }
  560. /**
  561. * 工单工艺复制
  562. * @param workorder 工单编号
  563. * @param product_code 产品编号
  564. * @param sys_id 操作人
  565. * @return array
  566. * @throws \think\db\exception\DataNotFoundException
  567. * @throws \think\db\exception\ModelNotFoundException
  568. * @throws \think\exception\DbException
  569. * @throws \think\exception\PDOException
  570. */
  571. public function workorderprocessCopy()
  572. {
  573. if (!$this->request->isGet()) {
  574. $this->error('请求方法错误');
  575. }
  576. $params = $this->request->param();
  577. if (empty($params['workorder'])) {
  578. $this->error('工单编号不能为空');
  579. }
  580. if (empty($params['product_code'])) {
  581. $this->error('产品编号不能为空');
  582. }
  583. if (empty($params['sys_id'])) {
  584. $this->error('操作人不能为空');
  585. }
  586. $now = date('Y-m-d H:i:s');
  587. $productParts = db('产品_部件资料')
  588. ->where('product_code', $params['product_code'])
  589. ->where('mod_rq', null)
  590. ->field('part_sort as part_code,part_name')
  591. ->select();
  592. if (empty($productParts)) {
  593. $this->error('该产品没有有效的部件');
  594. }
  595. $productProcesses = db('产品_工艺资料')
  596. ->where('product_code', $params['product_code'])
  597. ->field('part_sort as part_code,part_name,gy_sort as process_code,gy_name as process_name,
  598. big_process,standard_hour,standard_score,difficulty_coef as coefficient')
  599. ->select();
  600. if (empty($productProcesses)) {
  601. $this->error('该产品没有工艺');
  602. }
  603. $workOrderParts = [];
  604. //配置工单部件数据
  605. foreach ($productParts as $value) {
  606. $workOrderParts[] = [
  607. 'work_order' => $params['workorder'],
  608. 'part_code' => $value['part_code'],
  609. 'part_name' => $value['part_name'],
  610. 'part_type' => '',
  611. 'remark' => '',
  612. 'status' => 1,
  613. 'sys_id' => $params['sys_id'],
  614. 'sys_rq' => $now,
  615. ];
  616. }
  617. $workOrderProcesses = [];
  618. //配置工单工艺数据
  619. foreach ($productProcesses as $value) {
  620. $workOrderProcesses[] = [
  621. 'work_order' => $params['workorder'],
  622. 'part_code' => $value['part_code'],
  623. 'part_name' => $value['part_name'],
  624. 'process_code' => $value['process_code'],
  625. 'process_name' => $value['process_name'],
  626. 'big_process' => $value['big_process'],
  627. 'standard_hour' => $value['standard_hour'],
  628. 'standard_score' => $value['standard_score'],
  629. 'coefficient' => $value['coefficient'],
  630. 'remark' => '',
  631. 'sys_id' => $params['sys_id'],
  632. 'sys_rq' => $now,
  633. ];
  634. }
  635. Db::startTrans();
  636. try {
  637. //删除数据库现有的工单部件数据
  638. Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
  639. //删除数据库现有的工单工艺数据
  640. Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
  641. //插入工单部件数据
  642. $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
  643. if ($partInsertCount === false) {
  644. throw new \Exception('工单部件复制失败');
  645. }
  646. //插入工单工艺数据
  647. $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
  648. if ($processInsertCount === false) {
  649. throw new \Exception('工单工艺复制失败');
  650. }
  651. //提交事务
  652. Db::commit();
  653. } catch (\Exception $e) {
  654. Db::rollback();
  655. $this->error('复制失败');
  656. }
  657. $this->success('复制成功');
  658. }
  659. /**
  660. * 查询产品类型
  661. * @ApiMethod (GET)
  662. * @param string $workorder 工单编号
  663. * @return array
  664. * @throws \think\db\exception\DataNotFoundException
  665. * @throws \think\db\exception\ModelNotFoundException
  666. * @throws \think\exception\DbException
  667. */
  668. public function getproducttype()
  669. {
  670. if(!$this->request->isGet()){
  671. $this->error('请求方法错误');
  672. }
  673. $params = $this->request->get();
  674. if (empty($params['product'])) {
  675. $this->error('产品类型不能为空');
  676. }
  677. $list = db('产品_基本资料')
  678. ->where('product_type', $params['product'])
  679. ->where('status', 1)
  680. ->field('product_code as 产品编号,product_type as 产品类型,product_name as 产品名称')
  681. ->select();
  682. if(empty($list)){
  683. $this->error('该产品类型没有产品');
  684. }
  685. $this->success('成功', $list);
  686. }
  687. /**
  688. * 工单工艺排序
  689. * @param workorder 工单编号
  690. * @param part_code 部件编号
  691. * @param process_code 工艺编号
  692. * @param process_name 工艺名称
  693. * @param big_process 大工艺
  694. * @param standard_hour 标准工时
  695. * @param standard_score 标准公分
  696. * @param coefficient 系数
  697. * @param remark 备注
  698. */
  699. public function sortProcess()
  700. {
  701. if (!$this->request->isPost()) {
  702. $this->error('请求方法错误');
  703. }
  704. $params = $this->request->post();
  705. if (empty($params)) {
  706. $this->error('参数不能为空');
  707. }
  708. if (!is_array($params)) {
  709. $this->error('参数格式错误');
  710. }
  711. Db::startTrans();
  712. try {
  713. foreach ($params as $value) {
  714. if (empty($value['id'])) {
  715. throw new \Exception('缺少工艺ID');
  716. }
  717. if (!isset($value['process_code']) || $value['process_code'] === '') {
  718. throw new \Exception('缺少工艺编号');
  719. }
  720. $result = db('工单_基础工艺资料')
  721. ->where('id', intval($value['id']))
  722. ->whereNull('del_rq')
  723. ->update(['process_code' => intval($value['process_code'])]);
  724. if ($result === false) {
  725. throw new \Exception('排序失败');
  726. }
  727. }
  728. Db::commit();
  729. } catch (\Exception $e) {
  730. Db::rollback();
  731. $this->error($e->getMessage());
  732. }
  733. $this->success('排序成功');
  734. }
  735. /**
  736. * 工单工艺excel导入
  737. * @param workorder 工单编号
  738. *
  739. */
  740. public function importProcess()
  741. {
  742. if (!$this->request->isPost()) {
  743. $this->error('请求方法错误');
  744. }
  745. $params = $this->request->post();
  746. if (empty($params['workorder'])) {
  747. $this->error('工单编号不能为空');
  748. }
  749. if (empty($params['sys_id'])) {
  750. $this->error('操作人不能为空');
  751. }
  752. $file = $this->request->file('file');
  753. if (!$file) {
  754. $this->error('文件不能为空');
  755. }
  756. $uploadDir = ROOT_PATH . 'public' . DS . 'uploads';
  757. $info = $file->validate(['size' => 1024 * 1024 * 10, 'ext' => 'xlsx,xls,csv,txt'])
  758. ->move($uploadDir);
  759. if (!$info) {
  760. $this->error($file->getError());
  761. }
  762. $filePath = $uploadDir . DS . $info->getSaveName();
  763. // 定额表:上方为标题与元数据,第 5 行表头,第 6 行起为数据
  764. $data = $this->readExcel($filePath, 5, 6);
  765. if (empty($data)) {
  766. $this->error('文件内容为空');
  767. }
  768. $seenSeq = [];
  769. foreach ($data as $row) {
  770. $seq = isset($row['序号']) ? $row['序号'] : null;
  771. if ($seq === null || $seq === '') {
  772. continue;
  773. }
  774. $seqKey = is_scalar($seq) ? (string)$seq : $seq;
  775. if (isset($seenSeq[$seqKey])) {
  776. $this->error('工序序号重复,请重新调整之后再上传');
  777. }
  778. $seenSeq[$seqKey] = true;
  779. }
  780. $now = date('Y-m-d H:i:s');
  781. $partNameToCode = [];
  782. $nextPartCode = 0;
  783. foreach ($data as $row) {
  784. $partName = isset($row['部件名称']) ? $row['部件名称'] : '';
  785. if ($partName === '' || isset($partNameToCode[$partName])) {
  786. continue;
  787. }
  788. $partNameToCode[$partName] = ++$nextPartCode;
  789. }
  790. $workOrderParts = [];
  791. $i = 0;
  792. foreach ($partNameToCode as $partName => $partCode) {
  793. $workOrderParts[$i++] = [
  794. 'work_order' => $params['workorder'],
  795. 'part_name' => $partName,
  796. 'part_code' => $partCode,
  797. 'part_type' => '',
  798. 'remark' => '',
  799. 'status' => 1,
  800. 'sys_id' => $params['sys_id'],
  801. 'sys_rq' => $now,
  802. ];
  803. }
  804. Db::startTrans();
  805. try {
  806. //删除数据库现有的工单部件数据
  807. Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
  808. //插入工单部件数据
  809. $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
  810. if ($partInsertCount === false) {
  811. throw new \Exception('工单部件导入失败');
  812. }
  813. //提交事务
  814. Db::commit();
  815. } catch (\Exception $e) {
  816. Db::rollback();
  817. $this->error($e->getMessage());
  818. }
  819. $workOrderProcesses = [];
  820. foreach ($data as $value) {
  821. $name = isset($value['部件名称']) ? $value['部件名称'] : '';
  822. $partCode = ($name !== '' && isset($partNameToCode[$name])) ? $partNameToCode[$name] : '';
  823. $workOrderProcesses[] = [
  824. 'work_order' => $params['workorder'],
  825. 'part_code' => $partCode,
  826. 'part_name' => $name,
  827. 'process_code' => $value['序号'],
  828. 'process_name' => $value['工序名称'],
  829. 'big_process' => $value['生产工序'],
  830. 'standard_hour' => $value['秒'],
  831. 'standard_minutes' => $value['分'],
  832. 'standard_score' => $value['定额分'],
  833. 'money' => $value['金额'],
  834. 'coefficient' => $value['难度系数'],
  835. 'remark' => isset($value['备注']) ? $value['备注'] : '',
  836. 'sys_id' => $params['sys_id'],
  837. 'sys_rq' => $now,
  838. ];
  839. }
  840. Db::startTrans();
  841. try {
  842. //删除数据库现有的工单工艺数据
  843. Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
  844. //插入工单工艺数据
  845. $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
  846. if ($processInsertCount === false) {
  847. throw new \Exception('工单工艺导入失败');
  848. }
  849. //提交事务
  850. Db::commit();
  851. } catch (\Exception $e) {
  852. Db::rollback();
  853. $this->error($e->getMessage());
  854. }
  855. $this->success('导入成功');
  856. }
  857. /**
  858. * 读取 Excel/CSV(PhpSpreadsheet)
  859. *
  860. * @param string $filePath
  861. * @param int $headerRowNum 表头所在行(1 起计,如定额表为第 5 行)
  862. * @param int $dataStartRowNum 首条数据行(1 起计,须大于表头行,如第 6 行)
  863. * @return array 每行一条关联数组,键为表头单元格文本
  864. */
  865. public function readExcel($filePath, $headerRowNum = 5, $dataStartRowNum = 6)
  866. {
  867. if (!is_file($filePath) || !is_readable($filePath)) {
  868. return [];
  869. }
  870. if ($dataStartRowNum <= $headerRowNum) {
  871. return [];
  872. }
  873. $spreadsheet = IOFactory::load($filePath);
  874. $sheet = $spreadsheet->getActiveSheet();
  875. $rows = $sheet->toArray();
  876. if (empty($rows)) {
  877. return [];
  878. }
  879. $headerIdx = $headerRowNum - 1;
  880. $dataStartIdx = $dataStartRowNum - 1;
  881. if (!isset($rows[$headerIdx])) {
  882. return [];
  883. }
  884. $headers = array_map(function ($cell) {
  885. return is_string($cell) ? trim($cell) : $cell;
  886. }, $rows[$headerIdx]);
  887. $data = [];
  888. $rowCount = count($rows);
  889. for ($r = $dataStartIdx; $r < $rowCount; $r++) {
  890. $row = $rows[$r];
  891. $hasCell = false;
  892. foreach ($row as $cell) {
  893. if ($cell !== null && $cell !== '') {
  894. $hasCell = true;
  895. break;
  896. }
  897. }
  898. if (!$hasCell) {
  899. continue;
  900. }
  901. $assoc = [];
  902. foreach ($headers as $i => $key) {
  903. if ($key === '' || $key === null) {
  904. continue;
  905. }
  906. $assoc[$key] = array_key_exists($i, $row) ? $row[$i] : null;
  907. }
  908. $data[] = $assoc;
  909. }
  910. return $data;
  911. }
  912. /**
  913. * 获取工单工艺信息
  914. * @param workorder 工单编号
  915. * @return array
  916. * @throws \think\db\exception\DataNotFoundException
  917. * @throws \think\db\exception\ModelNotFoundException
  918. * @throws \think\exception\DbException
  919. */
  920. public function getWorkOrderProcess()
  921. {
  922. if (!$this->request->isGet()) {
  923. $this->error('请求方法错误');
  924. }
  925. $params = $this->request->param();
  926. if (empty($params['workorder'])) {
  927. $this->error('工单编号不能为空');
  928. }
  929. $where = [
  930. 'work_order' => $params['workorder'],
  931. 'del_rq' => null,
  932. ];
  933. $workOrderProcess = Db::table('工单_基础工艺资料')
  934. ->where($where)
  935. ->field('id,part_code as 部件编号,part_name as 部件名称,process_code as 工序编号,process_name as 工序名称,
  936. big_process as 大工序,standard_hour as 秒,standard_minutes as 分,standard_score as 定额分,money as 金额,
  937. coefficient as 难度系数,remark as 备注,status as 状态')
  938. ->select();
  939. if (empty($workOrderProcess)) {
  940. $this->error('工单工艺不存在');
  941. }
  942. $this->success('成功', $workOrderProcess);
  943. }
  944. /**
  945. * 修改工单计划制造工分
  946. * @param id 工单Uniqid或订单编号
  947. * @param number 计划制造工分
  948. * @param sys_id 操作人
  949. */
  950. public function updateWorkOrderPlanManufacture()
  951. {
  952. if (!$this->request->isPost()) {
  953. $this->error('请求方法错误');
  954. }
  955. $params = $this->request->post();
  956. if (empty($params['id'])) {
  957. $this->error('工单ID不能为空');
  958. }
  959. if (!isset($params['number']) || $params['number'] === '') {
  960. $this->error('制造工分不能为空');
  961. }
  962. if (empty($params['sys_id'])) {
  963. $this->error('操作人不能为空');
  964. }
  965. $workOrder = Db::table('工单_基本资料')
  966. ->where('Uniqid|订单编号', $params['id'])
  967. ->where('Mod_rq', null)
  968. ->field('Uniqid,订单编号,计划制造工分')
  969. ->find();
  970. if (empty($workOrder)) {
  971. $this->error('工单不存在');
  972. }
  973. $oldValue = $workOrder['计划制造工分'];
  974. $newValue = $params['number'];
  975. if ((string)$oldValue === (string)$newValue) {
  976. $this->error('计划制造工分未发生变化');
  977. }
  978. $totalStandardScore = Db::table('工单_基础工艺资料')
  979. ->where('work_order', $workOrder['订单编号'])
  980. ->where('status', 0)
  981. ->whereNull('del_rq')
  982. ->sum('standard_score');
  983. $totalStandardScore = $totalStandardScore ? floatval($totalStandardScore) : 0;
  984. if ($totalStandardScore > floatval($newValue)) {
  985. $this->error('工序定额分和大于制造工分,请确认之后再修改');
  986. }
  987. $operTime = date('Y-m-d H:i:s');
  988. $logData = [
  989. 'order_no' => $workOrder['订单编号'],
  990. 'change_field' => '计划制造工分',
  991. 'old_value' => $oldValue === null ? '' : (string)$oldValue,
  992. 'new_value' => (string)$newValue,
  993. 'oper_type' => '修改工单计划制造工分',
  994. 'oper_user_name' => $params['sys_id'],
  995. 'oper_time' => $operTime,
  996. ];
  997. Db::startTrans();
  998. try {
  999. $updateResultSql = Db::table('工单_基本资料')
  1000. ->where('Uniqid', $workOrder['Uniqid'])
  1001. ->fetchSql(true)
  1002. ->update(['计划制造工分' => $newValue]);
  1003. $updateResult = Db::query($updateResultSql);
  1004. if ($updateResult === false) {
  1005. throw new \Exception('更新工单失败');
  1006. }
  1007. $logResult = Db::name('work_order_operation_log')->insert($logData);
  1008. if ($logResult === false) {
  1009. throw new \Exception('写入操作日志失败');
  1010. }
  1011. Db::commit();
  1012. } catch (\Exception $e) {
  1013. Db::rollback();
  1014. $this->error('修改失败');
  1015. }
  1016. $this->success('修改成功');
  1017. }
  1018. /**
  1019. * 拆分工序时判断工序是否存在报工
  1020. * @param workorder 工单编号
  1021. * @param process_code 工艺编号
  1022. * @return array
  1023. * @throws \think\db\exception\DataNotFoundException
  1024. * @throws \think\db\exception\ModelNotFoundException
  1025. * @throws \think\exception\DbException
  1026. */
  1027. public function checkProcessReport()
  1028. {
  1029. if (!$this->request->isPost()) {
  1030. $this->error('请求方法错误');
  1031. }
  1032. $params = $this->request->post();
  1033. if (empty($params['workorder'])) {
  1034. $this->error('工单编号不能为空');
  1035. }
  1036. if (empty($params['process_code'])) {
  1037. $this->error('工艺编号不能为空');
  1038. }
  1039. $process = db('设备_工分计酬')
  1040. ->where('work_order', $params['workorder'])
  1041. ->where('process_code', $params['process_code'])
  1042. ->find();
  1043. if (!empty($process)) {
  1044. $this->error('');
  1045. }
  1046. $this->success('工序不存在报工,可以拆分');
  1047. }
  1048. /**
  1049. * 确认拆分工序:母工序置为已拆分,按数量生成子工序
  1050. * @param id 工艺ID
  1051. * @param number 拆分数量
  1052. * @param sys_id 操作人员
  1053. * @param sys_rq 操作时间(可选,默认当前时间)
  1054. */
  1055. public function confirmProcessSplit()
  1056. {
  1057. if (!$this->request->isPost()) {
  1058. $this->error('请求方法错误');
  1059. }
  1060. $params = $this->request->post();
  1061. if (empty($params['id'])) {
  1062. $this->error('工艺ID不能为空');
  1063. }
  1064. if (empty($params['number']) ) {
  1065. $this->error('拆分工序数量不能为空');
  1066. }
  1067. if (empty($params['sys_id'])) {
  1068. $this->error('操作人不能为空');
  1069. }
  1070. $splitCount = intval($params['number']);
  1071. $operTime = !empty($params['sys_rq']) ? $params['sys_rq'] : date('Y-m-d H:i:s');
  1072. $process = Db::table('工单_基础工艺资料')
  1073. ->where('id', intval($params['id']))
  1074. ->where('status', 'in', [0, '0'])
  1075. ->find();
  1076. if (empty($process)) {
  1077. $this->error('工序不存在或已确认拆分');
  1078. }
  1079. $workorder = $process['work_order'];
  1080. $processCodes = Db::table('工单_基础工艺资料')
  1081. ->where('work_order', $workorder)
  1082. ->whereNull('del_rq')
  1083. ->column('process_code');
  1084. $maxNumeric = 0;
  1085. foreach ($processCodes as $code) {
  1086. if (preg_match('/^(\d+)/', (string)$code, $match)) {
  1087. $num = intval($match[1]);
  1088. if ($num > $maxNumeric) {
  1089. $maxNumeric = $num;
  1090. }
  1091. }
  1092. }
  1093. $baseCode = $maxNumeric + 1;
  1094. $splitFields = ['standard_hour', 'standard_minutes', 'standard_score', 'money'];
  1095. $splitValues = [];
  1096. foreach ($splitFields as $field) {
  1097. $splitValues[$field] = $this->splitNumericValue($process[$field], $splitCount);
  1098. }
  1099. $insertList = [];
  1100. for ($i = 1; $i <= $splitCount; $i++) {
  1101. $newProcessCode = $baseCode + $i - 1;
  1102. $exists = Db::table('工单_基础工艺资料')
  1103. ->where('work_order', $workorder)
  1104. ->where('process_code', $newProcessCode)
  1105. ->whereNull('del_rq')
  1106. ->count();
  1107. if ($exists > 0) {
  1108. $this->error('工序编号已存在:' . $newProcessCode);
  1109. }
  1110. $insertList[] = [
  1111. 'work_order' => $workorder,
  1112. 'part_code' => $process['part_code'],
  1113. 'part_name' => isset($process['part_name']) ? $process['part_name'] : '',
  1114. 'process_code' => $newProcessCode,
  1115. 'process_name' => $process['process_name'] . '-' . $i,
  1116. 'big_process' => $process['big_process'],
  1117. 'standard_hour' => $splitValues['standard_hour'][$i - 1],
  1118. 'standard_minutes' => $splitValues['standard_minutes'][$i - 1],
  1119. 'standard_score' => $splitValues['standard_score'][$i - 1],
  1120. 'money' => $splitValues['money'][$i - 1],
  1121. 'coefficient' => isset($process['coefficient']) && $process['coefficient'] !== '' && $process['coefficient'] !== null
  1122. ? $process['coefficient'] : 'C',
  1123. 'remark' => isset($process['remark']) && $process['remark'] !== null ? $process['remark'] : '',
  1124. 'status' => 0,
  1125. 'sys_id' => $params['sys_id'],
  1126. 'sys_rq' => $operTime,
  1127. ];
  1128. }
  1129. $logList = [
  1130. [
  1131. 'order_no' => $workorder,
  1132. 'process_code' => $process['process_code'],
  1133. 'change_field' => '状态',
  1134. 'old_value' => '0',
  1135. 'new_value' => '1',
  1136. 'oper_type' => '确认拆分工序',
  1137. 'oper_user_name' => $params['sys_id'],
  1138. 'oper_time' => $operTime,
  1139. ],
  1140. ];
  1141. foreach ($insertList as $row) {
  1142. $logList[] = [
  1143. 'order_no' => $workorder,
  1144. 'process_code' => $row['process_code'],
  1145. 'change_field' => '工序拆分',
  1146. 'old_value' => (string)$process['process_code'],
  1147. 'new_value' => sprintf(
  1148. '秒:%s,分:%s,定额分:%s,金额:%s',
  1149. $row['standard_hour'],
  1150. $row['standard_minutes'],
  1151. $row['standard_score'],
  1152. $row['money']
  1153. ),
  1154. 'oper_type' => '确认拆分工序',
  1155. 'oper_user_name' => $params['sys_id'],
  1156. 'oper_time' => $operTime,
  1157. ];
  1158. }
  1159. Db::startTrans();
  1160. try {
  1161. $updateResult = Db::table('工单_基础工艺资料')
  1162. ->where('id', $process['id'])
  1163. ->where('status', 'in', [0, '0'])
  1164. ->update(['status' => 1]);
  1165. if ($updateResult === false) {
  1166. throw new \Exception('更新母工序状态失败');
  1167. }
  1168. if ($updateResult === 0) {
  1169. throw new \Exception('母工序状态已变更,请刷新后重试');
  1170. }
  1171. $insertResult = Db::name('工单_基础工艺资料')->insertAll($insertList);
  1172. if ($insertResult === false || $insertResult === 0) {
  1173. throw new \Exception('拆分工序新增失败');
  1174. }
  1175. $logResult = Db::name('work_order_operation_log')->insertAll($logList);
  1176. if ($logResult === false || $logResult === 0) {
  1177. throw new \Exception('写入操作日志失败,请确认日志表已包含 process_code 字段');
  1178. }
  1179. Db::commit();
  1180. } catch (\think\exception\HttpResponseException $e) {
  1181. throw $e;
  1182. } catch (\Exception $e) {
  1183. Db::rollback();
  1184. $this->error('修改失败:' . $e->getMessage());
  1185. }
  1186. $this->success('确认拆分工序成功', ['count' => $splitCount]);
  1187. }
  1188. /**
  1189. * 按拆分数量分配数值:不能整除时第一条为剩余后的最大值,其余为向下取整的平均值
  1190. * @param mixed $total
  1191. * @param int $count
  1192. * @return array
  1193. */
  1194. private function splitNumericValue($total, $count)
  1195. {
  1196. $count = intval($count);
  1197. if ($count <= 0) {
  1198. return [];
  1199. }
  1200. $total = floatval($total);
  1201. $avg = floor($total / $count * 100) / 100;
  1202. $first = round($total - $avg * ($count - 1), 2);
  1203. $values = [$first];
  1204. for ($i = 1; $i < $count; $i++) {
  1205. $values[] = $avg;
  1206. }
  1207. return $values;
  1208. }
  1209. }