WorkOrderSpotCheck.php 181 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use \think\Request;
  5. use \think\Db;
  6. use const http\Client\Curl\IPRESOLVE_V4;
  7. class WorkOrderSpotCheck extends Api{
  8. protected $noNeedLogin = ['*'];
  9. protected $noNeedRight = ['*'];
  10. /**
  11. * index测试接口是否
  12. */
  13. public function index() {$this->success('请求成功');}
  14. /**
  15. * 报工查询接口
  16. *
  17. */
  18. public function getList() {
  19. if (!$this->request->isGet()) {
  20. $this->error('请求方式错误');
  21. }
  22. $params = $this->request->param();
  23. //参数是否存在且不为空
  24. $requiredParams = ['order','code'];
  25. foreach ($requiredParams as $param) {
  26. if (!isset($params[$param]) || empty($params[$param])) {
  27. $this->error("$param 参数错误");
  28. }
  29. }
  30. //如果订单编号没有 DC 前缀,自动添加 DC
  31. if (strpos($params['order'], 'DC') !== 0) {
  32. $params['order'] = 'DC' . $params['order'];
  33. }
  34. $order = $params['order'];
  35. switch ($params['code']) {
  36. case '裁剪':
  37. //获取订单编号
  38. $ddbh = explode('-', $order)[0];
  39. //查询未出库的记录,若存在则进行批量出库和日志记录
  40. $data = \db('工单_印件资料')
  41. ->where(['子订单编号' => $order, 'ck_rq' => null])
  42. ->find();
  43. if ($data) {
  44. // 检查并更新工单_基本资料表的出库日期
  45. $sql = \db('工单_基本资料')
  46. ->where('订单编号', $ddbh)
  47. ->whereNull('出库日期')
  48. ->fetchSql(true)
  49. ->update(['出库日期' => date("Y-m-d H:i:s")]);
  50. \db()->query($sql);
  51. // 更新工单_印件资料表的出库日期
  52. $sql = \db('工单_印件资料')
  53. ->where('子订单编号', $order)
  54. ->whereNull('Mod_rq')
  55. ->whereNull('ck_rq')
  56. ->fetchSql(true)
  57. ->update(['ck_rq' => date("Y-m-d H:i:s")]);
  58. \db()->query($sql);
  59. }
  60. //查询子订单编号对应数据
  61. if (strpos($order, '-') !== false) {
  62. //判断 $order -后面是否是两位
  63. if (preg_match('/-(\d{2})$/', $order, $matches)) {
  64. $number = (int)$matches[1];
  65. //船样单子为:订单编号-90~99
  66. if ($number > 70) {
  67. $data = \db('工单_基本资料')->alias('j')
  68. ->distinct('y.子订单编号')
  69. ->field('
  70. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  71. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  72. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  73. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,y.cm11,y.cm12,y.cm13,y.cm14,
  74. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,y.cmsl11,y.cmsl12,y.cmsl13,y.cmsl14,
  75. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10, y.scsl11, y.scsl12, y.scsl13, y.scsl14'
  76. )
  77. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  78. ->where('y.船样',1)
  79. ->where('y.子订单编号', $order)
  80. ->select();
  81. } else {
  82. //获取原始子订单编号【-01】后期不需要else中代码可以删除掉
  83. //【查询尺码、制单数、填写报工新增实裁数】
  84. $data = \db('工单_基本资料')->alias('j')
  85. ->distinct('y.子订单编号')
  86. ->field('
  87. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  88. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  89. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  90. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,y.cm11,y.cm12,y.cm13,y.cm14,
  91. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,y.cmsl11,y.cmsl12,y.cmsl13,y.cmsl14,
  92. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10, y.scsl11, y.scsl12, y.scsl13, y.scsl14'
  93. )
  94. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  95. ->where('y.船样',0)
  96. ->whereNull('y.Mod_rq')
  97. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  98. ->select();
  99. }
  100. }else{
  101. // $ddbh = explode('-', $order)[0];//获取订单编号
  102. //【查询尺码、制单数、填写报工新增实裁数】
  103. $data = \db('工单_基本资料')->alias('j')
  104. ->distinct('y.子订单编号')
  105. ->field('
  106. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  107. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  108. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  109. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,y.cm11,y.cm12,y.cm13,y.cm14,
  110. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,y.cmsl11,y.cmsl12,y.cmsl13,y.cmsl14,
  111. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10, y.scsl11, y.scsl12, y.scsl13, y.scsl14'
  112. )
  113. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  114. ->where('y.船样',0)
  115. ->whereNull('y.Mod_rq')
  116. ->where('y.订单编号', 'like', '%' . $ddbh . '%')
  117. ->select();
  118. }
  119. $dataEntry = [];
  120. $list = [];
  121. $headers = [];//存放尺码表头
  122. foreach ($data as $item) {
  123. // 初始化尺码与数量映射数组
  124. $cmScslMapping = [];
  125. for ($i = 1; $i <= 14; $i++) {
  126. $cmValue = $item['cm' . $i];
  127. $scslValue = $item['scsl' . $i];
  128. if (!is_null($cmValue) && $cmValue !== 0) {
  129. $cmScslMapping[$cmValue] = ($scslValue === 0 || is_null($scslValue)) ? '' : $scslValue;
  130. if (!in_array($cmValue, $headers)) {
  131. $headers[] = $cmValue;
  132. }
  133. }
  134. }
  135. // 将尺码数据平铺到主要数据中(表格数据)
  136. $orderData = [
  137. '订单编号' => $item['订单编号'],
  138. '子订单编号' => $item['子订单编号'],
  139. '款号' => $item['款号'],
  140. '款式' => $item['款式'],
  141. '颜色' => $item['颜色备注'],
  142. 'zdtotal' => $item['zdtotal'],
  143. 'sctotal' => $item['sctotal'],
  144. '单位' => $item['单位'],
  145. 'Sys_id' => $item['Sys_id'],
  146. 'Sys_rq' => $item['Sys_rq'],
  147. ];
  148. // 平铺尺码映射数据
  149. $orderData = array_merge($orderData, $cmScslMapping);
  150. $dataEntry[] = $orderData;
  151. // list 列表部分
  152. $entry = [];
  153. for ($i = 1; $i <= 14; $i++) {
  154. $entry['cm' . $i] = ($item['cm' . $i] === 0 || is_null($item['cm' . $i])) ? '' : $item['cm' . $i];
  155. $entry['cmsl' . $i] = ($item['cmsl' . $i] === 0 || is_null($item['cmsl' . $i])) ? '' : $item['cmsl' . $i];
  156. $entry['scsl' . $i] = ($item['scsl' . $i] === 0 || is_null($item['scsl' . $i])) ? '' : $item['scsl' . $i];
  157. }
  158. $entry['订单编号'] = $item['订单编号'];
  159. $entry['子订单编号'] = $item['子订单编号'];
  160. $entry['UniqId'] = $item['UniqId'];
  161. $entry['zdtotal'] = $item['zdtotal'];
  162. $entry['颜色'] = $item['颜色'];
  163. $entry['款式'] = $item['款式'];
  164. $entry['款号'] = $item['款号'];
  165. $entry['color_id'] = $item['color_id'];
  166. $entry['颜色备注'] = $item['颜色备注'];
  167. $list[] = $entry;
  168. }
  169. // 创建新的合并子订单列表
  170. $finalMerged = [];
  171. foreach ($list as $mergedItem) {
  172. $colorId = $mergedItem['color_id'];
  173. // 查找原始列表中具有相同 color_id 的子订单编号
  174. foreach ($list as $item) {
  175. if ($item['color_id'] === $colorId) {
  176. // 复制合并后的数据,并替换子订单编号
  177. $newMergedItem = $mergedItem;
  178. $newMergedItem['子订单编号'] = $item['子订单编号'];
  179. $finalMerged[] = $newMergedItem;
  180. }
  181. }
  182. }
  183. // 在 $mergeds 中查找匹配的子订单编号 对应的一条数据
  184. $matchedOrder = null;
  185. foreach ($list as $mergedOrder) {
  186. if ($mergedOrder['子订单编号'] === $order) {
  187. $matchedOrder = $mergedOrder;
  188. break; // 找到后可以退出循环
  189. }
  190. }
  191. //产量进度汇总记录
  192. $rows = db()->table('设备_产量计酬')->alias('c')
  193. // ->join('工单_印件资料 y', 'c.订单编号 = y.订单编号', 'left')
  194. ->field('c.款号, c.订单编号, c.子订单编号, c.sys_rq as 上报时间, c.尺码, c.数量')
  195. ->where([
  196. 'c.子订单编号' => $params['order'],
  197. 'c.mod_rq' => null,
  198. 'c.工序名称' => $params['code']
  199. ])
  200. ->select();
  201. $formattedRows = [];
  202. foreach ($rows as $item) {
  203. if (!empty($item['尺码'])) {
  204. $key = $item['款号'];
  205. if (!isset($formattedRows[$key])) {
  206. $formattedRows[$key] = [
  207. '订单编号' => $item['订单编号'],
  208. '款号' => $item['款号'],
  209. '上报时间' => $item['上报时间'],
  210. '子订单编号' => $item['子订单编号'],
  211. '颜色' => $list[0]['颜色备注'],
  212. '裁剪数' => 0
  213. ];
  214. }
  215. // 累加尺码数量
  216. if (isset($formattedRows[$key][$item['尺码']])) {
  217. $formattedRows[$key][$item['尺码']] += $item['数量'];
  218. } else {
  219. $formattedRows[$key][$item['尺码']] = $item['数量'];
  220. }
  221. // 累加当前尺码的数量【裁剪数】
  222. $formattedRows[$key]['裁剪数'] += $item['数量'];
  223. }
  224. }
  225. //重置索引为连续数字索引
  226. $finalResult = array_values($formattedRows);
  227. // 报工历史记录
  228. $existingRecords = \db('设备_产量计酬')->alias('c')
  229. ->field('
  230. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  231. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  232. j.款式,
  233. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  234. ')
  235. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  236. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  237. ->where('c.子订单编号', $params['order'])
  238. ->where('c.工序名称', $params['code'])
  239. // ->where('c.sczl_jtbh', $params['sys_sbID'])
  240. ->whereNull('c.mod_rq') // 查询未删除数据
  241. ->whereNull('y.mod_rq') // 查询未删除数据
  242. ->order('c.UniqId', 'desc')
  243. ->distinct('c.子订单编号')
  244. ->select();
  245. $sizeSummary = [];
  246. $sizes = [];
  247. foreach ($existingRecords as $record) {
  248. $size = $record['尺码'];
  249. $quantity = $record['数量'];
  250. $serial = $record['serial'];
  251. // 如果尺码还没有出现过,加入尺码列表
  252. if (!in_array($size, $sizes)) {
  253. $sizes[] = $size;
  254. }
  255. if (!isset($sizeSummary[$record['UniqId']])) {
  256. $sizeSummary[$record['UniqId']] = [
  257. 'UniqId' => $record['UniqId'],
  258. '订单编号' => $record['订单编号'],
  259. '子订单编号' => $record['子订单编号'],
  260. '上报数量' => $record['s_num'],
  261. '剩余数量' => $record['ci_num'],
  262. '次品数量' => $record['数量'] - $record['ci_num'],
  263. '生产款号' => $record['款号'],
  264. '尺码' => $record['尺码'],
  265. '工序编号' => $record['工序编号'],
  266. '工序名称' => $record['工序名称'],
  267. '数量' => $quantity,
  268. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  269. '颜色' => $record['颜色备注'],
  270. '组别' => $record['sczl_jtbh'],
  271. '款式' => $record['款式'],
  272. 'serial' => '第('.$serial.')包',
  273. 'sys_rq' => $record['sys_rq']
  274. ];
  275. }
  276. }
  277. // 将 $sizeSummary 转换为索引数组
  278. $sizeSummary = array_values($sizeSummary);
  279. // 对 $sizeSummary 按 sys_rq 降序排序
  280. usort($sizeSummary, function ($a, $b) {
  281. return strtotime($b['UniqId']) - strtotime($a['UniqId']);
  282. });
  283. $result = [
  284. 'list' => $matchedOrder,
  285. 'headers' => $headers,
  286. 'table' => $finalResult,
  287. 'bgtable' => $existingRecords
  288. ];
  289. }else {
  290. //查询订单编号对应的数据
  291. $data = \db('工单_基本资料')->alias('j')
  292. ->distinct('y.子订单编号')
  293. ->field('
  294. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  295. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  296. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  297. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,y.cm11,y.cm12,y.cm13,y.cm14,
  298. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,y.cmsl11,y.cmsl12,y.cmsl13,y.cmsl14,
  299. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10, y.scsl11, y.scsl12, y.scsl13, y.scsl14'
  300. )
  301. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  302. // ->where('y.船样',1)
  303. ->whereNull('y.Mod_rq')
  304. ->where('y.订单编号', 'like', '%' . $order . '%')
  305. ->select();
  306. $headers = [];//存放尺码表头
  307. foreach ($data as $item) {
  308. // 初始化尺码与数量映射数组
  309. $cmScslMapping = [];
  310. for ($i = 1; $i <= 14; $i++) {
  311. $cmValue = $item['cm' . $i];
  312. $scslValue = $item['scsl' . $i];
  313. if (!is_null($cmValue) && $cmValue !== 0) {
  314. $cmScslMapping[$cmValue] = ($scslValue === 0 || is_null($scslValue)) ? '' : $scslValue;
  315. if (!in_array($cmValue, $headers)) {
  316. $headers[] = $cmValue;
  317. }
  318. }
  319. }
  320. }
  321. //【查询产量计酬历史记录】
  322. $where['c.订单编号'] = ['like','%'.explode('C',$order)[1].'%'];
  323. $where['c.工序名称'] = $params['code'];
  324. $rows = db()->table('设备_产量计酬')->alias('c')
  325. ->field('c.款号, c.订单编号, c.子订单编号, c.sys_rq as 上报时间, c.尺码, c.数量')
  326. ->where([
  327. 'c.mod_rq' => null,
  328. // 'c.订单编号' => $order,
  329. // 'c.工序名称' => $params['code']
  330. ])
  331. ->where($where)
  332. ->select();
  333. $formattedRows = [];
  334. foreach ($rows as $item) {
  335. if (!empty($item['尺码'])) {
  336. $key = $item['上报时间'];
  337. if (!isset($formattedRows[$key])) {
  338. $formattedRows[$key] = [
  339. '订单编号' => $item['订单编号'],
  340. '款号' => $item['款号'],
  341. '上报时间' => $item['上报时间'],
  342. '子订单编号' => $item['子订单编号'],
  343. '裁剪数' => 0
  344. ];
  345. }
  346. // 累加尺码数量
  347. if (isset($formattedRows[$key][$item['尺码']])) {
  348. $formattedRows[$key][$item['尺码']] += $item['数量'];
  349. } else {
  350. $formattedRows[$key][$item['尺码']] = $item['数量'];
  351. }
  352. // 累加当前尺码的数量【裁剪数】
  353. $formattedRows[$key]['裁剪数'] += $item['数量'];
  354. }
  355. }
  356. // 重置索引为连续数字索引
  357. $finalResult = array_values($formattedRows);
  358. $result = [
  359. 'table' => $finalResult,
  360. 'headers' => $headers,
  361. 'list' => '',
  362. 'total' => '',
  363. ];
  364. }
  365. $this->success('请求成功', $result);
  366. break;
  367. case '车缝':
  368. // 查询历史记录并生成尺寸汇总
  369. function getSizeSummary($order) {
  370. $where = [];
  371. if (strpos($order, '-') !== false) {
  372. $where['c.子订单编号'] = $order;
  373. } else {
  374. $where['c.订单编号'] = ['like', '%' . $order . '%'];
  375. }
  376. $where['c.工序编号'] = '3';
  377. $where['c.mod_rq'] = null;
  378. $where['j.Mod_rq'] = null;
  379. $existingRecords = \db('设备_产量计酬')->alias('c')
  380. ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码,
  381. CASE
  382. WHEN c.工序名称 IN ("裁剪", "车缝") THEN c.数量
  383. ELSE c.s_num
  384. END as 数量,
  385. c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num,c.UniqId,
  386. y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注, CAST(c.serial_num AS SIGNED) as serial_num_numeric')
  387. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  388. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  389. ->where($where)
  390. // ->limit(1,100)
  391. ->order('c.sys_rq desc')
  392. ->group('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num, j.款式')
  393. ->select();
  394. $sizeSummary = [];
  395. $sizes = [];
  396. foreach ($existingRecords as $record) {
  397. $size = $record['尺码'];
  398. $quantity = $record['数量'];
  399. $serial = $record['serial'];
  400. $serialNum = $record['serial_num']; // serial_num 分组
  401. $processNumber = $record['工序编号']; // 工序编号
  402. // 初始化尺码
  403. if (!in_array($size, $sizes)) {
  404. $sizes[] = $size;
  405. }
  406. // 检查是否已有相同的 serial_num 组合
  407. if (!isset($sizeSummary[$serialNum])) {
  408. $sizeSummary[$serialNum] = [
  409. '订单编号' => $record['订单编号'],
  410. '子订单编号' => $record['子订单编号'],
  411. '款号' => $record['款号'],
  412. '款式' => $record['款式'],
  413. '工序编号' => $record['工序编号'],
  414. '工序名称' => $record['工序名称'],
  415. '数量' => '0', // 记录总数量
  416. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  417. '颜色' => $record['颜色'],
  418. '颜色备注' => $record['颜色备注'],
  419. '机台号' => $record['sczl_jtbh'],
  420. '组别' => $record['sczl_bh'],
  421. 'serial' => [], // 流水号数组
  422. 'sys_rq' => $record['sys_rq'],
  423. 'UniqId' => $record['UniqId'],
  424. '尺码数据' => [] // 用于存储每个尺码对应的流水号和数量
  425. ];
  426. }
  427. // 将流水号添加到 serial 数组
  428. $sizeSummary[$serialNum]['serial'][] = $serial;
  429. // 累加数量到总数量
  430. $sizeSummary[$serialNum]['数量'] += $quantity;
  431. // 记录尺码数据
  432. if (!isset($sizeSummary[$serialNum]['尺码数据'][$size])) {
  433. $sizeSummary[$serialNum]['尺码数据'][$size] = [];
  434. }
  435. // 添加尺码对应的流水号和数量
  436. $sizeSummary[$serialNum]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  437. }
  438. // 对每个工序的 serial 进行处理,并转换为逗号分隔的字符串
  439. $finalRecords = [];
  440. foreach ($sizeSummary as &$summary) {
  441. // 将 serial 数组按数值降序排序
  442. sort($summary['serial'], SORT_NUMERIC);
  443. $summary['serial'] = implode(',', $summary['serial']);
  444. // 合并尺码数据到同一层级
  445. foreach ($summary['尺码数据'] as $size => $serials) {
  446. $summary[$size] = implode(', ', $serials); // 尺码对应的流水号和数量
  447. }
  448. unset($summary['尺码数据']); // 移除尺码数据数组,直接将数据合并到 summary
  449. $finalRecords[] = $summary;
  450. }
  451. // 尺码排序(数字在前,字母在后)
  452. usort($sizes, function ($a, $b) {
  453. $sizeOrder = [
  454. 'XXXS' => 1,
  455. 'XXS' => 2,
  456. 'XS' => 3,
  457. 'S' => 4,
  458. 'M' => 5,
  459. 'L' => 6,
  460. 'XL' => 7,
  461. '2XL' => 8,
  462. 'XXL' => 9,
  463. '3XL' => 10,
  464. 'XXXL' => 11,
  465. '4XL' => 12,
  466. 'XXXXL' => 13,
  467. '5XL' => 14,
  468. 'XXXXXL' => 15
  469. ];
  470. if (is_numeric($a) && is_numeric($b)) return $a - $b;
  471. if (is_numeric($a)) return -1;
  472. if (is_numeric($b)) return 1;
  473. return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  474. });
  475. return ['headers' => $sizes, 'records' => $finalRecords];
  476. }
  477. if (strpos($order, '-') !== false) {
  478. if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
  479. $this->error('sys_sbID 参数错误');
  480. }
  481. if (preg_match('/-(\d{2})$/', $order, $matches)) {
  482. $number = (int)$matches[1];
  483. if ($number > 70) {
  484. //echo '船样';
  485. //获取订单编号
  486. // $ddbh = explode('-', $order)[0];
  487. //1.查询尺码、制单数、填写报工新增实裁数
  488. $data = \db('工单_基本资料')->alias('j')
  489. ->distinct('y.子订单编号')
  490. ->field('
  491. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  492. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  493. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  494. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,y.cm11,y.cm12,y.cm13,y.cm14,
  495. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,y.cmsl11,y.cmsl12,y.cmsl13,y.cmsl14,
  496. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10, y.scsl11, y.scsl12, y.scsl13, y.scsl14'
  497. )
  498. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  499. ->where('y.船样',1)
  500. ->whereNull('y.Mod_rq')
  501. ->where('y.子订单编号', $order)
  502. ->select();
  503. } else {
  504. //echo '两位数子订单';
  505. $ddbh = explode('-', $order)[0];//获取订单编号
  506. //【查询尺码、制单数、填写报工新增实裁数】
  507. $data = \db('工单_基本资料')->alias('j')
  508. ->distinct('y.子订单编号')
  509. ->field('
  510. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  511. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  512. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  513. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,y.cm11,y.cm12,y.cm13,y.cm14,
  514. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,y.cmsl11,y.cmsl12,y.cmsl13,y.cmsl14,
  515. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10, y.scsl11, y.scsl12, y.scsl13, y.scsl14'
  516. )
  517. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  518. ->where('y.船样',0)->whereNull('y.Mod_rq')
  519. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  520. ->select();
  521. }
  522. }else{
  523. $ddbh = explode('-', $order)[0];//获取订单编号
  524. //【查询尺码、制单数、填写报工新增实裁数】
  525. $data = \db('工单_基本资料')->alias('j')
  526. ->distinct('y.子订单编号')
  527. ->field('
  528. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  529. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  530. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  531. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,y.cm11,y.cm12,y.cm13,y.cm14,
  532. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,y.cmsl11,y.cmsl12,y.cmsl13,y.cmsl14,
  533. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10, y.scsl11, y.scsl12, y.scsl13, y.scsl14'
  534. )
  535. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  536. ->where('y.船样',0)->whereNull('y.Mod_rq')
  537. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  538. ->select();
  539. }
  540. $dataEntry = [];
  541. $list = [];
  542. $headers = [];//存放尺码表头
  543. foreach ($data as $item) {
  544. // 初始化尺码与数量映射数组
  545. $cmScslMapping = [];
  546. for ($i = 1; $i <= 14; $i++) {
  547. $cmValue = $item['cm' . $i];
  548. $scslValue = $item['scsl' . $i];
  549. if (!is_null($cmValue) && $cmValue !== 0) {
  550. $cmScslMapping[$cmValue] = ($scslValue === 0 || is_null($scslValue)) ? '' : $scslValue;
  551. if (!in_array($cmValue, $headers)) {
  552. $headers[] = $cmValue;
  553. }
  554. }
  555. }
  556. // 将尺码数据平铺到主要数据中(表格数据)
  557. $orderData = [
  558. '订单编号' => $item['订单编号'],
  559. '子订单编号' => $item['子订单编号'],
  560. '款号' => $item['款号'],
  561. '款式' => $item['款式'],
  562. '颜色' => $item['颜色备注'],
  563. 'zdtotal' => $item['zdtotal'],
  564. 'sctotal' => $item['sctotal'],
  565. '单位' => $item['单位'],
  566. 'Sys_id' => $item['Sys_id'],
  567. 'Sys_rq' => $item['Sys_rq'],
  568. ];
  569. // 平铺尺码映射数据
  570. $orderData = array_merge($orderData, $cmScslMapping);
  571. $dataEntry[] = $orderData;
  572. // list 列表部分
  573. $entry = [];
  574. for ($i = 1; $i <= 14; $i++) {
  575. $entry['cm' . $i] = ($item['cm' . $i] === 0 || is_null($item['cm' . $i])) ? '' : $item['cm' . $i];
  576. $entry['cmsl' . $i] = ($item['cmsl' . $i] === 0 || is_null($item['cmsl' . $i])) ? '' : $item['cmsl' . $i];
  577. $entry['scsl' . $i] = ($item['scsl' . $i] === 0 || is_null($item['scsl' . $i])) ? '' : $item['scsl' . $i];
  578. }
  579. $entry['订单编号'] = $item['订单编号'];
  580. $entry['子订单编号'] = $item['子订单编号'];
  581. $entry['UniqId'] = $item['UniqId'];
  582. $entry['zdtotal'] = $item['zdtotal'];
  583. $entry['颜色'] = $item['颜色'];
  584. $entry['款式'] = $item['款式'];
  585. $entry['款号'] = $item['款号'];
  586. $entry['color_id'] = $item['color_id'];
  587. $entry['颜色备注'] = $item['颜色备注'];
  588. $list[] = $entry;
  589. }
  590. // 在 $mergeds 中查找匹配的子订单编号 对应的一条数据
  591. $matchedOrder = null;
  592. foreach ($list as $mergedOrder) {
  593. if ($mergedOrder['子订单编号'] === $order) {
  594. $matchedOrder = $mergedOrder;
  595. break; // 找到后可以退出循环
  596. }
  597. }
  598. $allScslEmpty = true;
  599. for ($i = 1; $i <= 14; $i++) {
  600. if (!empty($matchedOrder["scsl$i"])) {
  601. $allScslEmpty = false;
  602. break;
  603. }
  604. }
  605. if ($allScslEmpty) {
  606. $this->success('当前订单未裁剪 请裁剪后报工', [
  607. 'code' => 1,
  608. 'serial' => '',
  609. 'serial_num' => '',
  610. 'headers' => '',
  611. 'records' => '',
  612. ]);
  613. return;
  614. }
  615. // echo "<pre>";print_r($matchedOrder);echo "</pre>";die;
  616. $sizeData = getSizeSummary($order);
  617. // 查询流水号(包数)和序号
  618. $serial = \db('设备_产量计酬')
  619. ->field('子订单编号, MAX(CAST(serial AS UNSIGNED)) as serial, MAX(CAST(serial_num AS UNSIGNED)) as serial_num')
  620. ->where('子订单编号', $order)
  621. ->where(function($query) {
  622. $query->whereNull('mod_rq')->whereOr('mod_rq', '');
  623. }) // 查询未删除数据
  624. ->where('sczl_jtbh', $params['sys_sbID'])
  625. ->group('子订单编号')
  626. ->find();
  627. $serial['serial'] = empty($serial['serial']) ? '000' : str_pad($serial['serial'], 3, '0', STR_PAD_LEFT);
  628. $serial['serial_num'] = empty($serial['serial_num']) ? '0' : $serial['serial_num'];
  629. $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  630. $clwhere['子订单编号'] = $order;
  631. $clwhere['mod_rq'] = null;
  632. $clwhere['工序名称'] = $params['code'];
  633. $cljclist = \db('设备_产量计酬')
  634. ->field('订单编号,子订单编号,尺码,数量,mod_rq')
  635. ->where($clwhere)
  636. ->select();
  637. $result = [];
  638. $totalQuantity = 0; // 总数量初始化
  639. $orderNumber = ''; // 订单编号初始化
  640. foreach ($cljclist as $item) {
  641. $size = $item['尺码'];
  642. $orderNumber = $item['订单编号'];
  643. if (isset($result[$size])) {
  644. $result[$size] += $item['数量'];
  645. } else {
  646. $result[$size] = $item['数量'];
  647. }
  648. // 累加总数量
  649. $totalQuantity += $item['数量'];
  650. }
  651. $result['订单编号'] = $orderNumber;
  652. foreach ($result as $size => $quantity) {
  653. if ($size !== '订单编号' && $size !== '数量') {
  654. $result[$size] = '合计(' . $quantity . ')';
  655. }
  656. }
  657. $result['数量'] = '合计(' . $totalQuantity . ')';
  658. // 初始化 $sizeData['records'],以防万一它不存在
  659. if (!isset($sizeData['records'])) {
  660. $sizeData['records'] = [];
  661. }
  662. // 将 $result 插入到 $sizeData['records'] 的最前面
  663. array_unshift($sizeData['records'], $result);
  664. $this->success('请求成功', [
  665. 'serial' => $serial['serial']+1, // 流水号
  666. 'serial_num' => $serial['serial_num']+1, // 序号
  667. 'list' => $matchedOrder, // 报工
  668. 'headers' => $sizeData['headers'], // 尺码表头
  669. 'records' => $sizeData['records'] // 查询历史记录
  670. ]);
  671. }else{
  672. $sizeData = getSizeSummary($order);
  673. // 查询流水号(包数)和序号
  674. $serial = \db('设备_产量计酬')
  675. ->field('子订单编号, MAX(CAST(serial AS UNSIGNED)) as serial, MAX(CAST(serial_num AS UNSIGNED)) as serial_num')
  676. ->where('订单编号', $order)
  677. ->group('订单编号')
  678. ->find();
  679. $serial['serial'] = empty($serial['serial']) ? '000' : str_pad($serial['serial'], 3, '0', STR_PAD_LEFT);
  680. $serial['serial_num'] = empty($serial['serial_num']) ? '0' : $serial['serial_num'];
  681. $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  682. $clwhere['订单编号'] = $order;
  683. $clwhere['工序名称'] = $params['code'];
  684. $clwhere['mod_rq'] = null;
  685. $cljclist = \db('设备_产量计酬')
  686. ->field('订单编号,子订单编号,尺码,数量,mod_rq')
  687. ->where($clwhere)
  688. ->select();
  689. $result = [];
  690. $totalQuantity = 0; // 总数量初始化
  691. $orderNumber = ''; // 订单编号初始化
  692. foreach ($cljclist as $item) {
  693. $size = $item['尺码'];
  694. $orderNumber = $item['订单编号'];
  695. if (isset($result[$size])) {
  696. $result[$size] += $item['数量'];
  697. } else {
  698. $result[$size] = $item['数量'];
  699. }
  700. // 累加总数量
  701. $totalQuantity += $item['数量'];
  702. }
  703. foreach ($result as $size => $quantity) {
  704. if ($size !== '订单编号' && $size !== '数量') {
  705. $result[$size] = '合计(' . $quantity . ')';
  706. }
  707. }
  708. $result['订单编号'] = $orderNumber;
  709. $result['数量'] = '合计(' . $totalQuantity . ')';
  710. // 初始化 $sizeData['records'],以防万一它不存在
  711. if (!isset($sizeData['records'])) {
  712. $sizeData['records'] = [];
  713. }
  714. // 将 $result 插入到 $sizeData['records'] 的最前面
  715. array_unshift($sizeData['records'], $result);
  716. $this->success('请求成功', [
  717. 'serial' => $serial['serial'], // 流水号
  718. 'serial_num' => $serial['serial_num'], // 序号
  719. // 'list' => $matchedOrder, // 报工
  720. 'headers' => $sizeData['headers'], // 尺码表头
  721. 'records' => $sizeData['records'] // 查询历史记录
  722. ]);
  723. }
  724. break;
  725. case '手工':
  726. $gongxuname = '手工';
  727. $gongxuncode = '4';
  728. //判断是否为标签二维码参数数据
  729. if (strpos($order, ',') !== false) {
  730. //将子订单编号, 机台号, 工序, 尺码, 数量, 尾包, 流水号(包)分割为数组
  731. $paramArray = explode(',', $order);
  732. //通过子订单编号,尺码, 数量,流水号(包)查询数据库是否存在(且未删除数据)
  733. $res = \db('设备_产量计酬')
  734. ->where([
  735. '子订单编号' => $paramArray[0],
  736. '尺码' => $paramArray[3],
  737. '数量' => $paramArray[4],
  738. 'serial' => $paramArray[6],
  739. 'mod_rq' => null,
  740. ])
  741. ->order('UniqId desc')
  742. ->find();
  743. //如果不存在, 则返回当前扫码小票无效
  744. if (!$res) {
  745. $this->success('当前扫码小票无效', [
  746. 'code' => 1,
  747. 'result' => '',
  748. 'headers' => '',
  749. 'records' => ''
  750. ]);
  751. return;
  752. }
  753. $orderlist = \db('工单_基本资料')
  754. ->where('订单编号', $res['订单编号'])
  755. ->field('订单编号, 生产款号, 款式')
  756. ->find();
  757. $yinjianlist = \db('工单_印件资料')
  758. ->where('订单编号', $res['订单编号'])
  759. ->field('zdtotal, sctotal,颜色,颜色备注')
  760. ->find();
  761. if (empty($res)) {
  762. $ci_num = $paramArray[4];
  763. } else {
  764. $ci_num = $res["ci_num"];
  765. }
  766. $results = [
  767. 'sys_sbID' => $params['sys_sbID'],//设备编号
  768. 'ci_num' => $ci_num,//剩余数量
  769. 'UniqId' => $res['UniqId'],
  770. 'order' => $paramArray[0],//子订单编号
  771. 'zb' => $params['sys_sbID'],//组别
  772. 'gx' => $gongxuncode,//默认固定工序
  773. 'cm' => $paramArray[3],//尺码
  774. 'sl' => $paramArray[4],//数量
  775. 'wb' => $paramArray[5],//尾包
  776. '订单编号' => $orderlist['订单编号'],
  777. '生产款号' => $orderlist['生产款号'],
  778. '款式' => $orderlist['款式'],
  779. 'sctotal' => $yinjianlist['sctotal'],
  780. 'zdtotal' => $yinjianlist['zdtotal'],
  781. '颜色' => $yinjianlist['颜色备注'],
  782. ];
  783. $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  784. $clwhere['订单编号'] = $res['订单编号'];
  785. $clwhere['工序名称'] = $gongxuname;
  786. $clwhere['mod_rq'] = null;
  787. $cljclist = \db('设备_产量计酬')
  788. ->field('订单编号,子订单编号,尺码,s_num as 数量,mod_rq')
  789. ->where($clwhere)
  790. ->select();
  791. $result = [];
  792. $totalQuantity = 0; // 总数量初始化
  793. $orderNumber = ''; // 订单编号初始化
  794. foreach ($cljclist as $item) {
  795. $size = $item['尺码'];
  796. $orderNumber = $item['订单编号'];
  797. if (isset($result[$size])) {
  798. $result[$size] += $item['数量'];
  799. } else {
  800. $result[$size] = $item['数量'];
  801. }
  802. // 累加总数量
  803. $totalQuantity += $item['数量'];
  804. }
  805. foreach ($result as $size => $quantity) {
  806. if ($size !== '订单编号' && $size !== '数量') {
  807. $result[$size] = '合计(' . $quantity . ')';
  808. }
  809. }
  810. $result['订单编号'] = $orderNumber;
  811. $result['数量'] = '合计(' . $totalQuantity . ')';
  812. $where = [];
  813. if (strpos($paramArray[0], '-') !== false) {
  814. $where['c.子订单编号'] = $paramArray[0];
  815. } else {
  816. $where['c.订单编号'] = ['like', '%' . $paramArray[0] . '%'];
  817. }
  818. $where['c.工序编号'] = $gongxuncode;
  819. $where['c.mod_rq'] = null;
  820. $existingRecords = \db('设备_产量计酬')->alias('c')
  821. ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.s_num 数量, c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num,c.UniqId,
  822. ci_num,s_num,
  823. y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注, CAST(c.serial_num AS SIGNED) as serial_num_numeric')
  824. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  825. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  826. ->where($where)
  827. ->order('c.sys_rq desc')
  828. ->select();
  829. $sizeSummary = [];
  830. $sizes = [];
  831. // 遍历数据库查询结果,根据 UniqId + 尺码 进行分组
  832. foreach ($existingRecords as $record) {
  833. $size = $record['尺码'];
  834. $quantity = $record['数量'];
  835. $serial = $record['serial'];
  836. $uniqId = $record['UniqId'];
  837. // 初始化尺码数据
  838. if (!in_array($size, $sizes)) {
  839. $sizes[] = $size;
  840. }
  841. // 为每条记录创建独立的分组
  842. $groupKey = $uniqId . '_' . $size;
  843. if (!isset($sizeSummary[$groupKey])) {
  844. $sizeSummary[$groupKey] = [
  845. '订单编号' => $record['订单编号'],
  846. '子订单编号' => $record['子订单编号'],
  847. '款号' => $record['款号'],
  848. '款式' => $record['款式'],
  849. '工序编号' => $record['工序编号'],
  850. '工序名称' => $record['工序名称'],
  851. '剩余数量' => $record['ci_num'],
  852. '数量' => $quantity, // 直接使用当前记录的数量
  853. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  854. '颜色' => $record['颜色'],
  855. '颜色备注' => $record['颜色备注'],
  856. '机台号' => $record['sczl_jtbh'],
  857. '组别' => $record['sczl_bh'],
  858. 'serial' => $serial, // 直接使用流水号,不需要数组
  859. 'sys_rq' => $record['sys_rq'],
  860. 'UniqId' => $record['UniqId'],
  861. '尺码数据' => [] // 用于存储尺码对应的数据
  862. ];
  863. }
  864. // 记录尺码数据
  865. if (!isset($sizeSummary[$groupKey]['尺码数据'][$size])) {
  866. $sizeSummary[$groupKey]['尺码数据'][$size] = [];
  867. }
  868. // 添加尺码对应的流水号和数量
  869. $sizeSummary[$groupKey]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  870. }
  871. // 处理最终记录
  872. $finalRecords = [];
  873. foreach ($sizeSummary as &$summary) {
  874. // 合并尺码数据到同一层级
  875. foreach ($summary['尺码数据'] as $size => $serials) {
  876. $summary[$size] = implode(', ', $serials); // 尺码对应的流水号和数量
  877. }
  878. unset($summary['尺码数据']); // 移除尺码数据数组
  879. $finalRecords[] = $summary;
  880. }
  881. // 尺码排序(数字在前,字母在后)
  882. usort($sizes, function ($a, $b) {
  883. $sizeOrder = [
  884. 'XXXS' => 1,
  885. 'XXS' => 2,
  886. 'XS' => 3,
  887. 'S' => 4,
  888. 'M' => 5,
  889. 'L' => 6,
  890. 'XL' => 7,
  891. '2XL' => 8,
  892. 'XXL' => 9,
  893. '3XL' => 10,
  894. 'XXXL' => 11,
  895. '4XL' => 12,
  896. 'XXXXL' => 13,
  897. '5XL' => 14,
  898. 'XXXXXL' => 15
  899. ];
  900. if (is_numeric($a) && is_numeric($b)) return $a - $b;
  901. if (is_numeric($a)) return -1;
  902. if (is_numeric($b)) return 1;
  903. return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  904. });
  905. // 将汇总合计记录添加到最前面
  906. array_unshift($finalRecords, $result);
  907. $this->success('请求成功', [
  908. 'result' => $results,//小票数据
  909. 'headers' => $sizes, // 尺码表头
  910. 'records' => $finalRecords // 查询历史记录
  911. ]);
  912. } else {
  913. // 查询历史记录
  914. $this->success('请求成功', [
  915. 'result' => '',
  916. 'headers' => '',
  917. 'records' => ''
  918. ]);
  919. }
  920. break;
  921. case '大烫':
  922. $gongxuname = '大烫';
  923. $gongxuncode = '5';
  924. //判断是否为标签二维码参数数据
  925. if (strpos($order, ',') !== false) {
  926. //将子订单编号, 机台号, 工序, 尺码, 数量, 尾包, 流水号(包)分割为数组
  927. $paramArray = explode(',', $order);
  928. //通过子订单编号,尺码, 数量,流水号(包)查询数据库是否存在(且未删除数据)
  929. $res = \db('设备_产量计酬')
  930. ->where([
  931. '子订单编号' => $paramArray[0],
  932. '尺码' => $paramArray[3],
  933. '数量' => $paramArray[4],
  934. 'serial' => $paramArray[6],
  935. 'mod_rq' => null,
  936. ])
  937. ->order('UniqId desc')
  938. ->find();
  939. //如果不存在, 则返回当前扫码小票无效
  940. if (!$res) {
  941. $this->success('当前扫码小票无效', [
  942. 'code' => 1,
  943. 'result' => '',
  944. 'headers' => '',
  945. 'records' => ''
  946. ]);
  947. return;
  948. }
  949. $orderlist = \db('工单_基本资料')
  950. ->where('订单编号', $res['订单编号'])
  951. ->field('订单编号, 生产款号, 款式')
  952. ->find();
  953. $yinjianlist = \db('工单_印件资料')
  954. ->where('订单编号', $res['订单编号'])
  955. ->field('zdtotal, sctotal,颜色,颜色备注')
  956. ->find();
  957. // if (empty($res)) {
  958. $ci_num = $paramArray[4];
  959. // } else {
  960. // $ci_num = $res["ci_num"];
  961. // }
  962. $results = [
  963. 'sys_sbID' => $params['sys_sbID'],//设备编号
  964. 'ci_num' => $ci_num,//剩余数量
  965. 'UniqId' => $res['UniqId'],
  966. 'order' => $paramArray[0],//子订单编号
  967. 'zb' => $params['sys_sbID'],//组别
  968. 'gx' => $gongxuncode,//默认固定工序
  969. 'cm' => $paramArray[3],//尺码
  970. 'sl' => $paramArray[4],//数量
  971. 'wb' => $paramArray[5],//尾包
  972. '订单编号' => $orderlist['订单编号'],
  973. '生产款号' => $orderlist['生产款号'],
  974. '款式' => $orderlist['款式'],
  975. 'sctotal' => $yinjianlist['sctotal'],
  976. 'zdtotal' => $yinjianlist['zdtotal'],
  977. '颜色' => $yinjianlist['颜色备注'],
  978. ];
  979. $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  980. $clwhere['订单编号'] = $res['订单编号'];
  981. $clwhere['工序名称'] = $gongxuname;
  982. $clwhere['mod_rq'] = null;
  983. $cljclist = \db('设备_产量计酬')
  984. ->field('订单编号,子订单编号,尺码,s_num as 数量,mod_rq')
  985. ->where($clwhere)
  986. ->select();
  987. $result = [];
  988. $totalQuantity = 0; // 总数量初始化
  989. $orderNumber = ''; // 订单编号初始化
  990. foreach ($cljclist as $item) {
  991. $size = $item['尺码'];
  992. $orderNumber = $item['订单编号'];
  993. if (isset($result[$size])) {
  994. $result[$size] += $item['数量'];
  995. } else {
  996. $result[$size] = $item['数量'];
  997. }
  998. // 累加总数量
  999. $totalQuantity += $item['数量'];
  1000. }
  1001. foreach ($result as $size => $quantity) {
  1002. if ($size !== '订单编号' && $size !== '数量') {
  1003. $result[$size] = '合计(' . $quantity . ')';
  1004. }
  1005. }
  1006. $result['订单编号'] = $orderNumber;
  1007. $result['数量'] = '合计(' . $totalQuantity . ')';
  1008. $where = [];
  1009. if (strpos($paramArray[0], '-') !== false) {
  1010. $where['c.子订单编号'] = $paramArray[0];
  1011. } else {
  1012. $where['c.订单编号'] = ['like', '%' . $paramArray[0] . '%'];
  1013. }
  1014. $where['c.工序编号'] = $gongxuncode;
  1015. $where['c.mod_rq'] = null;
  1016. $existingRecords = \db('设备_产量计酬')->alias('c')
  1017. ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.s_num 数量, c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num,c.UniqId,
  1018. ci_num,s_num,
  1019. y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注, CAST(c.serial_num AS SIGNED) as serial_num_numeric')
  1020. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1021. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1022. ->where($where)
  1023. ->order('c.sys_rq desc')
  1024. ->select();
  1025. $sizeSummary = [];
  1026. $sizes = [];
  1027. // 遍历数据库查询结果,根据 UniqId + 尺码 进行分组
  1028. foreach ($existingRecords as $record) {
  1029. $size = $record['尺码'];
  1030. $quantity = $record['数量'];
  1031. $serial = $record['serial'];
  1032. $uniqId = $record['UniqId'];
  1033. // 初始化尺码数据
  1034. if (!in_array($size, $sizes)) {
  1035. $sizes[] = $size;
  1036. }
  1037. // 为每条记录创建独立的分组
  1038. $groupKey = $uniqId . '_' . $size;
  1039. if (!isset($sizeSummary[$groupKey])) {
  1040. $sizeSummary[$groupKey] = [
  1041. '订单编号' => $record['订单编号'],
  1042. '子订单编号' => $record['子订单编号'],
  1043. '款号' => $record['款号'],
  1044. '款式' => $record['款式'],
  1045. '工序编号' => $record['工序编号'],
  1046. '工序名称' => $record['工序名称'],
  1047. '剩余数量' => $record['ci_num'],
  1048. '数量' => $quantity, // 直接使用当前记录的数量
  1049. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1050. '颜色' => $record['颜色'],
  1051. '颜色备注' => $record['颜色备注'],
  1052. '机台号' => $record['sczl_jtbh'],
  1053. '组别' => $record['sczl_bh'],
  1054. 'serial' => $serial, // 直接使用流水号,不需要数组
  1055. 'sys_rq' => $record['sys_rq'],
  1056. 'UniqId' => $record['UniqId'],
  1057. '尺码数据' => [] // 用于存储尺码对应的数据
  1058. ];
  1059. }
  1060. // 记录尺码数据
  1061. if (!isset($sizeSummary[$groupKey]['尺码数据'][$size])) {
  1062. $sizeSummary[$groupKey]['尺码数据'][$size] = [];
  1063. }
  1064. // 添加尺码对应的流水号和数量
  1065. $sizeSummary[$groupKey]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  1066. }
  1067. // 处理最终记录
  1068. $finalRecords = [];
  1069. foreach ($sizeSummary as &$summary) {
  1070. // 合并尺码数据到同一层级
  1071. foreach ($summary['尺码数据'] as $size => $serials) {
  1072. $summary[$size] = implode(', ', $serials); // 尺码对应的流水号和数量
  1073. }
  1074. unset($summary['尺码数据']); // 移除尺码数据数组
  1075. $finalRecords[] = $summary;
  1076. }
  1077. // 尺码排序(数字在前,字母在后)
  1078. usort($sizes, function ($a, $b) {
  1079. $sizeOrder = [
  1080. 'XXXS' => 1,
  1081. 'XXS' => 2,
  1082. 'XS' => 3,
  1083. 'S' => 4,
  1084. 'M' => 5,
  1085. 'L' => 6,
  1086. 'XL' => 7,
  1087. '2XL' => 8,
  1088. 'XXL' => 9,
  1089. '3XL' => 10,
  1090. 'XXXL' => 11,
  1091. '4XL' => 12,
  1092. 'XXXXL' => 13,
  1093. '5XL' => 14,
  1094. 'XXXXXL' => 15
  1095. ];
  1096. if (is_numeric($a) && is_numeric($b)) return $a - $b;
  1097. if (is_numeric($a)) return -1;
  1098. if (is_numeric($b)) return 1;
  1099. return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  1100. });
  1101. // 将汇总合计记录添加到最前面
  1102. array_unshift($finalRecords, $result);
  1103. $this->success('请求成功', [
  1104. 'result' => $results,//小票数据
  1105. 'headers' => $sizes, // 尺码表头
  1106. 'records' => $finalRecords // 查询历史记录
  1107. ]);
  1108. } else {
  1109. // 查询历史记录
  1110. $this->success('请求成功', [
  1111. 'result' => '',
  1112. 'headers' => '',
  1113. 'records' => ''
  1114. ]);
  1115. }
  1116. break;
  1117. case '总检':
  1118. //判断是否为子订单编号【$order:DC2503291-0703 子订单编号】
  1119. if (strpos($order, '-') !== false) {
  1120. // 定义查询字段
  1121. $field = "
  1122. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  1123. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  1124. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  1125. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,y.cm11,y.cm12,y.cm13,y.cm14,
  1126. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,y.cmsl11,y.cmsl12,y.cmsl13,y.cmsl14,
  1127. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10, y.scsl11, y.scsl12, y.scsl13, y.scsl14
  1128. ";
  1129. // 根据订单类型查询数据
  1130. if (preg_match('/-(\d{2})$/', $order, $matches)) {
  1131. $number = (int)$matches[1];
  1132. if ($number > 70) {
  1133. // 船样订单查询
  1134. $data = \db('工单_基本资料')->alias('j')
  1135. ->distinct('y.子订单编号')
  1136. ->field($field)
  1137. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1138. ->where('y.船样',1)
  1139. ->whereNull('y.Mod_rq')
  1140. ->where('y.子订单编号', $order)
  1141. ->select();
  1142. } else {
  1143. // 普通两位数子订单
  1144. $ddbh = explode('-', $order)[0];//获取订单编号
  1145. $data = \db('工单_基本资料')->alias('j')
  1146. ->distinct('y.子订单编号')
  1147. ->field($field)
  1148. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1149. ->where('y.船样',0)->whereNull('y.Mod_rq')
  1150. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  1151. ->select();
  1152. }
  1153. } else {
  1154. // 其他子订单
  1155. $ddbh = explode('-', $order)[0];//获取订单编号
  1156. $data = \db('工单_基本资料')->alias('j')
  1157. ->distinct('y.子订单编号')
  1158. ->field($field)
  1159. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1160. ->where('y.船样',0)->whereNull('y.Mod_rq')
  1161. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  1162. ->select();
  1163. }
  1164. // 处理查询到的数据
  1165. $dataEntry = [];
  1166. $list = [];
  1167. $headers = [];//存放尺码表头
  1168. foreach ($data as $item) {
  1169. // 处理尺码与数量映射
  1170. $cmScslMapping = [];
  1171. for ($i = 1; $i <= 14; $i++) {
  1172. $cmValue = $item['cm' . $i];
  1173. $scslValue = $item['scsl' . $i];
  1174. if (!is_null($cmValue) && $cmValue !== 0) {
  1175. $cmScslMapping[$cmValue] = ($scslValue === 0 || is_null($scslValue)) ? '' : $scslValue;
  1176. if (!in_array($cmValue, $headers)) {
  1177. $headers[] = $cmValue;
  1178. }
  1179. }
  1180. }
  1181. // 构建表格数据
  1182. $orderData = [
  1183. '订单编号' => $item['订单编号'],
  1184. '子订单编号' => $item['子订单编号'],
  1185. '款号' => $item['款号'],
  1186. '款式' => $item['款式'],
  1187. '颜色' => $item['颜色备注'],
  1188. 'zdtotal' => $item['zdtotal'],
  1189. 'sctotal' => $item['sctotal'],
  1190. '单位' => $item['单位'],
  1191. 'Sys_id' => $item['Sys_id'],
  1192. 'Sys_rq' => $item['Sys_rq'],
  1193. ];
  1194. $orderData = array_merge($orderData, $cmScslMapping);
  1195. $dataEntry[] = $orderData;
  1196. // 构建列表数据
  1197. $entry = [];
  1198. for ($i = 1; $i <= 14; $i++) {
  1199. $entry['cm' . $i] = ($item['cm' . $i] === 0 || is_null($item['cm' . $i])) ? '' : $item['cm' . $i];
  1200. $entry['cmsl' . $i] = ($item['cmsl' . $i] === 0 || is_null($item['cmsl' . $i])) ? '' : $item['cmsl' . $i];
  1201. $entry['scsl' . $i] = ($item['scsl' . $i] === 0 || is_null($item['scsl' . $i])) ? '' : $item['scsl' . $i];
  1202. }
  1203. $entry['订单编号'] = $item['订单编号'];
  1204. $entry['子订单编号'] = $item['子订单编号'];
  1205. $entry['UniqId'] = $item['UniqId'];
  1206. $entry['zdtotal'] = $item['zdtotal'];
  1207. $entry['颜色'] = $item['颜色'];
  1208. $entry['款式'] = $item['款式'];
  1209. $entry['款号'] = $item['款号'];
  1210. $entry['color_id'] = $item['color_id'];
  1211. $entry['颜色备注'] = $item['颜色备注'];
  1212. $list[] = $entry;
  1213. }
  1214. // 获取匹配的订单数据
  1215. $order = explode(',', $order)[0];
  1216. $matchedOrder = null;
  1217. foreach ($list as $mergedOrder) {
  1218. if ($mergedOrder['子订单编号'] === $order) {
  1219. $matchedOrder = $mergedOrder;
  1220. break;
  1221. }
  1222. }
  1223. // 检查实裁数是否为空
  1224. $allScslEmpty = true;
  1225. if ($matchedOrder) {
  1226. for ($i = 1; $i <= 14; $i++) {
  1227. if (!empty($matchedOrder["scsl$i"])) {
  1228. $allScslEmpty = false;
  1229. break;
  1230. }
  1231. }
  1232. }
  1233. if ($allScslEmpty) {
  1234. // 当前订单未裁剪 请裁剪后报工
  1235. $this->success('查询失败', [
  1236. 'code' => 1,
  1237. 'serial' => '',
  1238. 'serial_num' => '',
  1239. 'headers' => '',
  1240. 'records' => '',
  1241. ]);
  1242. return;
  1243. }
  1244. // 查询产量计酬记录
  1245. $clwhere = [
  1246. 'sczl_jtbh' => $params['sys_sbID'],
  1247. '子订单编号' => $order,
  1248. 'mod_rq' => null,
  1249. '工序名称' => $params['code']
  1250. ];
  1251. $cljclist = \db('设备_产量计酬')
  1252. ->field('订单编号,子订单编号,尺码,数量,mod_rq')
  1253. ->where($clwhere)
  1254. ->select();
  1255. // 汇总产量记录
  1256. $result = [];
  1257. $totalQuantity = 0;
  1258. $orderNumber = '';
  1259. foreach ($cljclist as $item) {
  1260. $size = $item['尺码'];
  1261. $orderNumber = $item['订单编号'];
  1262. if (isset($result[$size])) {
  1263. $result[$size] += $item['数量'];
  1264. } else {
  1265. $result[$size] = $item['数量'];
  1266. }
  1267. $totalQuantity += $item['数量'];
  1268. }
  1269. $result['订单编号'] = $orderNumber;
  1270. foreach ($result as $size => $quantity) {
  1271. if ($size !== '订单编号' && $size !== '数量') {
  1272. $result[$size] = '合计(' . $quantity . ')';
  1273. }
  1274. }
  1275. $result['数量'] = '合计(' . $totalQuantity . ')';
  1276. // 查询现有工序记录
  1277. $where = [
  1278. 'c.子订单编号' => $order,
  1279. 'c.工序编号' => '6',
  1280. 'c.mod_rq' => null
  1281. ];
  1282. $existingRecords = \db('设备_产量计酬')->alias('c')
  1283. ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码,
  1284. CASE
  1285. WHEN c.工序名称 IN ("裁剪", "车缝") THEN c.数量
  1286. ELSE c.s_num
  1287. END as 数量,
  1288. c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num,c.UniqId,
  1289. y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注')
  1290. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1291. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1292. ->where($where)
  1293. ->order('c.UniqId desc')
  1294. ->group('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num, j.款式')
  1295. ->select();
  1296. // 处理现有记录
  1297. $sizeSummary = [];
  1298. $sizes = [];
  1299. foreach ($existingRecords as $record) {
  1300. $size = $record['尺码'];
  1301. $quantity = $record['数量'];
  1302. $serial = $record['serial'];
  1303. $serialNum = $record['serial_num'];
  1304. // 收集尺码
  1305. if (!in_array($size, $sizes)) {
  1306. $sizes[] = $size;
  1307. }
  1308. // 初始化serial_num分组
  1309. if (!isset($sizeSummary[$serialNum])) {
  1310. $sizeSummary[$serialNum] = [
  1311. '订单编号' => $record['订单编号'],
  1312. '子订单编号' => $record['子订单编号'],
  1313. '款号' => $record['款号'],
  1314. '款式' => $record['款式'],
  1315. '工序编号' => $record['工序编号'],
  1316. '工序名称' => $record['工序名称'],
  1317. '数量' => 0,
  1318. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1319. '颜色' => $record['颜色'],
  1320. '颜色备注' => $record['颜色备注'],
  1321. '机台号' => $record['sczl_jtbh'],
  1322. '组别' => $record['sczl_bh'],
  1323. 'serial' => [],
  1324. 'sys_rq' => $record['sys_rq'],
  1325. 'UniqId' => $record['UniqId'],
  1326. '尺码数据' => []
  1327. ];
  1328. }
  1329. // 添加流水号
  1330. $sizeSummary[$serialNum]['serial'][] = $serial;
  1331. $sizeSummary[$serialNum]['数量'] += $quantity;
  1332. // 记录尺码数据
  1333. if (!isset($sizeSummary[$serialNum]['尺码数据'][$size])) {
  1334. $sizeSummary[$serialNum]['尺码数据'][$size] = [];
  1335. }
  1336. $sizeSummary[$serialNum]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  1337. }
  1338. // 最终记录处理
  1339. $finalRecords = [];
  1340. foreach ($sizeSummary as &$summary) {
  1341. // 排序并合并流水号
  1342. sort($summary['serial'], SORT_NUMERIC);
  1343. $summary['serial'] = implode(',', $summary['serial']);
  1344. // 合并尺码数据
  1345. foreach ($summary['尺码数据'] as $size => $serials) {
  1346. $summary[$size] = implode(', ', $serials);
  1347. }
  1348. unset($summary['尺码数据']);
  1349. $finalRecords[] = $summary;
  1350. }
  1351. // 尺码排序
  1352. $sizeOrder = [
  1353. 'XXXS' => 1, 'XXS' => 2, 'XS' => 3, 'S' => 4, 'M' => 5,
  1354. 'L' => 6, 'XL' => 7, '2XL' => 8, 'XXL' => 9, '3XL' => 10,
  1355. 'XXXL' => 11, '4XL' => 12, 'XXXXL' => 13, '5XL' => 14, 'XXXXXL' => 15
  1356. ];
  1357. usort($sizes, function ($a, $b) use ($sizeOrder) {
  1358. if (is_numeric($a) && is_numeric($b)) return $a - $b;
  1359. if (is_numeric($a)) return -1;
  1360. if (is_numeric($b)) return 1;
  1361. return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  1362. });
  1363. // 添加汇总记录到最前面
  1364. array_unshift($finalRecords, $result);
  1365. // 查询流水号信息
  1366. $res_serial = \db('设备_产量计酬')
  1367. ->field('子订单编号, MAX(CAST(serial AS UNSIGNED)) as serial, MAX(CAST(serial_num AS UNSIGNED)) as serial_num')
  1368. ->where('子订单编号', $order)
  1369. ->whereNull('mod_rq')
  1370. // ->where('sczl_jtbh', $params['sys_sbID'])
  1371. ->group('子订单编号')
  1372. ->find();
  1373. // 处理流水号
  1374. $res_serial['serial'] = empty($res_serial['serial']) ? '000' : str_pad($res_serial['serial'], 3, '0', STR_PAD_LEFT);
  1375. $res_serial['serial_num'] = empty($res_serial['serial_num']) ? '000' : str_pad($res_serial['serial_num'], 3, '0', STR_PAD_LEFT);
  1376. $next_serial = str_pad((int)$res_serial['serial'] + 1, 3, '0', STR_PAD_LEFT);
  1377. $next_serial_num = str_pad((int)$res_serial['serial_num'] + 1, 3, '0', STR_PAD_LEFT);
  1378. $this->success('请求成功', [
  1379. 'serial' => $next_serial,
  1380. 'serial_num' => $next_serial_num,
  1381. 'list' => $matchedOrder,
  1382. 'headers' => $sizes,
  1383. 'records' => $finalRecords
  1384. ]);
  1385. } else {
  1386. // 按照订单编号查询
  1387. $this->success('请求成功', [
  1388. 'serial' => '',
  1389. 'serial_num' => '',
  1390. 'list' => '',
  1391. 'headers' => '',
  1392. 'records' => ''
  1393. ]);
  1394. }
  1395. // //判断是否为子订单编号【$order:DC2503291-0703 子订单编号】
  1396. // if (strpos($order, '-') !== false) {
  1397. // $field = "
  1398. // y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  1399. // y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  1400. // y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  1401. // y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,y.cm11,y.cm12,y.cm13,y.cm14,
  1402. // y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,y.cmsl11,y.cmsl12,y.cmsl13,y.cmsl14,
  1403. // y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10, y.scsl11, y.scsl12, y.scsl13, y.scsl14
  1404. // ";
  1405. // if (preg_match('/-(\d{2})$/', $order, $matches)) {
  1406. // $number = (int)$matches[1];
  1407. // if ($number > 70) {
  1408. // $data = \db('工单_基本资料')->alias('j')
  1409. // ->distinct('y.子订单编号')
  1410. // ->field($field)
  1411. // ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1412. // ->where('y.船样',1)
  1413. // ->whereNull('y.Mod_rq')
  1414. // ->where('y.子订单编号', $order)
  1415. // ->select();
  1416. // } else {
  1417. // //echo '两位数子订单';
  1418. // $ddbh = explode('-', $order)[0];//获取订单编号
  1419. // //【查询尺码、制单数、填写报工新增实裁数】
  1420. // $data = \db('工单_基本资料')->alias('j')
  1421. // ->distinct('y.子订单编号')
  1422. // ->field($field)
  1423. // ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1424. // ->where('y.船样',0)->whereNull('y.Mod_rq')
  1425. // ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  1426. // ->select();
  1427. // }
  1428. // }else{
  1429. // $ddbh = explode('-', $order)[0];//获取订单编号
  1430. // //【查询尺码、制单数、填写报工新增实裁数】
  1431. // $data = \db('工单_基本资料')->alias('j')
  1432. // ->distinct('y.子订单编号')
  1433. // ->field($field)
  1434. // ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1435. // ->where('y.船样',0)->whereNull('y.Mod_rq')
  1436. // ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  1437. // ->select();
  1438. // }
  1439. //
  1440. // $dataEntry = [];
  1441. // $list = [];
  1442. // $headers = [];//存放尺码表头
  1443. // foreach ($data as $item) {
  1444. // // 初始化尺码与数量映射数组
  1445. // $cmScslMapping = [];
  1446. // for ($i = 1; $i <= 14; $i++) {
  1447. // $cmValue = $item['cm' . $i];
  1448. // $scslValue = $item['scsl' . $i];
  1449. // if (!is_null($cmValue) && $cmValue !== 0) {
  1450. // $cmScslMapping[$cmValue] = ($scslValue === 0 || is_null($scslValue)) ? '' : $scslValue;
  1451. // if (!in_array($cmValue, $headers)) {
  1452. // $headers[] = $cmValue;
  1453. // }
  1454. // }
  1455. // }
  1456. //
  1457. // // 将尺码数据平铺到主要数据中(表格数据)
  1458. // $orderData = [
  1459. // '订单编号' => $item['订单编号'],
  1460. // '子订单编号' => $item['子订单编号'],
  1461. // '款号' => $item['款号'],
  1462. // '款式' => $item['款式'],
  1463. // '颜色' => $item['颜色备注'],
  1464. // 'zdtotal' => $item['zdtotal'],
  1465. // 'sctotal' => $item['sctotal'],
  1466. // '单位' => $item['单位'],
  1467. // 'Sys_id' => $item['Sys_id'],
  1468. // 'Sys_rq' => $item['Sys_rq'],
  1469. // ];
  1470. // // 平铺尺码映射数据
  1471. // $orderData = array_merge($orderData, $cmScslMapping);
  1472. // $dataEntry[] = $orderData;
  1473. // // list 列表部分
  1474. // $entry = [];
  1475. // for ($i = 1; $i <= 14; $i++) {
  1476. // $entry['cm' . $i] = ($item['cm' . $i] === 0 || is_null($item['cm' . $i])) ? '' : $item['cm' . $i];
  1477. // $entry['cmsl' . $i] = ($item['cmsl' . $i] === 0 || is_null($item['cmsl' . $i])) ? '' : $item['cmsl' . $i];
  1478. // $entry['scsl' . $i] = ($item['scsl' . $i] === 0 || is_null($item['scsl' . $i])) ? '' : $item['scsl' . $i];
  1479. // }
  1480. // $entry['订单编号'] = $item['订单编号'];
  1481. // $entry['子订单编号'] = $item['子订单编号'];
  1482. // $entry['UniqId'] = $item['UniqId'];
  1483. // $entry['zdtotal'] = $item['zdtotal'];
  1484. // $entry['颜色'] = $item['颜色'];
  1485. // $entry['款式'] = $item['款式'];
  1486. // $entry['款号'] = $item['款号'];
  1487. // $entry['color_id'] = $item['color_id'];
  1488. // $entry['颜色备注'] = $item['颜色备注'];
  1489. // $list[] = $entry;
  1490. // }
  1491. //
  1492. //
  1493. // // 如果 $order 含逗号,取第一个逗号前的子订单编号
  1494. // $order = explode(',', $order)[0];
  1495. // $matchedOrder = null;
  1496. // foreach ($list as $mergedOrder) {
  1497. // if ($mergedOrder['子订单编号'] === $order) {
  1498. // $matchedOrder = $mergedOrder;
  1499. // break;
  1500. // }
  1501. // }
  1502. // $allScslEmpty = true;
  1503. // for ($i = 1; $i <= 14; $i++) {
  1504. // if (!empty($matchedOrder["scsl$i"])) {
  1505. // $allScslEmpty = false;
  1506. // break;
  1507. // }
  1508. // }
  1509. //
  1510. // if ($allScslEmpty) {
  1511. // //当前订单未裁剪 请裁剪后报工
  1512. // $this->success('查询失败', [
  1513. // 'code' => 1,
  1514. // 'serial' => '',
  1515. // 'serial_num' => '',
  1516. // 'headers' => '',
  1517. // 'records' => '',
  1518. // ]);
  1519. // return;
  1520. // }
  1521. //
  1522. // $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  1523. // $clwhere['子订单编号'] = $order;
  1524. // $clwhere['mod_rq'] = null;
  1525. // $clwhere['工序名称'] = $params['code'];
  1526. // $cljclist = \db('设备_产量计酬')
  1527. // ->field('订单编号,子订单编号,尺码,数量,mod_rq')
  1528. // ->where($clwhere)
  1529. // ->select();
  1530. // $result = [];
  1531. // $totalQuantity = 0; // 总数量初始化
  1532. // $orderNumber = ''; // 订单编号初始化
  1533. // foreach ($cljclist as $item) {
  1534. // $size = $item['尺码'];
  1535. // $orderNumber = $item['订单编号'];
  1536. // if (isset($result[$size])) {
  1537. // $result[$size] += $item['数量'];
  1538. // } else {
  1539. // $result[$size] = $item['数量'];
  1540. // }
  1541. // // 累加总数量
  1542. // $totalQuantity += $item['数量'];
  1543. // }
  1544. // $result['订单编号'] = $orderNumber;
  1545. // foreach ($result as $size => $quantity) {
  1546. // if ($size !== '订单编号' && $size !== '数量') {
  1547. // $result[$size] = '合计(' . $quantity . ')';
  1548. // }
  1549. // }
  1550. // $result['数量'] = '合计(' . $totalQuantity . ')';
  1551. //
  1552. // $where = [];
  1553. // if (strpos($order, '-') !== false) {
  1554. // $where['c.子订单编号'] = $order;
  1555. // } else {
  1556. // $where['c.订单编号'] = ['like', '%' . $order . '%'];
  1557. // }
  1558. //
  1559. // $where['c.工序编号'] = '6';
  1560. // $where['c.mod_rq'] = null;
  1561. // $existingRecords = \db('设备_产量计酬')->alias('c')
  1562. // ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码,
  1563. // CASE
  1564. // WHEN c.工序名称 IN ("裁剪", "车缝") THEN c.数量
  1565. // ELSE c.s_num
  1566. // END as 数量,
  1567. // c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num,c.UniqId,
  1568. // y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注')
  1569. // ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1570. // ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1571. // ->where($where)
  1572. // ->order('c.UniqId desc')
  1573. // ->group('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num, j.款式')
  1574. // ->select();
  1575. //
  1576. // $sizeSummary = [];
  1577. // $sizes = [];
  1578. // foreach ($existingRecords as $record) {
  1579. // $size = $record['尺码'];
  1580. // $quantity = $record['数量'];
  1581. // $serial = $record['serial'];
  1582. // $serialNum = $record['serial_num']; // serial_num 分组
  1583. // $processNumber = $record['工序编号']; // 工序编号
  1584. // // 初始化尺码
  1585. // if (!in_array($size, $sizes)) {
  1586. // $sizes[] = $size;
  1587. // }
  1588. // // 检查是否已有相同的 serial_num 组合
  1589. // if (!isset($sizeSummary[$serialNum])) {
  1590. // $sizeSummary[$serialNum] = [
  1591. // '订单编号' => $record['订单编号'],
  1592. // '子订单编号' => $record['子订单编号'],
  1593. // '款号' => $record['款号'],
  1594. // '款式' => $record['款式'],
  1595. // '工序编号' => $record['工序编号'],
  1596. // '工序名称' => $record['工序名称'],
  1597. // '数量' => '0', // 记录总数量
  1598. // '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1599. // '颜色' => $record['颜色'],
  1600. // '颜色备注' => $record['颜色备注'],
  1601. // '机台号' => $record['sczl_jtbh'],
  1602. // '组别' => $record['sczl_bh'],
  1603. // 'serial' => [], // 流水号数组
  1604. // 'sys_rq' => $record['sys_rq'],
  1605. // 'UniqId' => $record['UniqId'],
  1606. // '尺码数据' => [] // 用于存储每个尺码对应的流水号和数量
  1607. // ];
  1608. // }
  1609. //
  1610. // // 将流水号添加到 serial 数组
  1611. // $sizeSummary[$serialNum]['serial'][] = $serial;
  1612. //
  1613. // // 累加数量到总数量
  1614. // $sizeSummary[$serialNum]['数量'] += $quantity;
  1615. //
  1616. // // 记录尺码数据
  1617. // if (!isset($sizeSummary[$serialNum]['尺码数据'][$size])) {
  1618. // $sizeSummary[$serialNum]['尺码数据'][$size] = [];
  1619. // }
  1620. //
  1621. // // 添加尺码对应的流水号和数量
  1622. // $sizeSummary[$serialNum]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  1623. // }
  1624. //
  1625. // // 对每个工序的 serial 进行处理,并转换为逗号分隔的字符串
  1626. // $finalRecords = [];
  1627. // foreach ($sizeSummary as &$summary) {
  1628. // // 将 serial 数组按数值降序排序
  1629. // sort($summary['serial'], SORT_NUMERIC);
  1630. // $summary['serial'] = implode(',', $summary['serial']);
  1631. //
  1632. // // 合并尺码数据到同一层级
  1633. // foreach ($summary['尺码数据'] as $size => $serials) {
  1634. // $summary[$size] = implode(', ', $serials); // 尺码对应的流水号和数量
  1635. // }
  1636. //
  1637. // unset($summary['尺码数据']); // 移除尺码数据数组,直接将数据合并到 summary
  1638. // $finalRecords[] = $summary;
  1639. // }
  1640. //
  1641. // // 尺码排序(数字在前,字母在后)
  1642. // usort($sizes, function ($a, $b) {
  1643. // $sizeOrder = [
  1644. // 'XXXS' => 1,
  1645. // 'XXS' => 2,
  1646. // 'XS' => 3,
  1647. // 'S' => 4,
  1648. // 'M' => 5,
  1649. // 'L' => 6,
  1650. // 'XL' => 7,
  1651. // '2XL' => 8,
  1652. // 'XXL' => 9,
  1653. // '3XL' => 10,
  1654. // 'XXXL' => 11,
  1655. // '4XL' => 12,
  1656. // 'XXXXL' => 13,
  1657. // '5XL' => 14,
  1658. // 'XXXXXL' => 15
  1659. // ];
  1660. // if (is_numeric($a) && is_numeric($b)) return $a - $b;
  1661. // if (is_numeric($a)) return -1;
  1662. // if (is_numeric($b)) return 1;
  1663. // return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  1664. // });
  1665. //
  1666. // // 将汇总合计记录添加到最前面
  1667. // array_unshift($finalRecords, $result);
  1668. //
  1669. // // 查询流水号(包数)序号
  1670. // $res_serial = \db('设备_产量计酬')
  1671. // ->field('子订单编号, MAX(CAST(serial AS UNSIGNED)) as serial, MAX(CAST(serial_num AS UNSIGNED)) as serial_num')
  1672. // ->where('子订单编号', $order)
  1673. // ->whereNull('mod_rq')// 查询未删除数据
  1674. // ->where('sczl_jtbh', $params['sys_sbID'])
  1675. // ->group('子订单编号')
  1676. // ->find();
  1677. // // 默认值填充(字符串带前导零)
  1678. // $res_serial['serial'] = empty($res_serial['serial']) ? '000' : str_pad($res_serial['serial'], 3, '0', STR_PAD_LEFT);
  1679. // $res_serial['serial_num'] = empty($res_serial['serial_num']) ? '000' : str_pad($res_serial['serial_num'], 3, '0', STR_PAD_LEFT);
  1680. // // 先加 1,再格式化为 3 位
  1681. // $next_serial = str_pad((int)$res_serial['serial'] + 1, 3, '0', STR_PAD_LEFT);
  1682. // $next_serial_num = str_pad((int)$res_serial['serial_num'] + 1, 3, '0', STR_PAD_LEFT);
  1683. //
  1684. // $this->success('请求成功', [
  1685. // 'serial' => $next_serial, // 流水号
  1686. // 'serial_num' => $next_serial_num, // 序号
  1687. // 'list' => $matchedOrder, // 报工前需要的数据
  1688. // 'headers' => $sizes, // 尺码表头
  1689. // 'records' =>$finalRecords // 查询报工后历史记录
  1690. // ]);
  1691. // }else{
  1692. // //按照订单编号查询
  1693. // $this->success('请求成功', [
  1694. // 'serial' => '', // // 流水号(包数)
  1695. // 'serial_num' => '', // 序号
  1696. // 'list' => '', // 报工
  1697. // 'headers' => '',// 尺码表头
  1698. // 'records' => '' // 查询历史记录
  1699. // ]);
  1700. // }
  1701. break;
  1702. case '包装':
  1703. //判断是否为子订单编号【$order:DC2503291-0703 子订单编号】
  1704. if (strpos($order, '-') !== false) {
  1705. // 定义查询字段
  1706. $field = "
  1707. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  1708. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  1709. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  1710. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,y.cm11,y.cm12,y.cm13,y.cm14,
  1711. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,y.cmsl11,y.cmsl12,y.cmsl13,y.cmsl14,
  1712. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10, y.scsl11, y.scsl12, y.scsl13, y.scsl14
  1713. ";
  1714. // 根据订单类型查询数据
  1715. if (preg_match('/-(\d{2})$/', $order, $matches)) {
  1716. $number = (int)$matches[1];
  1717. if ($number > 70) {
  1718. // 船样订单查询
  1719. $data = \db('工单_基本资料')->alias('j')
  1720. ->distinct('y.子订单编号')
  1721. ->field($field)
  1722. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1723. ->where('y.船样',1)
  1724. ->whereNull('y.Mod_rq')
  1725. ->where('y.子订单编号', $order)
  1726. ->select();
  1727. } else {
  1728. // 普通两位数子订单
  1729. $ddbh = explode('-', $order)[0];//获取订单编号
  1730. $data = \db('工单_基本资料')->alias('j')
  1731. ->distinct('y.子订单编号')
  1732. ->field($field)
  1733. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1734. ->where('y.船样',0)->whereNull('y.Mod_rq')
  1735. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  1736. ->select();
  1737. }
  1738. } else {
  1739. // 其他子订单
  1740. $ddbh = explode('-', $order)[0];//获取订单编号
  1741. $data = \db('工单_基本资料')->alias('j')
  1742. ->distinct('y.子订单编号')
  1743. ->field($field)
  1744. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1745. ->where('y.船样',0)->whereNull('y.Mod_rq')
  1746. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  1747. ->select();
  1748. }
  1749. // 处理查询到的数据
  1750. $dataEntry = [];
  1751. $list = [];
  1752. $headers = [];//存放尺码表头
  1753. foreach ($data as $item) {
  1754. // 处理尺码与数量映射
  1755. $cmScslMapping = [];
  1756. for ($i = 1; $i <= 14; $i++) {
  1757. $cmValue = $item['cm' . $i];
  1758. $scslValue = $item['scsl' . $i];
  1759. if (!is_null($cmValue) && $cmValue !== 0) {
  1760. $cmScslMapping[$cmValue] = ($scslValue === 0 || is_null($scslValue)) ? '' : $scslValue;
  1761. if (!in_array($cmValue, $headers)) {
  1762. $headers[] = $cmValue;
  1763. }
  1764. }
  1765. }
  1766. // 构建表格数据
  1767. $orderData = [
  1768. '订单编号' => $item['订单编号'],
  1769. '子订单编号' => $item['子订单编号'],
  1770. '款号' => $item['款号'],
  1771. '款式' => $item['款式'],
  1772. '颜色' => $item['颜色备注'],
  1773. 'zdtotal' => $item['zdtotal'],
  1774. 'sctotal' => $item['sctotal'],
  1775. '单位' => $item['单位'],
  1776. 'Sys_id' => $item['Sys_id'],
  1777. 'Sys_rq' => $item['Sys_rq'],
  1778. ];
  1779. $orderData = array_merge($orderData, $cmScslMapping);
  1780. $dataEntry[] = $orderData;
  1781. // 构建列表数据
  1782. $entry = [];
  1783. for ($i = 1; $i <= 14; $i++) {
  1784. $entry['cm' . $i] = ($item['cm' . $i] === 0 || is_null($item['cm' . $i])) ? '' : $item['cm' . $i];
  1785. $entry['cmsl' . $i] = ($item['cmsl' . $i] === 0 || is_null($item['cmsl' . $i])) ? '' : $item['cmsl' . $i];
  1786. $entry['scsl' . $i] = ($item['scsl' . $i] === 0 || is_null($item['scsl' . $i])) ? '' : $item['scsl' . $i];
  1787. }
  1788. $entry['订单编号'] = $item['订单编号'];
  1789. $entry['子订单编号'] = $item['子订单编号'];
  1790. $entry['UniqId'] = $item['UniqId'];
  1791. $entry['zdtotal'] = $item['zdtotal'];
  1792. $entry['颜色'] = $item['颜色'];
  1793. $entry['款式'] = $item['款式'];
  1794. $entry['款号'] = $item['款号'];
  1795. $entry['color_id'] = $item['color_id'];
  1796. $entry['颜色备注'] = $item['颜色备注'];
  1797. $list[] = $entry;
  1798. }
  1799. // 获取匹配的订单数据
  1800. $order = explode(',', $order)[0];
  1801. $matchedOrder = null;
  1802. foreach ($list as $mergedOrder) {
  1803. if ($mergedOrder['子订单编号'] === $order) {
  1804. $matchedOrder = $mergedOrder;
  1805. break;
  1806. }
  1807. }
  1808. // 检查实裁数是否为空
  1809. $allScslEmpty = true;
  1810. if ($matchedOrder) {
  1811. for ($i = 1; $i <= 14; $i++) {
  1812. if (!empty($matchedOrder["scsl$i"])) {
  1813. $allScslEmpty = false;
  1814. break;
  1815. }
  1816. }
  1817. }
  1818. if ($allScslEmpty) {
  1819. // 当前子订单未裁剪 请裁剪后报工
  1820. $this->success('当前子订单未裁剪 请裁剪后报工', [
  1821. 'code' => 1,
  1822. 'serial' => '',
  1823. 'serial_num' => '',
  1824. 'headers' => '',
  1825. 'records' => '',
  1826. ]);
  1827. return;
  1828. }
  1829. // 查询产量计酬记录
  1830. $clwhere = [
  1831. 'sczl_jtbh' => $params['sys_sbID'],
  1832. '子订单编号' => $order,
  1833. 'mod_rq' => null,
  1834. '工序名称' => $params['code']
  1835. ];
  1836. $cljclist = \db('设备_产量计酬')
  1837. ->field('订单编号,子订单编号,尺码,数量,mod_rq')
  1838. ->where($clwhere)
  1839. ->select();
  1840. // 汇总产量记录
  1841. $result = [];
  1842. $totalQuantity = 0;
  1843. $orderNumber = '';
  1844. foreach ($cljclist as $item) {
  1845. $size = $item['尺码'];
  1846. $orderNumber = $item['订单编号'];
  1847. if (isset($result[$size])) {
  1848. $result[$size] += $item['数量'];
  1849. } else {
  1850. $result[$size] = $item['数量'];
  1851. }
  1852. $totalQuantity += $item['数量'];
  1853. }
  1854. $result['订单编号'] = $orderNumber;
  1855. foreach ($result as $size => $quantity) {
  1856. if ($size !== '订单编号' && $size !== '数量') {
  1857. $result[$size] = '合计(' . $quantity . ')';
  1858. }
  1859. }
  1860. $result['数量'] = '合计(' . $totalQuantity . ')';
  1861. // 查询现有工序记录
  1862. $where = [
  1863. 'c.子订单编号' => $order,
  1864. 'c.工序编号' => '7',
  1865. 'c.mod_rq' => null
  1866. ];
  1867. $existingRecords = \db('设备_产量计酬')->alias('c')
  1868. ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码,
  1869. CASE
  1870. WHEN c.工序名称 IN ("裁剪", "车缝") THEN c.数量
  1871. ELSE c.s_num
  1872. END as 数量,
  1873. c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num,c.UniqId,
  1874. y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注')
  1875. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1876. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1877. ->where($where)
  1878. ->order('c.UniqId desc')
  1879. ->group('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num, j.款式')
  1880. ->select();
  1881. // 处理现有记录
  1882. $sizeSummary = [];
  1883. $sizes = [];
  1884. foreach ($existingRecords as $record) {
  1885. $size = $record['尺码'];
  1886. $quantity = $record['数量'];
  1887. $serial = $record['serial'];
  1888. $serialNum = $record['serial_num'];
  1889. // 收集尺码
  1890. if (!in_array($size, $sizes)) {
  1891. $sizes[] = $size;
  1892. }
  1893. // 初始化serial_num分组
  1894. if (!isset($sizeSummary[$serialNum])) {
  1895. $sizeSummary[$serialNum] = [
  1896. '订单编号' => $record['订单编号'],
  1897. '子订单编号' => $record['子订单编号'],
  1898. '款号' => $record['款号'],
  1899. '款式' => $record['款式'],
  1900. '工序编号' => $record['工序编号'],
  1901. '工序名称' => $record['工序名称'],
  1902. '数量' => 0,
  1903. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1904. '颜色' => $record['颜色'],
  1905. '颜色备注' => $record['颜色备注'],
  1906. '机台号' => $record['sczl_jtbh'],
  1907. '组别' => $record['sczl_bh'],
  1908. 'serial' => [],
  1909. 'sys_rq' => $record['sys_rq'],
  1910. 'UniqId' => $record['UniqId'],
  1911. '尺码数据' => []
  1912. ];
  1913. }
  1914. // 添加流水号
  1915. $sizeSummary[$serialNum]['serial'][] = $serial;
  1916. $sizeSummary[$serialNum]['数量'] += $quantity;
  1917. // 记录尺码数据
  1918. if (!isset($sizeSummary[$serialNum]['尺码数据'][$size])) {
  1919. $sizeSummary[$serialNum]['尺码数据'][$size] = [];
  1920. }
  1921. $sizeSummary[$serialNum]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  1922. }
  1923. // 最终记录处理
  1924. $finalRecords = [];
  1925. foreach ($sizeSummary as &$summary) {
  1926. // 排序并合并流水号
  1927. sort($summary['serial'], SORT_NUMERIC);
  1928. $summary['serial'] = implode(',', $summary['serial']);
  1929. // 合并尺码数据
  1930. foreach ($summary['尺码数据'] as $size => $serials) {
  1931. $summary[$size] = implode(', ', $serials);
  1932. }
  1933. unset($summary['尺码数据']);
  1934. $finalRecords[] = $summary;
  1935. }
  1936. // 尺码排序
  1937. $sizeOrder = [
  1938. 'XXXS' => 1, 'XXS' => 2, 'XS' => 3, 'S' => 4, 'M' => 5,
  1939. 'L' => 6, 'XL' => 7, '2XL' => 8, 'XXL' => 9, '3XL' => 10,
  1940. 'XXXL' => 11, '4XL' => 12, 'XXXXL' => 13, '5XL' => 14, 'XXXXXL' => 15
  1941. ];
  1942. usort($sizes, function ($a, $b) use ($sizeOrder) {
  1943. if (is_numeric($a) && is_numeric($b)) return $a - $b;
  1944. if (is_numeric($a)) return -1;
  1945. if (is_numeric($b)) return 1;
  1946. return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  1947. });
  1948. // 添加汇总记录到最前面
  1949. array_unshift($finalRecords, $result);
  1950. // 查询流水号信息
  1951. $res_serial = \db('设备_产量计酬')
  1952. ->field('子订单编号, MAX(CAST(serial AS UNSIGNED)) as serial, MAX(CAST(serial_num AS UNSIGNED)) as serial_num')
  1953. ->where('子订单编号', $order)
  1954. ->whereNull('mod_rq')
  1955. ->where('sczl_jtbh', $params['sys_sbID'])
  1956. ->group('子订单编号')
  1957. ->find();
  1958. // 处理流水号
  1959. $res_serial['serial'] = empty($res_serial['serial']) ? '000' : str_pad($res_serial['serial'], 3, '0', STR_PAD_LEFT);
  1960. $res_serial['serial_num'] = empty($res_serial['serial_num']) ? '000' : str_pad($res_serial['serial_num'], 3, '0', STR_PAD_LEFT);
  1961. $next_serial = str_pad((int)$res_serial['serial'] + 1, 3, '0', STR_PAD_LEFT);
  1962. $next_serial_num = str_pad((int)$res_serial['serial_num'] + 1, 3, '0', STR_PAD_LEFT);
  1963. $this->success('请求成功', [
  1964. 'serial' => $next_serial,
  1965. 'serial_num' => $next_serial_num,
  1966. 'list' => $matchedOrder,
  1967. 'headers' => $sizes,
  1968. 'records' => $finalRecords
  1969. ]);
  1970. } else {
  1971. // 按照订单编号查询
  1972. $this->success('请求成功', [
  1973. 'serial' => '',
  1974. 'serial_num' => '',
  1975. 'list' => '',
  1976. 'headers' => '',
  1977. 'records' => ''
  1978. ]);
  1979. }
  1980. break;
  1981. default:
  1982. $this->error('无效的 code 参数');
  1983. break;
  1984. }
  1985. }
  1986. /**
  1987. * 出库报工日志
  1988. * 确认出库 恢复出库状态
  1989. */
  1990. public function getTab() {
  1991. if (!$this->request->isPost()) {
  1992. $this->error('非法请求');
  1993. }
  1994. $params = $this->request->param();
  1995. // 校验子订单编号和出库日期
  1996. if (empty($params['order_id'])) {
  1997. $this->error('子订单编号不能为空');
  1998. }
  1999. // 处理出库逻辑
  2000. if ($params['code'] == '出库') {
  2001. if (strpos($params['order_id'], '-') !== false) {
  2002. // 查询传入的子订单编号的记录,获取其颜色备注
  2003. $cklist = \db('工单_印件资料')
  2004. ->field('子订单编号, ck_rq, 颜色备注')
  2005. ->where('子订单编号', $params['order_id'])
  2006. ->find();
  2007. if ($cklist) {
  2008. if (!empty($cklist['ck_rq'])) {
  2009. $this->error('该子订单已完成出库操作,请勿重复操作。');
  2010. } else {
  2011. // 获取相同颜色备注的所有子订单记录
  2012. $sameColorList = \db('工单_印件资料')
  2013. ->field('子订单编号, ck_rq')
  2014. ->where('颜色备注', $cklist['颜色备注'])
  2015. ->whereNull('ck_rq') // 只选择未出库的记录
  2016. ->select();
  2017. // 开始出库操作
  2018. $updateCount1 = 0;
  2019. foreach ($sameColorList as $item) {
  2020. // 更新 '工单_印件资料' 表中所有相同颜色备注的子订单的出库日期
  2021. $result1 = \db('工单_印件资料')
  2022. ->where('子订单编号', $item['子订单编号'])
  2023. ->update(['ck_rq' => date("Y-m-d H:i:s")]);
  2024. if ($result1 !== false) {
  2025. $updateCount1 += $result1;
  2026. }
  2027. }
  2028. // 检查 '工单_基本资料' 表的 '出库日期' 是否为空
  2029. $order = explode('-', $params['order_id'])[0];
  2030. $basicInfo = \db('工单_基本资料')
  2031. ->field('出库日期')
  2032. ->where('订单编号', $order)
  2033. ->find();
  2034. $updateCount2 = 0;
  2035. if (empty($basicInfo['出库日期'])) {
  2036. // 如果 '出库日期' 为空,则更新
  2037. $jbzl = \db('工单_基本资料')
  2038. ->where('订单编号', $order)
  2039. ->fetchSql(true)
  2040. ->update(['出库日期' => date("Y-m-d H:i:s")]);
  2041. $result2 = \db()->query($jbzl);
  2042. if ($result2 !== false) {
  2043. $updateCount2 = $result2;
  2044. }
  2045. }
  2046. // if ($updateCount1 > 0) {
  2047. // // 插入日志
  2048. // foreach ($sameColorList as $item) {
  2049. // $addlist = [
  2050. // 'order_id' => $item['子订单编号'],
  2051. // 'rq' => date("Y-m-d H:i:s"),
  2052. // 'name' => $params['code'],
  2053. // 'sys_id' => $params['sys_id']
  2054. // ];
  2055. // \db('库存_出入库明细')->insertGetId($addlist);
  2056. // }
  2057. // $this->success("出库成功");
  2058. // } else {
  2059. // $this->error('出库失败');
  2060. // }
  2061. }
  2062. } else {
  2063. $this->error('未找到对应的出库订单');
  2064. }
  2065. } else {
  2066. $this->error('请扫描子订单编号进行出库');
  2067. }
  2068. }
  2069. // 处理恢复逻辑
  2070. else if ($params['code'] == '恢复') {
  2071. $cklist = \db('工单_印件资料')
  2072. ->field('子订单编号, ck_rq')
  2073. ->where('子订单编号', $params['order_id'])
  2074. ->find();
  2075. if (!empty($cklist['ck_rq'])) {
  2076. $result1 = \db('工单_印件资料')
  2077. ->where('子订单编号', $params['order_id'])
  2078. ->fetchSql(true)
  2079. ->update(['ck_rq' => null]);
  2080. $updateCount1 = \db()->query($result1);
  2081. // 更新 '工单_基本资料' 表
  2082. $order = explode('-', $params['order_id'])[0];
  2083. $result2 = \db('工单_基本资料')
  2084. ->where('订单编号', $order)
  2085. ->fetchSql(true)
  2086. ->update(['出库日期' => null,'gd_statu'=>'1-计划中']);
  2087. $updateCount2 = \db()->query($result2);
  2088. // if ($updateCount1 > 0 && $updateCount2 > 0) {
  2089. // // 插入日志
  2090. // $addlist = [
  2091. // 'order_id' => $params['order_id'],
  2092. // 'rq' => date("Y-m-d H:i:s"),
  2093. // 'name' => $params['code'],
  2094. // 'sys_id' => $params['sys_id']
  2095. // ];
  2096. // \db('库存_出入库明细')->insertGetId($addlist);
  2097. // $this->success($params['order_id'] . "恢复未出库");
  2098. // } else {
  2099. // $this->error($params['order_id'] . '该子订单是未出库状态,无需再次恢复');
  2100. // }
  2101. } else {
  2102. $this->error('该子订单是未出库状态,无需再次恢复');
  2103. }
  2104. }
  2105. }
  2106. /**
  2107. * 裁剪完工报工修改工单_印件资料表一条数据
  2108. */
  2109. public function getInfo(){
  2110. if (!$this->request->isPost()) {
  2111. $this->error('非法请求');
  2112. }
  2113. $params = $this->request->param();
  2114. // 检查子订单编号和日期是否都存在
  2115. if (!isset($params['order_id']) || empty($params['order_id'])) {
  2116. $this->error('参数错误,子订单编号不能为空');
  2117. }
  2118. if (!isset($params['rq']) || empty($params['rq'])) {
  2119. $this->error('参数错误,日期不能为空');
  2120. }
  2121. function setIfNotEmpty($value) {
  2122. return (!empty($value) && $value != 0) ? $value : '';
  2123. }
  2124. // 先查询当前记录的原始数据
  2125. $existingData = \db('工单_印件资料')
  2126. ->where('子订单编号', $params['order_id'])
  2127. // ->where(function($query) {
  2128. // $query->whereNull('Mod_rq')->whereOr('Mod_rq', '');
  2129. // }) // 查询未删除数据
  2130. // ->order('子订单编号 desc')
  2131. ->find();
  2132. if ($existingData) {
  2133. // 计算新的累加值
  2134. $updateData = [
  2135. 'sc_rq' => date('Y-m-d H:i:s'),//生产报工日期
  2136. 'scsl1' => isset($params['scsl1']) && is_numeric($params['scsl1']) ? $existingData['scsl1'] + $params['scsl1'] : $existingData['scsl1'] + 0,
  2137. 'scsl2' => isset($params['scsl2']) && is_numeric($params['scsl2']) ? $existingData['scsl2'] + $params['scsl2'] : $existingData['scsl2'] + 0,
  2138. 'scsl3' => isset($params['scsl3']) && is_numeric($params['scsl3']) ? $existingData['scsl3'] + $params['scsl3'] : $existingData['scsl3'] + 0,
  2139. 'scsl4' => isset($params['scsl4']) && is_numeric($params['scsl4']) ? $existingData['scsl4'] + $params['scsl4'] : $existingData['scsl4'] + 0,
  2140. 'scsl5' => isset($params['scsl5']) && is_numeric($params['scsl5']) ? $existingData['scsl5'] + $params['scsl5'] : $existingData['scsl5'] + 0,
  2141. 'scsl6' => isset($params['scsl6']) && is_numeric($params['scsl6']) ? $existingData['scsl6'] + $params['scsl6'] : $existingData['scsl6'] + 0,
  2142. 'scsl7' => isset($params['scsl7']) && is_numeric($params['scsl7']) ? $existingData['scsl7'] + $params['scsl7'] : $existingData['scsl7'] + 0,
  2143. 'scsl8' => isset($params['scsl8']) && is_numeric($params['scsl8']) ? $existingData['scsl8'] + $params['scsl8'] : $existingData['scsl8'] + 0,
  2144. 'scsl9' => isset($params['scsl9']) && is_numeric($params['scsl9']) ? $existingData['scsl9'] + $params['scsl9'] : $existingData['scsl9'] + 0,
  2145. 'scsl10' => isset($params['scsl10']) && is_numeric($params['scsl10']) ? $existingData['scsl10'] + $params['scsl10'] : $existingData['scsl10'] + 0,
  2146. 'scsl11' => isset($params['scsl11']) && is_numeric($params['scsl11']) ? $existingData['scsl11'] + $params['scsl11'] : $existingData['scsl11'] + 0,
  2147. 'scsl12' => isset($params['scsl12']) && is_numeric($params['scsl12']) ? $existingData['scsl12'] + $params['scsl12'] : $existingData['scsl12'] + 0,
  2148. 'scsl13' => isset($params['scsl13']) && is_numeric($params['scsl13']) ? $existingData['scsl13'] + $params['scsl13'] : $existingData['scsl13'] + 0,
  2149. 'scsl14' => isset($params['scsl14']) && is_numeric($params['scsl14']) ? $existingData['scsl14'] + $params['scsl14'] : $existingData['scsl14'] + 0,
  2150. 'sctotal' => isset($params['sctotal']) && is_numeric($params['sctotal']) ? $existingData['sctotal'] + $params['sctotal'] : $existingData['sctotal'] + 0,
  2151. // 你可以在这里继续添加其他字段
  2152. ];
  2153. $updateCount = \db('工单_印件资料')
  2154. ->where('子订单编号', $params['order_id'])
  2155. ->fetchSql(true)
  2156. // ->where(function($query) {
  2157. // $query->whereNull('Mod_rq')->whereOr('Mod_rq', '');
  2158. // }) // 查询未删除数据
  2159. ->update($updateData);
  2160. $list_gd = \db()->query($updateCount);
  2161. } else {
  2162. echo "没有找到相关记录";
  2163. }
  2164. // 检查更新结果
  2165. if ($list_gd > 0) {
  2166. $this->success("更新成功");
  2167. } else {
  2168. // 查询数据库,是否能找到对应的子订单编号
  2169. $existingRecord = \db('工单_印件资料')
  2170. ->where('子订单编号', $params['order_id'])
  2171. // ->where('UniqId', $params['UniqId'])
  2172. ->find();
  2173. if ($existingRecord) {
  2174. $this->error('更新失败,可能是提供的更新数据与现有数据相同,未进行任何更改');
  2175. } else {
  2176. $this->error('更新失败,未找到对应的子订单编号');
  2177. }
  2178. }
  2179. }
  2180. /**
  2181. * 手工、大烫、总检、包装完工报工【通用的报工操作函数】
  2182. */
  2183. function reportWork($params) {
  2184. $list = [];
  2185. // 查询设备_产量计酬表的数据
  2186. $bglist = \db('设备_产量计酬')->field('订单编号,子订单编号,数量,ci_num,款号,sys_rq,serial,serial_num')
  2187. ->where('UniqId', $params['UniqId'])->fetchSql(true)->find();
  2188. $list_gd = \db()->query($bglist);
  2189. if (empty($list_gd)) {
  2190. $this->error('报工记录不存在');
  2191. }
  2192. // 查询工单基本资料表的数据
  2193. $orderlist = \db('工单_基本资料')->where('订单编号', $list_gd[0]['订单编号'])->whereNull('Mod_rq')->find();
  2194. // 检查code是否为 "包装"
  2195. if ($params['code'] == '包装') {
  2196. // 如果是 "包装",则增加工单基本资料表中的入仓数量
  2197. $addorder['工单入仓数量'] = $orderlist['工单入仓数量'] + $params['sl'];
  2198. $result = \db('工单_基本资料')->where('订单编号', $list_gd[0]['订单编号'])->whereNull('Mod_rq')->fetchSql(true)->update($addorder);
  2199. $sql = \db()->query($result);
  2200. }
  2201. // 构建单条记录的函数,传递 $list_gd 作为参数
  2202. function buildRecords($item, $list_gd) {
  2203. return [
  2204. '订单编号' => $item['order'],
  2205. '子订单编号' => $item['order_id'],
  2206. '款号' => $list_gd[0]['款号'],
  2207. 'serial' => $list_gd[0]['serial'],
  2208. 'serial_num' => $list_gd[0]['serial_num'],
  2209. '工序编号' => $item['gx'],
  2210. '工序名称' => $item['gxmc'],
  2211. '尺码' => $item['cm'],
  2212. '数量' => $item['sl'],
  2213. 'sczl_jtbh' => $item['sczl_jtbh'],
  2214. 'sczl_bh' => $item['sczl_bh'],
  2215. '尾包' => $item['wb'],
  2216. 'sys_id' => $item['sys_id'],
  2217. 'sys_rq' => date('Y-m-d H:i:s'),
  2218. 'sczl_rq' => date('Y-m-d'),
  2219. 'ci_num' => $item['ci_num'] - $item['s_num'],
  2220. 's_num' => $item['s_num'],
  2221. ];
  2222. }
  2223. // 检查是否是索引数组(多条记录)
  2224. if (isset($params[0]) && is_array($params[0])) {
  2225. foreach ($params as $item) {
  2226. $list[] = buildRecords($item, $list_gd);
  2227. }
  2228. } else {
  2229. // 处理单条记录
  2230. $list[] = buildRecords($params, $list_gd);
  2231. }
  2232. // 插入数据到设备_产量计酬表
  2233. $result = \db('设备_产量计酬')->insertAll($list);
  2234. if ($result) {
  2235. $this->success('数据插入成功 : ' . date('H:i:s'));
  2236. } else {
  2237. $this->error('数据插入失败');
  2238. }
  2239. }
  2240. /**
  2241. * 报工:裁剪、车缝、手工、大烫、总检、包装
  2242. */
  2243. public function search(){
  2244. if (!$this->request->isPost()) {
  2245. $this->error('非法请求');
  2246. }
  2247. $params = $this->request->param();
  2248. if($params['order_id'] == ''){
  2249. $this->error('请扫描订单后再报工');
  2250. }
  2251. // 根据不同的 code 值执行不同的逻辑
  2252. switch ($params['code']) {
  2253. case '裁剪':
  2254. $order_id = explode('-', $params['order_id']);
  2255. $order = $order_id[0];
  2256. //先查询这条订单是否存在
  2257. $sql = \db('工单_基本资料')->where('订单编号', $order)->whereNull('Mod_rq')->fetchSql(true)->find();
  2258. $order_list = \db()->query($sql);
  2259. if (empty($order_list)) {
  2260. $this->error('没有找到对应的订单信息');
  2261. return;
  2262. }
  2263. if($order_list[0]['gd_statu'] == '1-计划中' || $order_list[0]['gd_statu'] == '生产中' || $order_list[0]['gd_statu'] == '2-生产中'){
  2264. DB::name('工单_基本资料')
  2265. ->where('订单编号', $order)
  2266. ->update(['gd_statu' => '已裁剪']);
  2267. }
  2268. //将前端传过来的数据存到数据库
  2269. $insertData = [];
  2270. $dataList = isset($params[0]) && is_array($params[0]) ? $params : [$params];
  2271. foreach ($dataList as $item) {
  2272. $list = [
  2273. '订单编号' => $order_list[0]['订单编号'],
  2274. '款号' => $order_list[0]['生产款号'],
  2275. '子订单编号' => $item['order_id'],
  2276. '工序编号' => '2',
  2277. '工序名称' => '裁剪',
  2278. '尺码' => $item['cm'],
  2279. '数量' => $item['sl'],
  2280. 'sczl_jtbh' => $item['sczl_jtbh'],
  2281. 'sys_id' => $item['sys_id'],
  2282. 'sczl_bh' => $item['sczl_bh'],
  2283. 'sczl_rq' => date('Y-m-d 00:00:00'),
  2284. 'sys_rq' => date('Y-m-d H:i:s')
  2285. ];
  2286. $insertData[] = $list;
  2287. }
  2288. $result = \db('设备_产量计酬')->insertAll($insertData);
  2289. $result ? $this->success('数据插入成功 : ' . date('H:i:s')) : $this->error('数据插入失败');
  2290. break;
  2291. case '车缝':
  2292. $list = [];
  2293. $order_id = explode('-', $params['order_id']);
  2294. $order = $order_id[0];
  2295. $sql = \db('工单_基本资料')->where('订单编号', $order)->whereNull('Mod_rq')->fetchSql(true)->find();
  2296. $list_gd = \db()->query($sql); // 查询的结果可能是多条记录,确保是数组
  2297. if (empty($list_gd)) {
  2298. $this->error('没有找到对应的订单信息');
  2299. }
  2300. if($list_gd[0]['gd_statu'] == '1-计划中' || $list_gd[0]['gd_statu'] == '已裁剪' || $list_gd[0]['gd_statu'] == '2-生产中'){
  2301. DB::name('工单_基本资料')
  2302. ->where('订单编号', $order)
  2303. ->update(['gd_statu' => '已车缝']);
  2304. }
  2305. // 查询子订单编号对应的最大 serial 值
  2306. $list_order_id = \db('设备_产量计酬')
  2307. ->field('子订单编号, MAX(serial) as serial')
  2308. ->where('子订单编号', $params['order_id'])
  2309. ->where(function($query) {
  2310. $query->whereNull('mod_rq')->whereOr('mod_rq', '');
  2311. }) // 查询未删除数据
  2312. ->group('子订单编号')
  2313. ->find();
  2314. function buildRecord($item, $list_gd, $list_order_id) {
  2315. return [
  2316. '订单编号' => $list_gd[0]['订单编号'],
  2317. '款号' => $list_gd[0]['生产款号'],
  2318. '子订单编号' => $item['order_id'],
  2319. '工序编号' => 3,
  2320. '工序名称' => '车缝',
  2321. 'serial' => $item['serial'],
  2322. 'ci_num' => (int)$item['sl'],
  2323. 'serial_num' => $item['serial_num'],
  2324. '尺码' => $item['cm'],
  2325. '数量' => (int)$item['sl'],
  2326. 'sczl_jtbh' => $item['sczl_jtbh'],
  2327. 'sys_rq' => date('Y-m-d H:i:s'),
  2328. 'sczl_rq' => date('Y-m-d'),
  2329. '尾包' => $item['wb'],
  2330. 'sys_id' => $item['sys_id'],
  2331. 'sczl_bh' => $item['sczl_bh']
  2332. ];
  2333. }
  2334. if (isset($params[0]) && is_array($params[0])) {
  2335. foreach ($params as $item) {
  2336. $list[] = buildRecord($item, $list_gd, $list_order_id); // 所有记录使用相同的 serial
  2337. }
  2338. } else {
  2339. // 处理单条记录
  2340. $list[] = buildRecord($params, $list_gd, $list_order_id); // 单条记录
  2341. }
  2342. $result = \db('设备_产量计酬')->insertAll($list);
  2343. if ($result) {
  2344. $this->success('数据插入成功 : ' . date('H:i:s'));
  2345. } else {
  2346. $this->error('数据插入失败');
  2347. }
  2348. break;
  2349. case '手工':
  2350. $this->reportWork($params);
  2351. break;
  2352. case '大烫':
  2353. $this->reportWork($params);
  2354. break;
  2355. case '总检':
  2356. $order_id_parts = explode('-', $params['order_id']);
  2357. $order = $order_id_parts[0];
  2358. //查询订单信息
  2359. $list_gd = Db::name('工单_基本资料')
  2360. ->where('订单编号', $order)
  2361. ->whereNull('Mod_rq')
  2362. ->find();
  2363. if (!$list_gd) {
  2364. $this->error('订单不存在');
  2365. }
  2366. $insert_cp_list = [];
  2367. $insert_cj_list = [];
  2368. $isBatch = isset($params[0]) && is_array($params[0]);
  2369. $dataList = $isBatch ? $params : [$params];
  2370. foreach ($dataList as $item) {
  2371. // ---------------- 入仓数据 ----------------
  2372. $insert_cp_list[] = [
  2373. 'order' => $list_gd['订单编号'],
  2374. 'customer' => $list_gd['客户编号'],
  2375. 'order_id' => $item['order_id'],
  2376. 'sl' => (int)$item['sl'],
  2377. 'cm' => $item['cm'],
  2378. 'wb' => $item['wb'],
  2379. 'sys_id' => $item['sys_id'],
  2380. 'sys_rq' => date('Y-m-d H:i:s')
  2381. ];
  2382. // ---------------- 产量计酬数据 ----------------
  2383. $insert_cj_list[] = [
  2384. '订单编号' => $list_gd['订单编号'],
  2385. '款号' => $list_gd['生产款号'],
  2386. '子订单编号' => $item['order_id'],
  2387. '工序编号' => 6,
  2388. '工序名称' => '总检',
  2389. 'serial' => $item['serial'],
  2390. 'ci_num' => (int)$item['sl'],
  2391. 'serial_num' => $item['serial_num'],
  2392. '尺码' => $item['cm'],
  2393. '数量' => (int)$item['sl'],
  2394. 's_num' => (int)$item['sl'],
  2395. 'sczl_jtbh' => $item['sczl_jtbh'],
  2396. 'sys_rq' => date('Y-m-d H:i:s'),
  2397. 'sczl_rq' => date('Y-m-d'),
  2398. '尾包' => $item['wb'],
  2399. 'sys_id' => $item['sys_id'],
  2400. 'sczl_bh' => $item['sczl_bh']
  2401. ];
  2402. }
  2403. $res1 = \db('成品入仓')->insertAll($insert_cp_list);
  2404. $res2 = \db('设备_产量计酬')->insertAll($insert_cj_list);
  2405. $orderNo = $list_gd['订单编号'];
  2406. // 累计入仓(所有时间)
  2407. $cumulativeData = Db::name('成品入仓')
  2408. ->alias('a')
  2409. ->field('a.order as 订单编号, SUM(a.sl) as 累计入仓数量')
  2410. ->where('a.order', $orderNo)
  2411. ->whereNull('a.mod_rq')
  2412. ->group('a.order')
  2413. ->select();
  2414. $cumulativeMap = [];
  2415. foreach ($cumulativeData as $row) {
  2416. $cumulativeMap[$row['订单编号']] = (int)$row['累计入仓数量'];
  2417. }
  2418. // 裁剪产量(所有时间)
  2419. $prodRecords = Db::name('设备_产量计酬')
  2420. ->field('订单编号, SUM(数量) as 裁剪数量')
  2421. ->where('订单编号', $orderNo)
  2422. ->where('工序名称', '裁剪')
  2423. ->group('订单编号')
  2424. ->select();
  2425. $cutMap = [];
  2426. foreach ($prodRecords as $row) {
  2427. $cutMap[$row['订单编号']] = (int)$row['裁剪数量'];
  2428. }
  2429. $cutQty = $cutMap[$orderNo] ?? 0;
  2430. $cumulativeQty = $cumulativeMap[$orderNo] ?? 0;
  2431. $passRate = $cutQty > 0 ? round($cumulativeQty / $cutQty * 100, 2) : 0;
  2432. $passRateDisplay = $cutQty > 0 ? $passRate . '%' : '0%';
  2433. // 根据合格率决定显示状态
  2434. if($passRate > 95) {
  2435. $status = '已完工';
  2436. } else {
  2437. $status = '已总检' . $passRateDisplay;
  2438. }
  2439. if (in_array($list_gd['gd_statu'], ['1-计划中','已车缝', '2-生产中']) || strpos($list_gd['gd_statu'], '已总检') !== false) {
  2440. DB::name('工单_基本资料')
  2441. ->where('订单编号', $order)
  2442. ->update(['gd_statu' => $status]);
  2443. }
  2444. if ($res1 && $res2) {
  2445. $this->success('数据报工成功 : ' . date('H:i:s'));
  2446. } else {
  2447. $this->error('数据插入失败');
  2448. }
  2449. break;
  2450. case '包装':
  2451. $order_id_parts = explode('-', $params['order_id']);
  2452. $order = $order_id_parts[0];
  2453. $list_gd = \db('工单_基本资料')->where('订单编号', $order)
  2454. ->whereNull('Mod_rq')
  2455. ->find();
  2456. $insert_cj_list = [];
  2457. $isBatch = isset($params[0]) && is_array($params[0]);
  2458. $dataList = $isBatch ? $params : [$params];
  2459. foreach ($dataList as $item) {
  2460. // ---------------- 产量计酬数据 ----------------
  2461. $insert_cj_list[] = [
  2462. '订单编号' => $list_gd['订单编号'],
  2463. '款号' => $list_gd['生产款号'],
  2464. '子订单编号' => $item['order_id'],
  2465. '工序编号' => 7,
  2466. '工序名称' => '包装',
  2467. 'serial' => $item['serial'],
  2468. 'ci_num' => (int)$item['sl'],
  2469. 'serial_num' => $item['serial_num'],
  2470. '尺码' => $item['cm'],
  2471. '数量' => (int)$item['sl'],
  2472. 's_num' => (int)$item['sl'],
  2473. 'sczl_jtbh' => $item['sczl_jtbh'],
  2474. 'sys_rq' => date('Y-m-d H:i:s'),
  2475. 'sczl_rq' => date('Y-m-d'),
  2476. '尾包' => $item['wb'],
  2477. 'sys_id' => $item['sys_id'],
  2478. 'sczl_bh' => $item['sczl_bh']
  2479. ];
  2480. }
  2481. $res2 = \db('设备_产量计酬')->insertAll($insert_cj_list);
  2482. if ($res2) {
  2483. $this->success('数据报工成功 : ' . date('H:i:s'));
  2484. } else {
  2485. $this->error('数据插入失败');
  2486. }
  2487. break;
  2488. default:
  2489. $this->error('无效的 code 参数');
  2490. break;
  2491. }
  2492. }
  2493. /**
  2494. * 物理地址获取机台编号
  2495. * /work_order_spot_check/getMachineMac
  2496. */
  2497. public function getMachineMac(){
  2498. if ($this->request->isGet() == false){$this->error('非法请求');}
  2499. $params = $this->request->get();
  2500. if (empty($params['sys_sbID'])){$this->error('MAC地址参数不能为空');}
  2501. $data = \db('设备_基本资料')->where('sys_sbID', $params['sys_sbID'])
  2502. ->field('设备编号 as 机台号,生产工序,工序,rtrim(设备编组) as 组别,组长,sys_sbID')
  2503. // ->where('生产工序',$params['code'])
  2504. ->find();
  2505. $this->success('请求成功', $data);
  2506. }
  2507. /**
  2508. * 删除报工记录
  2509. * /work_order_spot_check/del
  2510. */
  2511. public function del() {
  2512. // 判断请求方式是否为 GET
  2513. if (!$this->request->isGet()) {
  2514. return $this->error('请求错误');
  2515. }
  2516. // 获取请求参数
  2517. $param = $this->request->param();
  2518. // 检查 UniqId 参数是否存在
  2519. if (empty($param['UniqId'])) {
  2520. return $this->error('UniqId 参数错误');
  2521. }
  2522. // 将 UniqId 的值按逗号分割成数组
  2523. $printIdArray = explode(',', $param['UniqId']);
  2524. // 初始化失败计数器
  2525. $failureCount = 0;
  2526. $logList = [];
  2527. $operTime = date('Y-m-d H:i:s');
  2528. $operUser = isset($param['sys_id']) ? $param['sys_id'] : '';
  2529. // 开启事务
  2530. \db()->startTrans();
  2531. try {
  2532. foreach ($printIdArray as $uniqId) {
  2533. $uniqId = trim($uniqId);
  2534. if ($uniqId === '') {
  2535. $failureCount++;
  2536. continue;
  2537. }
  2538. // 根据 UniqId 查询未删除的产量报工记录
  2539. $result = \db('设备_产量计酬')->where('UniqId', $uniqId)
  2540. ->whereNull('mod_rq')
  2541. ->find();
  2542. // 如果查询不到数据,跳过当前处理
  2543. if (!$result) {
  2544. $failureCount++;
  2545. continue;
  2546. }
  2547. $processName = $result['工序名称'] ?? '';
  2548. $isCutOrSew = in_array($processName, ['裁剪', '车缝'], true);
  2549. $displayQty = $isCutOrSew ? $result['数量'] : ($result['s_num'] ?? $result['数量']);
  2550. // 裁剪报工删除时回退印件实裁数(按工序名称判断,印件不存在也不阻断删除)
  2551. if ($processName === '裁剪') {
  2552. $this->syncCuttingPrintPartQty(
  2553. $result['子订单编号'],
  2554. $result['尺码'],
  2555. -floatval($result['数量'])
  2556. );
  2557. }
  2558. // 软删除产量报工记录
  2559. $updateResult = \db('设备_产量计酬')
  2560. ->where('UniqId', $uniqId)
  2561. ->whereNull('mod_rq')
  2562. ->update([
  2563. 'mod_rq' => $operTime,
  2564. 'delsys_id' => $operUser,
  2565. ]);
  2566. // 如果更新失败,记录失败
  2567. if ($updateResult === false || $updateResult === 0) {
  2568. $failureCount++;
  2569. continue;
  2570. }
  2571. $logList[] = $this->buildProductionReportLogRow([
  2572. 'uniq_id' => $uniqId,
  2573. 'oper_type' => '删除产量报工',
  2574. 'oper_user' => $operUser,
  2575. 'oper_time' => $operTime,
  2576. 'work_order' => $result['订单编号'] ?? '',
  2577. 'sub_order_no' => $result['子订单编号'] ?? '',
  2578. 'process_name' => $processName,
  2579. 'process_code' => $result['工序编号'] ?? '',
  2580. 'size' => $result['尺码'] ?? '',
  2581. 'machine' => $result['sczl_jtbh'] ?? '',
  2582. 'team' => $result['sczl_bh'] ?? '',
  2583. 'report_date' => !empty($result['sczl_rq']) ? date('Y-m-d', strtotime($result['sczl_rq'])) : null,
  2584. ], '数量', $displayQty, '');
  2585. }
  2586. if (!empty($logList)) {
  2587. $this->writeProductionReportLogs($logList);
  2588. }
  2589. // 提交事务
  2590. \db()->commit();
  2591. } catch (\think\exception\HttpResponseException $e) {
  2592. throw $e;
  2593. } catch (\Exception $e) {
  2594. // 发生异常时回滚事务
  2595. \db()->rollback();
  2596. return $this->error('操作失败:' . $e->getMessage());
  2597. }
  2598. // 返回操作结果
  2599. if ($failureCount === 0) {
  2600. return $this->success('删除成功');
  2601. } else {
  2602. return $this->error("操作完成,但有 {$failureCount} 条记录更新失败");
  2603. }
  2604. }
  2605. /**
  2606. * 面料出库报工
  2607. */
  2608. public function outReport()
  2609. {
  2610. if ($this->request->isPost() === false){
  2611. $this->error('请求错误');
  2612. }
  2613. $param = Request::instance()->post();
  2614. if (empty($param)){
  2615. $this->error('参数错误');
  2616. }
  2617. foreach ($param as $item) {
  2618. // 更新库存数据
  2619. $Inventory = \db('物料_库存')
  2620. ->where('批次号',$item['批次号'])
  2621. ->field('库存数量,领用数量')
  2622. ->find();
  2623. $InventoryData = [
  2624. '批次号' => $item['批次号'],
  2625. '关联号' => $item['关联编号'],
  2626. '物料编号' => $item['物料编码'],
  2627. '物料名称' => $item['物料名称'],
  2628. '领用数量' => (float)$Inventory['领用数量'] + (float)$item['number'],
  2629. '库存数量' => (float)$Inventory['库存数量'] - (float)$item['number'],
  2630. '实际门幅' => $item['实际门幅'],
  2631. '单位' => $item['单位'],
  2632. '状态' => 1,
  2633. 'departname' => $item['departname'],
  2634. 'remark' => isset($item['remark']) ? $item['remark'] : '',
  2635. 'sys_id' => $item['sys_id'],
  2636. 'sys_rq' => date('Y-m-d H:i:s'),
  2637. ];
  2638. $sql = \db('物料_库存')->where('批次号',$item['批次号'])->fetchSql(true)->update($InventoryData);
  2639. \db()->query($sql);
  2640. // 记录入库报工日志
  2641. $ReportData = [
  2642. '批次号' => $item['批次号'],
  2643. 'order_id' => $item['order_id'],
  2644. '款号' => $item['款号'],
  2645. '物料编码' => $item['物料编码'],
  2646. '物料名称' => $item['物料名称'],
  2647. '库存数量' => $item['number'],
  2648. 'number' => $item['number'],
  2649. 'rq' => $item['rq'],
  2650. 'name' => $item['name'],
  2651. 'sys_id' => $item['sys_id'],
  2652. 'receipt_number' => $item['receipt_number'],
  2653. '客户编号' => $item['客户编号'],
  2654. 'departname' => $item['departname'],
  2655. 'remark' => isset($item['remark']) ? $item['remark'] : '',
  2656. '款式' => $item['款式'],
  2657. 'sys_rq' => date('Y-m-d H:i:s'),
  2658. ];
  2659. $sql = \db('库存_出入库明细')->fetchSql(true)->insert($ReportData);
  2660. \db()->query($sql);
  2661. }
  2662. $this->success('成功');
  2663. }
  2664. /**
  2665. * 入库订单信息模糊查询
  2666. */
  2667. public function InputOrderSearch()
  2668. {
  2669. if ($this->request->isGet() === false){
  2670. $this->error('请求错误');
  2671. }
  2672. $param = $this->request->param();
  2673. if (empty($param)){
  2674. $this->error('参数错误');
  2675. }
  2676. $list = \db('工单_基本资料')->alias('a')
  2677. ->field('
  2678. a.订单编号,
  2679. a.生产款号,
  2680. a.客户编号,
  2681. a.款式,
  2682. a.订单数量,
  2683. a.落货日期,
  2684. a.接单日期,
  2685. a.Sys_id as 创建人员,
  2686. a.Sys_rq as 创建时间,
  2687. b.BOM_颜色 as 颜色,
  2688. b.BOM_物料编码 as 物料编码,
  2689. b.BOM_物料名称 as 物料名称,
  2690. GROUP_CONCAT(DISTINCT c.订单编号 SEPARATOR ",") AS 关联订单,
  2691. c.关联编号')
  2692. ->group('物料编码')
  2693. ->join('工单_面料资料 b','a.订单编号 = b.BOM_工单编号')
  2694. ->join('工单关联表 c','a.订单编号 = c.订单编号 AND b.BOM_物料编码 = c.物料编号')
  2695. ->where('a.生产款号|a.订单编号|b.BOM_物料编码','like','%'.$param['search'].'%')
  2696. ->where('a.Mod_rq',null)
  2697. ->where('b.Mod_rq',null)
  2698. ->where('c.Mod_rq',null)
  2699. ->order('a.订单编号 desc')
  2700. ->select();
  2701. if (empty($list)){
  2702. $res = \db('工单_基本资料')->alias('a')
  2703. ->join('工单_印件资料 b','a.订单编号 = b.订单编号')
  2704. ->join('工单_bom资料 c','a.订单编号 = c.BOM_工单编号')
  2705. ->where('b.订单编号','like','%'.$param['search'].'%')
  2706. ->field('a.订单编号,a.生产款号,b.颜色备注,c.BOM_物料名称,a.Sys_id')
  2707. ->where('a.Mod_rq',null)
  2708. ->where('b.Mod_rq',null)
  2709. ->where('c.Mod_rq',null)
  2710. ->group('c.BOM_物料名称')
  2711. ->select();
  2712. foreach ($res as $index => &$item) {
  2713. $item['物料编码'] = $item['生产款号'] . '-' . $item['颜色备注'] . ($index + 1);
  2714. }
  2715. unset($item);
  2716. $arr = \db('工单关联表')
  2717. ->orderRaw("CAST(SUBSTRING(关联编号, 5) AS UNSIGNED) DESC")
  2718. ->find();
  2719. // 2. 提取数字部分并加一
  2720. $num = intval(substr($arr['关联编号'], 4)) + 1;
  2721. // 3. 重新拼接新的编号(保留 GDGL 前缀)
  2722. $newCode = 'GDGL' . $num;
  2723. // 3. 定义当前日期
  2724. $today = date('Y-m-d');
  2725. // 4. 循环插入到两个表
  2726. foreach ($res as $row) {
  2727. // 插入到 工单_面料资料
  2728. $sql = \db('工单_面料资料')->fetchSql(true)->insert([
  2729. 'BOM_工单编号' => $row['订单编号'],
  2730. 'BOM_颜色' => $row['颜色备注'],
  2731. 'BOM_物料编码' => $row['物料编码'],
  2732. 'BOM_物料名称' => $row['BOM_物料名称'],
  2733. 'Sys_ID' => $row['Sys_id'],
  2734. 'Sys_rq' => $today,
  2735. ]);
  2736. \db()->query($sql);
  2737. // 插入到 工单关联表
  2738. $sqll = \db('工单关联表')->fetchSql(true)->insert([
  2739. '关联编号' => $newCode,
  2740. '订单编号' => $row['订单编号'],
  2741. '生产款号' => $row['生产款号'],
  2742. '颜色' => $row['颜色备注'],
  2743. '物料编号' => $row['物料编码'],
  2744. '物料名称' => $row['BOM_物料名称'],
  2745. 'Sys_id' => $row['Sys_id'],
  2746. 'Sys_rq' => $today,
  2747. ]);
  2748. \db()->query($sqll);
  2749. }
  2750. $list = \db('工单_基本资料')
  2751. ->alias('a')
  2752. ->join('工单_面料资料 b','a.订单编号 = b.BOM_工单编号')
  2753. ->join('工单关联表 c','a.订单编号 = c.订单编号 AND b.BOM_物料编码 = c.物料编号')
  2754. ->where('a.生产款号|a.订单编号|b.BOM_物料编码','like','%'.$param['search'].'%')
  2755. ->where('a.Mod_rq',null)
  2756. ->where('b.Mod_rq',null)
  2757. ->where('c.Mod_rq',null)
  2758. ->field('a.订单编号,a.生产款号,a.客户编号,a.款式,a.订单数量,a.落货日期,a.接单日期,a.Sys_id as 创建人员,
  2759. a.Sys_rq as 创建时间,b.BOM_颜色 as 颜色,b.BOM_物料编码 as 物料编码,b.BOM_物料名称 as 物料名称,GROUP_CONCAT(DISTINCT c.订单编号 SEPARATOR ",") AS 关联订单,c.关联编号')
  2760. ->group('物料编码')
  2761. ->order('a.订单编号 desc')
  2762. ->select();
  2763. $this->success('成功',$list);
  2764. }else{
  2765. if(count($list) > 800){
  2766. $this->success('成功','');
  2767. }else{
  2768. $this->success('成功',$list);
  2769. }
  2770. // $this->success('成功',$list);
  2771. }
  2772. }
  2773. /**
  2774. * 面料入库报工
  2775. */
  2776. public function inputStash()
  2777. {
  2778. if (!$this->request->isPost()) {
  2779. $this->error('请求错误');
  2780. }
  2781. $param = $this->request->post();
  2782. if (empty($param)) {
  2783. $input = file_get_contents('php://input');
  2784. $decoded = json_decode($input, true);
  2785. if (is_array($decoded)) {
  2786. $param = $decoded;
  2787. }
  2788. }
  2789. if (empty($param)) {
  2790. $this->error('参数错误');
  2791. }
  2792. // 单条对象转为列表
  2793. if (!isset($param[0]) || !is_array($param[0])) {
  2794. if (isset($param['物料编码']) || isset($param['order_id'])) {
  2795. $param = [$param];
  2796. }
  2797. }
  2798. \db()->startTrans();
  2799. try {
  2800. $sysRq = date('Y-m-d H:i:s');
  2801. $lastBatch = \db('物料_库存')
  2802. ->where('批次号', 'like', 'PCH%')
  2803. ->orderRaw('CAST(SUBSTRING(批次号, 4) AS UNSIGNED) DESC')
  2804. ->value('批次号');
  2805. $currentNumber = 0;
  2806. if (!empty($lastBatch) && preg_match('/^PCH(\d+)$/i', $lastBatch, $match)) {
  2807. $currentNumber = intval($match[1]);
  2808. }
  2809. $processed = 0;
  2810. foreach ($param as $item) {
  2811. if (!is_array($item)) {
  2812. continue;
  2813. }
  2814. if (empty($item['物料编码']) || empty($item['关联编号']) || !isset($item['number']) || !is_numeric($item['number']) || floatval($item['number']) <= 0) {
  2815. continue;
  2816. }
  2817. $qty = floatval($item['number']);
  2818. $processed++;
  2819. // 唯一KEY:物料编码 + 物料名称 + 关联号 + 未删除
  2820. $existingBatch = \db('物料_库存')
  2821. ->where('物料编号', $item['物料编码'])
  2822. ->where('物料名称', $item['物料名称'])
  2823. ->where('关联号', $item['关联编号'])
  2824. ->whereNull('Mod_rq')
  2825. ->value('批次号');
  2826. if (!$existingBatch && !empty($item['批次号'])) {
  2827. $existingBatch = \db('物料_库存')
  2828. ->where('批次号', $item['批次号'])
  2829. ->whereNull('Mod_rq')
  2830. ->value('批次号');
  2831. }
  2832. if ($existingBatch) {
  2833. $inventory = \db('物料_库存')
  2834. ->where('批次号', $existingBatch)
  2835. ->field('库存数量,入仓总量')
  2836. ->find();
  2837. $inventoryData = [
  2838. '关联号' => $item['关联编号'],
  2839. '物料编号' => $item['物料编码'],
  2840. '物料名称' => $item['物料名称'],
  2841. '入仓总量' => (float)$inventory['入仓总量'] + $qty,
  2842. '库存数量' => (float)$inventory['库存数量'] + $qty,
  2843. '实际门幅' => $item['实际门幅'] ?? '',
  2844. 'departname' => $item['departname'],
  2845. 'remark' => $item['remark'] ?? '',
  2846. 'sys_id' => $item['sys_id'],
  2847. 'sys_rq' => $sysRq,
  2848. ];
  2849. $sql = \db('物料_库存')
  2850. ->where('批次号', $existingBatch)
  2851. ->fetchSql(true)
  2852. ->update($inventoryData);
  2853. \db()->query($sql);
  2854. $BatchNumber = $existingBatch;
  2855. } else {
  2856. $currentNumber++;
  2857. $BatchNumber = 'PCH' . $currentNumber;
  2858. $inventoryData = [
  2859. '批次号' => $BatchNumber,
  2860. '关联号' => $item['关联编号'],
  2861. '物料编号' => $item['物料编码'],
  2862. '物料名称' => $item['物料名称'],
  2863. '入仓总量' => $qty,
  2864. '库存数量' => $qty,
  2865. '单位' => $item['单位'] ?? '',
  2866. '实际门幅' => $item['实际门幅'] ?? '',
  2867. '状态' => 1,
  2868. 'departname' => $item['departname'],
  2869. 'remark' => $item['remark'] ?? '',
  2870. 'sys_id' => $item['sys_id'],
  2871. 'sys_rq' => $sysRq,
  2872. ];
  2873. $sql = \db('物料_库存')->fetchSql(true)->insert($inventoryData);
  2874. \db()->query($sql);
  2875. }
  2876. $reportData = [
  2877. '批次号' => $BatchNumber,
  2878. 'order_id' => $item['order_id'],
  2879. '款号' => $item['款号'],
  2880. '物料编码' => $item['物料编码'],
  2881. '物料名称' => $item['物料名称'],
  2882. '库存数量' => $qty,
  2883. 'number' => $qty,
  2884. 'rq' => $item['rq'],
  2885. 'name' => $item['name'],
  2886. 'sys_id' => $item['sys_id'],
  2887. 'receipt_number' => $item['receipt_number'],
  2888. 'departname' => $item['departname'],
  2889. 'remark' => $item['remark'] ?? '',
  2890. '客户编号' => $item['客户编号'],
  2891. '款式' => $item['款式'],
  2892. 'sys_rq' => $sysRq,
  2893. ];
  2894. $sql = \db('库存_出入库明细')->fetchSql(true)->insert($reportData);
  2895. \db()->query($sql);
  2896. }
  2897. if ($processed === 0) {
  2898. throw new \Exception('无有效入库数据');
  2899. }
  2900. \db()->commit();
  2901. } catch (\think\exception\HttpResponseException $e) {
  2902. throw $e;
  2903. } catch (\Exception $e) {
  2904. \db()->rollback();
  2905. $msg = trim($e->getMessage());
  2906. $this->error('操作失败:' . ($msg !== '' ? $msg : '未知错误'));
  2907. }
  2908. $this->success('入库成功');
  2909. }
  2910. // public function inputStash()
  2911. // {
  2912. // if (!$this->request->isPost()) {
  2913. // $this->error('请求错误');
  2914. // }
  2915. //
  2916. // $param = Request::instance()->post();
  2917. // if (empty($param)) {
  2918. // $this->error('参数错误');
  2919. // }
  2920. //
  2921. // // 获取当前最大批次号
  2922. // $lastBatch = \db('物料_库存')
  2923. // ->field("批次号")
  2924. // ->orderRaw("CAST(SUBSTRING(批次号, 4) AS UNSIGNED) DESC")
  2925. // ->limit(1)
  2926. // ->value('批次号');
  2927. //
  2928. // $currentNumber = 0;
  2929. // if (!empty($lastBatch) && preg_match('/^PCH(\d+)$/i', $lastBatch, $match)) {
  2930. // $currentNumber = intval($match[1]);
  2931. // }
  2932. //
  2933. // foreach ($param as $item) {
  2934. // // 查找是否已有相同物料编号 + 关联号的库存记录
  2935. // $existingBatch = \db('物料_库存')
  2936. // ->where('物料编号', $item['物料编码'])
  2937. // ->where('物料名称', $item['物料名称'])
  2938. // ->whereNull('Mod_rq')
  2939. // ->where('关联号', $item['关联编号'])
  2940. // ->value('批次号');
  2941. //
  2942. //
  2943. // // 更新库存数据
  2944. // $Inventory = \db('物料_库存')
  2945. // ->where('批次号',$item['批次号'])
  2946. // ->field('库存数量,入仓总量')
  2947. // ->find();
  2948. //
  2949. //
  2950. // if ($existingBatch) {
  2951. // $BatchNumber = $existingBatch; // 已入库,使用原批次号
  2952. // $InventoryData = [
  2953. // '批次号' => $item['批次号'],
  2954. // '关联号' => $item['关联编号'],
  2955. // '物料编号' => $item['物料编码'],
  2956. // '物料名称' => $item['物料名称'],
  2957. // '入仓总量' => $Inventory['入仓总量'] + $item['number'],
  2958. // '库存数量' => $Inventory['库存数量'] + $item['number'],
  2959. // '实际门幅' => $item['实际门幅'],
  2960. // '单位' => $item['单位'],
  2961. // '状态' => 1,
  2962. // 'departname' => $item['departname'],
  2963. // 'remark' => isset($item['remark']) ? $item['remark'] : '',
  2964. // 'sys_id' => $item['sys_id'],
  2965. // 'sys_rq' => date('Y-m-d H:i:s'),
  2966. // ];
  2967. // $sql = \db('物料_库存')->fetchSql(true)->where('批次号',$BatchNumber)->update($InventoryData);
  2968. // \db()->query($sql);
  2969. // } else {
  2970. // // 生成新批次号
  2971. // $currentNumber++;
  2972. // $BatchNumber = 'PCH' . $currentNumber;
  2973. //
  2974. // // 插入库存表
  2975. // $InventoryData = [
  2976. // '批次号' => $BatchNumber,
  2977. // '关联号' => $item['关联编号'],
  2978. // '物料编号' => $item['物料编码'],
  2979. // '物料名称' => $item['物料名称'],
  2980. // '入仓总量' => $item['number'],
  2981. // '库存数量' => $item['number'],
  2982. // '单位' => $item['单位'],
  2983. // '实际门幅' => $item['实际门幅'],
  2984. // '状态' => 1,
  2985. // 'departname' => $item['departname'],
  2986. // 'remark' => isset($item['remark']) ? $item['remark'] : '',
  2987. // 'sys_id' => $item['sys_id'],
  2988. // 'sys_rq' => date('Y-m-d H:i:s'),
  2989. // ];
  2990. // $sql = \db('物料_库存')->fetchSql(true)->insert($InventoryData);
  2991. // \db()->query($sql);
  2992. // }
  2993. //
  2994. // // 每条都写一条报工日志
  2995. // $ReportData = [
  2996. // '批次号' => $BatchNumber,
  2997. // 'order_id' => $item['order_id'],
  2998. // '款号' => $item['款号'],
  2999. // '物料编码' => $item['物料编码'],
  3000. // '物料名称' => $item['物料名称'],
  3001. // '库存数量' => $item['number'],
  3002. // 'number' => $item['number'],
  3003. // 'rq' => $item['rq'],
  3004. // 'name' => $item['name'],
  3005. // 'sys_id' => $item['sys_id'],
  3006. // 'receipt_number' => $item['receipt_number'],
  3007. // 'departname' => $item['departname'],
  3008. // 'remark' => isset($item['remark']) ? $item['remark'] : '',
  3009. // '客户编号' => $item['客户编号'],
  3010. // '款式' => $item['款式'],
  3011. // 'sys_rq' => date('Y-m-d H:i:s'),
  3012. // ];
  3013. // $sql = \db('库存_出入库明细')->fetchSql(true)->insert($ReportData);
  3014. // \db()->query($sql);
  3015. // }
  3016. //
  3017. // $this->success('成功');
  3018. // }
  3019. /**
  3020. * 出库订单信息查询
  3021. */
  3022. public function OutOrderSearch()
  3023. {
  3024. if ($this->request->isGet() === false){
  3025. $this->error('请求错误');
  3026. }
  3027. $param = $this->request->param();
  3028. if (empty($param)){
  3029. $this->error('参数错误');
  3030. }
  3031. $list = \db('工单_基本资料')
  3032. ->alias('a')
  3033. ->join('工单_面料资料 b','a.订单编号 = b.BOM_工单编号')
  3034. ->join('物料_库存 c','b.BOM_物料编码 = c.物料编号','left')
  3035. ->join('工单关联表 d','a.订单编号 = d.订单编号 AND d.物料编号 = b.BOM_物料编码')
  3036. ->where('a.生产款号|a.订单编号|b.BOM_物料编码','like','%'.$param['search'].'%')
  3037. ->where('a.Mod_rq',null)
  3038. ->where('b.Mod_rq',null)
  3039. ->where('c.状态',1)
  3040. ->field('a.订单编号,a.生产款号,a.客户编号,a.款式,a.订单数量,a.落货日期,a.接单日期,a.Sys_id as 创建人员,
  3041. a.Sys_rq as 创建时间,b.BOM_颜色 as 颜色,b.BOM_物料编码 as 物料编码,b.BOM_物料名称 as 物料名称,c.批次号,d.关联编号')
  3042. ->order('a.订单编号 desc')
  3043. ->select();
  3044. if (empty($list)){
  3045. $this->success('未找到相关订单信息');
  3046. }else{
  3047. $this->success('成功',$list);
  3048. }
  3049. }
  3050. /**
  3051. * 获取入库面料信息
  3052. */
  3053. public function getInputDetail()
  3054. {
  3055. if ($this->request->isGet() === false){
  3056. $this->error('请求错误');
  3057. }
  3058. $param = $this->request->param();
  3059. if (empty($param)){
  3060. $this->error('参数错误');
  3061. }
  3062. $list = \db('工单_面料资料')
  3063. ->alias('b')
  3064. ->join('物料_库存 a', 'a.物料编号 = b.BOM_物料编码 AND a.物料名称 = b.BOM_物料名称 AND a.Mod_rq IS NULL', 'left')
  3065. ->join('工单关联表 c', 'b.BOM_物料编码 = c.物料编号 AND b.BOM_工单编号 = c.订单编号')
  3066. ->field('a.Mod_rq, a.批次号, c.关联编号, b.BOM_物料编码, b.BOM_物料名称, a.入仓总量, a.库存数量, a.领用数量, a.退还数量, a.单位, a.实际门幅,
  3067. a.departname, b.BOM_工单编号, b.BOM_颜色, b.BOM_计划门幅 as 计划门幅, b.BOM_定额门幅, b.BOM_计划用量 as 计划用料, b.BOM_标准用量 as 定额用料')
  3068. ->where('c.关联编号', $param['关联编号'])
  3069. ->where('c.订单编号', $param['订单编号'])
  3070. ->where('b.BOM_物料编码', $param['物料编码'])
  3071. ->where('a.Mod_rq',null)
  3072. ->where('b.Mod_rq',null)
  3073. ->where('c.Mod_rq',null)
  3074. ->find();
  3075. if (empty($list)) {
  3076. $this->success('成功', []);
  3077. }
  3078. $orderList = \db('工单关联表')
  3079. ->where('关联编号',$param['关联编号'])
  3080. ->column('GROUP_CONCAT(DISTINCT 订单编号 SEPARATOR ",") AS 关联订单');
  3081. $list['关联订单'] = isset($orderList[0]) ? $orderList[0] : '';
  3082. $this->success('成功',$list);
  3083. }
  3084. /**
  3085. * 裁剪面料退货包工
  3086. * @return void
  3087. * @throws \think\db\exception\DataNotFoundException
  3088. * @throws \think\db\exception\ModelNotFoundException
  3089. * @throws \think\exception\DbException
  3090. * @throws \think\exception\PDOException
  3091. */
  3092. public function TuihuoReport()
  3093. {
  3094. if ($this->request->isPost() === false){
  3095. $this->error('请求错误');
  3096. }
  3097. $param = Request::instance()->post();
  3098. if (empty($param)){
  3099. $this->error('参数错误');
  3100. }
  3101. foreach ($param as $item) {
  3102. // 查询库存数据
  3103. $number = \db('物料_库存')
  3104. ->where('批次号', $item['批次号'])
  3105. ->where('关联号', $item['关联编号'])
  3106. ->where('物料编号', $item['物料编码'])
  3107. ->whereNull('Mod_rq')
  3108. ->field('库存数量, 退还数量')
  3109. ->find();
  3110. // 根据类型调整库存
  3111. if ($item['type'] === '退仓') {
  3112. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  3113. $stockQuantity = (float)$number['库存数量'] + (float)$item['number'];
  3114. } elseif ($item['type'] === '退厂商') {
  3115. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  3116. $stockQuantity = (float)$number['库存数量'] - (float)$item['number'];
  3117. } elseif ($item['type'] === '销售') {
  3118. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  3119. $stockQuantity = (float)$number['库存数量'] - (float)$item['number'];
  3120. }elseif ($item['type'] === '退客户') {
  3121. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  3122. $stockQuantity = (float)$number['库存数量'] - (float)$item['number'];
  3123. // 检查库存是否足够
  3124. if ($stockQuantity < 0) {
  3125. $this->error('库存不足,无法进行退客户操作。当前库存:' . $number['库存数量']);
  3126. }
  3127. }else{
  3128. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  3129. $stockQuantity = (float)$number['库存数量'] ;
  3130. }
  3131. // 构建库存数据
  3132. $inventoryData = [
  3133. '批次号' => $item['批次号'],
  3134. '关联号' => $item['关联编号'],
  3135. '物料编号' => $item['物料编码'],
  3136. '物料名称' => $item['物料名称'],
  3137. '实际门幅' => $item['实际门幅'],
  3138. '单位' => $item['单位'],
  3139. '状态' => 1,
  3140. 'sys_id' => $item['sys_id'],
  3141. 'departname' => $item['departname'],
  3142. 'remark' => isset($item['remark']) ? $item['remark'] : '',
  3143. 'sys_rq' => date('Y-m-d H:i:s'),
  3144. '退还数量' => $returnQuantity,
  3145. '库存数量' => $stockQuantity,
  3146. ];
  3147. $InventorySql = \db('物料_库存')
  3148. ->where('批次号',$item['批次号'])
  3149. ->where('关联号',$item['关联编号'])
  3150. ->where('物料编号',$item['物料编码'])
  3151. ->whereNull('Mod_rq')
  3152. ->fetchSql(true)
  3153. ->update($inventoryData);
  3154. \db()->query($InventorySql);
  3155. // 记录入库报工日志
  3156. $ReportData = [
  3157. '批次号' => $item['批次号'],
  3158. 'order_id' => $item['order_id'],
  3159. '款号' => $item['款号'],
  3160. '物料编码' => $item['物料编码'],
  3161. '物料名称' => $item['物料名称'],
  3162. '库存数量' => $item['number'],
  3163. 'number' => $item['number'],
  3164. 'rq' => $item['rq'],
  3165. 'name' => $item['name'],
  3166. 'sys_id' => $item['sys_id'],
  3167. 'receipt_number' => $item['receipt_number'],
  3168. 'departname' => $item['departname'],
  3169. 'remark' => isset($item['remark']) ? $item['remark'] : '',
  3170. '客户编号' => $item['客户编号'],
  3171. '款式' => $item['款式'],
  3172. 'type' => $item['type'],
  3173. 'sys_rq' => date('Y-m-d H:i:s'),
  3174. ];
  3175. $sql = \db('库存_出入库明细')->fetchSql(true)->insert($ReportData);
  3176. \db()->query($sql);
  3177. }
  3178. $this->success('成功');
  3179. }
  3180. /**
  3181. * 总检日产量汇总
  3182. * code:工序名称
  3183. */
  3184. public function DailyProductionReport(){
  3185. if ($this->request->isGet() === false) {
  3186. $this->error('请求错误');
  3187. }
  3188. $param = $this->request->param();
  3189. if (!empty($param['code'])) {
  3190. $threeMonthsAgo = date('Y-m-d H:i:s', strtotime('-1 months'));
  3191. // 先获取每个分组的最新日期和汇总数据
  3192. $subQuery = Db::name('设备_产量计酬')->alias('c')
  3193. ->field('c.订单编号, c.子订单编号, c.尺码, SUM(c.数量) as 总数量, MAX(c.sys_rq) as rq')
  3194. ->where('工序名称', $param['code'])
  3195. ->where('c.sys_rq', '>=', $threeMonthsAgo)
  3196. ->group('c.子订单编号, c.尺码')
  3197. ->buildSql();
  3198. // 主查询关联工单信息并排序
  3199. $res = Db::table($subQuery . ' t')
  3200. ->field('t.订单编号, t.子订单编号, t.总数量, t.尺码,j.生产款号, j.款式')
  3201. ->join('工单_基本资料 j', 't.订单编号 = j.订单编号', 'left')
  3202. ->order('t.rq desc, t.订单编号, t.子订单编号, t.尺码')
  3203. ->select();
  3204. $this->success('成功', [
  3205. 'riqi' => date('Y-m-d'),
  3206. 'list' => $res,
  3207. 'total' => count($res)
  3208. ]);
  3209. }else{
  3210. $this->error('缺少工序,参数错误');
  3211. }
  3212. }
  3213. /**
  3214. * 修改产量报工数量
  3215. * @ApiMethod (POST)
  3216. * @param string $UniqId 报工唯一标识
  3217. * @param float $number 修改后数量
  3218. * @param string $mod_id 修改人(可选)
  3219. */
  3220. public function updateReport()
  3221. {
  3222. if (!$this->request->isPost()) {
  3223. $this->error('请求方法错误');
  3224. }
  3225. $params = $this->request->post();
  3226. if (empty($params['UniqId'])) {
  3227. $this->error('UniqId不能为空');
  3228. }
  3229. if (!isset($params['number']) || $params['number'] === '') {
  3230. $this->error('数量不能为空');
  3231. }
  3232. $record = db('设备_产量计酬')
  3233. ->where('UniqId', $params['UniqId'])
  3234. ->where(function ($query) {
  3235. $query->whereNull('mod_rq')->whereOr('mod_rq', '');
  3236. })
  3237. ->find();
  3238. if (empty($record)) {
  3239. $this->error('报工记录不存在或已删除');
  3240. }
  3241. $number = floatval($params['number']);
  3242. if ($number < 0) {
  3243. $this->error('数量不能为负数');
  3244. }
  3245. $processName = $record['工序名称'] ?? '';
  3246. $isCutOrSew = in_array($processName, ['裁剪', '车缝'], true);
  3247. $oldDisplayQty = floatval($isCutOrSew ? $record['数量'] : ($record['s_num'] ?? $record['数量']));
  3248. if ($oldDisplayQty == $number) {
  3249. $this->error('数量未发生变化');
  3250. }
  3251. $operTime = date('Y-m-d H:i:s');
  3252. $operUser = !empty($params['mod_id']) ? $params['mod_id'] : ($record['sys_id'] ?? '');
  3253. $updateData = [];
  3254. if ($isCutOrSew) {
  3255. $updateData['数量'] = $number;
  3256. if (isset($record['ci_num']) && floatval($record['ci_num']) == floatval($record['数量'])) {
  3257. $updateData['ci_num'] = $number;
  3258. }
  3259. } else {
  3260. $updateData['s_num'] = $number;
  3261. $updateData['数量'] = $number;
  3262. if (isset($record['ci_num']) && floatval($record['ci_num']) == floatval($record['s_num'] ?? $record['数量'])) {
  3263. $updateData['ci_num'] = $number;
  3264. }
  3265. }
  3266. $logContext = [
  3267. 'uniq_id' => $params['UniqId'],
  3268. 'oper_type' => '修改产量报工',
  3269. 'oper_user' => $operUser,
  3270. 'oper_time' => $operTime,
  3271. 'work_order' => $record['订单编号'] ?? '',
  3272. 'sub_order_no' => $record['子订单编号'] ?? '',
  3273. 'process_name' => $processName,
  3274. 'process_code' => $record['工序编号'] ?? '',
  3275. 'size' => $record['尺码'] ?? '',
  3276. 'machine' => $record['sczl_jtbh'] ?? '',
  3277. 'team' => $record['sczl_bh'] ?? '',
  3278. 'report_date' => !empty($record['sczl_rq']) ? date('Y-m-d', strtotime($record['sczl_rq'])) : null,
  3279. ];
  3280. $logList = [
  3281. $this->buildProductionReportLogRow($logContext, '数量', $oldDisplayQty, $number),
  3282. ];
  3283. Db::startTrans();
  3284. try {
  3285. if ($processName === '裁剪') {
  3286. $this->syncCuttingPrintPartQty($record['子订单编号'], $record['尺码'], $number - $oldDisplayQty);
  3287. }
  3288. if ($processName === '包装') {
  3289. $this->syncPackagingWarehouseQty($record['订单编号'], $number - $oldDisplayQty);
  3290. }
  3291. $result = db('设备_产量计酬')
  3292. ->where('UniqId', $params['UniqId'])
  3293. ->where(function ($query) {
  3294. $query->whereNull('mod_rq')->whereOr('mod_rq', '');
  3295. })
  3296. ->update($updateData);
  3297. if ($result === false) {
  3298. throw new \Exception('修改失败');
  3299. }
  3300. if ($result === 0) {
  3301. throw new \Exception('报工记录不存在或已删除');
  3302. }
  3303. $this->writeProductionReportLogs($logList);
  3304. Db::commit();
  3305. } catch (\think\exception\HttpResponseException $e) {
  3306. throw $e;
  3307. } catch (\Exception $e) {
  3308. Db::rollback();
  3309. $this->error($e->getMessage());
  3310. }
  3311. $this->success('修改成功', [
  3312. 'UniqId' => $params['UniqId'],
  3313. 'number' => $number,
  3314. ]);
  3315. }
  3316. /** @var bool */
  3317. private static $productionReportLogTableReady = false;
  3318. /**
  3319. * 裁剪报工数量变更时,同步工单印件实裁数
  3320. */
  3321. private function syncCuttingPrintPartQty($subOrderNo, $size, $delta)
  3322. {
  3323. if ($delta == 0 || empty($subOrderNo) || $size === null || $size === '') {
  3324. return;
  3325. }
  3326. $yresult = db('工单_印件资料')
  3327. ->where('子订单编号', $subOrderNo)
  3328. ->whereNull('Mod_rq')
  3329. ->find();
  3330. if (empty($yresult)) {
  3331. return;
  3332. }
  3333. $partUpdate = [];
  3334. for ($i = 1; $i <= 14; $i++) {
  3335. $cmField = 'cm' . $i;
  3336. $scslField = 'scsl' . $i;
  3337. if (isset($yresult[$cmField]) && $yresult[$cmField] === $size) {
  3338. $newVal = floatval($yresult[$scslField]) + floatval($delta);
  3339. if ($newVal < 0) {
  3340. $newVal = 0;
  3341. }
  3342. $partUpdate[$scslField] = $newVal;
  3343. $yresult[$scslField] = $newVal;
  3344. break;
  3345. }
  3346. }
  3347. $newTotal = 0;
  3348. for ($i = 1; $i <= 14; $i++) {
  3349. $scslField = 'scsl' . $i;
  3350. if (isset($yresult[$scslField])) {
  3351. $newTotal += floatval($yresult[$scslField]);
  3352. }
  3353. }
  3354. db('工单_印件资料')
  3355. ->where('子订单编号', $subOrderNo)
  3356. ->whereNull('Mod_rq')
  3357. ->update(['sctotal' => $newTotal] + $partUpdate);
  3358. }
  3359. /**
  3360. * 包装报工数量变更时,同步工单入仓数量
  3361. */
  3362. private function syncPackagingWarehouseQty($workOrder, $delta)
  3363. {
  3364. if ($delta == 0 || empty($workOrder)) {
  3365. return;
  3366. }
  3367. $orderInfo = db('工单_基本资料')
  3368. ->where('订单编号', $workOrder)
  3369. ->whereNull('Mod_rq')
  3370. ->field('工单入仓数量')
  3371. ->find();
  3372. if (empty($orderInfo)) {
  3373. return;
  3374. }
  3375. $newQty = floatval($orderInfo['工单入仓数量']) + floatval($delta);
  3376. if ($newQty < 0) {
  3377. $newQty = 0;
  3378. }
  3379. db('工单_基本资料')
  3380. ->where('订单编号', $workOrder)
  3381. ->whereNull('Mod_rq')
  3382. ->update(['工单入仓数量' => $newQty]);
  3383. }
  3384. private function ensureProductionReportLogTable()
  3385. {
  3386. if (self::$productionReportLogTableReady) {
  3387. return;
  3388. }
  3389. $sql = <<<SQL
  3390. CREATE TABLE IF NOT EXISTS `设备_产量报工操作日志` (
  3391. `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
  3392. `uniq_id` varchar(64) NOT NULL DEFAULT '' COMMENT '报工UniqId',
  3393. `oper_type` varchar(32) NOT NULL DEFAULT '' COMMENT '操作类型',
  3394. `oper_user` varchar(64) NOT NULL DEFAULT '' COMMENT '操作人',
  3395. `oper_time` datetime NOT NULL COMMENT '操作时间',
  3396. `work_order` varchar(64) NOT NULL DEFAULT '' COMMENT '订单编号',
  3397. `sub_order_no` varchar(64) NOT NULL DEFAULT '' COMMENT '子订单编号',
  3398. `process_code` varchar(32) NOT NULL DEFAULT '' COMMENT '工序编号',
  3399. `process_name` varchar(32) NOT NULL DEFAULT '' COMMENT '工序名称',
  3400. `size` varchar(32) NOT NULL DEFAULT '' COMMENT '尺码',
  3401. `machine` varchar(64) NOT NULL DEFAULT '' COMMENT '机台编号',
  3402. `team` varchar(64) NOT NULL DEFAULT '' COMMENT '小组',
  3403. `report_date` date DEFAULT NULL COMMENT '报工日期',
  3404. `change_field` varchar(32) NOT NULL DEFAULT '' COMMENT '变更字段',
  3405. `old_value` varchar(255) NOT NULL DEFAULT '' COMMENT '旧值',
  3406. `new_value` varchar(255) NOT NULL DEFAULT '' COMMENT '新值',
  3407. PRIMARY KEY (`id`),
  3408. KEY `idx_uniq_id` (`uniq_id`),
  3409. KEY `idx_work_order` (`work_order`),
  3410. KEY `idx_oper_time` (`oper_time`)
  3411. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='产量报工操作日志'
  3412. SQL;
  3413. Db::execute($sql);
  3414. self::$productionReportLogTableReady = true;
  3415. }
  3416. private function writeProductionReportLogs(array $logs)
  3417. {
  3418. if (empty($logs)) {
  3419. return;
  3420. }
  3421. $this->ensureProductionReportLogTable();
  3422. $result = Db::table('设备_产量报工操作日志')->insertAll($logs);
  3423. if ($result === false) {
  3424. throw new \Exception('写入产量报工操作日志失败');
  3425. }
  3426. }
  3427. private function buildProductionReportLogRow(array $context, $changeField, $oldValue, $newValue)
  3428. {
  3429. return [
  3430. 'uniq_id' => $context['uniq_id'] ?? '',
  3431. 'oper_type' => $context['oper_type'] ?? '',
  3432. 'oper_user' => $context['oper_user'] ?? '',
  3433. 'oper_time' => $context['oper_time'] ?? date('Y-m-d H:i:s'),
  3434. 'work_order' => $context['work_order'] ?? '',
  3435. 'sub_order_no' => $context['sub_order_no'] ?? '',
  3436. 'process_code' => $context['process_code'] ?? '',
  3437. 'process_name' => $context['process_name'] ?? '',
  3438. 'size' => $context['size'] ?? '',
  3439. 'machine' => $context['machine'] ?? '',
  3440. 'team' => $context['team'] ?? '',
  3441. 'report_date' => !empty($context['report_date']) ? $context['report_date'] : null,
  3442. 'change_field' => (string)$changeField,
  3443. 'old_value' => $oldValue === null ? '' : (string)$oldValue,
  3444. 'new_value' => $newValue === null ? '' : (string)$newValue,
  3445. ];
  3446. }
  3447. }