WorkOrder.php 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use Monolog\Handler\IFTTTHandler;
  5. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  6. use think\Db;
  7. use think\Request;
  8. use PhpOffice\PhpSpreadsheet\IOFactory;
  9. /**
  10. * 工单资料管理
  11. */
  12. class WorkOrder extends Api
  13. {
  14. protected $noNeedLogin = ['*'];
  15. protected $noNeedRight = ['*'];
  16. /**
  17. * 工单资料菜单列表
  18. *
  19. * @ApiMethod (GET)
  20. * @return false|string
  21. * @throws \think\Exception
  22. */
  23. public function DataList()
  24. {
  25. if ($this->request->isGet() === false){
  26. $this->error('请求错误');
  27. }
  28. $param = $this->request->param();
  29. // if (isset($param['sort'])){
  30. // $where['工单分类'] = 2;
  31. // }else{
  32. // $where['工单分类'] = ['in',[1,3]];
  33. // }
  34. $where['行号'] = ['in',['1','10']];
  35. //获取总计划中数量和总生产中数量
  36. $productingAll = \db('工单_基本资料')->where($where)->where('成品代号','<>','')->where('行号','1')->where('gd_statu','2-生产中')->cache(true)->count();
  37. $progressAll = \db('工单_基本资料')->where($where)->where('成品代号','<>','')->where('行号','1')->where('gd_statu','3-计划中')->cache(true)->count();
  38. $data = [
  39. 'productingAll' => $productingAll,
  40. 'progressAll' => $progressAll,
  41. // '客户编号' => []
  42. ];
  43. $list = \db('产品_基本资料')
  44. ->where('客户编号','<>','')
  45. ->field('客户编号,客户名称')
  46. ->group('客户编号,客户名称')
  47. ->order('客户编号')
  48. ->select();
  49. if (empty($list)){
  50. $this->success('',[]);
  51. }
  52. $data['翌星工单']=$data['MN工单']=[];
  53. foreach ($list as $key=>$value){
  54. $value['客户编号'] = rtrim($value['客户编号']);
  55. //这条sql查出来的数据可能不对,试一下以下sql
  56. $productIng = \db('工单_基本资料')->where($where)->where('Gd_cpdh|成品代号','LIKE',rtrim($value['客户编号']).'%')->where('gd_statu','2-生产中')->count();
  57. $proGress = \db('工单_基本资料')->where($where)->where('Gd_cpdh|成品代号','LIKE',rtrim($value['客户编号']).'%')->where('gd_statu','3-计划中')->count();
  58. $string = '';
  59. if ($productIng != 0){
  60. $string = $string."生产中:".$productIng;
  61. }
  62. if ($proGress != 0){
  63. $string = $string."计划中:".$proGress;
  64. }
  65. if ($string !== ''){
  66. $name = $value['客户编号'].'【'.$string.'】'.rtrim($value['客户名称']);
  67. if (strpos($value['客户编号'],'J') !== false || strpos($value['客户编号'],'Y') !== false){
  68. array_push($data['翌星工单'],$name);
  69. }else{
  70. array_push($data['MN工单'],$name);
  71. }
  72. // array_push($data['客户编号'],$name);
  73. }
  74. // else{
  75. // $name = $value['客户编号'].$value['客户名称'];
  76. // if (strpos($value['客户编号'],'J') !== false){
  77. // array_push($data['糊盒工单'],$name);
  78. // }else{
  79. // array_push($data['印刷工单'],$name);
  80. // }
  81. // }
  82. }
  83. $this->success('成功',$data);
  84. }
  85. /**
  86. * 工单基本资料列表
  87. * @ApiMethod (GET)
  88. * @param string $limit 查询长度
  89. * @param string $Gd_khdh 客户代号
  90. * @param string $startTime 接单日期开始时间
  91. * @param string $endTime 接单日期结束时间
  92. * @return \think\response\Json
  93. * @throws \think\exception\DbException
  94. */
  95. public function WorkList()
  96. {
  97. if ($this->request->isGet() === false){
  98. $this->error('请求错误');
  99. }
  100. $search = input('search');
  101. $clientNumber = input('Gd_khdh');
  102. $startTime = input('start');
  103. $endTime = input('end');
  104. $page = input('page');
  105. $limit = input('limit');
  106. $param = $this->request->param();
  107. if (isset($param['sort'])){
  108. $sort = 2;
  109. }else{
  110. $sort = 1;
  111. }
  112. $where = [];
  113. if (!empty($clientNumber)){
  114. if (substr($clientNumber,0,1) !== 'Y' && substr($clientNumber,0,1) !== 'J')
  115. {
  116. $clientNumber = substr($clientNumber,0,4);
  117. }
  118. if ($clientNumber === 'Y1401'){
  119. $where['Gd_cpdh'] = ['like',$clientNumber.'%'];
  120. }else{
  121. $where['成品代号'] = ['like',$clientNumber.'%'];
  122. }
  123. }
  124. if (!empty($workOrder)){
  125. $where['Gd_gdbh'] = $workOrder;
  126. }
  127. if (!empty($productCode)){
  128. $where['Gd_cpdh'] = $productCode;
  129. }
  130. if (!empty($search)){
  131. $where['Gd_lx|Gd_gdbh|Gd_客户代号|Gd_客户名称|Gd_khdh|Gd_khmc|Gd_cpdh|Gd_cpmc|成品代号|成品名称|产品版本号'] = ['like','%'.$search.'%'];
  132. }
  133. if (!empty($startTime) && !empty($endTime)){
  134. $where['接单日期'] = ['between',[$startTime,$endTime]];
  135. }
  136. $total = \db('工单_基本资料')->where($where)->distinct(true)->field('Gd_gdbh')->count();
  137. $list = \db('工单_基本资料')
  138. ->where($where)
  139. ->order('Gd_statu desc')
  140. ->limit(($page-1)*$limit,$limit)
  141. ->group('Gd_gdbh')
  142. ->select();
  143. //工单基本资料数据整理
  144. $data = [];
  145. foreach ($list as $key=>$value){
  146. $data[$key] = [
  147. '工单编号' => rtrim($value['Gd_gdbh']),
  148. '生产分类' => rtrim($value['Gd_生产分类']),
  149. '销售订单号' => rtrim($value['销售订单号']),
  150. '产品代号' => rtrim($value['成品代号']),
  151. '产品名称' => rtrim($value['成品名称']),
  152. '订单数量' => floatval($value['订单数量']),
  153. '单位' => rtrim($value['计量单位']),
  154. '折合大箱' => rtrim((int)$value['投料大箱']),
  155. '投料率' => rtrim($value['投料率']),
  156. '平均合格率' => '',
  157. '开单日期' => date('Y-m-d',strtotime(rtrim($value['接单日期']))),
  158. '交货日期' => date('Y-m-d',strtotime(rtrim($value['交货日期']))),
  159. '工单类型' => rtrim($value['Gd_lx']),
  160. '工单状态' => rtrim($value['gd_statu']),
  161. '当前生产工序' => '',
  162. '产量提交时间' => '',
  163. '建档用户' => rtrim($value['Sys_id']),
  164. '建档时间' => rtrim($value['Sys_rq']),
  165. '更新时间' => rtrim($value['Mod_rq']),
  166. 'Uniqid' => rtrim($value['Uniqid'])
  167. ];
  168. $number = \db('工单_工艺资料')->where('Gy0_gdbh',$data[$key]['工单编号'])->count();
  169. if ($number === 0){
  170. $data[$key]['status'] = '*';
  171. }else{
  172. $data[$key]['status'] = '';
  173. }
  174. }
  175. $this->success('成功',['data'=>$data,'total'=>$total]);
  176. }
  177. /**
  178. * 工单详情
  179. * @ApiMethod (GET)
  180. * @param void
  181. * @return void
  182. * @throws \think\db\exception\DataNotFoundException
  183. * @throws \think\db\exception\ModelNotFoundException
  184. * @throws \think\exception\DbException
  185. */
  186. public function workOrderListDetail()
  187. {
  188. if ($this->request->isGet() === false){
  189. $this->error('请求错误');
  190. }
  191. $Gd_gdbh = input('Gd_gdbh');
  192. $param = $this->request->param();
  193. if (empty($Gd_gdbh)){
  194. $this->error('参数错误');
  195. }
  196. if (isset($param['sort'])){
  197. $where['yj_Yjno'] = ['>=','10'];
  198. }else{
  199. $where['yj_Yjno'] = ['<','10'];
  200. }
  201. $data = [];
  202. //印件资料
  203. $printList = \db('工单_印件资料')
  204. ->where('Yj_Gdbh',$Gd_gdbh)
  205. ->where($where)
  206. ->select();
  207. if (!empty($printList)){
  208. foreach ($printList as $key=>$value){
  209. $data['printList'][$key] = [
  210. '印件号' => rtrim($value['yj_Yjno']),
  211. '印件代号' => rtrim($value['yj_Yjdh']),
  212. '印件名称' => rtrim($value['yj_yjmc']),
  213. '纸张代号' => rtrim($value['yj_zzdh']),
  214. '纸张名称' => rtrim($value['yj_zzmc']),
  215. '投料规格' => rtrim($value['yj_tlgg']),
  216. '平张投料' => rtrim($value['yj_平张投料']),
  217. '开料规格' => rtrim($value['yj_klgg']),
  218. '开数*联数' => rtrim($value['yj_ks']).'*'.rtrim($value['yj_ls']),
  219. '建档用户' => rtrim($value['Sys_id']),
  220. '建档时间' => rtrim($value['Sys_rq']),
  221. '更新时间' => rtrim($value['Mod_rq']),
  222. 'zzdh1' => rtrim($value['yj_zzdh1']),
  223. 'zzdh2' => rtrim($value['yj_zzdh2']),
  224. 'zzdh3' => rtrim($value['yj_zzdh3']),
  225. 'zzdh4' => rtrim($value['yj_zzdh4']),
  226. 'zzmc1' => rtrim($value['yj_zzmc1']),
  227. 'zzmc2' => rtrim($value['yj_zzmc2']),
  228. 'zzmc3' => rtrim($value['yj_zzmc3']),
  229. 'zzmc4' => rtrim($value['yj_zzmc4']),
  230. '订单数量' => rtrim($value['yj_成品数量']),
  231. '万小张' => rtrim($value['yj_实际投料']),
  232. '开数' => rtrim($value['yj_ks']),
  233. '联数' => rtrim($value['yj_ls']),
  234. '核算规格' => rtrim($value['Yj_核算规格']),
  235. '备注' => rtrim($value['yj_desc']),
  236. 'Uniqid' => rtrim($value['Uniqid'])
  237. ];
  238. }
  239. }else{
  240. $data['printList'] = [];
  241. }
  242. //工艺资料
  243. if (isset($param['sort'])){
  244. $Prowhere['Gy0_yjno'] = ['>=','10'];
  245. }else{
  246. $Prowhere['Gy0_yjno'] = ['<','10'];
  247. }
  248. $processList = \db('工单_工艺资料')
  249. ->where('Gy0_gdbh',$Gd_gdbh)
  250. ->where($Prowhere)
  251. ->order('Gy0_yjno,Gy0_gxh')
  252. ->select();
  253. if (!empty($processList)){
  254. foreach ($processList as $key=>$value){
  255. if ($value['Gy0_yjno']<10){
  256. $value['Gy0_yjno'] = '0'.$value['Gy0_yjno'];
  257. }
  258. if ($value['Gy0_gxh']<10){
  259. $value['Gy0_gxh'] = '0'.$value['Gy0_gxh'];
  260. }
  261. if (rtrim($value['Gy0_shdh']) == '' || $value['Gy0_计划接货数'] == 0){
  262. $number = 0;
  263. }else{
  264. $number = round(((int)$value['Gy0_计划损耗']/(int)($value['Gy0_计划接货数']))*100,2).'%';
  265. }
  266. if ($value['PD_WG'] !== '1900-01-01 00:00:00'){
  267. $status = '已完工';
  268. }else{
  269. $status = '未完工';
  270. }
  271. $data['processList'][$key] = [
  272. '重点工序' => rtrim($value['重点工序']),
  273. '印件-工序' => rtrim($value['Gy0_yjno']).'-'.rtrim($value['Gy0_gxh']),
  274. '备选工序' => rtrim($value['备选工序']),
  275. '工序名称' => rtrim($value['Gy0_gxmc']).'【'.rtrim($value['Add_gxmc']).'】',
  276. '印件号' => rtrim($value['Gy0_yjno']),
  277. '工序号' => rtrim($value['Gy0_gxh']),
  278. '工艺名称' => rtrim($value['Gy0_gxmc']),
  279. 'gxmc' => rtrim($value['Add_gxmc']),
  280. '计划产量' => $value['Gy0_计划接货数'],
  281. '基础损耗' => rtrim($value['Gy0_Rate0']),
  282. '损耗率' => rtrim($value['Gy0_Rate1']),
  283. '机台编号' => rtrim($value['Gy0_sbbh']),
  284. '报废定额' => rtrim($value['Gy0_计划损耗']),
  285. '计损色数' => rtrim($value['Gy0_ms']),
  286. '允损比例' => $number,
  287. '难度系数' => isset($value['工价系数'])?rtrim($value['工价系数']):'',
  288. '损耗系数' => isset($value['损耗系数'])?rtrim($value['损耗系数']):'',
  289. '人工检_次品板' => (int)$value['人工检_次品板']=0?'':$value['人工检_次品板'],
  290. '人工检_废检' => (int)$value['人工检_废检']=0?'':$value['人工检_废检'],
  291. '机检_正品板' => (int)$value['机检_正品板']=0?'':$value['机检_正品板'],
  292. '机检_次品板' => (int)$value['机检_次品板']=0?'':$value['机检_次品板'],
  293. '机检_废检' => (int)$value['机检_废检']=0?'':$value['机检_废检'],
  294. '开数*联数' => rtrim($value['Gy0_ks']).'*'.rtrim($value['Gy0_ls']),
  295. '备注' => isset($value['工序备注'])?rtrim($value['工序备注']):'',
  296. '印刷方式' => isset($value['印刷方式'])?rtrim($value['印刷方式']):'',
  297. '版距' => isset($value['版距'])?rtrim($value['版距']):'',
  298. '建档用户' => rtrim($value['Sys_id']),
  299. '建档日期' => isset($value['Sys_rq'])?rtrim($value['Sys_rq']):'',
  300. '更新时间' => isset($value['Mod_rq'])?rtrim($value['Mod_rq']):'',
  301. '车间名称' => rtrim($value['Gy0_SITE']),
  302. '质量要求' => rtrim($value['质量要求']),
  303. '质量隐患' => rtrim($value['质量隐患']),
  304. '开数' => rtrim($value['Gy0_ks']),
  305. '联数' => rtrim($value['Gy0_ls']),
  306. 'UniqId' => rtrim($value['UniqId']),
  307. 'shdh' => rtrim($value['Gy0_shdh']),
  308. '辅助工时' => rtrim($value['Gy0_辅助工时']),
  309. '小时产能' => rtrim($value['Gy0_小时产能']),
  310. 'status' => $status
  311. ];
  312. }
  313. }else{
  314. $data['processList'] = [];
  315. }
  316. //BOM资料
  317. $field = 'rtrim(BOM_方案) as 方案,rtrim(BOM_物料编码) as 物料编号,rtrim(BOM_物料名称) as 物料名称,BOM_投料单位,
  318. BOM_投入数,BOM_产出数,BOM_产出单位,rtrim(BOM_计划用量) as 计划用量,rtrim(Sys_ID) as 建档用户,rtrim(Sys_rq) as 建档时间,rtrim(Mod_rq) as 更新时间';
  319. $bomList = \db('工单_bom资料')
  320. ->where('BOM_工单编号',$Gd_gdbh)
  321. ->field($field)
  322. ->select();
  323. if (!empty($bomList)){
  324. foreach ($bomList as $key=>$value){
  325. $bomList[$key]['消耗定量'] = rtrim($value['BOM_投入数']).rtrim($value['BOM_投料单位']).'/'.rtrim($value['BOM_产出数']).rtrim($value['BOM_产出单位']);
  326. unset($bomList[$key]['BOM_投料单位'],$bomList[$key]['BOM_投入数'],$bomList[$key]['BOM_产出数'],$bomList[$key]['BOM_产出单位']);
  327. $bomList[$key]['计划用量'] = rtrim((float)$value['计划用量']);
  328. }
  329. $data['BOM'] = $bomList;
  330. }else{
  331. $data['BOM'] = [];
  332. }
  333. //工单附件资料
  334. $jsRes = db('工单_相关附件')
  335. ->where('关联编号','like','%'.$Gd_gdbh.'%')
  336. ->select();
  337. if (!empty($jsRes)){
  338. foreach ($jsRes as $key=>&$value){
  339. if(mb_detect_encoding($value['附件内容'])!='ASCII'){
  340. $value['附件内容'] = '';
  341. }
  342. }
  343. $data['jsData'] = $jsRes;
  344. }else{
  345. $data['jsData'] = [];
  346. }
  347. $this->success('成功',$data);
  348. }
  349. /**
  350. * 编辑页面展示
  351. * @ApiMethod (GET)
  352. * @param string $workOrder 工单编号
  353. * @return void
  354. * @throws \think\db\exception\DataNotFoundException
  355. * @throws \think\db\exception\ModelNotFoundException
  356. * @throws \think\exception\DbException
  357. */
  358. public function DataCorrection()
  359. {
  360. if ($this->request->isGet() === false){
  361. $this->error('请求错误');
  362. }
  363. $workOrder = input('Gd_gdbh');
  364. if (empty($workOrder)){
  365. $this->error('参数错误');
  366. }
  367. $field = 'rtrim(Gd_lx) as 重点工单,rtrim(Gd_gdbh) as 工单编号,rtrim(Gd_生产分类) as 生产类型,rtrim(Gd_khdh) as 客户代号,rtrim(Gd_客户名称) as 客户名称,
  368. rtrim(Gd_cpdh) as 印件代号,rtrim(Gd_cpmc) as 印件名称,rtrim(成品代号) as 产品代号,rtrim(成品名称) as 产品名称,rtrim(开单日期) as 开单日期,rtrim(订单数量) as 订单数量,rtrim(交货日期) as 交货日期,
  369. rtrim(投料率) as 投料率,rtrim(实际投料) as 万小张,rtrim(计量单位) as 单位,rtrim(投料大箱) as 投料大箱,rtrim(排产时库存) as 库存大箱,rtrim(警语版面) as 警语版面,
  370. rtrim(销售订单号) as 销售订单号,rtrim(产品版本号) as 版本号,rtrim(客户ERP编码) as 客户ERP编码,rtrim(码源数量) as 码源数量,rtrim(进程备注) as 进程备注,rtrim(Gd_desc) as 备注,rtrim(Uniqid) as Uniqid,rtrim(计划投料) as 平张投料';
  371. $list = \db('工单_基本资料')->where('Gd_gdbh',$workOrder)->field($field)->find();
  372. if (empty($list)){
  373. $this->error('未找到该工单信息');
  374. }
  375. $printData = \db('工单_印件资料')
  376. ->where('Yj_Gdbh',$workOrder)
  377. ->field('rtrim(yj_yjmc) as 印件名称,rtrim(yj_Yjdh) as 印件代号,rtrim(Uniqid) as id')
  378. ->cache(true,84600)
  379. ->find();
  380. if (empty($printData)){
  381. $list['印件名称'] = '';
  382. $list['印件代号'] = '';
  383. $list['印件ID'] = '';
  384. }else{
  385. $list['印件名称'] = $printData['印件名称'];
  386. $list['印件代号'] = $printData['印件代号'];
  387. $list['印件ID'] = $printData['id'];
  388. }
  389. $this->success('成功',$list);
  390. }
  391. /**
  392. * 工单资料修改
  393. * @ApiMethod (POST)
  394. * @param void
  395. * @return void
  396. * @throws \think\Exception
  397. * @throws \think\exception\PDOException
  398. */
  399. public function WorkOrderEdit()
  400. {
  401. if (Request::instance()->isPost() === false){
  402. $this->error('请求错误');
  403. }
  404. $param = Request::instance()->post();
  405. if (empty($param) || isset($param['Uniqid']) === false){
  406. $this->error('参数错误');
  407. }
  408. // 先查询原始数据用于日志对比
  409. $oldData = \db('工单_基本资料')
  410. ->where('Uniqid', $param['Uniqid'])
  411. ->find();
  412. //配置修改数据
  413. $row = [
  414. 'Gd_lx' => isset($param['lx'])?$param['lx']:'',
  415. '开单日期' => isset($param['kdrq'])?$param['kdrq']:'',
  416. 'Gd_gdbh' => isset($param['gdbh'])?$param['gdbh']:'',
  417. 'Gd_生产分类' => isset($param['scfl'])?$param['scfl']:'',
  418. 'Gd_客户代号' => isset($param['khdh'])?$param['khdh']:'',
  419. 'Gd_客户名称' => isset($param['khmc'])?$param['khmc']:'',
  420. '成品代号' => isset($param['cpdh'])?$param['cpdh']:'',
  421. '成品名称' => isset($param['cpmc'])?$param['cpmc']:'',
  422. '订单数量' => isset($param['ddsl'])?$param['ddsl']:'',
  423. '交货日期' => isset($param['jhrq'])?$param['jhrq']:'',
  424. '投料率' => isset($param['tll'])?$param['tll']:'',
  425. '计划投料' => isset($param['jhtl'])?$param['jhtl']:'',
  426. '实际投料' => isset($param['sjtl'])?$param['sjtl']:'',
  427. '计量单位' => isset($param['jldw'])?$param['jldw']:'',
  428. '投料大箱' => isset($param['tldx'])?$param['tldx']:'',
  429. '销售订单号' => isset($param['xsddh'])?$param['xsddh']:'',
  430. '警语版面' => isset($param['jymb'])?$param['jymb']:'',
  431. '产品版本号' => isset($param['bbh'])?$param['bbh']:'',
  432. '客户ERP编码' => isset($param['erp'])?$param['erp']:'',
  433. '码源数量' => isset($param['ymsl'])?$param['ymsl']:'',
  434. '进程备注' => isset($param['jcbz'])?$param['jcbz']:'',
  435. 'Gd_desc' => isset($param['desc'])?$param['desc']:'',
  436. '排产时库存' => isset($param['kc'])?$param['kc']:'',
  437. '平均合格率' => isset($param['avg'])?$param['avg']:'',
  438. ];
  439. // 修改工单主信息时记录变更
  440. $changes = [];
  441. foreach ($row as $field => $newValue) {
  442. $oldValue = $oldData[$field] ?? null;
  443. if ($oldValue != $newValue) {
  444. $changes[] = [
  445. 'field' => $field,
  446. 'old' => $oldValue,
  447. 'new' => $newValue
  448. ];
  449. }
  450. }
  451. //获取印件资料
  452. $printCode = \db('工单_印件资料')
  453. ->where('Uniqid',$param['printID'])
  454. ->value('yj_Yjno');
  455. if (!empty($printCode)){
  456. if ((int)$param['jhtl'] >0){
  457. //修改工单工艺计划产量
  458. $result = $this->PlannedProcessYield($param['gdbh'],$printCode,0,$param['jhtl']);
  459. if ($result === false){
  460. $this->success('分配工序计划产量失败');
  461. }
  462. }
  463. //修改印件信息
  464. $printSql = \db('工单_印件资料')
  465. ->where('Uniqid',$param['printID'])
  466. ->fetchSql(true)
  467. ->update(['Yj_Gdbh'=>$param['gdbh'],'yj_yjmc'=>$param['yjmc'],'yj_Yjdh'=>$param['yjdh'],'yj_平张投料'=>$param['jhtl']]);
  468. $printRes = Db::query($printSql);
  469. }
  470. //修改工单信息
  471. $sql = \db('工单_基本资料')->where('Uniqid',$param['Uniqid'])->fetchSql(true)->update($row);
  472. $res = Db::query($sql);
  473. // 记录修改日志(仅当有字段变更时)
  474. if (!empty($changes)) {
  475. $logData = [];
  476. foreach ($changes as $change) {
  477. $logData[] = [
  478. 'Gd_gdbh' => $oldData['Gd_gdbh'], // 使用原始工单编号
  479. 'ModifyUser' => $param['sys_id'],
  480. 'ModifyTime' => date('Y-m-d H:i:s'),
  481. 'FieldName' => $change['field'],
  482. 'OldValue' => $change['old'],
  483. 'NewValue' => $change['new'],
  484. 'ModifySource' => '工单编辑'
  485. ];
  486. }
  487. $this->saveWorkOrderOperateLog($logData);
  488. }
  489. //修改订单数量该工单下的订单数量
  490. //1. 查询该id下的工单编号, 防止其手动修改工单编号导致条件错误
  491. $gdbh = \db('工单_基本资料')->where('Uniqid',$param['Uniqid'])->value('Gd_gdbh');
  492. // 获取修改前的订单数量
  493. $oldQuantity = \db('工单_基本资料')
  494. ->where('Gd_gdbh', $gdbh)
  495. ->value('订单数量');
  496. //2. 根据查出的工单编号修改订单数量
  497. $sql = \db('工单_基本资料')->where('Gd_gdbh',$param['gdbh'])->fetchSql(true)->update(['订单数量'=>$row['订单数量']]);
  498. $res1 = Db::query($sql);
  499. // 记录订单数量单独变更
  500. if ($res1 && $oldQuantity != $row['订单数量']) {
  501. $this->saveWorkOrderOperateLog([[
  502. 'Gd_gdbh' => $gdbh,
  503. 'ModifyUser' => $param['sys_id'],
  504. 'FieldName' => '订单数量',
  505. 'OldValue' => $oldQuantity,
  506. 'NewValue' => $row['订单数量'],
  507. 'ModifySource' => '数量修正'
  508. ]]);
  509. }
  510. if ($res !== false && $res1 !== false){
  511. $this->success('成功');
  512. }else{
  513. $this->error('失败');
  514. }
  515. }
  516. /**
  517. * U8投料试算
  518. * @ApiMethod (GET)
  519. * @param string $processCode 产品编号
  520. * @return void
  521. * @throws \think\db\exception\DataNotFoundException
  522. * @throws \think\db\exception\ModelNotFoundException
  523. * @throws \think\exception\DbException
  524. */
  525. public function U8Trial()
  526. {
  527. if ($this->request->isGet() === false)
  528. {
  529. $this->error('请求错误');
  530. }
  531. $productCode = input('productCode');
  532. if (empty($productCode)){
  533. $this->error('参数错误');
  534. }
  535. $field = 'rtrim(Gy0_cpdh) as 产品代号,rtrim(Gy0_yjno) as 印件号,rtrim(Gy0_gxh) as 工序号,rtrim(gy0_gxmc) as gxmc,rtrim(Add_gxmc) as add_gxmc,
  536. rtrim(Gy0_Ms) as 墨色数,rtrim(Gy0_shdh) as 损耗代号,rtrim(损耗系数) as 损耗系数,rtrim(Gy0_ls) as 加工联数';
  537. $list = \db('产品_工艺资料')->where('Gy0_cpdh',$productCode)->field($field)->select();
  538. if (empty($list)){
  539. $this->success('未找到该产品工序');
  540. }
  541. foreach ($list as $key=>$value){
  542. $data = \db('dic_lzsh')->where('sys_bh',$value['损耗代号'])->field('rtrim(sys_rate0) as rate0,rtrim(sys_rate1) as rate1')->cache(true)->find();
  543. $list[$key]['调机损耗'] = isset($data['rate0'])?$data['rate0']:'';
  544. $list[$key]['运行损耗率'] = isset($data['rate1'])?$data['rate1']:'';
  545. if ($value['add_gxmc'] !== ''){
  546. $list[$key]['工序名称'] = $value['gxmc'].'【'.$value['add_gxmc'].'】';
  547. }else{
  548. $list[$key]['工序名称'] = $value['gxmc'];
  549. }
  550. unset($list[$key]['gxmc'],$list[$key]['add_gxmc']);
  551. }
  552. $this->success('成功',$list);
  553. }
  554. /**
  555. * 引用产品资料->获取产品资料
  556. * @ApiMethod (GET)
  557. * @param string $workOrder 工单编号
  558. * @return void
  559. * @throws \think\db\exception\DataNotFoundException
  560. * @throws \think\db\exception\ModelNotFoundException
  561. * @throws \think\exception\DbException
  562. */
  563. public function ProductInformation()
  564. {
  565. if ($this->request->isGet() === false)
  566. {
  567. $this->error('请求错误');
  568. }
  569. $workOrder = input('workOrder');
  570. if (empty($workOrder)){
  571. $this->error('参数错误');
  572. }
  573. $field = 'rtrim(Gd_gdbh) as 工单编号,rtrim(Gd_客户名称) as 客户名称,rtrim(成品代号) as 产品代号,rtrim(成品名称) as 产品名称';
  574. $Detail = \db('工单_基本资料')->where('Gd_gdbh',$workOrder)->field($field)->find();
  575. if (empty($Detail)){
  576. $this->success('未找到工单信息');
  577. }
  578. $Detail['客户代号'] = substr($Detail['产品代号'],0,4);
  579. $this->success('成功',$Detail);
  580. }
  581. /**
  582. * 引用产品资料->复制产品工艺资料
  583. * @ApiMethod (POST)
  584. * @param string $oldWorkOrder 被复制工单编号
  585. * @param string $newWorkOrder 复制工单编号
  586. * @return void
  587. * @throws \think\Exception
  588. * @throws \think\db\exception\DataNotFoundException
  589. * @throws \think\db\exception\ModelNotFoundException
  590. * @throws \think\exception\DbException
  591. * @throws \think\exception\PDOException
  592. */
  593. public function ProductInformationEdit()
  594. {
  595. if (Request::instance()->isPost() === false)
  596. {
  597. $this->error('请求错误');
  598. }
  599. $oldWorkOrder = input('oldWorkOrder');
  600. $newWorkOrder = input('newWorkOrder');
  601. if (empty($oldWorkOrder) || empty($newWorkOrder))
  602. {
  603. $this->error('参数错误');
  604. }
  605. $lastId = \db('工单_工艺资料')->order('UniqId desc')->value('UniqId');
  606. if ($lastId<10000000){
  607. $lastId = 10000000;
  608. }else{
  609. $lastId = $lastId + 1;
  610. }
  611. //获取原工单工艺资料
  612. $oldProcessData = \db('工单_工艺资料')->where('Gy0_gdbh',$newWorkOrder)->select();
  613. $ProsessUniqId = \db('工单_工艺资料')->field('UniqId')->order('UniqId desc')->find();
  614. foreach ($oldProcessData as $k=>$v){
  615. $oldProcessData[$k]['Gy0_gdbh'] = $oldWorkOrder;
  616. $oldProcessData[$k]['Sys_id'] = '';
  617. $oldProcessData[$k]['Gy0_sj1'] = '1900-01-01 00:00:00';
  618. $oldProcessData[$k]['Gy0_sj2'] = '1900-01-01 00:00:00';
  619. $oldProcessData[$k]['UniqId'] = $lastId + $k;
  620. $oldProcessData[$k]['PD_WG'] = '1900-01-01 00:00:00';
  621. }
  622. if (\db('工单_工艺资料')->where('Gy0_gdbh',$oldWorkOrder)->find()){
  623. \db('工单_工艺资料')->where('Gy0_gdbh',$oldWorkOrder)->delete();
  624. }
  625. //获取原工单印件资料
  626. $lastUniqId = \db('工单_印件资料')->order('UniqId desc')->value('UniqId');
  627. if ($lastUniqId<1000000){
  628. $lastUniqId = 1000000;
  629. }else{
  630. $lastUniqId = $lastUniqId + 1;
  631. }
  632. $oldPrintData = \db('工单_印件资料')->where('Yj_Gdbh',$newWorkOrder)->select();
  633. $PrintUniqId = \db('工单_印件资料')->field('Uniqid')->order('Uniqid desc')->find();
  634. foreach ($oldPrintData as $k=>$v){
  635. $oldPrintData[$k]['Yj_Gdbh'] = $oldWorkOrder;
  636. $oldPrintData[$k]['Sys_id'] = '';
  637. $oldPrintData[$k]['Uniqid'] = $lastUniqId +$k +1;
  638. $oldPrintData[$k]['质量考核'] = 1;
  639. }
  640. if (\db('工单_印件资料')->where('Yj_Gdbh',$oldWorkOrder)->find()){
  641. \db('工单_印件资料')->where('Yj_Gdbh',$oldWorkOrder)->delete();
  642. }
  643. //复制印件、工艺资料
  644. $ProcessSQL = \db('工单_工艺资料')->fetchSql(true)->insertAll($oldProcessData);
  645. $ProcessRes = Db::query($ProcessSQL);
  646. $PrintSQL = \db('工单_印件资料')->fetchSql(true)->insertAll($oldPrintData);
  647. $PrintRes = Db::query($PrintSQL);
  648. if ($ProcessRes !== false && $PrintRes !== false){
  649. $this->success('成功');
  650. }else{
  651. $this->error('失败');
  652. }
  653. }
  654. /**
  655. * 工艺流程调成->获取当前工单工艺资料
  656. * @ApiMethod (GET)
  657. * @param string $workorder 当前工单编号
  658. * @return void
  659. * @throws \think\db\exception\DataNotFoundException
  660. * @throws \think\db\exception\ModelNotFoundException
  661. * @throws \think\exception\DbException
  662. */
  663. public function ProcessFlow()
  664. {
  665. if ($this->request->isGet() === false){
  666. $this->error('请求错误');
  667. }
  668. $workOrder = input('workOrder');
  669. if (empty($workOrder)){
  670. $this->error('参数错误');
  671. }
  672. $list = \db('工单_基本资料')->where('Gd_gdbh',$workOrder)->field('rtrim(成品代号) as 成品编号,rtrim(成品名称) as 成品名称')->find();
  673. if (empty($list)){
  674. $this->success('未找到工单信息');
  675. }
  676. $filed = 'rtrim(Gy0_方案) as 方案,rtrim(Gy0_yjno) as 印件号,rtrim(Gy0_gxh) as 工序号,rtrim(Gy0_gxmc) as gxmc,rtrim(Add_gxmc) as add_gxmc,
  677. rtrim(工价系数) as 工价系数,rtrim(损耗系数) as 损耗系数,rtrim(Gy0_ks) as ks,rtrim(Gy0_ls) as ls,rtrim(工序备注) as 备注,rtrim(Gy0_SITE) as 车间,
  678. rtrim(Gy0_sbbh) as 设备编号,rtrim(Gy0_sbmc) as 设备名称,rtrim(Sys_id) as 建档用户,rtrim(Sys_rq) as 建档时间,rtrim(Mod_rq) as 更新时间,rtrim(UniqId) as UniqId';
  679. $process = \db('工单_工艺资料')->where('Gy0_gdbh',$workOrder)->cache(true,84600)->field($filed)->select();
  680. if (empty($process)){
  681. $this->error('未找到该工单工艺资料');
  682. }
  683. foreach ($process as $key=>$value){
  684. if (isset($value['add_gxmc'])){
  685. $process[$key]['工序名称'] = $value['gxmc'] . '【'.$value['add_gxmc'].'】';
  686. }else{
  687. $process[$key]['工序名称'] = $value['gxmc'];
  688. }
  689. $process[$key]['工价系数'] = (float)$value['工价系数'];
  690. $process[$key]['损耗系数'] = (float)$value['损耗系数'];
  691. $process[$key]['开数*联数'] = $value['ks'].'*'.$value['ls'];
  692. }
  693. $list['process'] = $process;
  694. $this->success('成功',$list);
  695. }
  696. /**
  697. * 参照工单列表获取
  698. * @ApiMethod (GET)
  699. * @param string $workOrder 工单编号
  700. * @param string $productCode 产品代号
  701. * @return void
  702. */
  703. public function ReferenceWorkOrder()
  704. {
  705. if ($this->request->isGet() === false){
  706. $this->error('请求错误');
  707. }
  708. $productCode = input('productCode');
  709. $workOrder = input('workOrder');
  710. if (empty($productCode) || empty($workOrder)){
  711. $this->error('参数错误');
  712. }
  713. $list = \db('工单_基本资料')->where('成品代号',$productCode)->distinct(true)->where('Gd_gdbh','<>',$workOrder)->column('Gd_gdbh');
  714. if (empty($list)){
  715. $this->success('未获取该产品其他工单信息');
  716. }
  717. $this->success('成功',$list);
  718. }
  719. /**
  720. * 工艺资料复制
  721. * @ApiMethod (POST)
  722. * @param string $oldWorkOrder 被复制工单编号
  723. * @param string $newWorkOrder 复制工单编号
  724. * @return void
  725. * @throws \think\Exception
  726. * @throws \think\db\exception\DataNotFoundException
  727. * @throws \think\db\exception\ModelNotFoundException
  728. * @throws \think\exception\DbException
  729. * @throws \think\exception\PDOException
  730. *
  731. */
  732. public function ProcessCopy()
  733. {
  734. if (Request::instance()->isPost() === false){
  735. $this->error('请求错误');
  736. }
  737. $param = Request::instance()->post();
  738. if (empty($param)){
  739. $this->error('参数错误');
  740. }
  741. $i = 0;
  742. foreach ($param as $key => $value){
  743. $sql = \db('工单_工艺资料')
  744. ->where('UniqId',$value['UniqId'])
  745. ->fetchSql(true)
  746. ->update(['Gy0_gxh'=>$value['gxh']]);
  747. $res = \db()->query($sql);
  748. if ($res === false){
  749. $i++;
  750. }
  751. }
  752. if ($i === 0){
  753. $this->success('成功');
  754. }else{
  755. $this->error('失败');
  756. }
  757. }
  758. /**
  759. * U8工单列表
  760. * @ApiMethod (GET)
  761. * @param void
  762. * @return void
  763. * @throws \think\db\exception\DataNotFoundException
  764. * @throws \think\db\exception\ModelNotFoundException
  765. * @throws \think\exception\DbException
  766. */
  767. public function U8workOrder()
  768. {
  769. if ($this->request->isGet() === false){
  770. $this->error('请求错误');
  771. }
  772. $param = $this->request->param();
  773. if (empty($param)){
  774. $this->error('参数错误');
  775. }
  776. $filed = 'rtrim(Gd_gdbh) as 工单编号,rtrim(行号) as 行号,rtrim(Gd_客户代号) as 客户代号,rtrim(Gd_客户名称) as 客户名称,
  777. rtrim(成品代号) as 成品代号,rtrim(成品名称) as 成品名称,rtrim(Mod_rq) as 获取日期,rtrim(Uniqid) as 序号';
  778. $list = \db('工单_基本资料')->where('Gd_gdbh',$param['workOrder'])->field($filed)->select();
  779. if (empty($list)){
  780. $this->success('未找到工单');
  781. }
  782. $this->success('成功',$list);
  783. }
  784. /**
  785. * U8工单资料删除
  786. * @param string $workOrder 工单编号
  787. * @return void
  788. * @throws \think\Exception
  789. * @throws \think\exception\PDOException
  790. */
  791. public function U8DataCorrection()
  792. {
  793. if($this->request->isGet() === false){
  794. $this->error('请求错误');
  795. }
  796. $workOrder = input('Uniqid');
  797. if (empty($workOrder)){
  798. $this->error('参数错误');
  799. }
  800. $data = [];
  801. if (strpos($workOrder,',')){
  802. $data = explode(',',$workOrder);
  803. }else{
  804. $data[0] = $workOrder;
  805. }
  806. $res = \db('工单_基本资料')->where('Uniqid','in',$data)->delete();
  807. if ($res !== false){
  808. $this->success('成功');
  809. }else{
  810. $this->error('失败');
  811. }
  812. }
  813. /**
  814. * 产品废检系数调整->质检工艺数据获取
  815. * @ApiMethod (GET)
  816. * @param string $workOrder 工单编号
  817. * @return void
  818. * @throws \think\db\exception\DataNotFoundException
  819. * @throws \think\db\exception\ModelNotFoundException
  820. * @throws \think\exception\DbException
  821. */
  822. public function TestCoefficient()
  823. {
  824. if ($this->request->isGet() === false){
  825. $this->error('请求错误');
  826. }
  827. $workOrder = input('workOrder');
  828. if (empty($workOrder)){
  829. $this->error('参数错误');
  830. }
  831. $where = [
  832. 'Gy0_gdbh' => $workOrder,
  833. 'Gy0_gxmc' => ['like','%检%']
  834. ];
  835. $filed = 'rtrim(Gy0_gdbh) as gdbh,rtrim(Gy0_yjno) as yjno,rtrim(Gy0_gxh) as gxh,rtrim(Gy0_gxmc) as gxmc,rtrim(人工检_正品板) as 人工正品板,
  836. rtrim(人工检_次品板) as 人工次品板,rtrim(人工检_废检) as 人工废检,rtrim(机检_正品板) as 机检正品板,rtrim(机检_次品板) as 机检次品板,
  837. rtrim(机检_废检) as 机检废检,rtrim(Gy0_sbbh) as 设备编号,rtrim(Uniqid) as Uniqid';
  838. $list = \db('工单_工艺资料')->where($where)->field($filed)->select();
  839. if (empty($list)){
  840. $this->success('未找到该工单工艺');
  841. }
  842. $name = \db('工单_基本资料')
  843. ->where('Gd_gdbh',$workOrder)
  844. ->field('rtrim(Gd_cpdh) as 产品代号,rtrim(Gd_khmc) as 客户名称,rtrim(Gd_cpmc) as 产品名称')
  845. ->find();
  846. if (empty($name)){
  847. $this->success('未找到该工单');
  848. }
  849. foreach ($list as $key=>$value){
  850. if ($value['yjno']<10){
  851. $value['yjno'] = '0'.$value['yjno'];
  852. }
  853. if ($value['gxh']<10){
  854. $value['gxh'] = '0'.$value['gxh'];
  855. }
  856. $list[$key]['印件工序及工艺'] = $value['gdbh'].'-'.$value['yjno'].'-'.$value['gxh'].'-'.$value['gxmc'];
  857. unset($list[$key]['gdbh'],$list[$key]['yjno'],$list[$key]['gxmc']);
  858. $list[$key]['产品编号'] = $name['产品代号'];
  859. $list[$key]['产品名称'] = $name['产品名称'];
  860. $list[$key]['客户名称'] = $name['客户名称'];
  861. }
  862. $this->success('成功',$list);
  863. }
  864. /**
  865. * 产品质检系数调整->系数修改
  866. * @ApiMethod (GET)
  867. * @param string $workorder 工单编号
  868. * @param string $processCode 工序号
  869. * @param float $code1 人工正品板
  870. * @param float $code2 人工次品板
  871. * @param float $code3 人工废检
  872. * @param float $code4 机检正品板
  873. * @param float $code5 机检次品板
  874. * @param float $code6 机检废检
  875. * @return void
  876. * @throws \think\Exception
  877. * @throws \think\exception\PDOException
  878. */
  879. public function TestCoefficientEdit()
  880. {
  881. if (Request::instance()->isPost() === false){
  882. $this->error('请求错误');
  883. }
  884. $workOrder = input('workOrder');
  885. $processCode = input('processCode');
  886. $row = [
  887. '人工检_正品板' => input('code1'),
  888. '人工检_次品板' => input('code2'),
  889. '人工检_废检' => input('code3'),
  890. '机检_正品板' => input('code4'),
  891. '机检_次品板' => input('code5'),
  892. '机检_废检' => input('code6'),
  893. ];
  894. if (empty($workOrder) || empty($processCode)){
  895. $this->error('参数错误');
  896. }
  897. $where = [
  898. 'Gy0_gdbh' => $workOrder,
  899. 'Gy0_gxh' => $processCode
  900. ];
  901. $sql = \db('工单_工艺资料')->where($where)->fetchSql(true)->update($row);
  902. $res = Db::query($sql);
  903. if ($res !== false){
  904. $this->success('成功');
  905. }else{
  906. $this->error('失败');
  907. }
  908. }
  909. /**
  910. * 修正工单核算参数->数据获取
  911. * @ApiMethod (GET)
  912. * @param string $workOrder 工单编号
  913. * @return void
  914. * @throws \think\db\exception\DataNotFoundException
  915. * @throws \think\db\exception\ModelNotFoundException
  916. * @throws \think\exception\DbException
  917. */
  918. public function AccountingParameter()
  919. {
  920. if ($this->request->isGet() === false){
  921. $this->error('请求错误');
  922. }
  923. $workOrder = input('workOrder');
  924. if (empty($workOrder)){
  925. $this->error('参数错误');
  926. }
  927. $field = 'rtrim(Gy0_方案) as 方案,rtrim(Gy0_yjno) as yjno,rtrim(Gy0_gxh) as gxh,rtrim(Gy0_gxmc) as gxmc,rtrim(Add_gxmc) as add_gxmc,
  928. rtrim(Gy0_sbbh) as 参照设备,rtrim(工价系数) as 难度系数,rtrim(Gy0_shdh) as 损耗代号,rtrim(Gy0_Rate0) as 基础损耗,rtrim(Gy0_Rate1) as 损耗率,
  929. rtrim(印刷方式) as 印刷方式,rtrim(版距) as 版距,rtrim(Gy0_ms) as 计损色数,rtrim(损耗系数) as 损耗系数,rtrim(UniqId) as UniqId';
  930. $list = \db('工单_工艺资料')->where('Gy0_gdbh',$workOrder)->field($field)->select();
  931. if (empty($list)){
  932. $this->success('未找到该工单工艺资料');
  933. }
  934. foreach ($list as $key=>$value){
  935. if ($value['yjno']<10){
  936. $value['yjno'] = '0'.$value['yjno'];
  937. }
  938. if ($value['gxh']<10){
  939. $value['gxh'] = '0'.$value['gxh'];
  940. }
  941. $list[$key]['印件号及工序名称'] = $value['yjno'].'-'.$value['gxh'].$value['gxmc'].'('.$value['add_gxmc'].')';
  942. }
  943. $this->success('成功',$list);
  944. }
  945. /**
  946. * 修正工单核算参数->参数修改
  947. * @ApiMethod (POST)
  948. * @param void
  949. * @return void
  950. * @throws \think\Exception
  951. * @throws \think\db\exception\DataNotFoundException
  952. * @throws \think\db\exception\ModelNotFoundException
  953. * @throws \think\exception\DbException
  954. * @throws \think\exception\PDOException
  955. */
  956. public function AccountingParameterEdit()
  957. {
  958. if (Request::instance()->isPost() === false){
  959. $this->error('请求错误');
  960. }
  961. $param = Request::instance()->post();
  962. if (empty($param) || isset($param[0]['Uniqid']) === false){
  963. $this->error('参数错误');
  964. }
  965. $i = 0;
  966. foreach ($param as $key=>$value){
  967. $data['rate0'] = 0;
  968. $data['rate1'] = 0;
  969. $rate = \db('dic_lzsh')->where('sys_bh',$value['loss'])->field('rtrim(sys_rate0) as rate0,rtrim(sys_rate1) as rate1')->find();
  970. if (!empty($rate)){
  971. $data['rate0'] = $rate['rate0'];
  972. $data['rate1'] = $rate['rate1'];
  973. }
  974. $row = [
  975. 'Gy0_shdh' => $value['loss']?:'',
  976. '印刷方式' => $value['printMode']?:'',
  977. '版距' => $value['plate']?:'',
  978. 'Gy0_ms' => $value['chromatic']?:'',
  979. '损耗系数' => $value['wastage']?:'',
  980. 'Gy0_Rate0' => $data['rate0'],
  981. 'Gy0_Rate1' => $data['rate1']
  982. ];
  983. if (isset($value['difficulty'])){
  984. $row['工价系数'] = $value['difficulty'];
  985. }
  986. $sql = \db('工单_工艺资料')->where('Uniqid',$value['Uniqid'])->fetchSql(true)->update($row);
  987. $res = Db::query($sql);
  988. if ($res !== false){
  989. $i++;
  990. }
  991. }
  992. if ($i !== 0){
  993. $this->success('成功');
  994. }else{
  995. $this->error('失败');
  996. }
  997. }
  998. /**
  999. * 印件资料修改
  1000. * @ApiMethod (POST)
  1001. * @param void
  1002. * @return void
  1003. * @throws \think\Exception
  1004. * @throws \think\exception\PDOException
  1005. */
  1006. public function PrintedEdit()
  1007. {
  1008. if (Request::instance()->isPost() === false){
  1009. $this->error('请求错误');
  1010. }
  1011. $param = Request::instance()->post();
  1012. if (empty($param) || isset($param['Uniqid']) === false){
  1013. $this->error('参数错误');
  1014. }
  1015. $oldPrint = \db('工单_印件资料')->where('Uniqid',$param['Uniqid'])->find();
  1016. $param['Yj_Gdbh'] = $oldPrint ? $oldPrint['Yj_Gdbh'] : null;
  1017. $data = [
  1018. 'Yj_Gdbh' =>$param['Yj_Gdbh'],
  1019. 'yj_Yjno' =>$param['yjno'],
  1020. 'yj_Yjdh' =>$param['yjdh'],
  1021. 'yj_yjmc' =>$param['yjmc'],
  1022. 'yj_zzdh' =>$param['zzdh'],
  1023. 'yj_zzdh1' =>$param['zzdh1'],
  1024. 'yj_zzdh2' =>$param['zzdh2'],
  1025. 'yj_zzdh3' =>$param['zzdh3'],
  1026. 'yj_zzdh4' =>$param['zzdh4'],
  1027. 'yj_zzmc' =>$param['zzmc'],
  1028. 'yj_zzmc1' =>$param['zzmc1'],
  1029. 'yj_zzmc2' =>$param['zzmc2'],
  1030. 'yj_zzmc3' =>$param['zzmc3'],
  1031. 'yj_zzmc4' =>$param['zzmc4'],
  1032. 'yj_tlgg' =>$param['tlgg'],
  1033. 'yj_klgg' =>$param['klgg'],
  1034. 'Yj_核算规格' =>$param['hsgg'],
  1035. 'yj_成品数量' =>$param['cpsl'],
  1036. 'yj_平张投料' =>$param['pztl'],
  1037. 'yj_ks' =>$param['ks'],
  1038. 'yj_ls' =>$param['ls'],
  1039. 'yj_desc' =>$param['desc'],
  1040. ];
  1041. $UniqId = $param['Uniqid'];
  1042. $sql = \db('工单_印件资料')->where('Uniqid',$UniqId)->fetchSql(true)->update($data);
  1043. $res = Db::query($sql);
  1044. if ($res !== false && !empty($oldPrint)){
  1045. $logs = [];
  1046. foreach ($data as $field => $newValue) {
  1047. $oldValue = array_key_exists($field, $oldPrint) ? $oldPrint[$field] : '';
  1048. if (trim((string)$oldValue) === trim((string)$newValue)) {
  1049. continue;
  1050. }
  1051. $logs[] = [
  1052. 'Yj_Gdbh' => $oldPrint['Yj_Gdbh'],
  1053. 'yj_Yjno' => $data['yj_Yjno'],
  1054. 'yj_Yjdh' => $data['yj_Yjdh'],
  1055. 'yj_yjmc' => $data['yj_yjmc'],
  1056. 'PrintUniqid' => $UniqId,
  1057. '操作类型' => '修改',
  1058. '字段名' => $field,
  1059. '修改前' => trim((string)$oldValue),
  1060. '修改后' => trim((string)$newValue),
  1061. 'sys_id' => isset($param['sys_id']) ? $param['sys_id'] : '',
  1062. ];
  1063. }
  1064. $this->savePrintOperateLog($logs);
  1065. }
  1066. $process = \db('工单_工艺资料')
  1067. ->where('Gy0_gdbh',$data['Yj_Gdbh'])
  1068. ->where('Gy0_yjno',$data['yj_Yjno'])
  1069. ->select();
  1070. if ((int)$data['yj_平张投料'] > 0 && !empty($process)){
  1071. //重新分配工序计划产量
  1072. $result = $this->PlannedProcessYield($data['Yj_Gdbh'],$data['yj_Yjno'],0,$data['yj_平张投料']);
  1073. if ($result === false){
  1074. $this->success('修改工序产量失败');
  1075. }
  1076. }
  1077. if ($res !== false){
  1078. $this->success('成功');
  1079. }else{
  1080. $this->error('失败');
  1081. }
  1082. }
  1083. /**
  1084. * 工艺资料修改
  1085. * @ApiMethod (POST)
  1086. * @param void
  1087. * @return void
  1088. * @throws \think\Exception
  1089. * @throws \think\db\exception\DataNotFoundException
  1090. * @throws \think\db\exception\ModelNotFoundException
  1091. * @throws \think\exception\DbException
  1092. * @throws \think\exception\PDOException
  1093. */
  1094. public function ProcessDetailEdit()
  1095. {
  1096. Db::startTrans();
  1097. try {
  1098. // 请求验证
  1099. if (!Request::instance()->isPost()) {
  1100. throw new Exception('非法请求方法');
  1101. }
  1102. $param = Request::instance()->post();
  1103. if (empty($param) || !isset($param['UniqId'])) {
  1104. throw new Exception('缺少必要参数');
  1105. }
  1106. $UniqId = $param['UniqId'];
  1107. // 获取原始数据(全字段)
  1108. $oldData = Db::name('工单_工艺资料')
  1109. ->where('UniqId', $UniqId)
  1110. ->find();
  1111. if (!$oldData) {
  1112. throw new Exception('目标数据不存在');
  1113. }
  1114. // 动态构建更新数据 --------------------------------------------------------
  1115. $updateData = [];
  1116. $logsData = [];
  1117. $systemFields = array('UniqId', 'Mod_rq'); // 显式声明数组
  1118. // 合并所有有效字段(PHP7.2兼容写法)
  1119. foreach ($param as $key => $value) {
  1120. if (!in_array($key, $systemFields)) {
  1121. $updateData[$key] = $value;
  1122. $logsData[$key] = $value;
  1123. }
  1124. }
  1125. // 处理业务逻辑字段
  1126. if (empty($param['Gy0_shdh'])) {
  1127. $updateData['Gy0_Rate0'] = 0;
  1128. $updateData['Gy0_Rate1'] = 0;
  1129. } else {
  1130. $rate = Db::name('dic_lzsh')
  1131. ->where('sys_bh', $param['Gy0_shdh'])
  1132. ->field('rtrim(sys_rate0) as rate0, rtrim(sys_rate1) as rate1')
  1133. ->find();
  1134. $rate = $rate ? $rate : array('rate0' => 0, 'rate1' => 0);
  1135. $updateData['Gy0_Rate0'] = $rate['rate0'];
  1136. $updateData['Gy0_Rate1'] = $rate['rate1'];
  1137. }
  1138. // 添加系统字段
  1139. $updateData['Mod_rq'] = date('Y-m-d H:i:s');
  1140. // 执行全字段更新(PHP7.2兼容的回调函数)-----------------------------------
  1141. $rawSql = Db::name('工单_工艺资料')
  1142. ->where('UniqId', $UniqId)
  1143. ->fetchSql(true)
  1144. ->update($updateData);
  1145. // 中文字段安全处理(传统匿名函数写法)
  1146. $safeSql = preg_replace_callback(
  1147. '/`([\x{4e00}-\x{9fa5}]+)`/u',
  1148. function($matches) {
  1149. return '`' . addslashes($matches[1]) . '`';
  1150. },
  1151. $rawSql
  1152. );
  1153. // 执行并验证
  1154. $affected = Db::execute($safeSql);
  1155. if ($affected < 0) {
  1156. throw new Exception('数据库执行失败');
  1157. }
  1158. // 记录全字段变更日志 -----------------------------------------------------
  1159. $changes = array();
  1160. foreach ($logsData as $field => $newValue) {
  1161. $oldValue = isset($oldData[$field]) ? $oldData[$field] : 'NULL';
  1162. // 严格值比较(PHP7.2兼容的类型转换)
  1163. if ((string)$oldValue !== (string)$newValue) {
  1164. $changes[] = array(
  1165. 'field' => $field,
  1166. 'old' => $oldValue,
  1167. 'new' => $newValue
  1168. );
  1169. }
  1170. }
  1171. if (!empty($changes)) {
  1172. $logs = array();
  1173. foreach ($changes as $change) {
  1174. $logs[] = array(
  1175. 'Gd_gdbh' => $oldData['Gy0_gdbh'],
  1176. 'yjno' => $oldData['Gy0_yjno'],
  1177. 'gxh' => $oldData['Gy0_gxh'],
  1178. 'gxmc' => isset($oldData['Gy0_gxmc']) ? $oldData['Gy0_gxmc'] : '',
  1179. 'ModifyUser' => $param['Sys_id'],
  1180. 'ModifyTime' => date('Y-m-d H:i:s'),
  1181. 'FieldName' => $change['field'],
  1182. 'OldValue' => $change['old'],
  1183. 'NewValue' => $change['new'],
  1184. 'ModifySource' => '工艺调整'
  1185. );
  1186. }
  1187. $this->saveWorkOrderOperateLog($logs);
  1188. }
  1189. //获取工单第一道工艺编号
  1190. $minGxh = \db('工单_工艺资料')->where('Gy0_gdbh',$param['Gy0_gdbh'])->column('min(Gy0_gxh) as gxh');
  1191. //获取工艺资料数据
  1192. $list = \db('工单_工艺资料')->where('UniqId',$UniqId)->field('Gy0_yjno,Gy0_gxh,rtrim(Gy0_计划接货数) as 计划接货数')->find();
  1193. //如果是第一道工序需从印件资料中获取计划产量
  1194. $list['计划接货数'] = \db('工单_印件资料')->where('Yj_Gdbh',$param['Gy0_gdbh'])->where('yj_Yjno',$param['Gy0_yjno'])->column('yj_平张投料')[0];
  1195. //修改工单状态
  1196. $status = \db('工单_基本资料')->where('Gd_gdbh',$param['Gy0_gdbh'])->field('rtrim(gd_statu) as status')->find();
  1197. if ($status['status'] !== '2-生产中'){
  1198. $statusSql = \db('工单_基本资料')->where('Gd_gdbh',$param['Gy0_gdbh'])->fetchSql(true)->update(['gd_statu'=>'2-生产中']);
  1199. Db::query($statusSql);
  1200. $logData = [
  1201. 'Gd_gdbh' => $param['Gy0_gdbh'],
  1202. 'ModifyUser' => $param['Sys_id'],
  1203. 'ModifyTime' => date('Y-m-d H:i:s'),
  1204. 'FieldName' => 'gd_statu',
  1205. 'OldValue' => $status['status'],
  1206. 'NewValue' => '2-生产中',
  1207. 'ModifySource' => '工单状态变更'
  1208. ];
  1209. $this->saveWorkOrderOperateLog([$logData]);
  1210. }
  1211. //重新分配工序计划产量
  1212. if ((int)$list['计划接货数'] > 0){
  1213. $result = $this->PlannedProcessYield($param['Gy0_gdbh'],$list['Gy0_yjno'],0,$list['计划接货数']);
  1214. }
  1215. Db::commit();
  1216. $this->success('操作成功');
  1217. } catch (Exception $e) {
  1218. Db::rollback();
  1219. $this->error($e->getMessage());
  1220. }
  1221. }
  1222. /**
  1223. * 工艺资料编辑->机台列表获取
  1224. * @return void
  1225. * @throws \think\db\exception\DataNotFoundException
  1226. * @throws \think\db\exception\ModelNotFoundException
  1227. * @throws \think\exception\DbException
  1228. */
  1229. public function MachineList()
  1230. {
  1231. if ($this->request->isGet() === false){
  1232. $this->error('请求错误');
  1233. }
  1234. $param = $this->request->param();
  1235. if (empty($param)){
  1236. $this->error('参数错误');
  1237. }
  1238. $list = \db('设备_基本资料')
  1239. ->where('使用部门',$param['address'])
  1240. ->where('sys_sbID','<>','')
  1241. ->field('rtrim(设备编号) as 设备编号,rtrim(设备名称) as 设备名称')
  1242. ->order('设备编组,设备编号')
  1243. ->select();
  1244. if (empty($list)){
  1245. $this->success('未找到该车间机台');
  1246. }
  1247. $data = [];
  1248. foreach ($list as $key=>$value){
  1249. $data[$key] = $value['设备编号'].'-->'.$value['设备名称'];
  1250. }
  1251. $this->success('成功',$data);
  1252. }
  1253. /**
  1254. * 打印作业通知单->工单印件和工序获取
  1255. * @ApiMethod (GET)
  1256. * @return void
  1257. * @throws \think\db\exception\DataNotFoundException
  1258. * @throws \think\db\exception\ModelNotFoundException
  1259. * @throws \think\exception\DbException
  1260. */
  1261. public function PrintCodeList()
  1262. {
  1263. if ($this->request->isGet() === false){
  1264. $this->error('请求错误');
  1265. }
  1266. $param = $this->request->param();
  1267. if (empty($param)){
  1268. $this->error('参数错误');
  1269. }
  1270. $list = \db('工单_印件资料')
  1271. ->field([
  1272. 'rtrim(yj_Yjno)' => '印件号',
  1273. 'rtrim(yj_yjmc)' => '印件名称'
  1274. ])
  1275. ->where('Yj_Gdbh',$param['workOrder'])
  1276. ->select();
  1277. $this->success('成功',$list);
  1278. }
  1279. /**
  1280. * 打印作业通知单->工艺编号获取
  1281. * @ApiMethod (GET)
  1282. * @return void
  1283. * @throws \think\db\exception\DataNotFoundException
  1284. * @throws \think\db\exception\ModelNotFoundException
  1285. * @throws \think\exception\DbException
  1286. */
  1287. public function ProcessCodeList()
  1288. {
  1289. if ($this->request->isGet() === false){
  1290. $this->error('请求错误');
  1291. }
  1292. $param = $this->request->param();
  1293. if (empty($param)){
  1294. $this->error('参数错误');
  1295. }
  1296. $list = \db('工单_工艺资料')
  1297. ->field('rtrim(Gy0_gxh) as 工序号')
  1298. ->where([
  1299. 'Gy0_gdbh' => $param['workOrder'],
  1300. 'Gy0_yjno' => $param['yjno']
  1301. ])
  1302. ->select();
  1303. $this->success('成功',$list);
  1304. }
  1305. /**
  1306. * 打印作业通知单
  1307. * @ApiMethod (POST)
  1308. * @param void
  1309. * @return void
  1310. * @throws \think\db\exception\DataNotFoundException
  1311. * @throws \think\db\exception\ModelNotFoundException
  1312. * @throws \think\exception\DbException
  1313. */
  1314. public function PrintJobOrder()
  1315. {
  1316. if ($this->request->isGet() === false) {
  1317. $this->error('请求错误');
  1318. }
  1319. $param = $this->request->param();
  1320. if (empty($param)){
  1321. $this->error('参数错误');
  1322. }
  1323. $data = $this->workOrderDetailGet($param['workOrder'],$param['yjno']);
  1324. if (empty($data)){
  1325. $this->success('未找到工单信息');
  1326. }
  1327. $materiel = $this->MaterielDetailGet($param['workOrder']);
  1328. $printDetail = $this->PrintDetailGet($param['workOrder'],$param['yjno'],$param['gxh']);
  1329. if (empty($printDetail)){
  1330. $this->success('未找到工艺信息');
  1331. }
  1332. $number = 0;
  1333. foreach ($printDetail as $key=>$value){
  1334. $value['允损比例'] = (float)substr($value['允损比例'],0,-1);
  1335. $number = $number+$value['允损比例'];
  1336. }
  1337. $data['制单'] = $param['PrepareDocument'];
  1338. $data['审核'] = $param['examine'];
  1339. $data['目标合格率'] = 100-$number.'%';
  1340. $data['materiel'] = $materiel;
  1341. $data['printDetail'] = $printDetail;
  1342. $this->success('成功',$data);
  1343. }
  1344. /**
  1345. * 打印作业通知单->工单及印件资料获取
  1346. * @param $workOrder
  1347. * @return mixed
  1348. */
  1349. public function workOrderDetailGet($workOrder,$yjno)
  1350. {
  1351. $sql = "SELECT
  1352. RTRIM( a.Gd_lx) AS 工单类型,
  1353. RTRIM( a.Gd_gdbh ) AS 生产批次号,
  1354. RTRIM( a.销售订单号 ) AS 销售订单号,
  1355. RTRIM( a.Gd_客户代号 ) AS 客户代号,
  1356. RTRIM( a.Gd_客户名称 ) AS 客户名称,
  1357. RTRIM( a.成品代号 ) AS 产品代码,
  1358. RTRIM( a.成品名称 ) AS 产品名称,
  1359. RTRIM( a.产品版本号 ) AS 版本号,
  1360. RTRIM( a.警语版面 ) AS 警语版面,
  1361. RTRIM( a.码源数量 ) AS 码源数量,
  1362. RTRIM( a.客户ERP编码 ) AS 客户ERP编码,
  1363. RTRIM( a.接单日期 ) AS 开单日期,
  1364. RTRIM( a.交货日期 ) AS 交货日期,
  1365. RTRIM( a.Gd_desc ) AS 工单说明,
  1366. RTRIM( a.投料率 ) AS 投料率,
  1367. RTRIM( a.订单数量 ) AS 订单数量,
  1368. RTRIM( a.平均合格率 ) AS 平均合格率,
  1369. RTRIM( a.投料大箱 ) AS 订货数量,
  1370. RTRIM( a.排产时库存 ) AS 排产时库存,
  1371. RTRIM( b.yj_Yjno ) AS 印件,
  1372. RTRIM( b.yj_Yjdh ) AS 印件代号,
  1373. RTRIM( b.yj_yjmc ) AS 印件名称,
  1374. RTRIM( b.yj_平张投料 ) AS 平张投料量,
  1375. RTRIM( b.yj_zzmc ) AS 纸张名称,
  1376. RTRIM( b.yj_tlgg ) AS 投料规格,
  1377. RTRIM( b.yj_ks ) AS 开数,
  1378. RTRIM( b.yj_ls ) AS 联数,
  1379. RTRIM( b.yj_desc ) AS 印件备注
  1380. FROM
  1381. `工单_基本资料` AS a
  1382. JOIN `工单_印件资料` AS b ON b.Yj_Gdbh = a.Gd_gdbh
  1383. WHERE
  1384. a.Gd_gdbh = '{$workOrder}' AND b.yj_Yjno = '{$yjno}'";
  1385. $list = Db::query($sql);
  1386. if (empty($list)){
  1387. $this->success('未找到订单数据');
  1388. }
  1389. $list[0]['开单日期'] = date('Y-m-d',strtotime($list[0]['开单日期']));
  1390. $list[0]['交货日期'] = date('Y-m-d',strtotime($list[0]['交货日期']));
  1391. if (strpos($list[0]['产品名称'],'条') !== false){
  1392. $list[0]['订货数量'] = (int)$list[0]['订单数量']*40;
  1393. $list[0]['投料数量'] = intval($list[0]['平张投料量']*$list[0]['联数']/250);
  1394. }else{
  1395. $list[0]['订货数量'] = (int)$list[0]['订单数量']*4;
  1396. $list[0]['投料数量'] = intval($list[0]['平张投料量']*$list[0]['联数']/2500);
  1397. }
  1398. $list[0]['联数'] = (int)$list[0]['联数'];
  1399. $list[0]['印件代号及名称'] = $list[0]['印件代号'].' '.$list[0]['印件名称'];
  1400. // $list[0]['投料数量'] = $list[0]['订货数量'];
  1401. unset($list[0]['印件代号'],$list[0]['印件名称']);
  1402. return($list[0]);
  1403. }
  1404. /**打印作业流程单->物料资料获取
  1405. * @param $workOrder
  1406. * @return bool|\PDOStatement|string|\think\Collection
  1407. * @throws \think\db\exception\DataNotFoundException
  1408. * @throws \think\db\exception\ModelNotFoundException
  1409. * @throws \think\exception\DbException
  1410. */
  1411. public function MaterielDetailGet($workOrder)
  1412. {
  1413. $where = [
  1414. 'BOM_工单编号' => $workOrder,
  1415. ];
  1416. $filed = 'rtrim(BOM_物料编码) as 物料编码,rtrim(BOM_物料名称) as 物料名称,rtrim(BOM_投料单位) as 投料单位,rtrim(BOM_计划用量) as 计划用量';
  1417. $list = \db('工单_bom资料')->where($where)->field($filed)->select();
  1418. if (!empty($list)){
  1419. foreach ($list as $key=>$value){
  1420. $list[$key]['物料代码及名称'] = $value['物料编码'].' '.$value['物料名称'];
  1421. $list[$key]['计划用量'] = (float)$value['计划用量'];
  1422. unset($list[$key]['物料编码'],$list[$key]['物料名称']);
  1423. }
  1424. }
  1425. return $list;
  1426. }
  1427. /**
  1428. * 打印作业流程单->工艺资料获取
  1429. * @param $workOrder
  1430. * @return bool|\PDOStatement|string|\think\Collection
  1431. * @throws \think\db\exception\DataNotFoundException
  1432. * @throws \think\db\exception\ModelNotFoundException
  1433. * @throws \think\exception\DbException
  1434. */
  1435. public function PrintDetailGet($workOrder,$yjno,$gxh)
  1436. {
  1437. $where = [
  1438. 'Gy0_gdbh' => $workOrder,
  1439. 'Gy0_yjno' => $yjno,
  1440. 'Gy0_gxh' => ['<=',$gxh]
  1441. ];
  1442. $filed = 'rtrim(Gy0_yjno) as yjno,rtrim(Gy0_gxh) as gxh,rtrim(Gy0_gxmc) as gxmc,rtrim(Add_gxmc) as add_gxmc,
  1443. rtrim(损耗系数) as 损耗系数,rtrim(Gy0_ls) as ls,rtrim(Gy0_计划接货数) as 计划接货数,rtrim(Gy0_计划损耗) as 计划损耗,
  1444. rtrim(Gy0_辅助工时) as 装版工时,rtrim(Gy0_小时产能) as 小时定额,rtrim(Gy0_生产工时) as 生产工时,rtrim(工序备注) as 工序备注';
  1445. $list = \db('工单_工艺资料')
  1446. ->where($where)
  1447. ->field($filed)
  1448. ->select();
  1449. if (empty($list)){
  1450. $this->success('工单工艺为空');
  1451. }
  1452. foreach ($list as $key=>$value){
  1453. if ($value['yjno']<10){
  1454. $value['yjno'] = '0'.$value['yjno'];
  1455. }
  1456. if ($value['gxh']<10){
  1457. $value['gxh'] = '0'.$value['gxh'];
  1458. }
  1459. if ($value['add_gxmc'] !== null){
  1460. $list[$key]['印件及工序名称'] = $value['yjno'].'-'.$value['gxh'].'-->'.$value['gxmc'].'【'.$value['add_gxmc'].'】';
  1461. }else{
  1462. $list[$key]['印件及工序名称'] = $value['yjno'].'-'.$value['gxh'].'-->'.$value['gxmc'];
  1463. }
  1464. $list[$key]['转序数'] = $value['计划接货数'];
  1465. $list[$key]['报废定额'] = $value['计划损耗'];
  1466. if ((int)$value['计划接货数'] === 0){
  1467. $list[$key]['允损比例'] = 0;
  1468. }else{
  1469. $list[$key]['允损比例'] = round(($value['计划损耗']/$value['计划接货数'])*100,2).'%';
  1470. }
  1471. unset($list[$key]['yjno'],$list[$key]['gxh'],$list[$key]['gxmc'],$list[$key]['add_gxmc'],$list[$key]['计划接货数'],$list[$key]['计划损耗']);
  1472. }
  1473. return $list;
  1474. }
  1475. /**
  1476. * 获取小时产能
  1477. * @ApiMethod (GET)
  1478. * @param void
  1479. * @return void
  1480. * @throws \think\db\exception\DataNotFoundException
  1481. * @throws \think\db\exception\ModelNotFoundException
  1482. * @throws \think\exception\DbException
  1483. */
  1484. public function capacityList()
  1485. {
  1486. if ($this->request->isGet() === false){
  1487. $this->error('请求错误');
  1488. }
  1489. $param = $this->request->param();
  1490. if (empty($param)){
  1491. $this->error('参数错误');
  1492. }
  1493. $machine = explode(' ',$param['machine'])[0];
  1494. $number = \db('设备_基本资料')->where('设备编号',$machine)->field('rtrim(排单小时定额) as 小时产能')->find();
  1495. if (empty($number)){
  1496. $this->error('未找到小时产能');
  1497. }
  1498. $this->success('成功',$number['小时产能']);
  1499. }
  1500. /**
  1501. * 新增工单->添加工单
  1502. * @ApiMethod (POST)
  1503. * @param
  1504. * @return void
  1505. * @throws \think\Exception
  1506. * @throws \think\db\exception\BindParamException
  1507. * @throws \think\db\exception\DataNotFoundException
  1508. * @throws \think\db\exception\ModelNotFoundException
  1509. * @throws \think\exception\DbException
  1510. * @throws \think\exception\PDOException
  1511. */
  1512. public function WorkOrderAdd()
  1513. {
  1514. if (Request::instance()->isPost() === false){
  1515. $this->error('请求错误');
  1516. }
  1517. $param = Request::instance()->post();
  1518. if (empty($param)){
  1519. $this->error('参数错误');
  1520. }
  1521. if (isset($param['sort'])){
  1522. $sort = 2;
  1523. unset($param['sort']);
  1524. }else{
  1525. $sort = 1;
  1526. }
  1527. $num = \db('工单_基本资料')->where('Gd_gdbh',$param['gdbh'])->count();
  1528. //插入产品资料
  1529. $productData = \db('产品_基本资料')
  1530. ->where('产品编号',$param['cpdh'])
  1531. ->count();
  1532. if ($productData === 0) {
  1533. $clientCode = substr($param['cpdh'], 0, 5);
  1534. $product = [
  1535. '客户编号' => $clientCode,
  1536. '客户名称' => $param['khmc'],
  1537. '产品编号' => $param['cpdh'],
  1538. '产品名称' => $param['cpmc'],
  1539. '计量单位' => '万张',
  1540. '状态' => '',
  1541. 'Mod_rq' => date('Y-m-d H:i:s', time()),
  1542. 'UniqID' => \db('产品_基本资料')->order('UniqID desc')->value('UniqID') + 1,
  1543. ];
  1544. if (strpos($product['产品名称'], '小盒') === false) {
  1545. $product['产品类别'] = '条盒';
  1546. } else {
  1547. $product['产品类别'] = '小盒';
  1548. }
  1549. $productSql = \db('产品_基本资料')->fetchSql(true)->insert($product);
  1550. \db()->query($productSql);
  1551. }
  1552. //添加工单
  1553. $productDetail = \db('产品_基本资料')->where('产品编号',$param['cpdh'])->find();
  1554. if (empty($productDetail)){
  1555. $productDetail['客户料号'] = '';
  1556. }
  1557. $lastId = \db('工单_基本资料')->order('Uniqid desc')->value('Uniqid');
  1558. $data = [
  1559. 'Gd_lx' =>$param['zdgd'],
  1560. 'Gd_生产分类' =>$param['sclx'],
  1561. '成本处理类别' =>'',
  1562. 'Gd_gdbh' =>$param['gdbh'],
  1563. '行号' =>$num+1 ,
  1564. 'Gd_客户代号' =>$param['khdh'],
  1565. 'Gd_客户名称' =>$param['khmc'],
  1566. 'Gd_khdh' =>$param['khdh'],
  1567. 'Gd_khmc' =>$param['khmc'],
  1568. '客户料号' =>rtrim($productDetail['客户料号']),
  1569. '客户ERP编码' =>$param['erp'],
  1570. 'Gd_cpdh' =>'',
  1571. 'Gd_cpmc' =>'',
  1572. '成品代号' =>$param['cpdh'],
  1573. '成品名称' =>$param['cpmc'],
  1574. '产品版本号' =>$param['bbh'],
  1575. '销售订单号' =>$param['xsddh'],
  1576. '警语版面' =>$param['jybm'],
  1577. '码源数量' =>$param['mysl'],
  1578. '排产时库存' =>$param['kcdx'],
  1579. '投料大箱' =>$param['tldx'],
  1580. '投料率' =>$param['tll'],
  1581. '平均合格率' =>$param['hgl'],
  1582. '进程备注' =>$param['jcbz'],
  1583. 'Gd_desc' =>$param['remark'],
  1584. '开单日期' =>date('Y-m-d H:i:s',time()),
  1585. '接单日期' =>$param['start'],
  1586. '交货日期' =>$param['end'],
  1587. '订单数量' =>$param['number'],
  1588. '计量单位' =>$param['tldw'],
  1589. '计划投料' =>$param['pztl'],
  1590. '实际投料' =>$param['wxz'],
  1591. '产品单价' =>0,
  1592. '入仓日期' =>'1900-01-01 00:00:00',
  1593. '工单入仓数量' =>0,
  1594. '工单制程废品' => 0,
  1595. '工单质检废品' => 0,
  1596. '工单无形损' => 0,
  1597. '工单计划损耗' => 0,
  1598. '工单完工日期' => $param['end'],
  1599. '投料确认' => '',
  1600. 'gd_statu' => '3-计划中',
  1601. '直接人工' => 0,
  1602. '分摊人工' => 0,
  1603. '直接材料' => 0,
  1604. '考核直接材料' => 0,
  1605. '分摊材料' => 0,
  1606. '考核分摊材料' => 0,
  1607. '直接折旧' => 0,
  1608. '水电气费' => 0,
  1609. '分摊空调' => 0,
  1610. '分摊锅炉' => 0,
  1611. '分摊废气处理' => 0,
  1612. '分摊空压机' => 0,
  1613. '分摊鼓风机' => 0,
  1614. '分摊其他' => 0,
  1615. '待摊人工' => 0,
  1616. '待摊折旧' => 0,
  1617. '待摊场地租金' => 0,
  1618. '待摊其他费用' => 0,
  1619. '加工费' => 0,
  1620. '加工费单价' => 0,
  1621. '成本考核_胶印' => 1,
  1622. '成本考核_凹印' => 1,
  1623. '成本考核_丝印' => 1,
  1624. '成本考核_模切' => 1,
  1625. '成本考核_检验' => 1,
  1626. '印版费' => 0,
  1627. 'V23制造成本' => 0,
  1628. 'Prt_rq' => date('Y-m-d H:i:s',time()),
  1629. 'Sys_id' => $param['Sys_id'],
  1630. 'Sys_rq' => date('Y-m-d H:i:s',time()),
  1631. 'Mod_rq' => date('Y-m-d H:i:s',time()),
  1632. 'U8UID' => '',
  1633. 'Uniqid' => $lastId+1,
  1634. '工序加工费' => 0,
  1635. '工单分类' => $sort,
  1636. ];
  1637. $sql= \db('工单_基本资料')->fetchSql(true)->insert($data);
  1638. $res = \db()->query($sql);
  1639. //创建日志数据
  1640. $logs = array(
  1641. 'Gd_gdbh' => $data['Gd_gdbh'],
  1642. 'ModifyUser' => $param['Sys_id'],
  1643. 'ModifyTime' => date('Y-m-d H:i:s'),
  1644. 'ModifySource' => '工单添加'
  1645. );
  1646. $this->saveWorkOrderOperateLog([$logs]);
  1647. if ($res !== false){
  1648. $this->success('成功');
  1649. }else{
  1650. $this->error('失败');
  1651. }
  1652. }
  1653. /**
  1654. * 新增工单->客户代号列表获取
  1655. * @return void
  1656. * @throws \think\db\exception\BindParamException
  1657. * @throws \think\exception\PDOException
  1658. */
  1659. public function ClientList()
  1660. {
  1661. if ($this->request->isGet() === false){
  1662. $this->error('请求错误');
  1663. }
  1664. $param = $this->request->param();
  1665. if (isset($param['search'])){
  1666. $where = $param['search'].'%';
  1667. $sql = "SELECT DISTINCT
  1668. (客户编号),rtrim(客户名称 ) as 客户名称
  1669. FROM
  1670. `产品_基本资料`
  1671. WHERE
  1672. 客户编号 LIKE "."'".$where."'"."
  1673. OR
  1674. 客户名称 LIKE "."'".$where."'"."
  1675. GROUP BY
  1676. 客户编号
  1677. order by
  1678. 客户编号";
  1679. }else{
  1680. $sql = "SELECT DISTINCT
  1681. (客户编号),rtrim(客户名称 ) as 客户名称
  1682. FROM
  1683. `产品_基本资料`
  1684. GROUP BY
  1685. 客户编号
  1686. order by
  1687. 客户编号";
  1688. }
  1689. $list = \db()->query($sql);
  1690. if (empty($list)){
  1691. $this->success('未找到客户列表');
  1692. }
  1693. foreach ($list as $key=>$value){
  1694. if (empty($value['客户编号'])){
  1695. unset($list[$key]);
  1696. }else{
  1697. $list[$key]['客户编号'] = rtrim($value['客户编号']);
  1698. $list[$key]['name'] = rtrim($value['客户编号']).'【'.$value['客户名称'].'】';
  1699. if ($value['客户编号'] == '1098'){
  1700. $list[$key]['客户名称'] = '打样专用';
  1701. }
  1702. }
  1703. }
  1704. $list = array_values($list);
  1705. $this->success('成功',$list);
  1706. }
  1707. /**
  1708. * 新增工单->产品代号获取
  1709. * @ApiMethod (GET)
  1710. * @param void
  1711. * @return void
  1712. * @throws \think\db\exception\DataNotFoundException
  1713. * @throws \think\db\exception\ModelNotFoundException
  1714. * @throws \think\exception\DbException
  1715. */
  1716. public function ProductCodeList()
  1717. {
  1718. if ($this->request->isGet() === false){
  1719. $this->error('请求错误');
  1720. }
  1721. $param = $this->request->param();
  1722. if (isset($param['cilent']) === false){
  1723. $this->error('参数错误');
  1724. }
  1725. $where = ['产品编号'=>['like',$param['cilent'].'%']];
  1726. $list = \db('产品_基本资料')
  1727. ->where($where)
  1728. ->field('rtrim(产品编号) as 产品编号,rtrim(产品名称) as 产品名称')
  1729. ->limit(50)
  1730. ->select();
  1731. if (empty($list)){
  1732. $this->success('未获取到产品数据');
  1733. }
  1734. $res = db('产品_工艺资料')->where('Gy0_cpdh',$list[0]['产品编号'])->distinct(true)->column('rtrim(Gy0_方案) as gy_plan');
  1735. $list[0]['gy'] = $res;
  1736. foreach ($list as $key=>$value){
  1737. $list[$key]['name'] = $value['产品编号'].'【'.$value['产品名称'].'】';
  1738. $list[$key]['yj'] = db('产品_印件资料')->where('yj_cpdh',$value['产品编号'])->column('yj_yjno');
  1739. }
  1740. $this->success('成功',$list);
  1741. }
  1742. /**
  1743. * 新增印件资料->印件资料添加
  1744. * @return void
  1745. * @throws \think\db\exception\BindParamException
  1746. * @throws \think\exception\PDOException
  1747. */
  1748. public function PrintDetailAdd()
  1749. {
  1750. if (Request::instance()->isPost() === false){
  1751. $this->error('请求错误');
  1752. }
  1753. $param = Request::instance()->post();
  1754. if (empty($param)){
  1755. $this->error('参数错误');
  1756. }
  1757. $lastId = \db('工单_印件资料')->order('Uniqid desc')->value('Uniqid');
  1758. if (empty($lastId)){
  1759. $lastId = 0;
  1760. }else{
  1761. $lastId = $lastId + 1;
  1762. }
  1763. $operator = '';
  1764. if (isset($param['sys_id']) && $param['sys_id'] !== '') {
  1765. $operator = $param['sys_id'];
  1766. } elseif (isset($param['Sys_id']) && $param['Sys_id'] !== '') {
  1767. $operator = $param['Sys_id'];
  1768. }
  1769. unset($param['sys_id']);
  1770. $param['Uniqid'] = $lastId;
  1771. $param['Sys_id'] = $operator;
  1772. $param['Sys_rq'] = date('Y-m-d H:i:s',time());
  1773. $param['Mod_rq'] = date('Y-m-d H:i:s',time());
  1774. //判断印件号是否存在在工单基本资料表中,存在不增加行号,不存在增加行号
  1775. if (empty(\db('工单_基本资料')->where('Gd_gdbh',$param['Yj_Gdbh'])->where('行号',$param['yj_Yjno'])->find())){
  1776. $list = \db('工单_基本资料')->where('Gd_gdbh',$param['Yj_Gdbh'])->find();
  1777. $list['行号'] = $param['yj_Yjno'];
  1778. $list['Gd_cpdh'] = $param['yj_Yjdh'];
  1779. $list['Gd_cpmc'] = $param['yj_yjmc'];
  1780. $list['Sys_id'] = $operator;
  1781. $list['Sys_rq'] = $param['Sys_rq'];
  1782. $list['Mod_rq'] = $param['Mod_rq'];
  1783. unset($list['Uniqid']);
  1784. $workSql = \db('工单_基本资料')->fetchSql(true)->insert($list);
  1785. $workAction = '新增工单基本资料行';
  1786. }else{
  1787. $data['Gd_cpdh'] = $param['yj_Yjdh'];
  1788. $data['Gd_cpmc'] = $param['yj_yjmc'];
  1789. $workSql = \db('工单_基本资料')
  1790. ->where('Gd_gdbh',$param['Yj_Gdbh'])
  1791. ->where('行号',$param['yj_Yjno'])
  1792. ->fetchSql(true)
  1793. ->update($data);
  1794. $workAction = '更新工单基本资料行';
  1795. }
  1796. //开启事务
  1797. $res = false;
  1798. $workRes = false;
  1799. Db::startTrans();
  1800. try{
  1801. $sql = \db('工单_印件资料')->fetchSql(true)->insert($param);
  1802. $res = \db()->query($sql);
  1803. $workRes = \db()->query($workSql);
  1804. if ($res !== false && $workRes !== false){
  1805. $this->savePrintOperateLog([[
  1806. 'Yj_Gdbh' => $param['Yj_Gdbh'],
  1807. 'yj_Yjno' => $param['yj_Yjno'],
  1808. 'yj_Yjdh' => isset($param['yj_Yjdh']) ? $param['yj_Yjdh'] : '',
  1809. 'yj_yjmc' => isset($param['yj_yjmc']) ? $param['yj_yjmc'] : '',
  1810. 'PrintUniqid' => $param['Uniqid'],
  1811. '操作类型' => '新增',
  1812. 'sys_id' => $operator,
  1813. '备注' => $workAction,
  1814. ]]);
  1815. }
  1816. //提交事务
  1817. \db()->commit();
  1818. } catch (\Exception $e) {
  1819. \db()->rollback();
  1820. $this->error($e->getMessage());
  1821. }
  1822. if ($res !== false && $workRes !== false){
  1823. $this->success('成功');
  1824. }else{
  1825. $this->error('失败');
  1826. }
  1827. }
  1828. /**
  1829. * 新增工艺资料->工艺资料添加
  1830. * @return void
  1831. * @throws \think\db\exception\BindParamException
  1832. * @throws \think\exception\PDOException
  1833. */
  1834. public function ProcessDetailAdd()
  1835. {
  1836. if (Request::instance()->isPost() === false){
  1837. $this->error('请求错误');
  1838. }
  1839. $param = Request::instance()->post();
  1840. if (empty($param)){
  1841. $this->error('参数错误');
  1842. }
  1843. $lastId = \db('工单_工艺资料')->order('UniqId desc')->value('UniqId');
  1844. if (empty($lastId)){
  1845. $lastId = 0;
  1846. }else{
  1847. $lastId = $lastId + 1;
  1848. }
  1849. if (empty($param['Gy0_shdh'])){
  1850. $param['Gy0_Rate0'] = 0;
  1851. $param['Gy0_Rate1'] = 0;
  1852. }else{
  1853. $detail = \db('dic_lzsh')->field('sys_rate0,sys_rate1')->where('sys_bh',$param['Gy0_shdh'])->find();
  1854. $param['Gy0_Rate0'] = $detail['sys_rate0'];
  1855. $param['Gy0_Rate1'] = $detail['sys_rate1'];
  1856. }
  1857. $param['Sys_rq'] = date('Y-m-d H:i:s',time());
  1858. $param['Mod_rq'] = '1900-01-01 00:00:00';
  1859. $param['Gy0_sj1'] = '1900-01-01 00:00:00';
  1860. $param['Gy0_sj2'] = '1900-01-01 00:00:00';
  1861. $param['PD_WG'] = '1900-01-01 00:00:00';
  1862. $param['UniqId'] = $lastId;
  1863. //添加工序
  1864. $sql = \db('工单_工艺资料')
  1865. ->fetchSql(true)
  1866. ->insert($param);
  1867. $res = \db()->query($sql);
  1868. //获取平张投料
  1869. $number = \db('工单_印件资料')
  1870. ->where('Yj_Gdbh',$param['Gy0_gdbh'])
  1871. ->where('yj_Yjno',$param['Gy0_yjno'])
  1872. ->value('rtrim(yj_平张投料)');
  1873. //分配工艺计划产量
  1874. if ((int)$number >0){
  1875. $this->PlannedProcessYield($param['Gy0_gdbh'],$param['Gy0_yjno'],0,$number);
  1876. }
  1877. //操作日志数据
  1878. $logs = array(
  1879. 'Gd_gdbh' => $param['Gy0_gdbh'],
  1880. 'yjno' => $param['Gy0_yjno'],
  1881. 'gxh' => $param['Gy0_gxh'],
  1882. 'gxmc' => isset($param['Gy0_gxmc']) ? $param['Gy0_gxmc'] : '',
  1883. 'ModifyUser' => $param['Sys_id'],
  1884. 'ModifyTime' => date('Y-m-d H:i:s'),
  1885. 'ModifySource' => '工艺新增'
  1886. );
  1887. $this->saveWorkOrderOperateLog([$logs]);
  1888. if ($res !== false){
  1889. $this->success('成功');
  1890. }else{
  1891. $this->error('失败');
  1892. }
  1893. }
  1894. /**
  1895. * 新增工艺资料->印件编号获取
  1896. * @return void
  1897. * @throws \think\db\exception\DataNotFoundException
  1898. * @throws \think\db\exception\ModelNotFoundException
  1899. * @throws \think\exception\DbException
  1900. */
  1901. public function PrintDetailList()
  1902. {
  1903. if ($this->request->isGet() === false){
  1904. $this->error('请求错误');
  1905. }
  1906. $param = $this->request->param();
  1907. if (empty($param)){
  1908. $this->error('参数错误');
  1909. }
  1910. $printList = \db('工单_印件资料')
  1911. ->where('Yj_Gdbh',$param['workOrder'])
  1912. ->order('yj_Yjno')
  1913. ->select();
  1914. if (empty($printList)){
  1915. $this->success('未找到该工单印件资料');
  1916. }
  1917. $list = [];
  1918. foreach ($printList as $key=>$value){
  1919. $list[$key]['name'] = rtrim($value['yj_Yjno']).'-->'.rtrim($value['yj_yjmc']);
  1920. $list[$key]['no'] = rtrim($value['yj_Yjno']);
  1921. }
  1922. $this->success('成功',$list);
  1923. }
  1924. /**
  1925. * 工单资料管理->印件资料删除
  1926. * @return void
  1927. * @throws \think\Exception
  1928. * @throws \think\exception\PDOException
  1929. */
  1930. public function PrintDetailDel()
  1931. {
  1932. if ($this->request->isGet() === false){
  1933. $this->error('请求错误');
  1934. }
  1935. $param = $this->request->param();
  1936. if (isset($param['UniqId']) === false){
  1937. $this->error('参数错误');
  1938. }
  1939. $printId = explode(',',$param['UniqId']);
  1940. $printRows = \db('工单_印件资料')->where('Uniqid','in',$printId)->select();
  1941. $printMap = [];
  1942. foreach ($printRows as $row) {
  1943. $printMap[$row['Uniqid']] = $row;
  1944. }
  1945. $i = 0;
  1946. $logs = [];
  1947. foreach ($printId as $value){
  1948. $res = \db('工单_印件资料')
  1949. ->where('Uniqid',$value)
  1950. ->delete();
  1951. if ($res === false){
  1952. $i++;
  1953. continue;
  1954. }
  1955. if ($res && isset($printMap[$value])) {
  1956. $row = $printMap[$value];
  1957. $logs[] = [
  1958. 'Yj_Gdbh' => $row['Yj_Gdbh'],
  1959. 'yj_Yjno' => $row['yj_Yjno'],
  1960. 'yj_Yjdh' => $row['yj_Yjdh'],
  1961. 'yj_yjmc' => $row['yj_yjmc'],
  1962. 'PrintUniqid' => $row['Uniqid'],
  1963. '操作类型' => '删除',
  1964. '修改前' => trim((string)$row['yj_yjmc']),
  1965. 'sys_id' => isset($param['sys_id']) ? $param['sys_id'] : '',
  1966. ];
  1967. }
  1968. }
  1969. $this->savePrintOperateLog($logs);
  1970. if ($i === 0){
  1971. $this->success('删除成功');
  1972. }else{
  1973. $this->error('删除失败');
  1974. }
  1975. }
  1976. /**
  1977. * 工单资料管理->工艺资料删除
  1978. * @return void
  1979. * @throws \think\Exception
  1980. * @throws \think\exception\PDOException
  1981. */
  1982. public function ProcessDetailDel()
  1983. {
  1984. if ($this->request->isGet() === false){
  1985. $this->error('请求错误');
  1986. }
  1987. $param = $this->request->param();
  1988. if (isset($param['UniqId']) === false){
  1989. $this->error('参数错误');
  1990. }
  1991. $printId = explode(',',$param['UniqId']);
  1992. $i = 0;
  1993. //获取工单编号、印件号
  1994. $codeList = \db('工单_工艺资料')
  1995. ->where('UniqId',$printId[0])
  1996. ->field('rtrim(Gy0_gdbh) as 工单编号,rtrim(Gy0_yjno) as 印件号')
  1997. ->find();
  1998. //获取平张投料数量
  1999. $number = \db('工单_印件资料')
  2000. ->where([
  2001. 'Yj_Gdbh' => $codeList['工单编号'],
  2002. 'yj_Yjno' => $codeList['印件号']
  2003. ])
  2004. ->value('rtrim(yj_平张投料)');
  2005. // 新增:批量获取要删除的工序号
  2006. $gxhList = \db('工单_工艺资料')
  2007. ->where('UniqId', 'in', $printId)
  2008. ->column('rtrim(Gy0_gxh) as gxh', 'UniqId');
  2009. //循环删除工艺资料
  2010. $deletedGxh = [];
  2011. $deletedGxmc = [];
  2012. foreach ($printId as $value){
  2013. $process = \db('工单_工艺资料')
  2014. ->where('UniqId',$value)
  2015. ->find();
  2016. $processCount = \db('设备_产量计酬')
  2017. ->where('sczl_gdbh',$process['Gy0_gdbh'])
  2018. ->where('sczl_yjno',$process['Gy0_yjno'])
  2019. ->where('sczl_gxh',$process['Gy0_gxh'])
  2020. ->count();
  2021. if ($processCount > 0){
  2022. $this->error('工序'.$process['Gy0_gxh'].'已上报数据,无法删除,请核对');
  2023. }
  2024. $res = \db('工单_工艺资料')
  2025. ->where('UniqId',$value)
  2026. ->delete();
  2027. if ($res) {
  2028. // 记录成功删除的工序号
  2029. if (isset($gxhList[$value])) {
  2030. $deletedGxh[] = $gxhList[$value];
  2031. $deletedGxmc[] = isset($process['Gy0_gxmc']) ? trim($process['Gy0_gxmc']) : '';
  2032. }
  2033. } else {
  2034. $i++;
  2035. }
  2036. }
  2037. //重新分配工艺计划产量
  2038. if ((int)$number>0){
  2039. $this->PlannedProcessYield($codeList['工单编号'],$codeList['印件号'],0,$number);
  2040. }
  2041. // 新增:写入操作日志
  2042. $this->saveWorkOrderOperateLog([[
  2043. 'ModifyUser' => $param['sys_id'],
  2044. 'ModifyTime' => date('Y-m-d H:i:s', time()),
  2045. 'Gd_gdbh' => $codeList['工单编号'],
  2046. 'yjno' => $codeList['印件号'],
  2047. 'gxh' => implode(',', $deletedGxh),
  2048. 'gxmc' => implode(',', $deletedGxmc),
  2049. 'ModifySource' => '工艺资料删除'
  2050. ]]);
  2051. if ($i === 0){
  2052. $this->success('删除成功');
  2053. }else{
  2054. $this->error('删除失败');
  2055. }
  2056. }
  2057. /**
  2058. * 工艺资料添加->工序损耗代码
  2059. * @return void
  2060. * @throws \think\db\exception\DataNotFoundException
  2061. * @throws \think\db\exception\ModelNotFoundException
  2062. * @throws \think\exception\DbException
  2063. */
  2064. public function WastageList()
  2065. {
  2066. if ($this->request->isGet() === false){
  2067. $this->error('请求错误');
  2068. }
  2069. $param = $this->request->param();
  2070. if (empty($param['process'])){
  2071. $this->error('参数错误');
  2072. }
  2073. if (strlen($param['process'])>6){
  2074. $param['process'] = substr($param['process'],0,6);
  2075. }
  2076. $order = \db('dic_lzsh')
  2077. ->where('sys_mc','like',$param['process'].'%')
  2078. ->value('rtrim(sys_bh) as 编号');
  2079. $this->success('成功',$order);
  2080. }
  2081. /**
  2082. * 获取产品附件列表
  2083. */
  2084. public function getAnnexTable(){
  2085. if(!$this->request->isGet()){
  2086. $this->error('请求方式错误');
  2087. }
  2088. $req = $this->request->param();
  2089. if (isset($req['cpdh']) && !empty($req['cpdh'])){
  2090. $where['关联产品'] = ['LIKE','%'.$req['cpdh'].'%'];
  2091. }else{
  2092. $this->error('参数错误');
  2093. }
  2094. $rows = db('产品_技术附件')
  2095. ->where($where)
  2096. ->select();
  2097. foreach ($rows as $key=>&$value){
  2098. if(mb_detect_encoding($value['附件内容'])!='ASCII'){
  2099. $value['附件内容'] = '';
  2100. }
  2101. }
  2102. $this->success('成功',$rows);
  2103. }
  2104. /**
  2105. * 产品附件新增
  2106. */
  2107. public function annexAdd(){
  2108. if(!$this->request->isPost()){
  2109. $this->error('请求方式错误');
  2110. }
  2111. $req = $this->request->param();
  2112. $arr = [
  2113. 'sys_id',
  2114. '序号',
  2115. '附件备注',
  2116. '附件内容',
  2117. '附件类型',
  2118. '适用工序',
  2119. '关联产品'
  2120. ];
  2121. $data = [];
  2122. foreach ($arr as $key => $value){
  2123. if (!isset($req[$value])){
  2124. $this->error('参数错误',$value,$key+1);
  2125. }
  2126. $data[$value] = $req[$value];
  2127. }
  2128. $data['sys_rq'] = date('Y-m-d H:i:s');
  2129. //查询UniqId
  2130. $UniqId = db('产品_技术附件')->max('UniqId');
  2131. $data['UniqId'] = $UniqId < 10000000 ? 10000000 : $UniqId + 1;
  2132. //开启事务
  2133. db()->startTrans();
  2134. try{
  2135. $sql = db('产品_技术附件')->fetchSql(true)->insert($data);
  2136. $bool = db()->query($sql);
  2137. // 提交事务
  2138. db()->commit();
  2139. } catch (\Exception $e) {
  2140. // 回滚事务
  2141. db()->rollback();
  2142. $this->error($e->getMessage());
  2143. }
  2144. if($bool===false) $this->error('失败');
  2145. $this->success('成功');
  2146. }
  2147. /**
  2148. * 产品附件修改
  2149. * @ApiMethod (GET)
  2150. * @param string 'UniqId'
  2151. */
  2152. public function annexDel()
  2153. {
  2154. if(!$this->request->isGet()){
  2155. $this->error('请求方式错误');
  2156. }
  2157. $req = $this->request->param();
  2158. if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
  2159. $this->error('参数错误','UniqId',100);
  2160. }
  2161. //开启事务
  2162. db()->startTrans();
  2163. try{
  2164. $bool = db('产品_技术附件')->where('UniqId',$req['UniqId'])->delete();
  2165. // 提交事务
  2166. db()->commit();
  2167. } catch (\Exception $e) {
  2168. // 回滚事务
  2169. db()->rollback();
  2170. $this->error($e->getMessage());
  2171. }
  2172. if($bool===false) $this->error('失败');
  2173. $this->success('成功');
  2174. }
  2175. /**
  2176. * 获取产品附件列表
  2177. */
  2178. public function getGdAnnexTable(){
  2179. if(!$this->request->isGet()){
  2180. $this->error('请求方式错误');
  2181. }
  2182. $req = $this->request->param();
  2183. if (isset($req['gdbh']) && !empty($req['gdbh'])){
  2184. $where['关联编号'] = ['LIKE','%'.$req['gdbh'].'%'];
  2185. }else{
  2186. $this->error('参数错误');
  2187. }
  2188. $rows = db('工单_相关附件')
  2189. ->where($where)
  2190. ->select();
  2191. foreach ($rows as $key=>&$value){
  2192. if(mb_detect_encoding($value['附件内容'])!='ASCII'){
  2193. $value['附件内容'] = '';
  2194. }
  2195. }
  2196. $this->success('成功',$rows);
  2197. }
  2198. /**
  2199. * 产品附件新增
  2200. */
  2201. public function gdAnnexAdd(){
  2202. if(!$this->request->isPost()){
  2203. $this->error('请求方式错误');
  2204. }
  2205. $req = $this->request->param();
  2206. $arr = [
  2207. 'sys_id',
  2208. '附件备注',
  2209. '附件内容',
  2210. '附件类型',
  2211. '关联编号'
  2212. ];
  2213. $data = [];
  2214. foreach ($arr as $key => $value){
  2215. if (!isset($req[$value])){
  2216. $this->error('参数错误',$value,$key+1);
  2217. }
  2218. $data[$value] = $req[$value];
  2219. }
  2220. $data['sys_rq'] = date('Y-m-d H:i:s');
  2221. //查询UniqId
  2222. $UniqId = db('工单_相关附件')->max('UniqId');
  2223. $data['UniqId'] = $UniqId < 10000000 ? 10000000 : $UniqId + 1;
  2224. //开启事务
  2225. db()->startTrans();
  2226. try{
  2227. $sql = db('工单_相关附件')->fetchSql(true)->insert($data);
  2228. $bool = db()->query($sql);
  2229. // 提交事务
  2230. db()->commit();
  2231. } catch (\Exception $e) {
  2232. // 回滚事务
  2233. db()->rollback();
  2234. $this->error($e->getMessage());
  2235. }
  2236. if($bool===false) $this->error('失败');
  2237. $this->success('成功');
  2238. }
  2239. /**
  2240. * 产品附件修改
  2241. * @ApiMethod (GET)
  2242. * @param string 'UniqId'
  2243. */
  2244. public function gdAnnexDel()
  2245. {
  2246. if(!$this->request->isGet()){
  2247. $this->error('请求方式错误');
  2248. }
  2249. $req = $this->request->param();
  2250. if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
  2251. $this->error('参数错误','UniqId',100);
  2252. }
  2253. //开启事务
  2254. db()->startTrans();
  2255. try{
  2256. $bool = db('工单_相关附件')->where('UniqId',$req['UniqId'])->delete();
  2257. // 提交事务
  2258. db()->commit();
  2259. } catch (\Exception $e) {
  2260. // 回滚事务
  2261. db()->rollback();
  2262. $this->error($e->getMessage());
  2263. }
  2264. if($bool===false) $this->error('失败');
  2265. $this->success('成功');
  2266. }
  2267. /**
  2268. * 引用工单资料数据查询
  2269. * @return void
  2270. * @throws \think\db\exception\DataNotFoundException
  2271. * @throws \think\db\exception\ModelNotFoundException
  2272. * @throws \think\exception\DbException
  2273. */
  2274. public function WorkOrderDetailCope()
  2275. {
  2276. if ($this->request->isGet() === false){
  2277. $this->error('请求错误');
  2278. }
  2279. $param = $this->request->param();
  2280. if (empty($param['search'])){
  2281. $this->error('参数错误');
  2282. }
  2283. $list = \Db('工单_基本资料')
  2284. ->alias('a')
  2285. ->field([
  2286. 'rtrim(a.Gd_gdbh)' => '工单编号',
  2287. 'rtrim(b.客户编号)' => '客户编号',
  2288. 'rtrim(b.客户名称)' => '客户名称',
  2289. 'rtrim(b.产品编号)' => '产品编号',
  2290. 'rtrim(b.产品名称)' => '产品名称'
  2291. ])
  2292. ->join('产品_基本资料 b', 'a.成品代号 = b.产品编号')
  2293. ->join('产品_工艺资料 c', 'a.成品代号 = c.Gy0_cpdh')
  2294. ->join('产品_印件资料 d', 'a.成品代号 = d.yj_cpdh')
  2295. ->where(function($query) use ($param) {
  2296. $query->where('a.Gd_gdbh', 'like', '%'.$param['search'].'%')
  2297. ->whereOr('a.成品名称', 'like', '%'.$param['search'].'%');
  2298. })
  2299. ->where(function($query) {
  2300. $query->where('c.Gy0_cpdh', '<>', '')
  2301. ->where('d.yj_cpdh', '<>', '')
  2302. ->where('b.状态', 'not like', '%停用%')
  2303. ->where('b.产品名称', 'not like', '%停用%');
  2304. })
  2305. ->group('a.Gd_gdbh')
  2306. ->order('a.Uniqid desc')
  2307. ->select();
  2308. if (empty($list)){
  2309. $this->success('未找到工单资料');
  2310. }else{
  2311. $this->success('成功',$list);
  2312. }
  2313. }
  2314. /**
  2315. * 引用产品印件、工艺资料
  2316. * @return void
  2317. * @throws \think\db\exception\BindParamException
  2318. * @throws \think\db\exception\DataNotFoundException
  2319. * @throws \think\db\exception\ModelNotFoundException
  2320. * @throws \think\exception\DbException
  2321. * @throws \think\exception\PDOException
  2322. */
  2323. public function WorkOrderDetailListCope()
  2324. {
  2325. if (Request::instance()->isPost() === false){
  2326. $this->error('请求错误');
  2327. }
  2328. $param = Request::instance()->post();
  2329. if (empty($param['workorder']) || empty($param['productCode']) || empty($param['option'])){
  2330. $this->error('参数错误');
  2331. }
  2332. if (isset($param['sort'])){
  2333. $priWhere['yj_yjno'] = ['>=',10];
  2334. $proWhere['a.Gy0_yjno'] = ['>=',10];
  2335. }else{
  2336. $priWhere['yj_yjno'] = ['<',10];
  2337. $proWhere['a.Gy0_yjno'] = ['<',10];
  2338. }
  2339. $list = \db('工单_基本资料')
  2340. ->where('Gd_gdbh',$param['workorder'])
  2341. // ->where('成品代号',$param['productCode'])
  2342. ->field('行号,Gd_cpdh')
  2343. ->select();
  2344. $cpdhList = \db('工单_基本资料')
  2345. ->where('Gd_gdbh',$param['workorder'])
  2346. ->column('Gd_cpdh');
  2347. //引用印件资料
  2348. $PrintList = \db('产品_印件资料')
  2349. ->where('yj_cpdh',$param['productCode'])
  2350. ->whereIn('yj_yjdh',$cpdhList)
  2351. ->where($priWhere)
  2352. ->select();
  2353. if (empty($PrintList)){
  2354. $this->error('未找到印件资料');
  2355. }
  2356. $number = \db('工单_基本资料')
  2357. ->where('Gd_gdbh',$param['workorder'])
  2358. ->where('成品代号',$param['productCode'])
  2359. ->value('rtrim(计划投料)');
  2360. $arr = [];
  2361. foreach ($PrintList as $key => $value){
  2362. foreach ($list as $k=>$v){
  2363. if (rtrim($value['yj_yjdh']) === $v['Gd_cpdh']){
  2364. $PrintList[$key]['Yj_Gdbh'] = $param['workorder'];
  2365. $PrintList[$key]['yj_Yjno'] = $value['yj_yjno'];
  2366. $PrintList[$key]['yj_Yjdh'] = $value['yj_yjdh'];
  2367. $PrintList[$key]['yj_ks'] = $value['yj_ks'];
  2368. $PrintList[$key]['yj_ls'] = $value['yj_ls'];
  2369. $PrintList[$key]['yj_平张投料'] = $number;
  2370. $PrintList[$key]['Sys_id'] = $value['sys_id'];
  2371. $PrintList[$key]['Sys_rq'] = date('Y-m-d H:i:s',time());
  2372. $PrintList[$key]['Mod_rq'] = date('Y-m-d H:i:s',time());
  2373. array_push($arr,[$value['yj_yjno'],$v['行号']]);
  2374. }
  2375. $sql = \db('工单_基本资料')
  2376. ->where('Gd_gdbh',$param['workorder'])
  2377. ->where('Gd_cpdh',$value['yj_yjdh'])
  2378. ->fetchSql(true)
  2379. ->update(['行号'=>$value['yj_yjno']]);
  2380. \db()->query($sql);
  2381. $bomSql = \db('工单_bom资料')
  2382. ->where('BOM_工单编号',$param['workorder'])
  2383. ->where('BOM_产品编号',$value['yj_yjdh'])
  2384. ->fetchSql(true)
  2385. ->update(['BOM_工单行号'=>$value['yj_yjno']]);
  2386. \db()->query($bomSql);
  2387. }
  2388. unset($PrintList[$key]['mod_rq'],$PrintList[$key]['sys_rq'],$PrintList[$key]['sys_id'],$PrintList[$key]['KgToPages'],$PrintList[$key]['yj_tll'],$PrintList[$key]['yj_yjdh'],$PrintList[$key]['yj_yjno'],$PrintList[$key]['UniqId']);
  2389. }
  2390. //引用工艺资料
  2391. $processWhere = [
  2392. 'a.Gy0_方案' => $param['option'],
  2393. 'a.Gy0_cpdh' => $param['productCode']
  2394. ];
  2395. $yjnolist = \db('工单_基本资料')
  2396. ->where('Gd_gdbh',$param['workorder'])
  2397. ->column('Gd_cpdh');
  2398. $ProcessList = \db('产品_工艺资料')
  2399. ->alias('a')
  2400. ->field('a.*')
  2401. ->join('产品_印件资料 b','a.Gy0_cpdh = b.yj_cpdh AND a.Gy0_yjno = yj_yjno')
  2402. ->where($processWhere)
  2403. ->where($proWhere)
  2404. ->whereIn('b.yj_yjdh',$yjnolist)
  2405. ->order('Gy0_yjno,Gy0_gxh')
  2406. ->select();
  2407. if (empty($ProcessList)){
  2408. $this->error('未找到产品工艺资料');
  2409. }
  2410. $ProcesslastId = \db('工单_工艺资料')->order('UniqId desc')->value('rtrim(UniqId)');
  2411. foreach ($ProcessList as $key=>$value){
  2412. if (empty(trim($value['Gy0_shdh']))){
  2413. $rate['rate0'] = 0;
  2414. $rate['rate1'] = 0;
  2415. }else{
  2416. $rate = \db('dic_lzsh')->where('sys_bh',$value['Gy0_shdh'])->field('rtrim(sys_rate0) as rate0,rtrim(sys_rate1) as rate1')->find();
  2417. if (empty($rate)){
  2418. $this->error('该产品印件号'.$value['Gy0_yjno'].'工序号为'.$value['gy0_gxmc'].'的损耗代号错误,请调整之后再引用');
  2419. }
  2420. }
  2421. //设置默认机台 + 对应排单小时定额
  2422. $productMachine = isset($value['Gy0_sbbh']) ? trim((string)$value['Gy0_sbbh']) : '';
  2423. $ProcessList[$key]['Gy0_sbbh'] = '';
  2424. $ProcessList[$key]['Gy0_小时产能'] = 0;
  2425. $sbbhForCapacity = '';
  2426. if ($productMachine !== '') {
  2427. $ProcessList[$key]['Gy0_sbbh'] = $productMachine;
  2428. $sbbhForCapacity = $productMachine;
  2429. } else {
  2430. $machineRow = \db('设备_基本资料')
  2431. ->where('生产工序', 'like', '%' . $value['gy0_gxmc'] . '%')
  2432. ->where('使用部门', '<>', '研发中心')
  2433. ->where('设备编组', '<>', '')
  2434. ->where('sys_sbID', '<>', '')
  2435. ->order('设备编号')
  2436. ->field('设备编号, 排单小时定额')
  2437. ->find();
  2438. if (!empty($machineRow)) {
  2439. $ProcessList[$key]['Gy0_sbbh'] = trim($machineRow['设备编号']);
  2440. $sbbhForCapacity = trim($machineRow['设备编号']);
  2441. if (!empty($machineRow['排单小时定额'])) {
  2442. $ProcessList[$key]['Gy0_小时产能'] = intval($machineRow['排单小时定额']);
  2443. }
  2444. }
  2445. }
  2446. // 产品自带机台:也去设备_基本资料取排单小时定额
  2447. if ($sbbhForCapacity !== '' && $productMachine !== '') {
  2448. $capacity = \db('设备_基本资料')->where('设备编号', $sbbhForCapacity)->value('排单小时定额');
  2449. if (!empty($capacity)) {
  2450. $ProcessList[$key]['Gy0_小时产能'] = intval($capacity);
  2451. }
  2452. }
  2453. $ProcessList[$key]['Gy0_yjno'] = $value['Gy0_yjno'];
  2454. $ProcessList[$key]['Gy0_gdbh'] = $param['workorder'];
  2455. $ProcessList[$key]['Gy0_gxmc'] = $value['gy0_gxmc'];
  2456. $ProcessList[$key]['Gy0_SITE'] = $value['Gy0_site'];
  2457. $ProcessList[$key]['Gy0_shdh'] = $value['Gy0_shdh'];
  2458. $ProcessList[$key]['Gy0_Rate0'] = $rate['rate0'];
  2459. $ProcessList[$key]['Gy0_Rate1'] = $rate['rate1'];
  2460. $ProcessList[$key]['Gy0_ks'] = $value['Gy0_Ks'];
  2461. $ProcessList[$key]['Gy0_ms'] = $value['Gy0_Ms'];
  2462. $ProcessList[$key]['Gy0_oil'] = $value['Gy0_Oil'];
  2463. $ProcessList[$key]['Gy0_计划接货数'] = 0;
  2464. $ProcessList[$key]['Gy0_计划损耗'] = 0;
  2465. $ProcessList[$key]['无形损承担比例'] = 0;
  2466. $ProcessList[$key]['超节损承担比例'] = 1;
  2467. $ProcessList[$key]['超节损核算单价'] = 0;
  2468. $ProcessList[$key]['Gy0_sj1'] = '1900-01-01 00:00:00';
  2469. $ProcessList[$key]['Gy0_sj2'] = '1900-01-01 00:00:00';
  2470. $ProcessList[$key]['PD_WG'] = '1900-01-01 00:00:00';
  2471. $ProcessList[$key]['Sys_rq'] = date('Y-m-d H:i:s', time());
  2472. unset($ProcessList[$key]['Gy0_Oil'],$ProcessList[$key]['Gy0_Ms'],$ProcessList[$key]['Gy0_Ks'],$ProcessList[$key]['UniqID'],$ProcessList[$key]['Gy0_cpdh'],$ProcessList[$key]['gy0_gxmc'],$ProcessList[$key]['Gy0_site']);
  2473. }
  2474. $oldPrintCount = \db('工单_印件资料')->where('Yj_Gdbh',$param['workorder'])->count();
  2475. $oldProcessCount = \db('工单_工艺资料')->where('Gy0_gdbh',$param['workorder'])->count();
  2476. $copyRemark = '产品代号'.$param['productCode'].',方案'.$param['option'].',原印件'.$oldPrintCount.'条、原工艺'.$oldProcessCount.'条已替换';
  2477. $printLogs = [];
  2478. foreach ($PrintList as $item) {
  2479. $printLogs[] = [
  2480. 'Yj_Gdbh' => $param['workorder'],
  2481. 'yj_Yjno' => isset($item['yj_Yjno']) ? $item['yj_Yjno'] : '',
  2482. 'yj_Yjdh' => isset($item['yj_Yjdh']) ? $item['yj_Yjdh'] : '',
  2483. 'yj_yjmc' => isset($item['yj_yjmc']) ? $item['yj_yjmc'] : '',
  2484. '操作类型' => '引用',
  2485. 'sys_id' => isset($param['sys_id']) ? $param['sys_id'] : '',
  2486. '备注' => $copyRemark,
  2487. ];
  2488. }
  2489. //插入数据
  2490. \db()->startTrans();
  2491. $copied = false;
  2492. try {
  2493. \db('工单_工艺资料')->where('Gy0_gdbh',$param['workorder'])->delete();
  2494. $processSql = \db('工单_工艺资料')->fetchSql(true)->insertAll($ProcessList);
  2495. \db()->query($processSql);
  2496. \db('工单_印件资料')->where('Yj_Gdbh',$param['workorder'])->delete();
  2497. $printSql = \db('工单_印件资料')->fetchSql(true)->insertAll($PrintList);
  2498. \db()->query($printSql);
  2499. \db()->commit();
  2500. $copied = true;
  2501. }catch (\Exception $e){
  2502. \db()->rollback();
  2503. }
  2504. if ($copied) {
  2505. $this->savePrintOperateLog($printLogs);
  2506. }
  2507. //计算工序计划生产数量
  2508. if ($number !== null){
  2509. $list = \db('工单_印件资料')->where('Yj_Gdbh',$param['workorder'])->field('yj_Yjno,yj_平张投料')->select();
  2510. foreach ($list as $value){
  2511. if ((int)$value['yj_平张投料'] >0){
  2512. $this->PlannedProcessYield($param['workorder'],$value['yj_Yjno'],0,$value['yj_平张投料']);
  2513. }
  2514. }
  2515. }
  2516. $this->success('成功');
  2517. }
  2518. /**
  2519. * 引用产品印件、工艺资料->信息获取
  2520. * @return void
  2521. * @throws \think\db\exception\DataNotFoundException
  2522. * @throws \think\db\exception\ModelNotFoundException
  2523. * @throws \think\exception\DbException
  2524. */
  2525. public function ProductCopeDetail()
  2526. {
  2527. if ($this->request->isGet() === false){
  2528. $this->error('请求错误');
  2529. }
  2530. $param = $this->request->param();
  2531. if (empty($param['workorder'])){
  2532. $this->error('参数错误');
  2533. }
  2534. $list = \db('工单_基本资料')
  2535. ->alias('a')
  2536. ->field([
  2537. 'rtrim(a.Gd_gdbh)' => '工单编号',
  2538. 'rtrim(b.客户编号)' => '客户编号',
  2539. 'rtrim(b.客户名称)' => '客户名称',
  2540. 'rtrim(b.产品编号)' => '产品编号',
  2541. 'rtrim(b.产品名称)' => '产品名称'
  2542. ])
  2543. ->join('产品_基本资料 b','a.成品代号 = b.产品编号')
  2544. ->where('a.Gd_gdbh',$param['workorder'])
  2545. ->find();
  2546. if (empty($list)){
  2547. $this->error('未找到工单信息');
  2548. }
  2549. $option = \db('产品_工艺资料')
  2550. ->field('rtrim(Gy0_方案) as 方案')
  2551. ->where('Gy0_cpdh',$list['产品编号'])
  2552. ->group('Gy0_方案')
  2553. ->select();
  2554. $list['方案'] = $option;
  2555. $this->success('成功',$list);
  2556. }
  2557. /**
  2558. * 工艺资料计划产量重新分配
  2559. * @param string $workOrder 工单
  2560. * @param int $yjno 印件号
  2561. * @param int $gxh 工序号
  2562. * @param int $feed 平张投料
  2563. * @return bool
  2564. * @throws \think\Exception
  2565. * @throws \think\db\exception\BindParamException
  2566. * @throws \think\db\exception\DataNotFoundException
  2567. * @throws \think\db\exception\ModelNotFoundException
  2568. * @throws \think\exception\DbException
  2569. * @throws \think\exception\PDOException
  2570. */
  2571. public function PlannedProcessYield($workOrder,$yjno,$gxh,$feed)
  2572. {
  2573. $where = [
  2574. 'Gy0_gdbh'=>$workOrder,
  2575. 'Gy0_yjno'=>$yjno,
  2576. ];
  2577. //获取工艺资料
  2578. $processList = \db('工单_工艺资料')
  2579. ->where($where)
  2580. ->field('CONCAT(rtrim(Gy0_gxmc),"[",rtrim(Add_gxmc),"]") as 工序名称,Gy0_Rate0 as 损耗定额,Gy0_Rate1 as 损耗率,rtrim(损耗系数) as 损耗系数,Gy0_ms,Gy0_ls,UniqId')
  2581. ->order('Gy0_gxh')
  2582. ->select();
  2583. if (empty($processList)){
  2584. $this->success('未找到该工单工序资料');
  2585. }
  2586. $processDetail = \db('工单_工艺资料')
  2587. ->where($where)
  2588. ->field('CONCAT(rtrim(Gy0_gxmc),"[",rtrim(Add_gxmc),"]") as 工序名称,Gy0_ls as 联数')
  2589. ->select();
  2590. $number1 = $number2 = 1;
  2591. foreach ($processDetail as $k => $v) {
  2592. if (strpos($v['工序名称'],'分切') !== false) {
  2593. $number1 = $v['联数'];
  2594. }
  2595. if (strpos($v['工序名称'],'分切') === false && $v['联数'] > 1) {
  2596. $number2 = $v['联数'];
  2597. }
  2598. }
  2599. $Multiple = $number1/$number2;
  2600. if (is_int($Multiple) === false) {
  2601. $Multiple = 1;
  2602. }
  2603. //计算损耗定额和计划产量
  2604. $production = [];
  2605. $i = $m = 0;
  2606. foreach ($processList as $key=>$value){
  2607. if (strpos($v['工序名称'],'分切') !== false) {
  2608. $m = $key;
  2609. }
  2610. if ($key === 0){
  2611. array_push($production,$feed);
  2612. }else{
  2613. if ($key === $m+1){
  2614. $production[$key] = $production[$key]/$value['Gy0_ls']/$Multiple;
  2615. }else{
  2616. $production[$key] = $production[$key]/$value['Gy0_ls'];
  2617. }
  2618. }
  2619. $wastage = 0;
  2620. if ($value['损耗率'] !== 0 && !empty($value['损耗率'])){
  2621. $wastage = ($production[$key]*$value['损耗率']+$value['损耗定额'])*$value['损耗系数'];
  2622. }
  2623. if ($value['Gy0_ms'] >0 && !empty($value['Gy0_ms'])){
  2624. $wastage = round($wastage * $value['Gy0_ms']);
  2625. }
  2626. array_push($production,ceil($production[$key]-$wastage)*$value['Gy0_ls']);
  2627. $processSql = \db('工单_工艺资料')
  2628. ->where('UniqId',$value['UniqId'])
  2629. ->fetchSql(true)
  2630. ->update(['Gy0_计划接货数'=>$production[$key],'Gy0_计划损耗'=>$wastage]);
  2631. $res = \db()->query($processSql);
  2632. if ($res === false){
  2633. $i++;
  2634. }
  2635. }
  2636. if ($i===0){
  2637. return true;
  2638. }else{
  2639. return false;
  2640. }
  2641. }
  2642. /**
  2643. * 工单资料删除
  2644. * @return void
  2645. * @throws \think\exception\PDOException
  2646. */
  2647. public function WorkOrderDel()
  2648. {
  2649. if ($this->request->isGet() === false){
  2650. $this->error('请求错误');
  2651. }
  2652. $param = $this->request->param();
  2653. if (isset($param['UniqId']) === false){
  2654. $this->error('参数错误');
  2655. }
  2656. $WorkOrderId = explode(',',$param['UniqId']);
  2657. $i = 0;
  2658. foreach ($WorkOrderId as $key=>$value){
  2659. //获取工单编号
  2660. $workorder = \db('工单_基本资料')
  2661. ->where('Uniqid',$value)
  2662. ->value('rtrim(Gd_gdbh)');
  2663. $status = \db('设备_产量计酬')
  2664. ->where('sczl_gdbh',$workorder)
  2665. ->count();
  2666. if ($status === 0){
  2667. //删除工单资料、工艺资料、印件资料
  2668. \db()->startTrans();
  2669. try {
  2670. \db('工单_基本资料')->where('Gd_gdbh',$workorder)->delete();
  2671. if (!empty(\db('工单_工艺资料')->where('Gy0_gdbh',$workorder)->find())){
  2672. \db('工单_工艺资料')->where('Gy0_gdbh',$workorder)->delete();
  2673. }
  2674. if (!empty(\db('工单_印件资料')->where('Yj_Gdbh',$workorder)->find())){
  2675. \db('工单_印件资料')->where('Yj_Gdbh',$workorder)->delete();
  2676. }
  2677. if (!empty(\db('工单_bom资料')->where('BOM_工单编号',$workorder)->find())){
  2678. \db('工单_bom资料')->where('BOM_工单编号',$workorder)->delete();
  2679. }
  2680. \db()->commit();
  2681. // 成功删除后记录日志
  2682. $this->saveWorkOrderOperateLog([[
  2683. 'Gd_gdbh' => $workorder,
  2684. 'ModifyUser' => $param['sys_id'],
  2685. 'ModifyTime' => date('Y-m-d H:i:s'),
  2686. 'FieldName' => '工单删除',
  2687. 'NewValue' => '已删除',
  2688. 'ModifySource' => '工单删除'
  2689. ]]);
  2690. }catch (\Exception $e){
  2691. \db()->rollback();
  2692. $i++;
  2693. }
  2694. }else{
  2695. $this->error('工单'.$workorder.'已上报数据无法删除,请重新确认');
  2696. }
  2697. }
  2698. if ($i === 0){
  2699. $this->success('删除成功');
  2700. }else{
  2701. $this->error('删除失败');
  2702. }
  2703. }
  2704. /**
  2705. * 修正核算参数-》工单产品信息获取
  2706. * @return void
  2707. * @throws \think\db\exception\DataNotFoundException
  2708. * @throws \think\db\exception\ModelNotFoundException
  2709. * @throws \think\exception\DbException
  2710. */
  2711. public function ProductNameData()
  2712. {
  2713. if ($this->request->isGet() === false){
  2714. $this->error('请求错误');
  2715. }
  2716. $workOrder = input('workOrder');
  2717. if (empty($workOrder)){
  2718. $this->error('参数错误');
  2719. }
  2720. $orderList = \db('工单_基本资料')
  2721. ->field('rtrim(Gd_cpdh) as 印件代号,rtrim(Gd_cpmc) as 印件名称,rtrim(成品代号) as 产品代号,rtrim(成品名称) as 产品名称')
  2722. ->where('Gd_gdbh',$workOrder)
  2723. ->find();
  2724. $this->success('成功',$orderList);
  2725. }
  2726. /**
  2727. * 获取联数
  2728. * @return void
  2729. * @throws \think\db\exception\DataNotFoundException
  2730. * @throws \think\db\exception\ModelNotFoundException
  2731. * @throws \think\exception\DbException
  2732. */
  2733. public function getCouplet()
  2734. {
  2735. if ($this->request->isGet() === false){
  2736. $this->error('请求错误');
  2737. }
  2738. $param = $this->request->param();
  2739. if (empty($param['gdbh']) || empty($param['yjno'])){
  2740. $this->error('参数错误');
  2741. }
  2742. $list = \db('工单_印件资料')
  2743. ->where('Yj_gdbh',$param['gdbh'])
  2744. ->where('yj_Yjno',$param['yjno'])
  2745. ->field('rtrim(yj_ks) as 开数,rtrim(yj_ls) as 联数')
  2746. ->find();
  2747. $process = \db('工单_工艺资料')
  2748. ->where('Gy0_gdbh',$param['gdbh'])
  2749. ->where('Gy0_yjno',$param['yjno'])
  2750. ->order('Gy0_gxh desc')
  2751. ->value('Gy0_gxh');
  2752. $list['工序号'] = $process + 1;
  2753. $this->success('成功',$list);
  2754. }
  2755. /**
  2756. * 糊盒工单配件完成度查询
  2757. * @return void
  2758. * @throws \think\db\exception\DataNotFoundException
  2759. * @throws \think\db\exception\ModelNotFoundException
  2760. * @throws \think\exception\DbException
  2761. */
  2762. public function Accessories()
  2763. {
  2764. if ($this->request->isGet() === false){
  2765. $this->error('请求错误');
  2766. }
  2767. $param = $this->request->param();
  2768. if (empty($param['batch'])){
  2769. $this->error('参数错误');
  2770. }
  2771. $list = \db('工单_基本资料')
  2772. ->alias('a')
  2773. ->join('工单_印件资料 b','a.Gd_gdbh=b.Yj_Gdbh')
  2774. ->join('成品入仓 c','a.Gd_gdbh=c.jjcp_gdbh and a.销售订单号 = c.订单编号 and b.yj_Yjno = c.jjcp_yjno','left')
  2775. ->where('a.销售订单号',$param['batch'])
  2776. ->field('a.Gd_gdbh as 工单编号,b.yj_Yjno as 印件号,b.yj_yjmc as 印件名称,b.yj_平张投料*b.yj_ls*b.yj_ks as 计划产量,SUM(c.jjcp_sl) as 入仓数量')
  2777. ->group('a.Gd_gdbh,b.yj_Yjno')
  2778. ->order('a.Gd_gdbh,b.yj_Yjno')
  2779. ->select();
  2780. if (empty($list)){
  2781. $this->error('未找到数据');
  2782. }else{
  2783. $this->success('成功',$list);
  2784. }
  2785. }
  2786. /**
  2787. *查询糊盒标准工单
  2788. * @return void
  2789. * @throws \think\db\exception\DataNotFoundException
  2790. * @throws \think\db\exception\ModelNotFoundException
  2791. * @throws \think\exception\DbException
  2792. *
  2793. */
  2794. public function HuheWorkOrderSearch()
  2795. {
  2796. if ($this->request->isGet() === false){
  2797. $this->error('请求错误');
  2798. }
  2799. $param = $this->request->param();
  2800. if (empty($param['search'])){
  2801. $this->error('参数错误');
  2802. }
  2803. $where['Gd_gdbh|Gd_cpmc|成品名称'] = ['like','%'.$param['search'].'%'];
  2804. $where['Gd_lx'] = '糊盒标准工单';
  2805. $list = \db('工单_基本资料')
  2806. ->field('Gd_gdbh as 工单编号,Gd_cpdh as 产品编号,Gd_cpmc as 产品名称,成品代号,成品名称,Uniqid')
  2807. ->where($where)
  2808. ->group('Gd_gdbh,Gd_cpdh,Gd_cpmc')
  2809. ->order('Gd_gdbh,Gd_cpdh')
  2810. ->select();
  2811. if (empty($list)){
  2812. $this->error('未找到数据');
  2813. }else{
  2814. $this->success('成功',$list);
  2815. }
  2816. }
  2817. /**
  2818. * 复制糊盒工单
  2819. * @return void
  2820. */
  2821. public function HuheWorkOrderCopy()
  2822. {
  2823. try {
  2824. // 1. 请求验证
  2825. if (!$this->request->isGet()) {
  2826. throw new \Exception('请求方式错误,请使用GET请求');
  2827. }
  2828. $param = $this->request->param();
  2829. $this->validateCopyParams($param);
  2830. // 2. 查询工单基本信息
  2831. $sourceWorkOrder = $this->getWorkOrderInfo($param['form_Uniqid'], ['Gd_gdbh', '行号']);
  2832. $targetWorkOrder = $this->getWorkOrderInfo($param['to_Uniqid'], ['Gd_gdbh', '行号', '订单数量', '实际投料']);
  2833. // 3. 复制印件资料和工艺资料
  2834. $this->copyPrintingData($sourceWorkOrder, $targetWorkOrder, $param['sys_id']);
  2835. $this->copyProcessData($sourceWorkOrder, $targetWorkOrder, $param['sys_id']);
  2836. $this->success('复制成功');
  2837. } catch (\Exception $e) {
  2838. $this->error($e->getMessage());
  2839. }
  2840. }
  2841. /**
  2842. * 验证复制所需的参数
  2843. * @param array $params
  2844. * @throws \Exception
  2845. */
  2846. private function validateCopyParams(array $params): void
  2847. {
  2848. if (empty($params['to_Uniqid'])) {
  2849. throw new \Exception('目标工单ID(to_Uniqid)不能为空');
  2850. }
  2851. if (empty($params['form_Uniqid'])) {
  2852. throw new \Exception('源工单ID(form_Uniqid)不能为空');
  2853. }
  2854. if (empty($params['sys_id'])) {
  2855. throw new \Exception('系统ID(sys_id)不能为空');
  2856. }
  2857. }
  2858. /**
  2859. * 获取工单信息
  2860. * @param string $uniqid 工单唯一标识
  2861. * @param array $fields 需要查询的字段
  2862. * @return array 工单信息
  2863. * @throws \Exception
  2864. */
  2865. private function getWorkOrderInfo(string $uniqid, array $fields = []): array
  2866. {
  2867. $info = \db('工单_基本资料')
  2868. ->field($fields)
  2869. ->where('Uniqid', $uniqid)
  2870. ->find();
  2871. if (empty($info)) {
  2872. throw new \Exception("工单不存在,ID: {$uniqid}");
  2873. }
  2874. return $info;
  2875. }
  2876. /**
  2877. * 复制印件资料
  2878. * @param array $sourceWorkOrder 源工单信息
  2879. * @param array $targetWorkOrder 目标工单信息
  2880. * @param string $sysId 系统ID
  2881. * @throws \Exception
  2882. */
  2883. private function copyPrintingData(array $sourceWorkOrder, array $targetWorkOrder, string $sysId): void
  2884. {
  2885. // 常量定义,便于维护
  2886. $tableName = '工单_印件资料';
  2887. $excludeFields = ['Uniqid', 'Yj_Gdbh', 'yj_Yjno', 'yj_成品数量', 'yj_平张投料', '印件完工日期', 'Sys_id', 'Sys_rq', 'Mod_rq'];
  2888. // 查询源印件资料
  2889. $printingData = \db($tableName)
  2890. ->where('Yj_Gdbh', $sourceWorkOrder['Gd_gdbh'])
  2891. ->where('yj_Yjno', $sourceWorkOrder['行号'])
  2892. ->find();
  2893. if (empty($printingData)) {
  2894. throw new \Exception('未找到源工单印件资料');
  2895. }
  2896. // 构建新的印件资料
  2897. $newPrintingData = $this->buildCopyData(
  2898. $printingData,
  2899. $excludeFields,
  2900. [
  2901. 'Yj_Gdbh' => $targetWorkOrder['Gd_gdbh'],
  2902. 'yj_Yjno' => $targetWorkOrder['行号'],
  2903. 'yj_成品数量' => $targetWorkOrder['订单数量'],
  2904. 'yj_平张投料' => $targetWorkOrder['实际投料'],
  2905. '印件完工日期' => null,
  2906. 'Sys_id' => $sysId,
  2907. 'Sys_rq' => date('Y-m-d H:i:s'),
  2908. 'Mod_rq' => null
  2909. ]
  2910. );
  2911. \db($tableName)
  2912. ->where('Yj_Gdbh',$newPrintingData['Yj_Gdbh'])
  2913. ->where('yj_Yjno',$newPrintingData['yj_Yjno'])
  2914. ->delete();
  2915. // 执行插入操作(保留fetchSql和query兼容中文字段)
  2916. $sql = \db($tableName)->fetchSql(true)->insert($newPrintingData);
  2917. $result = \db()->query($sql);
  2918. if ($result === false) {
  2919. throw new \Exception("印件资料复制失败,SQL: {$sql}");
  2920. }
  2921. }
  2922. /**
  2923. * 复制工艺资料
  2924. * @param array $sourceWorkOrder 源工单信息
  2925. * @param array $targetWorkOrder 目标工单信息
  2926. * @param string $sysId 系统ID
  2927. * @throws \Exception
  2928. */
  2929. private function copyProcessData(array $sourceWorkOrder, array $targetWorkOrder, string $sysId): void
  2930. {
  2931. // 常量定义
  2932. $tableName = '工单_工艺资料';
  2933. $excludeFields = ['UniqId', 'Gy0_gdbh', 'Gy0_yjno', 'Gy0_sbbh', 'Gy0_计划接货数', 'Gy0_最早开工时间', 'Gy0_sj1', 'Gy0_sj2', 'PD_WG', 'Sys_id', 'Sys_rq', 'Mod_rq'];
  2934. $defaultDate = '1900-01-01 00:00:00';
  2935. $currentTime = date('Y-m-d H:i:s');
  2936. // 查询源工艺资料列表
  2937. $processList = \db($tableName)
  2938. ->where('Gy0_gdbh', $sourceWorkOrder['Gd_gdbh'])
  2939. ->where('Gy0_yjno', $sourceWorkOrder['行号'])
  2940. ->select();
  2941. if (empty($processList)) {
  2942. throw new \Exception('未找到源工单工艺资料');
  2943. }
  2944. // 构建新的工艺资料列表
  2945. $newProcessData = [];
  2946. foreach ($processList as $item) {
  2947. $newProcessData[] = $this->buildCopyData(
  2948. $item,
  2949. $excludeFields,
  2950. [
  2951. 'Gy0_gdbh' => $targetWorkOrder['Gd_gdbh'],
  2952. 'Gy0_yjno' => $targetWorkOrder['行号'],
  2953. 'Gy0_sbbh' => '',
  2954. 'Gy0_计划接货数' => $targetWorkOrder['实际投料'],
  2955. 'Gy0_最早开工时间' => $defaultDate,
  2956. 'Gy0_sj1' => $defaultDate,
  2957. 'Gy0_sj2' => $defaultDate,
  2958. 'PD_WG' => $defaultDate,
  2959. 'Sys_id' => $sysId,
  2960. 'Sys_rq' => $currentTime,
  2961. 'Mod_rq' => null
  2962. ]
  2963. );
  2964. }
  2965. \db($tableName)
  2966. ->where('Gy0_gdbh', $targetWorkOrder['Gd_gdbh'])
  2967. ->where('Gy0_yjno', $targetWorkOrder['行号'])
  2968. ->delete();
  2969. // 执行批量插入(保留fetchSql和query兼容中文字段)
  2970. if (!empty($newProcessData)) {
  2971. $sql = \db($tableName)->fetchSql(true)->insertAll($newProcessData);
  2972. $result = \db()->query($sql);
  2973. if ($result === false) {
  2974. throw new \Exception("工艺资料复制失败,SQL: {$sql}");
  2975. }
  2976. }
  2977. }
  2978. /**
  2979. * 构建复制的数据
  2980. * @param array $sourceData 源数据
  2981. * @param array $excludeFields 需要排除的字段
  2982. * @param array $overrideData 需要覆盖的字段和值
  2983. * @return array 构建后的新数据
  2984. */
  2985. private function buildCopyData(array $sourceData, array $excludeFields, array $overrideData): array
  2986. {
  2987. $newData = [];
  2988. // 复制源数据中不需要排除的字段
  2989. foreach ($sourceData as $key => $value) {
  2990. if (!in_array($key, $excludeFields)) {
  2991. $newData[$key] = $value;
  2992. }
  2993. }
  2994. // 覆盖需要替换的字段
  2995. return array_merge($newData, $overrideData);
  2996. }
  2997. /**
  2998. * 糊盒标准工单左侧菜单栏
  2999. * @return void
  3000. * @throws \think\db\exception\DataNotFoundException
  3001. * @throws \think\db\exception\ModelNotFoundException
  3002. * @throws \think\exception\DbException
  3003. */
  3004. public function HuheDataList()
  3005. {
  3006. if ($this->request->isGet() === false){
  3007. $this->error('请求错误');
  3008. }
  3009. $list = \db('产品_基本资料')
  3010. ->where('客户编号','<>','')
  3011. ->field('rtrim(客户编号) as 客户编号,rtrim(客户名称) as 客户名称')
  3012. ->group('客户编号,客户名称')
  3013. ->order('客户编号')
  3014. ->select();
  3015. if (empty($list)){
  3016. $this->success('',[]);
  3017. }
  3018. $data['糊盒工单'] = [];
  3019. foreach ($list as $key=>$value){
  3020. $work_order_count = \db('工单_基本资料')
  3021. ->where('行号','1')
  3022. ->where('Gd_cpdh|成品代号','LIKE',rtrim($value['客户编号']).'%')
  3023. ->where('Gd_lx','糊盒标准工单')
  3024. ->count();
  3025. if (!empty($value['客户名称']) && $work_order_count !== 0){
  3026. $name = $value['客户编号'].rtrim($value['客户名称']);
  3027. if (strpos($value['客户编号'],'J') !== false ){
  3028. array_push($data['糊盒工单'],$name);
  3029. }
  3030. }
  3031. }
  3032. $this->success('成功',$data);
  3033. }
  3034. /**
  3035. * 糊盒标准工单列表
  3036. * @return void
  3037. * @throws \think\Exception
  3038. * @throws \think\db\exception\DataNotFoundException
  3039. * @throws \think\db\exception\ModelNotFoundException
  3040. * @throws \think\exception\DbException
  3041. */
  3042. public function HuheWorkList()
  3043. {
  3044. if ($this->request->isGet() === false){
  3045. $this->error('请求错误');
  3046. }
  3047. $search = input('search');
  3048. $clientNumber = input('Gd_khdh');
  3049. $startTime = input('start');
  3050. $endTime = input('end');
  3051. $page = input('page');
  3052. $limit = input('limit');
  3053. $param = $this->request->param();
  3054. $where = [];
  3055. if (!empty($clientNumber)){
  3056. if (substr($clientNumber,0,1) !== 'Y' && substr($clientNumber,0,1) !== 'J')
  3057. {
  3058. $clientNumber = substr($clientNumber,0,4);
  3059. }
  3060. if ($clientNumber === 'Y1401'){
  3061. $where['Gd_cpdh'] = ['like',$clientNumber.'%'];
  3062. }else{
  3063. $where['成品代号'] = ['like',$clientNumber.'%'];
  3064. }
  3065. }
  3066. if (!empty($workOrder)){
  3067. $where['Gd_gdbh'] = $workOrder;
  3068. }
  3069. if (!empty($productCode)){
  3070. $where['Gd_cpdh'] = $productCode;
  3071. }
  3072. if (!empty($search)){
  3073. $where['Gd_gdbh|Gd_客户代号|Gd_客户名称|Gd_khdh|Gd_khmc|Gd_cpdh|Gd_cpmc|成品代号|成品名称|产品版本号'] = ['like','%'.$search.'%'];
  3074. }
  3075. if (!empty($startTime) && !empty($endTime)){
  3076. $where['接单日期'] = ['between',[$startTime,$endTime]];
  3077. }
  3078. $where['Gd_lx'] = '糊盒标准工单';
  3079. $total = \db('工单_基本资料')->where($where)->distinct(true)->field('Gd_gdbh')->count();
  3080. $list = \db('工单_基本资料')
  3081. ->where($where)
  3082. ->order('Gd_statu desc')
  3083. ->limit(($page-1)*$limit,$limit)
  3084. ->group('Gd_gdbh')
  3085. ->select();
  3086. //工单基本资料数据整理
  3087. $data = [];
  3088. foreach ($list as $key=>$value){
  3089. $data[$key] = [
  3090. '工单编号' => rtrim($value['Gd_gdbh']),
  3091. '生产分类' => rtrim($value['Gd_生产分类']),
  3092. '销售订单号' => rtrim($value['销售订单号']),
  3093. '产品代号' => rtrim($value['成品代号']),
  3094. '产品名称' => rtrim($value['成品名称']),
  3095. '订单数量' => floatval($value['订单数量']),
  3096. '单位' => rtrim($value['计量单位']),
  3097. '折合大箱' => rtrim((int)$value['投料大箱']),
  3098. '投料率' => rtrim($value['投料率']),
  3099. '平均合格率' => '',
  3100. '开单日期' => date('Y-m-d',strtotime(rtrim($value['接单日期']))),
  3101. '交货日期' => date('Y-m-d',strtotime(rtrim($value['交货日期']))),
  3102. '工单类型' => rtrim($value['Gd_lx']),
  3103. '工单状态' => rtrim($value['gd_statu']),
  3104. '当前生产工序' => '',
  3105. '产量提交时间' => '',
  3106. '建档用户' => rtrim($value['Sys_id']),
  3107. '建档时间' => rtrim($value['Sys_rq']),
  3108. '更新时间' => rtrim($value['Mod_rq']),
  3109. 'Uniqid' => rtrim($value['Uniqid'])
  3110. ];
  3111. $number = \db('工单_工艺资料')->where('Gy0_gdbh',$data[$key]['工单编号'])->count();
  3112. if ($number === 0){
  3113. $data[$key]['status'] = '*';
  3114. }else{
  3115. $data[$key]['status'] = '';
  3116. }
  3117. }
  3118. $this->success('成功',['data'=>$data,'total'=>$total]);
  3119. }
  3120. /**
  3121. * 成品入仓之后定时任务,更新工单状态(建议每天凌晨执行)
  3122. * 条件:已录入末板,且最后一条入仓时间超过1个月,工单未完工
  3123. * @return void
  3124. */
  3125. public function updateWorkOrderStatus()
  3126. {
  3127. $deadline = date('Y-m-d H:i:s', strtotime('-1 month'));
  3128. $now = date('Y-m-d H:i:s');
  3129. $endBoardOrders = \db('成品入仓')
  3130. ->where('jjcp_smb', 'like', '末%')
  3131. ->field('jjcp_gdbh,jjcp_yjno')
  3132. ->group('jjcp_gdbh,jjcp_yjno')
  3133. ->select();
  3134. $updated = 0;
  3135. $skipped = 0;
  3136. $logs = [];
  3137. foreach ($endBoardOrders as $order) {
  3138. $gdbh = trim((string)$order['jjcp_gdbh']);
  3139. $yjno = trim((string)$order['jjcp_yjno']);
  3140. if ($gdbh === '' || $yjno === '') {
  3141. $skipped++;
  3142. continue;
  3143. }
  3144. $lastSj = \db('成品入仓')
  3145. ->where('jjcp_gdbh', $gdbh)
  3146. ->where('jjcp_yjno', $yjno)
  3147. ->max('jjcp_sj');
  3148. if (empty($lastSj) || $lastSj > $deadline) {
  3149. $skipped++;
  3150. continue;
  3151. }
  3152. $workOrder = \db('工单_基本资料')
  3153. ->where('Gd_gdbh', $gdbh)
  3154. ->where('行号', $yjno)
  3155. ->field('Gd_gdbh,行号,rtrim(gd_statu) as gd_statu,工单完工日期')
  3156. ->find();
  3157. if (empty($workOrder)) {
  3158. $skipped++;
  3159. continue;
  3160. }
  3161. if (!$this->isEmptyWorkOrderFinishDate($workOrder['工单完工日期'])) {
  3162. $skipped++;
  3163. continue;
  3164. }
  3165. if (trim((string)$workOrder['gd_statu']) === '1-已完工') {
  3166. $skipped++;
  3167. continue;
  3168. }
  3169. $updateResult = \db('工单_基本资料')
  3170. ->where('Gd_gdbh', $gdbh)
  3171. ->where('行号', $yjno)
  3172. ->update([
  3173. '工单完工日期' => $now,
  3174. 'gd_statu' => '1-已完工',
  3175. 'Mod_rq' => $now,
  3176. ]);
  3177. if ($updateResult === false) {
  3178. $skipped++;
  3179. continue;
  3180. }
  3181. $updated++;
  3182. $logs[] = [
  3183. 'Gd_gdbh' => $gdbh,
  3184. 'yjno' => $yjno,
  3185. 'ModifyUser' => 'system',
  3186. 'ModifyTime' => $now,
  3187. 'FieldName' => 'gd_statu',
  3188. 'OldValue' => trim((string)$workOrder['gd_statu']),
  3189. 'NewValue' => '1-已完工',
  3190. 'ModifySource' => '成品入仓定时任务',
  3191. ];
  3192. $logs[] = [
  3193. 'Gd_gdbh' => $gdbh,
  3194. 'yjno' => $yjno,
  3195. 'ModifyUser' => 'system',
  3196. 'ModifyTime' => $now,
  3197. 'FieldName' => '工单完工日期',
  3198. 'OldValue' => trim((string)$workOrder['工单完工日期']),
  3199. 'NewValue' => $now,
  3200. 'ModifySource' => '成品入仓定时任务',
  3201. ];
  3202. }
  3203. if (!empty($logs)) {
  3204. $this->saveWorkOrderOperateLog($logs);
  3205. }
  3206. $this->success('定时任务执行完成', [
  3207. 'updated' => $updated,
  3208. 'skipped' => $skipped,
  3209. 'total_checked' => count($endBoardOrders),
  3210. 'deadline' => $deadline,
  3211. ]);
  3212. }
  3213. /**
  3214. * 印件操作日志写入工单_操作日志
  3215. * @param array $rows
  3216. * @return void
  3217. */
  3218. protected function savePrintOperateLog(array $rows)
  3219. {
  3220. $this->saveWorkOrderOperateLog($rows);
  3221. }
  3222. /**
  3223. * 写入工单_操作日志,兼容系统操作日志和印件操作日志两种字段
  3224. * @param array $rows
  3225. * @return void
  3226. */
  3227. protected function saveWorkOrderOperateLog(array $rows)
  3228. {
  3229. if (empty($rows)) {
  3230. return;
  3231. }
  3232. $now = date('Y-m-d H:i:s');
  3233. $logs = [];
  3234. foreach ($rows as $row) {
  3235. $sysRq = $now;
  3236. if (isset($row['sys_rq']) && $row['sys_rq'] !== '') {
  3237. $sysRq = $row['sys_rq'];
  3238. } elseif (isset($row['ModifyTime']) && $row['ModifyTime'] !== '') {
  3239. $sysRq = $row['ModifyTime'];
  3240. }
  3241. $logs[] = [
  3242. 'Yj_Gdbh' => $this->workOrderLogValue($row, ['Yj_Gdbh', 'Gd_gdbh']),
  3243. 'yj_Yjno' => $this->workOrderLogValue($row, ['yj_Yjno', 'yjno']),
  3244. '工序号' => $this->workOrderLogValue($row, ['工序号', 'gxh']),
  3245. '工序名称' => $this->workOrderLogValue($row, ['工序名称', 'gxmc']),
  3246. 'yj_Yjdh' => $this->workOrderLogValue($row, ['yj_Yjdh']),
  3247. 'yj_yjmc' => $this->workOrderLogValue($row, ['yj_yjmc']),
  3248. 'PrintUniqid' => $this->workOrderLogValue($row, ['PrintUniqid']),
  3249. '操作类型' => $this->workOrderLogValue($row, ['操作类型', 'ModifySource']),
  3250. '字段名' => $this->workOrderLogValue($row, ['字段名', 'FieldName']),
  3251. '修改前' => $this->workOrderLogValue($row, ['修改前', 'OldValue']),
  3252. '修改后' => $this->workOrderLogValue($row, ['修改后', 'NewValue']),
  3253. 'sys_id' => $this->workOrderLogValue($row, ['sys_id', 'ModifyUser']),
  3254. 'sys_rq' => $sysRq,
  3255. '备注' => $this->workOrderLogValue($row, ['备注']),
  3256. ];
  3257. }
  3258. \db('工单_操作日志')->insertAll($logs);
  3259. }
  3260. /**
  3261. * @param array $row
  3262. * @param array $keys
  3263. * @return string
  3264. */
  3265. protected function workOrderLogValue(array $row, array $keys)
  3266. {
  3267. foreach ($keys as $key) {
  3268. if (array_key_exists($key, $row) && $row[$key] !== null && $row[$key] !== '') {
  3269. return is_scalar($row[$key]) ? (string)$row[$key] : '';
  3270. }
  3271. }
  3272. return '';
  3273. }
  3274. /**
  3275. * 判断工单完工日期是否为空
  3276. * @param mixed $date
  3277. * @return bool
  3278. */
  3279. protected function isEmptyWorkOrderFinishDate($date)
  3280. {
  3281. if ($date === null) {
  3282. return true;
  3283. }
  3284. $date = trim((string)$date);
  3285. if ($date === '') {
  3286. return true;
  3287. }
  3288. $timestamp = strtotime($date);
  3289. return $timestamp === false || $timestamp <= strtotime('1900-01-02');
  3290. }
  3291. }