WorkOrderProcess.php 67 KB

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