WorkOrder.php 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458
  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. $param['Uniqid'] = $lastId;
  1764. $param['Sys_rq'] = date('Y-m-d H:i:s',time());
  1765. $param['Mod_rq'] = date('Y-m-d H:i:s',time());
  1766. //判断印件号是否存在在工单基本资料表中,存在不增加行号,不存在增加行号
  1767. if (empty(\db('工单_基本资料')->where('Gd_gdbh',$param['Yj_Gdbh'])->where('行号',$param['yj_Yjno'])->find())){
  1768. $list = \db('工单_基本资料')->where('Gd_gdbh',$param['Yj_Gdbh'])->find();
  1769. $list['行号'] = $param['yj_Yjno'];
  1770. $list['Gd_cpdh'] = $param['yj_Yjdh'];
  1771. $list['Gd_cpmc'] = $param['yj_yjmc'];
  1772. $list['Sys_id'] = $param['sys_id'];
  1773. $list['Sys_rq'] = $param['Sys_rq'];
  1774. $list['Mod_rq'] = $param['Mod_rq'];
  1775. unset($list['Uniqid']);
  1776. $workSql = \db('工单_基本资料')->fetchSql(true)->insert($list);
  1777. $workAction = '新增工单基本资料行';
  1778. }else{
  1779. $data['Gd_cpdh'] = $param['yj_Yjdh'];
  1780. $data['Gd_cpmc'] = $param['yj_yjmc'];
  1781. $workSql = \db('工单_基本资料')
  1782. ->where('Gd_gdbh',$param['Yj_Gdbh'])
  1783. ->where('行号',$param['yj_Yjno'])
  1784. ->fetchSql(true)
  1785. ->update($data);
  1786. $workAction = '更新工单基本资料行';
  1787. }
  1788. //开启事务
  1789. Db::startTrans();
  1790. try{
  1791. $sql = \db('工单_印件资料')->fetchSql(true)->insert($param);
  1792. $res = \db()->query($sql);
  1793. $workRes = \db()->query($workSql);
  1794. if ($res !== false && $workRes !== false){
  1795. $this->savePrintOperateLog([[
  1796. 'Yj_Gdbh' => $param['Yj_Gdbh'],
  1797. 'yj_Yjno' => $param['yj_Yjno'],
  1798. 'yj_Yjdh' => isset($param['yj_Yjdh']) ? $param['yj_Yjdh'] : '',
  1799. 'yj_yjmc' => isset($param['yj_yjmc']) ? $param['yj_yjmc'] : '',
  1800. 'PrintUniqid' => $param['Uniqid'],
  1801. '操作类型' => '新增',
  1802. 'sys_id' => isset($param['sys_id']) ? $param['sys_id'] : '',
  1803. '备注' => $workAction,
  1804. ]]);
  1805. }
  1806. //提交事务
  1807. \db()->commit();
  1808. } catch (\Exception $e) {
  1809. echo $e->getMessage();
  1810. // 回滚事务
  1811. \db()->rollback();
  1812. }
  1813. if ($res !== false && $workRes !== false){
  1814. $this->success('成功');
  1815. }else{
  1816. $this->error('失败');
  1817. }
  1818. }
  1819. /**
  1820. * 新增工艺资料->工艺资料添加
  1821. * @return void
  1822. * @throws \think\db\exception\BindParamException
  1823. * @throws \think\exception\PDOException
  1824. */
  1825. public function ProcessDetailAdd()
  1826. {
  1827. if (Request::instance()->isPost() === false){
  1828. $this->error('请求错误');
  1829. }
  1830. $param = Request::instance()->post();
  1831. if (empty($param)){
  1832. $this->error('参数错误');
  1833. }
  1834. $lastId = \db('工单_工艺资料')->order('UniqId desc')->value('UniqId');
  1835. if (empty($lastId)){
  1836. $lastId = 0;
  1837. }else{
  1838. $lastId = $lastId + 1;
  1839. }
  1840. if (empty($param['Gy0_shdh'])){
  1841. $param['Gy0_Rate0'] = 0;
  1842. $param['Gy0_Rate1'] = 0;
  1843. }else{
  1844. $detail = \db('dic_lzsh')->field('sys_rate0,sys_rate1')->where('sys_bh',$param['Gy0_shdh'])->find();
  1845. $param['Gy0_Rate0'] = $detail['sys_rate0'];
  1846. $param['Gy0_Rate1'] = $detail['sys_rate1'];
  1847. }
  1848. $param['Sys_rq'] = date('Y-m-d H:i:s',time());
  1849. $param['Mod_rq'] = '1900-01-01 00:00:00';
  1850. $param['Gy0_sj1'] = '1900-01-01 00:00:00';
  1851. $param['Gy0_sj2'] = '1900-01-01 00:00:00';
  1852. $param['PD_WG'] = '1900-01-01 00:00:00';
  1853. $param['UniqId'] = $lastId;
  1854. //添加工序
  1855. $sql = \db('工单_工艺资料')
  1856. ->fetchSql(true)
  1857. ->insert($param);
  1858. $res = \db()->query($sql);
  1859. //获取平张投料
  1860. $number = \db('工单_印件资料')
  1861. ->where('Yj_Gdbh',$param['Gy0_gdbh'])
  1862. ->where('yj_Yjno',$param['Gy0_yjno'])
  1863. ->value('rtrim(yj_平张投料)');
  1864. //分配工艺计划产量
  1865. if ((int)$number >0){
  1866. $this->PlannedProcessYield($param['Gy0_gdbh'],$param['Gy0_yjno'],0,$number);
  1867. }
  1868. //操作日志数据
  1869. $logs = array(
  1870. 'Gd_gdbh' => $param['Gy0_gdbh'],
  1871. 'yjno' => $param['Gy0_yjno'],
  1872. 'gxh' => $param['Gy0_gxh'],
  1873. 'gxmc' => isset($param['Gy0_gxmc']) ? $param['Gy0_gxmc'] : '',
  1874. 'ModifyUser' => $param['Sys_id'],
  1875. 'ModifyTime' => date('Y-m-d H:i:s'),
  1876. 'ModifySource' => '工艺新增'
  1877. );
  1878. $this->saveWorkOrderOperateLog([$logs]);
  1879. if ($res !== false){
  1880. $this->success('成功');
  1881. }else{
  1882. $this->error('失败');
  1883. }
  1884. }
  1885. /**
  1886. * 新增工艺资料->印件编号获取
  1887. * @return void
  1888. * @throws \think\db\exception\DataNotFoundException
  1889. * @throws \think\db\exception\ModelNotFoundException
  1890. * @throws \think\exception\DbException
  1891. */
  1892. public function PrintDetailList()
  1893. {
  1894. if ($this->request->isGet() === false){
  1895. $this->error('请求错误');
  1896. }
  1897. $param = $this->request->param();
  1898. if (empty($param)){
  1899. $this->error('参数错误');
  1900. }
  1901. $printList = \db('工单_印件资料')
  1902. ->where('Yj_Gdbh',$param['workOrder'])
  1903. ->order('yj_Yjno')
  1904. ->select();
  1905. if (empty($printList)){
  1906. $this->success('未找到该工单印件资料');
  1907. }
  1908. $list = [];
  1909. foreach ($printList as $key=>$value){
  1910. $list[$key]['name'] = rtrim($value['yj_Yjno']).'-->'.rtrim($value['yj_yjmc']);
  1911. $list[$key]['no'] = rtrim($value['yj_Yjno']);
  1912. }
  1913. $this->success('成功',$list);
  1914. }
  1915. /**
  1916. * 工单资料管理->印件资料删除
  1917. * @return void
  1918. * @throws \think\Exception
  1919. * @throws \think\exception\PDOException
  1920. */
  1921. public function PrintDetailDel()
  1922. {
  1923. if ($this->request->isGet() === false){
  1924. $this->error('请求错误');
  1925. }
  1926. $param = $this->request->param();
  1927. if (isset($param['UniqId']) === false){
  1928. $this->error('参数错误');
  1929. }
  1930. $printId = explode(',',$param['UniqId']);
  1931. $printRows = \db('工单_印件资料')->where('Uniqid','in',$printId)->select();
  1932. $printMap = [];
  1933. foreach ($printRows as $row) {
  1934. $printMap[$row['Uniqid']] = $row;
  1935. }
  1936. $i = 0;
  1937. $logs = [];
  1938. foreach ($printId as $value){
  1939. $res = \db('工单_印件资料')
  1940. ->where('Uniqid',$value)
  1941. ->delete();
  1942. if ($res === false){
  1943. $i++;
  1944. continue;
  1945. }
  1946. if ($res && isset($printMap[$value])) {
  1947. $row = $printMap[$value];
  1948. $logs[] = [
  1949. 'Yj_Gdbh' => $row['Yj_Gdbh'],
  1950. 'yj_Yjno' => $row['yj_Yjno'],
  1951. 'yj_Yjdh' => $row['yj_Yjdh'],
  1952. 'yj_yjmc' => $row['yj_yjmc'],
  1953. 'PrintUniqid' => $row['Uniqid'],
  1954. '操作类型' => '删除',
  1955. '修改前' => trim((string)$row['yj_yjmc']),
  1956. 'sys_id' => isset($param['sys_id']) ? $param['sys_id'] : '',
  1957. ];
  1958. }
  1959. }
  1960. $this->savePrintOperateLog($logs);
  1961. if ($i === 0){
  1962. $this->success('删除成功');
  1963. }else{
  1964. $this->error('删除失败');
  1965. }
  1966. }
  1967. /**
  1968. * 工单资料管理->工艺资料删除
  1969. * @return void
  1970. * @throws \think\Exception
  1971. * @throws \think\exception\PDOException
  1972. */
  1973. public function ProcessDetailDel()
  1974. {
  1975. if ($this->request->isGet() === false){
  1976. $this->error('请求错误');
  1977. }
  1978. $param = $this->request->param();
  1979. if (isset($param['UniqId']) === false){
  1980. $this->error('参数错误');
  1981. }
  1982. $printId = explode(',',$param['UniqId']);
  1983. $i = 0;
  1984. //获取工单编号、印件号
  1985. $codeList = \db('工单_工艺资料')
  1986. ->where('UniqId',$printId[0])
  1987. ->field('rtrim(Gy0_gdbh) as 工单编号,rtrim(Gy0_yjno) as 印件号')
  1988. ->find();
  1989. //获取平张投料数量
  1990. $number = \db('工单_印件资料')
  1991. ->where([
  1992. 'Yj_Gdbh' => $codeList['工单编号'],
  1993. 'yj_Yjno' => $codeList['印件号']
  1994. ])
  1995. ->value('rtrim(yj_平张投料)');
  1996. // 新增:批量获取要删除的工序号
  1997. $gxhList = \db('工单_工艺资料')
  1998. ->where('UniqId', 'in', $printId)
  1999. ->column('rtrim(Gy0_gxh) as gxh', 'UniqId');
  2000. //循环删除工艺资料
  2001. $deletedGxh = [];
  2002. $deletedGxmc = [];
  2003. foreach ($printId as $value){
  2004. $process = \db('工单_工艺资料')
  2005. ->where('UniqId',$value)
  2006. ->find();
  2007. $processCount = \db('设备_产量计酬')
  2008. ->where('sczl_gdbh',$process['Gy0_gdbh'])
  2009. ->where('sczl_yjno',$process['Gy0_yjno'])
  2010. ->where('sczl_gxh',$process['Gy0_gxh'])
  2011. ->count();
  2012. if ($processCount > 0){
  2013. $this->error('工序'.$process['Gy0_gxh'].'已上报数据,无法删除,请核对');
  2014. }
  2015. $res = \db('工单_工艺资料')
  2016. ->where('UniqId',$value)
  2017. ->delete();
  2018. if ($res) {
  2019. // 记录成功删除的工序号
  2020. if (isset($gxhList[$value])) {
  2021. $deletedGxh[] = $gxhList[$value];
  2022. $deletedGxmc[] = isset($process['Gy0_gxmc']) ? trim($process['Gy0_gxmc']) : '';
  2023. }
  2024. } else {
  2025. $i++;
  2026. }
  2027. }
  2028. //重新分配工艺计划产量
  2029. if ((int)$number>0){
  2030. $this->PlannedProcessYield($codeList['工单编号'],$codeList['印件号'],0,$number);
  2031. }
  2032. // 新增:写入操作日志
  2033. $this->saveWorkOrderOperateLog([[
  2034. 'ModifyUser' => $param['sys_id'],
  2035. 'ModifyTime' => date('Y-m-d H:i:s', time()),
  2036. 'Gd_gdbh' => $codeList['工单编号'],
  2037. 'yjno' => $codeList['印件号'],
  2038. 'gxh' => implode(',', $deletedGxh),
  2039. 'gxmc' => implode(',', $deletedGxmc),
  2040. 'ModifySource' => '工艺资料删除'
  2041. ]]);
  2042. if ($i === 0){
  2043. $this->success('删除成功');
  2044. }else{
  2045. $this->error('删除失败');
  2046. }
  2047. }
  2048. /**
  2049. * 工艺资料添加->工序损耗代码
  2050. * @return void
  2051. * @throws \think\db\exception\DataNotFoundException
  2052. * @throws \think\db\exception\ModelNotFoundException
  2053. * @throws \think\exception\DbException
  2054. */
  2055. public function WastageList()
  2056. {
  2057. if ($this->request->isGet() === false){
  2058. $this->error('请求错误');
  2059. }
  2060. $param = $this->request->param();
  2061. if (empty($param['process'])){
  2062. $this->error('参数错误');
  2063. }
  2064. if (strlen($param['process'])>6){
  2065. $param['process'] = substr($param['process'],0,6);
  2066. }
  2067. $order = \db('dic_lzsh')
  2068. ->where('sys_mc','like',$param['process'].'%')
  2069. ->value('rtrim(sys_bh) as 编号');
  2070. $this->success('成功',$order);
  2071. }
  2072. /**
  2073. * 获取产品附件列表
  2074. */
  2075. public function getAnnexTable(){
  2076. if(!$this->request->isGet()){
  2077. $this->error('请求方式错误');
  2078. }
  2079. $req = $this->request->param();
  2080. if (isset($req['cpdh']) && !empty($req['cpdh'])){
  2081. $where['关联产品'] = ['LIKE','%'.$req['cpdh'].'%'];
  2082. }else{
  2083. $this->error('参数错误');
  2084. }
  2085. $rows = db('产品_技术附件')
  2086. ->where($where)
  2087. ->select();
  2088. foreach ($rows as $key=>&$value){
  2089. if(mb_detect_encoding($value['附件内容'])!='ASCII'){
  2090. $value['附件内容'] = '';
  2091. }
  2092. }
  2093. $this->success('成功',$rows);
  2094. }
  2095. /**
  2096. * 产品附件新增
  2097. */
  2098. public function annexAdd(){
  2099. if(!$this->request->isPost()){
  2100. $this->error('请求方式错误');
  2101. }
  2102. $req = $this->request->param();
  2103. $arr = [
  2104. 'sys_id',
  2105. '序号',
  2106. '附件备注',
  2107. '附件内容',
  2108. '附件类型',
  2109. '适用工序',
  2110. '关联产品'
  2111. ];
  2112. $data = [];
  2113. foreach ($arr as $key => $value){
  2114. if (!isset($req[$value])){
  2115. $this->error('参数错误',$value,$key+1);
  2116. }
  2117. $data[$value] = $req[$value];
  2118. }
  2119. $data['sys_rq'] = date('Y-m-d H:i:s');
  2120. //查询UniqId
  2121. $UniqId = db('产品_技术附件')->max('UniqId');
  2122. $data['UniqId'] = $UniqId < 10000000 ? 10000000 : $UniqId + 1;
  2123. //开启事务
  2124. db()->startTrans();
  2125. try{
  2126. $sql = db('产品_技术附件')->fetchSql(true)->insert($data);
  2127. $bool = db()->query($sql);
  2128. // 提交事务
  2129. db()->commit();
  2130. } catch (\Exception $e) {
  2131. // 回滚事务
  2132. db()->rollback();
  2133. $this->error($e->getMessage());
  2134. }
  2135. if($bool===false) $this->error('失败');
  2136. $this->success('成功');
  2137. }
  2138. /**
  2139. * 产品附件修改
  2140. * @ApiMethod (GET)
  2141. * @param string 'UniqId'
  2142. */
  2143. public function annexDel()
  2144. {
  2145. if(!$this->request->isGet()){
  2146. $this->error('请求方式错误');
  2147. }
  2148. $req = $this->request->param();
  2149. if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
  2150. $this->error('参数错误','UniqId',100);
  2151. }
  2152. //开启事务
  2153. db()->startTrans();
  2154. try{
  2155. $bool = db('产品_技术附件')->where('UniqId',$req['UniqId'])->delete();
  2156. // 提交事务
  2157. db()->commit();
  2158. } catch (\Exception $e) {
  2159. // 回滚事务
  2160. db()->rollback();
  2161. $this->error($e->getMessage());
  2162. }
  2163. if($bool===false) $this->error('失败');
  2164. $this->success('成功');
  2165. }
  2166. /**
  2167. * 获取产品附件列表
  2168. */
  2169. public function getGdAnnexTable(){
  2170. if(!$this->request->isGet()){
  2171. $this->error('请求方式错误');
  2172. }
  2173. $req = $this->request->param();
  2174. if (isset($req['gdbh']) && !empty($req['gdbh'])){
  2175. $where['关联编号'] = ['LIKE','%'.$req['gdbh'].'%'];
  2176. }else{
  2177. $this->error('参数错误');
  2178. }
  2179. $rows = db('工单_相关附件')
  2180. ->where($where)
  2181. ->select();
  2182. foreach ($rows as $key=>&$value){
  2183. if(mb_detect_encoding($value['附件内容'])!='ASCII'){
  2184. $value['附件内容'] = '';
  2185. }
  2186. }
  2187. $this->success('成功',$rows);
  2188. }
  2189. /**
  2190. * 产品附件新增
  2191. */
  2192. public function gdAnnexAdd(){
  2193. if(!$this->request->isPost()){
  2194. $this->error('请求方式错误');
  2195. }
  2196. $req = $this->request->param();
  2197. $arr = [
  2198. 'sys_id',
  2199. '附件备注',
  2200. '附件内容',
  2201. '附件类型',
  2202. '关联编号'
  2203. ];
  2204. $data = [];
  2205. foreach ($arr as $key => $value){
  2206. if (!isset($req[$value])){
  2207. $this->error('参数错误',$value,$key+1);
  2208. }
  2209. $data[$value] = $req[$value];
  2210. }
  2211. $data['sys_rq'] = date('Y-m-d H:i:s');
  2212. //查询UniqId
  2213. $UniqId = db('工单_相关附件')->max('UniqId');
  2214. $data['UniqId'] = $UniqId < 10000000 ? 10000000 : $UniqId + 1;
  2215. //开启事务
  2216. db()->startTrans();
  2217. try{
  2218. $sql = db('工单_相关附件')->fetchSql(true)->insert($data);
  2219. $bool = db()->query($sql);
  2220. // 提交事务
  2221. db()->commit();
  2222. } catch (\Exception $e) {
  2223. // 回滚事务
  2224. db()->rollback();
  2225. $this->error($e->getMessage());
  2226. }
  2227. if($bool===false) $this->error('失败');
  2228. $this->success('成功');
  2229. }
  2230. /**
  2231. * 产品附件修改
  2232. * @ApiMethod (GET)
  2233. * @param string 'UniqId'
  2234. */
  2235. public function gdAnnexDel()
  2236. {
  2237. if(!$this->request->isGet()){
  2238. $this->error('请求方式错误');
  2239. }
  2240. $req = $this->request->param();
  2241. if (!(isset($req['UniqId']) && trim($req['UniqId'])!='')){
  2242. $this->error('参数错误','UniqId',100);
  2243. }
  2244. //开启事务
  2245. db()->startTrans();
  2246. try{
  2247. $bool = db('工单_相关附件')->where('UniqId',$req['UniqId'])->delete();
  2248. // 提交事务
  2249. db()->commit();
  2250. } catch (\Exception $e) {
  2251. // 回滚事务
  2252. db()->rollback();
  2253. $this->error($e->getMessage());
  2254. }
  2255. if($bool===false) $this->error('失败');
  2256. $this->success('成功');
  2257. }
  2258. /**
  2259. * 引用工单资料数据查询
  2260. * @return void
  2261. * @throws \think\db\exception\DataNotFoundException
  2262. * @throws \think\db\exception\ModelNotFoundException
  2263. * @throws \think\exception\DbException
  2264. */
  2265. public function WorkOrderDetailCope()
  2266. {
  2267. if ($this->request->isGet() === false){
  2268. $this->error('请求错误');
  2269. }
  2270. $param = $this->request->param();
  2271. if (empty($param['search'])){
  2272. $this->error('参数错误');
  2273. }
  2274. $list = \Db('工单_基本资料')
  2275. ->alias('a')
  2276. ->field([
  2277. 'rtrim(a.Gd_gdbh)' => '工单编号',
  2278. 'rtrim(b.客户编号)' => '客户编号',
  2279. 'rtrim(b.客户名称)' => '客户名称',
  2280. 'rtrim(b.产品编号)' => '产品编号',
  2281. 'rtrim(b.产品名称)' => '产品名称'
  2282. ])
  2283. ->join('产品_基本资料 b', 'a.成品代号 = b.产品编号')
  2284. ->join('产品_工艺资料 c', 'a.成品代号 = c.Gy0_cpdh')
  2285. ->join('产品_印件资料 d', 'a.成品代号 = d.yj_cpdh')
  2286. ->where(function($query) use ($param) {
  2287. $query->where('a.Gd_gdbh', 'like', '%'.$param['search'].'%')
  2288. ->whereOr('a.成品名称', 'like', '%'.$param['search'].'%');
  2289. })
  2290. ->where(function($query) {
  2291. $query->where('c.Gy0_cpdh', '<>', '')
  2292. ->where('d.yj_cpdh', '<>', '')
  2293. ->where('b.状态', 'not like', '%停用%')
  2294. ->where('b.产品名称', 'not like', '%停用%');
  2295. })
  2296. ->group('a.Gd_gdbh')
  2297. ->order('a.Uniqid desc')
  2298. ->select();
  2299. if (empty($list)){
  2300. $this->success('未找到工单资料');
  2301. }else{
  2302. $this->success('成功',$list);
  2303. }
  2304. }
  2305. /**
  2306. * 引用产品印件、工艺资料
  2307. * @return void
  2308. * @throws \think\db\exception\BindParamException
  2309. * @throws \think\db\exception\DataNotFoundException
  2310. * @throws \think\db\exception\ModelNotFoundException
  2311. * @throws \think\exception\DbException
  2312. * @throws \think\exception\PDOException
  2313. */
  2314. public function WorkOrderDetailListCope()
  2315. {
  2316. if (Request::instance()->isPost() === false){
  2317. $this->error('请求错误');
  2318. }
  2319. $param = Request::instance()->post();
  2320. if (empty($param['workorder']) || empty($param['productCode']) || empty($param['option'])){
  2321. $this->error('参数错误');
  2322. }
  2323. if (isset($param['sort'])){
  2324. $priWhere['yj_yjno'] = ['>=',10];
  2325. $proWhere['a.Gy0_yjno'] = ['>=',10];
  2326. }else{
  2327. $priWhere['yj_yjno'] = ['<',10];
  2328. $proWhere['a.Gy0_yjno'] = ['<',10];
  2329. }
  2330. $list = \db('工单_基本资料')
  2331. ->where('Gd_gdbh',$param['workorder'])
  2332. // ->where('成品代号',$param['productCode'])
  2333. ->field('行号,Gd_cpdh')
  2334. ->select();
  2335. $cpdhList = \db('工单_基本资料')
  2336. ->where('Gd_gdbh',$param['workorder'])
  2337. ->column('Gd_cpdh');
  2338. //引用印件资料
  2339. $PrintList = \db('产品_印件资料')
  2340. ->where('yj_cpdh',$param['productCode'])
  2341. ->whereIn('yj_yjdh',$cpdhList)
  2342. ->where($priWhere)
  2343. ->select();
  2344. if (empty($PrintList)){
  2345. $this->error('未找到印件资料');
  2346. }
  2347. $number = \db('工单_基本资料')
  2348. ->where('Gd_gdbh',$param['workorder'])
  2349. ->where('成品代号',$param['productCode'])
  2350. ->value('rtrim(计划投料)');
  2351. $arr = [];
  2352. foreach ($PrintList as $key => $value){
  2353. foreach ($list as $k=>$v){
  2354. if (rtrim($value['yj_yjdh']) === $v['Gd_cpdh']){
  2355. $PrintList[$key]['Yj_Gdbh'] = $param['workorder'];
  2356. $PrintList[$key]['yj_Yjno'] = $value['yj_yjno'];
  2357. $PrintList[$key]['yj_Yjdh'] = $value['yj_yjdh'];
  2358. $PrintList[$key]['yj_ks'] = $value['yj_ks'];
  2359. $PrintList[$key]['yj_ls'] = $value['yj_ls'];
  2360. $PrintList[$key]['yj_平张投料'] = $number;
  2361. $PrintList[$key]['Sys_id'] = $value['sys_id'];
  2362. $PrintList[$key]['Sys_rq'] = date('Y-m-d H:i:s',time());
  2363. $PrintList[$key]['Mod_rq'] = date('Y-m-d H:i:s',time());
  2364. array_push($arr,[$value['yj_yjno'],$v['行号']]);
  2365. }
  2366. $sql = \db('工单_基本资料')
  2367. ->where('Gd_gdbh',$param['workorder'])
  2368. ->where('Gd_cpdh',$value['yj_yjdh'])
  2369. ->fetchSql(true)
  2370. ->update(['行号'=>$value['yj_yjno']]);
  2371. \db()->query($sql);
  2372. $bomSql = \db('工单_bom资料')
  2373. ->where('BOM_工单编号',$param['workorder'])
  2374. ->where('BOM_产品编号',$value['yj_yjdh'])
  2375. ->fetchSql(true)
  2376. ->update(['BOM_工单行号'=>$value['yj_yjno']]);
  2377. \db()->query($bomSql);
  2378. }
  2379. 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']);
  2380. }
  2381. //引用工艺资料
  2382. $processWhere = [
  2383. 'a.Gy0_方案' => $param['option'],
  2384. 'a.Gy0_cpdh' => $param['productCode']
  2385. ];
  2386. $yjnolist = \db('工单_基本资料')
  2387. ->where('Gd_gdbh',$param['workorder'])
  2388. ->column('Gd_cpdh');
  2389. $ProcessList = \db('产品_工艺资料')
  2390. ->alias('a')
  2391. ->field('a.*')
  2392. ->join('产品_印件资料 b','a.Gy0_cpdh = b.yj_cpdh AND a.Gy0_yjno = yj_yjno')
  2393. ->where($processWhere)
  2394. ->where($proWhere)
  2395. ->whereIn('b.yj_yjdh',$yjnolist)
  2396. ->order('Gy0_yjno,Gy0_gxh')
  2397. ->select();
  2398. if (empty($ProcessList)){
  2399. $this->error('未找到产品工艺资料');
  2400. }
  2401. $ProcesslastId = \db('工单_工艺资料')->order('UniqId desc')->value('rtrim(UniqId)');
  2402. foreach ($ProcessList as $key=>$value){
  2403. if (empty(trim($value['Gy0_shdh']))){
  2404. $rate['rate0'] = 0;
  2405. $rate['rate1'] = 0;
  2406. }else{
  2407. $rate = \db('dic_lzsh')->where('sys_bh',$value['Gy0_shdh'])->field('rtrim(sys_rate0) as rate0,rtrim(sys_rate1) as rate1')->find();
  2408. if (empty($rate)){
  2409. $this->error('该产品印件号'.$value['Gy0_yjno'].'工序号为'.$value['gy0_gxmc'].'的损耗代号错误,请调整之后再引用');
  2410. }
  2411. }
  2412. //设置默认机台 + 对应排单小时定额
  2413. $productMachine = isset($value['Gy0_sbbh']) ? trim((string)$value['Gy0_sbbh']) : '';
  2414. $ProcessList[$key]['Gy0_sbbh'] = '';
  2415. $ProcessList[$key]['Gy0_小时产能'] = 0;
  2416. $sbbhForCapacity = '';
  2417. if ($productMachine !== '') {
  2418. $ProcessList[$key]['Gy0_sbbh'] = $productMachine;
  2419. $sbbhForCapacity = $productMachine;
  2420. } else {
  2421. $machineRow = \db('设备_基本资料')
  2422. ->where('生产工序', 'like', '%' . $value['gy0_gxmc'] . '%')
  2423. ->where('使用部门', '<>', '研发中心')
  2424. ->where('设备编组', '<>', '')
  2425. ->where('sys_sbID', '<>', '')
  2426. ->order('设备编号')
  2427. ->field('设备编号, 排单小时定额')
  2428. ->find();
  2429. if (!empty($machineRow)) {
  2430. $ProcessList[$key]['Gy0_sbbh'] = trim($machineRow['设备编号']);
  2431. $sbbhForCapacity = trim($machineRow['设备编号']);
  2432. if (!empty($machineRow['排单小时定额'])) {
  2433. $ProcessList[$key]['Gy0_小时产能'] = intval($machineRow['排单小时定额']);
  2434. }
  2435. }
  2436. }
  2437. // 产品自带机台:也去设备_基本资料取排单小时定额
  2438. if ($sbbhForCapacity !== '' && $productMachine !== '') {
  2439. $capacity = \db('设备_基本资料')->where('设备编号', $sbbhForCapacity)->value('排单小时定额');
  2440. if (!empty($capacity)) {
  2441. $ProcessList[$key]['Gy0_小时产能'] = intval($capacity);
  2442. }
  2443. }
  2444. $ProcessList[$key]['Gy0_yjno'] = $value['Gy0_yjno'];
  2445. $ProcessList[$key]['Gy0_gdbh'] = $param['workorder'];
  2446. $ProcessList[$key]['Gy0_gxmc'] = $value['gy0_gxmc'];
  2447. $ProcessList[$key]['Gy0_SITE'] = $value['Gy0_site'];
  2448. $ProcessList[$key]['Gy0_shdh'] = $value['Gy0_shdh'];
  2449. $ProcessList[$key]['Gy0_Rate0'] = $rate['rate0'];
  2450. $ProcessList[$key]['Gy0_Rate1'] = $rate['rate1'];
  2451. $ProcessList[$key]['Gy0_ks'] = $value['Gy0_Ks'];
  2452. $ProcessList[$key]['Gy0_ms'] = $value['Gy0_Ms'];
  2453. $ProcessList[$key]['Gy0_oil'] = $value['Gy0_Oil'];
  2454. $ProcessList[$key]['Gy0_计划接货数'] = 0;
  2455. $ProcessList[$key]['Gy0_计划损耗'] = 0;
  2456. $ProcessList[$key]['无形损承担比例'] = 0;
  2457. $ProcessList[$key]['超节损承担比例'] = 1;
  2458. $ProcessList[$key]['超节损核算单价'] = 0;
  2459. $ProcessList[$key]['Gy0_sj1'] = '1900-01-01 00:00:00';
  2460. $ProcessList[$key]['Gy0_sj2'] = '1900-01-01 00:00:00';
  2461. $ProcessList[$key]['PD_WG'] = '1900-01-01 00:00:00';
  2462. $ProcessList[$key]['Sys_rq'] = date('Y-m-d H:i:s', time());
  2463. 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']);
  2464. }
  2465. $oldPrintCount = \db('工单_印件资料')->where('Yj_Gdbh',$param['workorder'])->count();
  2466. $oldProcessCount = \db('工单_工艺资料')->where('Gy0_gdbh',$param['workorder'])->count();
  2467. $copyRemark = '产品代号'.$param['productCode'].',方案'.$param['option'].',原印件'.$oldPrintCount.'条、原工艺'.$oldProcessCount.'条已替换';
  2468. $printLogs = [];
  2469. foreach ($PrintList as $item) {
  2470. $printLogs[] = [
  2471. 'Yj_Gdbh' => $param['workorder'],
  2472. 'yj_Yjno' => isset($item['yj_Yjno']) ? $item['yj_Yjno'] : '',
  2473. 'yj_Yjdh' => isset($item['yj_Yjdh']) ? $item['yj_Yjdh'] : '',
  2474. 'yj_yjmc' => isset($item['yj_yjmc']) ? $item['yj_yjmc'] : '',
  2475. '操作类型' => '引用',
  2476. 'sys_id' => isset($param['sys_id']) ? $param['sys_id'] : '',
  2477. '备注' => $copyRemark,
  2478. ];
  2479. }
  2480. //插入数据
  2481. \db()->startTrans();
  2482. $copied = false;
  2483. try {
  2484. \db('工单_工艺资料')->where('Gy0_gdbh',$param['workorder'])->delete();
  2485. $processSql = \db('工单_工艺资料')->fetchSql(true)->insertAll($ProcessList);
  2486. \db()->query($processSql);
  2487. \db('工单_印件资料')->where('Yj_Gdbh',$param['workorder'])->delete();
  2488. $printSql = \db('工单_印件资料')->fetchSql(true)->insertAll($PrintList);
  2489. \db()->query($printSql);
  2490. \db()->commit();
  2491. $copied = true;
  2492. }catch (\Exception $e){
  2493. \db()->rollback();
  2494. }
  2495. if ($copied) {
  2496. $this->savePrintOperateLog($printLogs);
  2497. }
  2498. //计算工序计划生产数量
  2499. if ($number !== null){
  2500. $list = \db('工单_印件资料')->where('Yj_Gdbh',$param['workorder'])->field('yj_Yjno,yj_平张投料')->select();
  2501. foreach ($list as $value){
  2502. if ((int)$value['yj_平张投料'] >0){
  2503. $this->PlannedProcessYield($param['workorder'],$value['yj_Yjno'],0,$value['yj_平张投料']);
  2504. }
  2505. }
  2506. }
  2507. $this->success('成功');
  2508. }
  2509. /**
  2510. * 引用产品印件、工艺资料->信息获取
  2511. * @return void
  2512. * @throws \think\db\exception\DataNotFoundException
  2513. * @throws \think\db\exception\ModelNotFoundException
  2514. * @throws \think\exception\DbException
  2515. */
  2516. public function ProductCopeDetail()
  2517. {
  2518. if ($this->request->isGet() === false){
  2519. $this->error('请求错误');
  2520. }
  2521. $param = $this->request->param();
  2522. if (empty($param['workorder'])){
  2523. $this->error('参数错误');
  2524. }
  2525. $list = \db('工单_基本资料')
  2526. ->alias('a')
  2527. ->field([
  2528. 'rtrim(a.Gd_gdbh)' => '工单编号',
  2529. 'rtrim(b.客户编号)' => '客户编号',
  2530. 'rtrim(b.客户名称)' => '客户名称',
  2531. 'rtrim(b.产品编号)' => '产品编号',
  2532. 'rtrim(b.产品名称)' => '产品名称'
  2533. ])
  2534. ->join('产品_基本资料 b','a.成品代号 = b.产品编号')
  2535. ->where('a.Gd_gdbh',$param['workorder'])
  2536. ->find();
  2537. if (empty($list)){
  2538. $this->error('未找到工单信息');
  2539. }
  2540. $option = \db('产品_工艺资料')
  2541. ->field('rtrim(Gy0_方案) as 方案')
  2542. ->where('Gy0_cpdh',$list['产品编号'])
  2543. ->group('Gy0_方案')
  2544. ->select();
  2545. $list['方案'] = $option;
  2546. $this->success('成功',$list);
  2547. }
  2548. /**
  2549. * 工艺资料计划产量重新分配
  2550. * @param string $workOrder 工单
  2551. * @param int $yjno 印件号
  2552. * @param int $gxh 工序号
  2553. * @param int $feed 平张投料
  2554. * @return bool
  2555. * @throws \think\Exception
  2556. * @throws \think\db\exception\BindParamException
  2557. * @throws \think\db\exception\DataNotFoundException
  2558. * @throws \think\db\exception\ModelNotFoundException
  2559. * @throws \think\exception\DbException
  2560. * @throws \think\exception\PDOException
  2561. */
  2562. public function PlannedProcessYield($workOrder,$yjno,$gxh,$feed)
  2563. {
  2564. $where = [
  2565. 'Gy0_gdbh'=>$workOrder,
  2566. 'Gy0_yjno'=>$yjno,
  2567. ];
  2568. //获取工艺资料
  2569. $processList = \db('工单_工艺资料')
  2570. ->where($where)
  2571. ->field('CONCAT(rtrim(Gy0_gxmc),"[",rtrim(Add_gxmc),"]") as 工序名称,Gy0_Rate0 as 损耗定额,Gy0_Rate1 as 损耗率,rtrim(损耗系数) as 损耗系数,Gy0_ms,Gy0_ls,UniqId')
  2572. ->order('Gy0_gxh')
  2573. ->select();
  2574. if (empty($processList)){
  2575. $this->success('未找到该工单工序资料');
  2576. }
  2577. $processDetail = \db('工单_工艺资料')
  2578. ->where($where)
  2579. ->field('CONCAT(rtrim(Gy0_gxmc),"[",rtrim(Add_gxmc),"]") as 工序名称,Gy0_ls as 联数')
  2580. ->select();
  2581. $number1 = $number2 = 1;
  2582. foreach ($processDetail as $k => $v) {
  2583. if (strpos($v['工序名称'],'分切') !== false) {
  2584. $number1 = $v['联数'];
  2585. }
  2586. if (strpos($v['工序名称'],'分切') === false && $v['联数'] > 1) {
  2587. $number2 = $v['联数'];
  2588. }
  2589. }
  2590. $Multiple = $number1/$number2;
  2591. if (is_int($Multiple) === false) {
  2592. $Multiple = 1;
  2593. }
  2594. //计算损耗定额和计划产量
  2595. $production = [];
  2596. $i = $m = 0;
  2597. foreach ($processList as $key=>$value){
  2598. if (strpos($v['工序名称'],'分切') !== false) {
  2599. $m = $key;
  2600. }
  2601. if ($key === 0){
  2602. array_push($production,$feed);
  2603. }else{
  2604. if ($key === $m+1){
  2605. $production[$key] = $production[$key]/$value['Gy0_ls']/$Multiple;
  2606. }else{
  2607. $production[$key] = $production[$key]/$value['Gy0_ls'];
  2608. }
  2609. }
  2610. $wastage = 0;
  2611. if ($value['损耗率'] !== 0 && !empty($value['损耗率'])){
  2612. $wastage = ($production[$key]*$value['损耗率']+$value['损耗定额'])*$value['损耗系数'];
  2613. }
  2614. if ($value['Gy0_ms'] >0 && !empty($value['Gy0_ms'])){
  2615. $wastage = round($wastage * $value['Gy0_ms']);
  2616. }
  2617. array_push($production,ceil($production[$key]-$wastage)*$value['Gy0_ls']);
  2618. $processSql = \db('工单_工艺资料')
  2619. ->where('UniqId',$value['UniqId'])
  2620. ->fetchSql(true)
  2621. ->update(['Gy0_计划接货数'=>$production[$key],'Gy0_计划损耗'=>$wastage]);
  2622. $res = \db()->query($processSql);
  2623. if ($res === false){
  2624. $i++;
  2625. }
  2626. }
  2627. if ($i===0){
  2628. return true;
  2629. }else{
  2630. return false;
  2631. }
  2632. }
  2633. /**
  2634. * 工单资料删除
  2635. * @return void
  2636. * @throws \think\exception\PDOException
  2637. */
  2638. public function WorkOrderDel()
  2639. {
  2640. if ($this->request->isGet() === false){
  2641. $this->error('请求错误');
  2642. }
  2643. $param = $this->request->param();
  2644. if (isset($param['UniqId']) === false){
  2645. $this->error('参数错误');
  2646. }
  2647. $WorkOrderId = explode(',',$param['UniqId']);
  2648. $i = 0;
  2649. foreach ($WorkOrderId as $key=>$value){
  2650. //获取工单编号
  2651. $workorder = \db('工单_基本资料')
  2652. ->where('Uniqid',$value)
  2653. ->value('rtrim(Gd_gdbh)');
  2654. $status = \db('设备_产量计酬')
  2655. ->where('sczl_gdbh',$workorder)
  2656. ->count();
  2657. if ($status === 0){
  2658. //删除工单资料、工艺资料、印件资料
  2659. \db()->startTrans();
  2660. try {
  2661. \db('工单_基本资料')->where('Gd_gdbh',$workorder)->delete();
  2662. if (!empty(\db('工单_工艺资料')->where('Gy0_gdbh',$workorder)->find())){
  2663. \db('工单_工艺资料')->where('Gy0_gdbh',$workorder)->delete();
  2664. }
  2665. if (!empty(\db('工单_印件资料')->where('Yj_Gdbh',$workorder)->find())){
  2666. \db('工单_印件资料')->where('Yj_Gdbh',$workorder)->delete();
  2667. }
  2668. if (!empty(\db('工单_bom资料')->where('BOM_工单编号',$workorder)->find())){
  2669. \db('工单_bom资料')->where('BOM_工单编号',$workorder)->delete();
  2670. }
  2671. \db()->commit();
  2672. // 成功删除后记录日志
  2673. $this->saveWorkOrderOperateLog([[
  2674. 'Gd_gdbh' => $workorder,
  2675. 'ModifyUser' => $param['sys_id'],
  2676. 'ModifyTime' => date('Y-m-d H:i:s'),
  2677. 'FieldName' => '工单删除',
  2678. 'NewValue' => '已删除',
  2679. 'ModifySource' => '工单删除'
  2680. ]]);
  2681. }catch (\Exception $e){
  2682. \db()->rollback();
  2683. $i++;
  2684. }
  2685. }else{
  2686. $this->error('工单'.$workorder.'已上报数据无法删除,请重新确认');
  2687. }
  2688. }
  2689. if ($i === 0){
  2690. $this->success('删除成功');
  2691. }else{
  2692. $this->error('删除失败');
  2693. }
  2694. }
  2695. /**
  2696. * 修正核算参数-》工单产品信息获取
  2697. * @return void
  2698. * @throws \think\db\exception\DataNotFoundException
  2699. * @throws \think\db\exception\ModelNotFoundException
  2700. * @throws \think\exception\DbException
  2701. */
  2702. public function ProductNameData()
  2703. {
  2704. if ($this->request->isGet() === false){
  2705. $this->error('请求错误');
  2706. }
  2707. $workOrder = input('workOrder');
  2708. if (empty($workOrder)){
  2709. $this->error('参数错误');
  2710. }
  2711. $orderList = \db('工单_基本资料')
  2712. ->field('rtrim(Gd_cpdh) as 印件代号,rtrim(Gd_cpmc) as 印件名称,rtrim(成品代号) as 产品代号,rtrim(成品名称) as 产品名称')
  2713. ->where('Gd_gdbh',$workOrder)
  2714. ->find();
  2715. $this->success('成功',$orderList);
  2716. }
  2717. /**
  2718. * 获取联数
  2719. * @return void
  2720. * @throws \think\db\exception\DataNotFoundException
  2721. * @throws \think\db\exception\ModelNotFoundException
  2722. * @throws \think\exception\DbException
  2723. */
  2724. public function getCouplet()
  2725. {
  2726. if ($this->request->isGet() === false){
  2727. $this->error('请求错误');
  2728. }
  2729. $param = $this->request->param();
  2730. if (empty($param['gdbh']) || empty($param['yjno'])){
  2731. $this->error('参数错误');
  2732. }
  2733. $list = \db('工单_印件资料')
  2734. ->where('Yj_gdbh',$param['gdbh'])
  2735. ->where('yj_Yjno',$param['yjno'])
  2736. ->field('rtrim(yj_ks) as 开数,rtrim(yj_ls) as 联数')
  2737. ->find();
  2738. $process = \db('工单_工艺资料')
  2739. ->where('Gy0_gdbh',$param['gdbh'])
  2740. ->where('Gy0_yjno',$param['yjno'])
  2741. ->order('Gy0_gxh desc')
  2742. ->value('Gy0_gxh');
  2743. $list['工序号'] = $process + 1;
  2744. $this->success('成功',$list);
  2745. }
  2746. /**
  2747. * 糊盒工单配件完成度查询
  2748. * @return void
  2749. * @throws \think\db\exception\DataNotFoundException
  2750. * @throws \think\db\exception\ModelNotFoundException
  2751. * @throws \think\exception\DbException
  2752. */
  2753. public function Accessories()
  2754. {
  2755. if ($this->request->isGet() === false){
  2756. $this->error('请求错误');
  2757. }
  2758. $param = $this->request->param();
  2759. if (empty($param['batch'])){
  2760. $this->error('参数错误');
  2761. }
  2762. $list = \db('工单_基本资料')
  2763. ->alias('a')
  2764. ->join('工单_印件资料 b','a.Gd_gdbh=b.Yj_Gdbh')
  2765. ->join('成品入仓 c','a.Gd_gdbh=c.jjcp_gdbh and a.销售订单号 = c.订单编号 and b.yj_Yjno = c.jjcp_yjno','left')
  2766. ->where('a.销售订单号',$param['batch'])
  2767. ->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 入仓数量')
  2768. ->group('a.Gd_gdbh,b.yj_Yjno')
  2769. ->order('a.Gd_gdbh,b.yj_Yjno')
  2770. ->select();
  2771. if (empty($list)){
  2772. $this->error('未找到数据');
  2773. }else{
  2774. $this->success('成功',$list);
  2775. }
  2776. }
  2777. /**
  2778. *查询糊盒标准工单
  2779. * @return void
  2780. * @throws \think\db\exception\DataNotFoundException
  2781. * @throws \think\db\exception\ModelNotFoundException
  2782. * @throws \think\exception\DbException
  2783. *
  2784. */
  2785. public function HuheWorkOrderSearch()
  2786. {
  2787. if ($this->request->isGet() === false){
  2788. $this->error('请求错误');
  2789. }
  2790. $param = $this->request->param();
  2791. if (empty($param['search'])){
  2792. $this->error('参数错误');
  2793. }
  2794. $where['Gd_gdbh|Gd_cpmc|成品名称'] = ['like','%'.$param['search'].'%'];
  2795. $where['Gd_lx'] = '糊盒标准工单';
  2796. $list = \db('工单_基本资料')
  2797. ->field('Gd_gdbh as 工单编号,Gd_cpdh as 产品编号,Gd_cpmc as 产品名称,成品代号,成品名称,Uniqid')
  2798. ->where($where)
  2799. ->group('Gd_gdbh,Gd_cpdh,Gd_cpmc')
  2800. ->order('Gd_gdbh,Gd_cpdh')
  2801. ->select();
  2802. if (empty($list)){
  2803. $this->error('未找到数据');
  2804. }else{
  2805. $this->success('成功',$list);
  2806. }
  2807. }
  2808. /**
  2809. * 复制糊盒工单
  2810. * @return void
  2811. */
  2812. public function HuheWorkOrderCopy()
  2813. {
  2814. try {
  2815. // 1. 请求验证
  2816. if (!$this->request->isGet()) {
  2817. throw new \Exception('请求方式错误,请使用GET请求');
  2818. }
  2819. $param = $this->request->param();
  2820. $this->validateCopyParams($param);
  2821. // 2. 查询工单基本信息
  2822. $sourceWorkOrder = $this->getWorkOrderInfo($param['form_Uniqid'], ['Gd_gdbh', '行号']);
  2823. $targetWorkOrder = $this->getWorkOrderInfo($param['to_Uniqid'], ['Gd_gdbh', '行号', '订单数量', '实际投料']);
  2824. // 3. 复制印件资料和工艺资料
  2825. $this->copyPrintingData($sourceWorkOrder, $targetWorkOrder, $param['sys_id']);
  2826. $this->copyProcessData($sourceWorkOrder, $targetWorkOrder, $param['sys_id']);
  2827. $this->success('复制成功');
  2828. } catch (\Exception $e) {
  2829. $this->error($e->getMessage());
  2830. }
  2831. }
  2832. /**
  2833. * 验证复制所需的参数
  2834. * @param array $params
  2835. * @throws \Exception
  2836. */
  2837. private function validateCopyParams(array $params): void
  2838. {
  2839. if (empty($params['to_Uniqid'])) {
  2840. throw new \Exception('目标工单ID(to_Uniqid)不能为空');
  2841. }
  2842. if (empty($params['form_Uniqid'])) {
  2843. throw new \Exception('源工单ID(form_Uniqid)不能为空');
  2844. }
  2845. if (empty($params['sys_id'])) {
  2846. throw new \Exception('系统ID(sys_id)不能为空');
  2847. }
  2848. }
  2849. /**
  2850. * 获取工单信息
  2851. * @param string $uniqid 工单唯一标识
  2852. * @param array $fields 需要查询的字段
  2853. * @return array 工单信息
  2854. * @throws \Exception
  2855. */
  2856. private function getWorkOrderInfo(string $uniqid, array $fields = []): array
  2857. {
  2858. $info = \db('工单_基本资料')
  2859. ->field($fields)
  2860. ->where('Uniqid', $uniqid)
  2861. ->find();
  2862. if (empty($info)) {
  2863. throw new \Exception("工单不存在,ID: {$uniqid}");
  2864. }
  2865. return $info;
  2866. }
  2867. /**
  2868. * 复制印件资料
  2869. * @param array $sourceWorkOrder 源工单信息
  2870. * @param array $targetWorkOrder 目标工单信息
  2871. * @param string $sysId 系统ID
  2872. * @throws \Exception
  2873. */
  2874. private function copyPrintingData(array $sourceWorkOrder, array $targetWorkOrder, string $sysId): void
  2875. {
  2876. // 常量定义,便于维护
  2877. $tableName = '工单_印件资料';
  2878. $excludeFields = ['Uniqid', 'Yj_Gdbh', 'yj_Yjno', 'yj_成品数量', 'yj_平张投料', '印件完工日期', 'Sys_id', 'Sys_rq', 'Mod_rq'];
  2879. // 查询源印件资料
  2880. $printingData = \db($tableName)
  2881. ->where('Yj_Gdbh', $sourceWorkOrder['Gd_gdbh'])
  2882. ->where('yj_Yjno', $sourceWorkOrder['行号'])
  2883. ->find();
  2884. if (empty($printingData)) {
  2885. throw new \Exception('未找到源工单印件资料');
  2886. }
  2887. // 构建新的印件资料
  2888. $newPrintingData = $this->buildCopyData(
  2889. $printingData,
  2890. $excludeFields,
  2891. [
  2892. 'Yj_Gdbh' => $targetWorkOrder['Gd_gdbh'],
  2893. 'yj_Yjno' => $targetWorkOrder['行号'],
  2894. 'yj_成品数量' => $targetWorkOrder['订单数量'],
  2895. 'yj_平张投料' => $targetWorkOrder['实际投料'],
  2896. '印件完工日期' => null,
  2897. 'Sys_id' => $sysId,
  2898. 'Sys_rq' => date('Y-m-d H:i:s'),
  2899. 'Mod_rq' => null
  2900. ]
  2901. );
  2902. \db($tableName)
  2903. ->where('Yj_Gdbh',$newPrintingData['Yj_Gdbh'])
  2904. ->where('yj_Yjno',$newPrintingData['yj_Yjno'])
  2905. ->delete();
  2906. // 执行插入操作(保留fetchSql和query兼容中文字段)
  2907. $sql = \db($tableName)->fetchSql(true)->insert($newPrintingData);
  2908. $result = \db()->query($sql);
  2909. if ($result === false) {
  2910. throw new \Exception("印件资料复制失败,SQL: {$sql}");
  2911. }
  2912. }
  2913. /**
  2914. * 复制工艺资料
  2915. * @param array $sourceWorkOrder 源工单信息
  2916. * @param array $targetWorkOrder 目标工单信息
  2917. * @param string $sysId 系统ID
  2918. * @throws \Exception
  2919. */
  2920. private function copyProcessData(array $sourceWorkOrder, array $targetWorkOrder, string $sysId): void
  2921. {
  2922. // 常量定义
  2923. $tableName = '工单_工艺资料';
  2924. $excludeFields = ['UniqId', 'Gy0_gdbh', 'Gy0_yjno', 'Gy0_sbbh', 'Gy0_计划接货数', 'Gy0_最早开工时间', 'Gy0_sj1', 'Gy0_sj2', 'PD_WG', 'Sys_id', 'Sys_rq', 'Mod_rq'];
  2925. $defaultDate = '1900-01-01 00:00:00';
  2926. $currentTime = date('Y-m-d H:i:s');
  2927. // 查询源工艺资料列表
  2928. $processList = \db($tableName)
  2929. ->where('Gy0_gdbh', $sourceWorkOrder['Gd_gdbh'])
  2930. ->where('Gy0_yjno', $sourceWorkOrder['行号'])
  2931. ->select();
  2932. if (empty($processList)) {
  2933. throw new \Exception('未找到源工单工艺资料');
  2934. }
  2935. // 构建新的工艺资料列表
  2936. $newProcessData = [];
  2937. foreach ($processList as $item) {
  2938. $newProcessData[] = $this->buildCopyData(
  2939. $item,
  2940. $excludeFields,
  2941. [
  2942. 'Gy0_gdbh' => $targetWorkOrder['Gd_gdbh'],
  2943. 'Gy0_yjno' => $targetWorkOrder['行号'],
  2944. 'Gy0_sbbh' => '',
  2945. 'Gy0_计划接货数' => $targetWorkOrder['实际投料'],
  2946. 'Gy0_最早开工时间' => $defaultDate,
  2947. 'Gy0_sj1' => $defaultDate,
  2948. 'Gy0_sj2' => $defaultDate,
  2949. 'PD_WG' => $defaultDate,
  2950. 'Sys_id' => $sysId,
  2951. 'Sys_rq' => $currentTime,
  2952. 'Mod_rq' => null
  2953. ]
  2954. );
  2955. }
  2956. \db($tableName)
  2957. ->where('Gy0_gdbh', $targetWorkOrder['Gd_gdbh'])
  2958. ->where('Gy0_yjno', $targetWorkOrder['行号'])
  2959. ->delete();
  2960. // 执行批量插入(保留fetchSql和query兼容中文字段)
  2961. if (!empty($newProcessData)) {
  2962. $sql = \db($tableName)->fetchSql(true)->insertAll($newProcessData);
  2963. $result = \db()->query($sql);
  2964. if ($result === false) {
  2965. throw new \Exception("工艺资料复制失败,SQL: {$sql}");
  2966. }
  2967. }
  2968. }
  2969. /**
  2970. * 构建复制的数据
  2971. * @param array $sourceData 源数据
  2972. * @param array $excludeFields 需要排除的字段
  2973. * @param array $overrideData 需要覆盖的字段和值
  2974. * @return array 构建后的新数据
  2975. */
  2976. private function buildCopyData(array $sourceData, array $excludeFields, array $overrideData): array
  2977. {
  2978. $newData = [];
  2979. // 复制源数据中不需要排除的字段
  2980. foreach ($sourceData as $key => $value) {
  2981. if (!in_array($key, $excludeFields)) {
  2982. $newData[$key] = $value;
  2983. }
  2984. }
  2985. // 覆盖需要替换的字段
  2986. return array_merge($newData, $overrideData);
  2987. }
  2988. /**
  2989. * 糊盒标准工单左侧菜单栏
  2990. * @return void
  2991. * @throws \think\db\exception\DataNotFoundException
  2992. * @throws \think\db\exception\ModelNotFoundException
  2993. * @throws \think\exception\DbException
  2994. */
  2995. public function HuheDataList()
  2996. {
  2997. if ($this->request->isGet() === false){
  2998. $this->error('请求错误');
  2999. }
  3000. $list = \db('产品_基本资料')
  3001. ->where('客户编号','<>','')
  3002. ->field('rtrim(客户编号) as 客户编号,rtrim(客户名称) as 客户名称')
  3003. ->group('客户编号,客户名称')
  3004. ->order('客户编号')
  3005. ->select();
  3006. if (empty($list)){
  3007. $this->success('',[]);
  3008. }
  3009. $data['糊盒工单'] = [];
  3010. foreach ($list as $key=>$value){
  3011. $work_order_count = \db('工单_基本资料')
  3012. ->where('行号','1')
  3013. ->where('Gd_cpdh|成品代号','LIKE',rtrim($value['客户编号']).'%')
  3014. ->where('Gd_lx','糊盒标准工单')
  3015. ->count();
  3016. if (!empty($value['客户名称']) && $work_order_count !== 0){
  3017. $name = $value['客户编号'].rtrim($value['客户名称']);
  3018. if (strpos($value['客户编号'],'J') !== false ){
  3019. array_push($data['糊盒工单'],$name);
  3020. }
  3021. }
  3022. }
  3023. $this->success('成功',$data);
  3024. }
  3025. /**
  3026. * 糊盒标准工单列表
  3027. * @return void
  3028. * @throws \think\Exception
  3029. * @throws \think\db\exception\DataNotFoundException
  3030. * @throws \think\db\exception\ModelNotFoundException
  3031. * @throws \think\exception\DbException
  3032. */
  3033. public function HuheWorkList()
  3034. {
  3035. if ($this->request->isGet() === false){
  3036. $this->error('请求错误');
  3037. }
  3038. $search = input('search');
  3039. $clientNumber = input('Gd_khdh');
  3040. $startTime = input('start');
  3041. $endTime = input('end');
  3042. $page = input('page');
  3043. $limit = input('limit');
  3044. $param = $this->request->param();
  3045. $where = [];
  3046. if (!empty($clientNumber)){
  3047. if (substr($clientNumber,0,1) !== 'Y' && substr($clientNumber,0,1) !== 'J')
  3048. {
  3049. $clientNumber = substr($clientNumber,0,4);
  3050. }
  3051. if ($clientNumber === 'Y1401'){
  3052. $where['Gd_cpdh'] = ['like',$clientNumber.'%'];
  3053. }else{
  3054. $where['成品代号'] = ['like',$clientNumber.'%'];
  3055. }
  3056. }
  3057. if (!empty($workOrder)){
  3058. $where['Gd_gdbh'] = $workOrder;
  3059. }
  3060. if (!empty($productCode)){
  3061. $where['Gd_cpdh'] = $productCode;
  3062. }
  3063. if (!empty($search)){
  3064. $where['Gd_gdbh|Gd_客户代号|Gd_客户名称|Gd_khdh|Gd_khmc|Gd_cpdh|Gd_cpmc|成品代号|成品名称|产品版本号'] = ['like','%'.$search.'%'];
  3065. }
  3066. if (!empty($startTime) && !empty($endTime)){
  3067. $where['接单日期'] = ['between',[$startTime,$endTime]];
  3068. }
  3069. $where['Gd_lx'] = '糊盒标准工单';
  3070. $total = \db('工单_基本资料')->where($where)->distinct(true)->field('Gd_gdbh')->count();
  3071. $list = \db('工单_基本资料')
  3072. ->where($where)
  3073. ->order('Gd_statu desc')
  3074. ->limit(($page-1)*$limit,$limit)
  3075. ->group('Gd_gdbh')
  3076. ->select();
  3077. //工单基本资料数据整理
  3078. $data = [];
  3079. foreach ($list as $key=>$value){
  3080. $data[$key] = [
  3081. '工单编号' => rtrim($value['Gd_gdbh']),
  3082. '生产分类' => rtrim($value['Gd_生产分类']),
  3083. '销售订单号' => rtrim($value['销售订单号']),
  3084. '产品代号' => rtrim($value['成品代号']),
  3085. '产品名称' => rtrim($value['成品名称']),
  3086. '订单数量' => floatval($value['订单数量']),
  3087. '单位' => rtrim($value['计量单位']),
  3088. '折合大箱' => rtrim((int)$value['投料大箱']),
  3089. '投料率' => rtrim($value['投料率']),
  3090. '平均合格率' => '',
  3091. '开单日期' => date('Y-m-d',strtotime(rtrim($value['接单日期']))),
  3092. '交货日期' => date('Y-m-d',strtotime(rtrim($value['交货日期']))),
  3093. '工单类型' => rtrim($value['Gd_lx']),
  3094. '工单状态' => rtrim($value['gd_statu']),
  3095. '当前生产工序' => '',
  3096. '产量提交时间' => '',
  3097. '建档用户' => rtrim($value['Sys_id']),
  3098. '建档时间' => rtrim($value['Sys_rq']),
  3099. '更新时间' => rtrim($value['Mod_rq']),
  3100. 'Uniqid' => rtrim($value['Uniqid'])
  3101. ];
  3102. $number = \db('工单_工艺资料')->where('Gy0_gdbh',$data[$key]['工单编号'])->count();
  3103. if ($number === 0){
  3104. $data[$key]['status'] = '*';
  3105. }else{
  3106. $data[$key]['status'] = '';
  3107. }
  3108. }
  3109. $this->success('成功',['data'=>$data,'total'=>$total]);
  3110. }
  3111. /**
  3112. * 成品入仓之后定时任务,更新工单状态(建议每天凌晨执行)
  3113. * 条件:已录入末板,且最后一条入仓时间超过1个月,工单未完工
  3114. * @return void
  3115. */
  3116. public function updateWorkOrderStatus()
  3117. {
  3118. $deadline = date('Y-m-d H:i:s', strtotime('-1 month'));
  3119. $now = date('Y-m-d H:i:s');
  3120. $endBoardOrders = \db('成品入仓')
  3121. ->where('jjcp_smb', 'like', '末%')
  3122. ->field('jjcp_gdbh,jjcp_yjno')
  3123. ->group('jjcp_gdbh,jjcp_yjno')
  3124. ->select();
  3125. $updated = 0;
  3126. $skipped = 0;
  3127. $logs = [];
  3128. foreach ($endBoardOrders as $order) {
  3129. $gdbh = trim((string)$order['jjcp_gdbh']);
  3130. $yjno = trim((string)$order['jjcp_yjno']);
  3131. if ($gdbh === '' || $yjno === '') {
  3132. $skipped++;
  3133. continue;
  3134. }
  3135. $lastSj = \db('成品入仓')
  3136. ->where('jjcp_gdbh', $gdbh)
  3137. ->where('jjcp_yjno', $yjno)
  3138. ->max('jjcp_sj');
  3139. if (empty($lastSj) || $lastSj > $deadline) {
  3140. $skipped++;
  3141. continue;
  3142. }
  3143. $workOrder = \db('工单_基本资料')
  3144. ->where('Gd_gdbh', $gdbh)
  3145. ->where('行号', $yjno)
  3146. ->field('Gd_gdbh,行号,rtrim(gd_statu) as gd_statu,工单完工日期')
  3147. ->find();
  3148. if (empty($workOrder)) {
  3149. $skipped++;
  3150. continue;
  3151. }
  3152. if (!$this->isEmptyWorkOrderFinishDate($workOrder['工单完工日期'])) {
  3153. $skipped++;
  3154. continue;
  3155. }
  3156. if (trim((string)$workOrder['gd_statu']) === '1-已完工') {
  3157. $skipped++;
  3158. continue;
  3159. }
  3160. $updateResult = \db('工单_基本资料')
  3161. ->where('Gd_gdbh', $gdbh)
  3162. ->where('行号', $yjno)
  3163. ->update([
  3164. '工单完工日期' => $now,
  3165. 'gd_statu' => '1-已完工',
  3166. 'Mod_rq' => $now,
  3167. ]);
  3168. if ($updateResult === false) {
  3169. $skipped++;
  3170. continue;
  3171. }
  3172. $updated++;
  3173. $logs[] = [
  3174. 'Gd_gdbh' => $gdbh,
  3175. 'yjno' => $yjno,
  3176. 'ModifyUser' => 'system',
  3177. 'ModifyTime' => $now,
  3178. 'FieldName' => 'gd_statu',
  3179. 'OldValue' => trim((string)$workOrder['gd_statu']),
  3180. 'NewValue' => '1-已完工',
  3181. 'ModifySource' => '成品入仓定时任务',
  3182. ];
  3183. $logs[] = [
  3184. 'Gd_gdbh' => $gdbh,
  3185. 'yjno' => $yjno,
  3186. 'ModifyUser' => 'system',
  3187. 'ModifyTime' => $now,
  3188. 'FieldName' => '工单完工日期',
  3189. 'OldValue' => trim((string)$workOrder['工单完工日期']),
  3190. 'NewValue' => $now,
  3191. 'ModifySource' => '成品入仓定时任务',
  3192. ];
  3193. }
  3194. if (!empty($logs)) {
  3195. $this->saveWorkOrderOperateLog($logs);
  3196. }
  3197. $this->success('定时任务执行完成', [
  3198. 'updated' => $updated,
  3199. 'skipped' => $skipped,
  3200. 'total_checked' => count($endBoardOrders),
  3201. 'deadline' => $deadline,
  3202. ]);
  3203. }
  3204. /**
  3205. * 印件操作日志写入工单_操作日志
  3206. * @param array $rows
  3207. * @return void
  3208. */
  3209. protected function savePrintOperateLog(array $rows)
  3210. {
  3211. $this->saveWorkOrderOperateLog($rows);
  3212. }
  3213. /**
  3214. * 写入工单_操作日志,兼容系统操作日志和印件操作日志两种字段
  3215. * @param array $rows
  3216. * @return void
  3217. */
  3218. protected function saveWorkOrderOperateLog(array $rows)
  3219. {
  3220. if (empty($rows)) {
  3221. return;
  3222. }
  3223. $now = date('Y-m-d H:i:s');
  3224. $logs = [];
  3225. foreach ($rows as $row) {
  3226. $sysRq = $now;
  3227. if (isset($row['sys_rq']) && $row['sys_rq'] !== '') {
  3228. $sysRq = $row['sys_rq'];
  3229. } elseif (isset($row['ModifyTime']) && $row['ModifyTime'] !== '') {
  3230. $sysRq = $row['ModifyTime'];
  3231. }
  3232. $logs[] = [
  3233. 'Yj_Gdbh' => $this->workOrderLogValue($row, ['Yj_Gdbh', 'Gd_gdbh']),
  3234. 'yj_Yjno' => $this->workOrderLogValue($row, ['yj_Yjno', 'yjno']),
  3235. '工序号' => $this->workOrderLogValue($row, ['工序号', 'gxh']),
  3236. '工序名称' => $this->workOrderLogValue($row, ['工序名称', 'gxmc']),
  3237. 'yj_Yjdh' => $this->workOrderLogValue($row, ['yj_Yjdh']),
  3238. 'yj_yjmc' => $this->workOrderLogValue($row, ['yj_yjmc']),
  3239. 'PrintUniqid' => $this->workOrderLogValue($row, ['PrintUniqid']),
  3240. '操作类型' => $this->workOrderLogValue($row, ['操作类型', 'ModifySource']),
  3241. '字段名' => $this->workOrderLogValue($row, ['字段名', 'FieldName']),
  3242. '修改前' => $this->workOrderLogValue($row, ['修改前', 'OldValue']),
  3243. '修改后' => $this->workOrderLogValue($row, ['修改后', 'NewValue']),
  3244. 'sys_id' => $this->workOrderLogValue($row, ['sys_id', 'ModifyUser']),
  3245. 'sys_rq' => $sysRq,
  3246. '备注' => $this->workOrderLogValue($row, ['备注']),
  3247. ];
  3248. }
  3249. \db('工单_操作日志')->insertAll($logs);
  3250. }
  3251. /**
  3252. * @param array $row
  3253. * @param array $keys
  3254. * @return string
  3255. */
  3256. protected function workOrderLogValue(array $row, array $keys)
  3257. {
  3258. foreach ($keys as $key) {
  3259. if (array_key_exists($key, $row) && $row[$key] !== null && $row[$key] !== '') {
  3260. return is_scalar($row[$key]) ? (string)$row[$key] : '';
  3261. }
  3262. }
  3263. return '';
  3264. }
  3265. /**
  3266. * 判断工单完工日期是否为空
  3267. * @param mixed $date
  3268. * @return bool
  3269. */
  3270. protected function isEmptyWorkOrderFinishDate($date)
  3271. {
  3272. if ($date === null) {
  3273. return true;
  3274. }
  3275. $date = trim((string)$date);
  3276. if ($date === '') {
  3277. return true;
  3278. }
  3279. $timestamp = strtotime($date);
  3280. return $timestamp === false || $timestamp <= strtotime('1900-01-02');
  3281. }
  3282. }