WorkOrderSpotCheck.php 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282
  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. $existingRecords = \db('设备_产量计酬')->alias('c')
  379. ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码,
  380. CASE
  381. WHEN c.工序名称 IN ("裁剪", "车缝") THEN c.数量
  382. ELSE c.s_num
  383. END as 数量,
  384. c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num,c.UniqId,
  385. y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注, CAST(c.serial_num AS SIGNED) as serial_num_numeric')
  386. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  387. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  388. ->where($where)
  389. // ->limit(1,100)
  390. ->order('c.sys_rq desc')
  391. ->group('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num, j.款式')
  392. ->select();
  393. $sizeSummary = [];
  394. $sizes = [];
  395. foreach ($existingRecords as $record) {
  396. $size = $record['尺码'];
  397. $quantity = $record['数量'];
  398. $serial = $record['serial'];
  399. $serialNum = $record['serial_num']; // serial_num 分组
  400. $processNumber = $record['工序编号']; // 工序编号
  401. // 初始化尺码
  402. if (!in_array($size, $sizes)) {
  403. $sizes[] = $size;
  404. }
  405. // 检查是否已有相同的 serial_num 组合
  406. if (!isset($sizeSummary[$serialNum])) {
  407. $sizeSummary[$serialNum] = [
  408. '订单编号' => $record['订单编号'],
  409. '子订单编号' => $record['子订单编号'],
  410. '款号' => $record['款号'],
  411. '款式' => $record['款式'],
  412. '工序编号' => $record['工序编号'],
  413. '工序名称' => $record['工序名称'],
  414. '数量' => '0', // 记录总数量
  415. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  416. '颜色' => $record['颜色'],
  417. '颜色备注' => $record['颜色备注'],
  418. '机台号' => $record['sczl_jtbh'],
  419. '组别' => $record['sczl_bh'],
  420. 'serial' => [], // 流水号数组
  421. 'sys_rq' => $record['sys_rq'],
  422. 'UniqId' => $record['UniqId'],
  423. '尺码数据' => [] // 用于存储每个尺码对应的流水号和数量
  424. ];
  425. }
  426. // 将流水号添加到 serial 数组
  427. $sizeSummary[$serialNum]['serial'][] = $serial;
  428. // 累加数量到总数量
  429. $sizeSummary[$serialNum]['数量'] += $quantity;
  430. // 记录尺码数据
  431. if (!isset($sizeSummary[$serialNum]['尺码数据'][$size])) {
  432. $sizeSummary[$serialNum]['尺码数据'][$size] = [];
  433. }
  434. // 添加尺码对应的流水号和数量
  435. $sizeSummary[$serialNum]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  436. }
  437. // 对每个工序的 serial 进行处理,并转换为逗号分隔的字符串
  438. $finalRecords = [];
  439. foreach ($sizeSummary as &$summary) {
  440. // 将 serial 数组按数值降序排序
  441. sort($summary['serial'], SORT_NUMERIC);
  442. $summary['serial'] = implode(',', $summary['serial']);
  443. // 合并尺码数据到同一层级
  444. foreach ($summary['尺码数据'] as $size => $serials) {
  445. $summary[$size] = implode(', ', $serials); // 尺码对应的流水号和数量
  446. }
  447. unset($summary['尺码数据']); // 移除尺码数据数组,直接将数据合并到 summary
  448. $finalRecords[] = $summary;
  449. }
  450. // 尺码排序(数字在前,字母在后)
  451. usort($sizes, function ($a, $b) {
  452. $sizeOrder = [
  453. 'XXXS' => 1,
  454. 'XXS' => 2,
  455. 'XS' => 3,
  456. 'S' => 4,
  457. 'M' => 5,
  458. 'L' => 6,
  459. 'XL' => 7,
  460. '2XL' => 8,
  461. 'XXL' => 9,
  462. '3XL' => 10,
  463. 'XXXL' => 11,
  464. '4XL' => 12,
  465. 'XXXXL' => 13,
  466. '5XL' => 14,
  467. 'XXXXXL' => 15
  468. ];
  469. if (is_numeric($a) && is_numeric($b)) return $a - $b;
  470. if (is_numeric($a)) return -1;
  471. if (is_numeric($b)) return 1;
  472. return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  473. });
  474. return ['headers' => $sizes, 'records' => $finalRecords];
  475. }
  476. if (strpos($order, '-') !== false) {
  477. if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
  478. $this->error('sys_sbID 参数错误');
  479. }
  480. if (preg_match('/-(\d{2})$/', $order, $matches)) {
  481. $number = (int)$matches[1];
  482. if ($number > 70) {
  483. //echo '船样';
  484. //获取订单编号
  485. // $ddbh = explode('-', $order)[0];
  486. //1.查询尺码、制单数、填写报工新增实裁数
  487. $data = \db('工单_基本资料')->alias('j')
  488. ->distinct('y.子订单编号')
  489. ->field('
  490. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  491. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  492. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  493. 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,
  494. 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,
  495. 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'
  496. )
  497. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  498. ->where('y.船样',1)
  499. ->whereNull('y.Mod_rq')
  500. ->where('y.子订单编号', $order)
  501. ->select();
  502. } else {
  503. //echo '两位数子订单';
  504. $ddbh = explode('-', $order)[0];//获取订单编号
  505. //【查询尺码、制单数、填写报工新增实裁数】
  506. $data = \db('工单_基本资料')->alias('j')
  507. ->distinct('y.子订单编号')
  508. ->field('
  509. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  510. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  511. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  512. 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,
  513. 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,
  514. 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'
  515. )
  516. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  517. ->where('y.船样',0)->whereNull('y.Mod_rq')
  518. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  519. ->select();
  520. }
  521. }else{
  522. $ddbh = explode('-', $order)[0];//获取订单编号
  523. //【查询尺码、制单数、填写报工新增实裁数】
  524. $data = \db('工单_基本资料')->alias('j')
  525. ->distinct('y.子订单编号')
  526. ->field('
  527. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  528. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  529. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  530. 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,
  531. 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,
  532. 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'
  533. )
  534. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  535. ->where('y.船样',0)->whereNull('y.Mod_rq')
  536. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  537. ->select();
  538. }
  539. $dataEntry = [];
  540. $list = [];
  541. $headers = [];//存放尺码表头
  542. foreach ($data as $item) {
  543. // 初始化尺码与数量映射数组
  544. $cmScslMapping = [];
  545. for ($i = 1; $i <= 14; $i++) {
  546. $cmValue = $item['cm' . $i];
  547. $scslValue = $item['scsl' . $i];
  548. if (!is_null($cmValue) && $cmValue !== 0) {
  549. $cmScslMapping[$cmValue] = ($scslValue === 0 || is_null($scslValue)) ? '' : $scslValue;
  550. if (!in_array($cmValue, $headers)) {
  551. $headers[] = $cmValue;
  552. }
  553. }
  554. }
  555. // 将尺码数据平铺到主要数据中(表格数据)
  556. $orderData = [
  557. '订单编号' => $item['订单编号'],
  558. '子订单编号' => $item['子订单编号'],
  559. '款号' => $item['款号'],
  560. '款式' => $item['款式'],
  561. '颜色' => $item['颜色备注'],
  562. 'zdtotal' => $item['zdtotal'],
  563. 'sctotal' => $item['sctotal'],
  564. '单位' => $item['单位'],
  565. 'Sys_id' => $item['Sys_id'],
  566. 'Sys_rq' => $item['Sys_rq'],
  567. ];
  568. // 平铺尺码映射数据
  569. $orderData = array_merge($orderData, $cmScslMapping);
  570. $dataEntry[] = $orderData;
  571. // list 列表部分
  572. $entry = [];
  573. for ($i = 1; $i <= 14; $i++) {
  574. $entry['cm' . $i] = ($item['cm' . $i] === 0 || is_null($item['cm' . $i])) ? '' : $item['cm' . $i];
  575. $entry['cmsl' . $i] = ($item['cmsl' . $i] === 0 || is_null($item['cmsl' . $i])) ? '' : $item['cmsl' . $i];
  576. $entry['scsl' . $i] = ($item['scsl' . $i] === 0 || is_null($item['scsl' . $i])) ? '' : $item['scsl' . $i];
  577. }
  578. $entry['订单编号'] = $item['订单编号'];
  579. $entry['子订单编号'] = $item['子订单编号'];
  580. $entry['UniqId'] = $item['UniqId'];
  581. $entry['zdtotal'] = $item['zdtotal'];
  582. $entry['颜色'] = $item['颜色'];
  583. $entry['款式'] = $item['款式'];
  584. $entry['款号'] = $item['款号'];
  585. $entry['color_id'] = $item['color_id'];
  586. $entry['颜色备注'] = $item['颜色备注'];
  587. $list[] = $entry;
  588. }
  589. // 在 $mergeds 中查找匹配的子订单编号 对应的一条数据
  590. $matchedOrder = null;
  591. foreach ($list as $mergedOrder) {
  592. if ($mergedOrder['子订单编号'] === $order) {
  593. $matchedOrder = $mergedOrder;
  594. break; // 找到后可以退出循环
  595. }
  596. }
  597. $allScslEmpty = true;
  598. for ($i = 1; $i <= 14; $i++) {
  599. if (!empty($matchedOrder["scsl$i"])) {
  600. $allScslEmpty = false;
  601. break;
  602. }
  603. }
  604. if ($allScslEmpty) {
  605. $this->success('当前订单未裁剪 请裁剪后报工', [
  606. 'code' => 1,
  607. 'serial' => '',
  608. 'serial_num' => '',
  609. 'headers' => '',
  610. 'records' => '',
  611. ]);
  612. return;
  613. }
  614. // echo "<pre>";print_r($matchedOrder);echo "</pre>";die;
  615. $sizeData = getSizeSummary($order);
  616. // 查询流水号(包数)和序号
  617. $serial = \db('设备_产量计酬')
  618. ->field('子订单编号, MAX(CAST(serial AS UNSIGNED)) as serial, MAX(CAST(serial_num AS UNSIGNED)) as serial_num')
  619. ->where('子订单编号', $order)
  620. ->where(function($query) {
  621. $query->whereNull('mod_rq')->whereOr('mod_rq', '');
  622. }) // 查询未删除数据
  623. ->where('sczl_jtbh', $params['sys_sbID'])
  624. ->group('子订单编号')
  625. ->find();
  626. $serial['serial'] = empty($serial['serial']) ? '000' : str_pad($serial['serial'], 3, '0', STR_PAD_LEFT);
  627. $serial['serial_num'] = empty($serial['serial_num']) ? '0' : $serial['serial_num'];
  628. $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  629. $clwhere['子订单编号'] = $order;
  630. $clwhere['mod_rq'] = null;
  631. $clwhere['工序名称'] = $params['code'];
  632. $cljclist = \db('设备_产量计酬')
  633. ->field('订单编号,子订单编号,尺码,数量,mod_rq')
  634. ->where($clwhere)
  635. ->select();
  636. $result = [];
  637. $totalQuantity = 0; // 总数量初始化
  638. $orderNumber = ''; // 订单编号初始化
  639. foreach ($cljclist as $item) {
  640. $size = $item['尺码'];
  641. $orderNumber = $item['订单编号'];
  642. if (isset($result[$size])) {
  643. $result[$size] += $item['数量'];
  644. } else {
  645. $result[$size] = $item['数量'];
  646. }
  647. // 累加总数量
  648. $totalQuantity += $item['数量'];
  649. }
  650. $result['订单编号'] = $orderNumber;
  651. foreach ($result as $size => $quantity) {
  652. if ($size !== '订单编号' && $size !== '数量') {
  653. $result[$size] = '合计(' . $quantity . ')';
  654. }
  655. }
  656. $result['数量'] = '合计(' . $totalQuantity . ')';
  657. // 初始化 $sizeData['records'],以防万一它不存在
  658. if (!isset($sizeData['records'])) {
  659. $sizeData['records'] = [];
  660. }
  661. // 将 $result 插入到 $sizeData['records'] 的最前面
  662. array_unshift($sizeData['records'], $result);
  663. $this->success('请求成功', [
  664. 'serial' => $serial['serial']+1, // 流水号
  665. 'serial_num' => $serial['serial_num']+1, // 序号
  666. 'list' => $matchedOrder, // 报工
  667. 'headers' => $sizeData['headers'], // 尺码表头
  668. 'records' => $sizeData['records'] // 查询历史记录
  669. ]);
  670. }else{
  671. $sizeData = getSizeSummary($order);
  672. // 查询流水号(包数)和序号
  673. $serial = \db('设备_产量计酬')
  674. ->field('子订单编号, MAX(CAST(serial AS UNSIGNED)) as serial, MAX(CAST(serial_num AS UNSIGNED)) as serial_num')
  675. ->where('订单编号', $order)
  676. ->group('订单编号')
  677. ->find();
  678. $serial['serial'] = empty($serial['serial']) ? '000' : str_pad($serial['serial'], 3, '0', STR_PAD_LEFT);
  679. $serial['serial_num'] = empty($serial['serial_num']) ? '0' : $serial['serial_num'];
  680. $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  681. $clwhere['订单编号'] = $order;
  682. $clwhere['工序名称'] = $params['code'];
  683. $clwhere['mod_rq'] = null;
  684. $cljclist = \db('设备_产量计酬')
  685. ->field('订单编号,子订单编号,尺码,数量,mod_rq')
  686. ->where($clwhere)
  687. ->select();
  688. $result = [];
  689. $totalQuantity = 0; // 总数量初始化
  690. $orderNumber = ''; // 订单编号初始化
  691. foreach ($cljclist as $item) {
  692. $size = $item['尺码'];
  693. $orderNumber = $item['订单编号'];
  694. if (isset($result[$size])) {
  695. $result[$size] += $item['数量'];
  696. } else {
  697. $result[$size] = $item['数量'];
  698. }
  699. // 累加总数量
  700. $totalQuantity += $item['数量'];
  701. }
  702. foreach ($result as $size => $quantity) {
  703. if ($size !== '订单编号' && $size !== '数量') {
  704. $result[$size] = '合计(' . $quantity . ')';
  705. }
  706. }
  707. $result['订单编号'] = $orderNumber;
  708. $result['数量'] = '合计(' . $totalQuantity . ')';
  709. // 初始化 $sizeData['records'],以防万一它不存在
  710. if (!isset($sizeData['records'])) {
  711. $sizeData['records'] = [];
  712. }
  713. // 将 $result 插入到 $sizeData['records'] 的最前面
  714. array_unshift($sizeData['records'], $result);
  715. $this->success('请求成功', [
  716. 'serial' => $serial['serial'], // 流水号
  717. 'serial_num' => $serial['serial_num'], // 序号
  718. // 'list' => $matchedOrder, // 报工
  719. 'headers' => $sizeData['headers'], // 尺码表头
  720. 'records' => $sizeData['records'] // 查询历史记录
  721. ]);
  722. }
  723. break;
  724. case '手工':
  725. $gongxuname = '手工';
  726. $gongxuncode = '4';
  727. //判断是否为标签二维码参数数据
  728. if (strpos($order, ',') !== false) {
  729. //将子订单编号, 机台号, 工序, 尺码, 数量, 尾包, 流水号(包)分割为数组
  730. $paramArray = explode(',', $order);
  731. //通过子订单编号,尺码, 数量,流水号(包)查询数据库是否存在(且未删除数据)
  732. $res = \db('设备_产量计酬')
  733. ->where([
  734. '子订单编号' => $paramArray[0],
  735. '尺码' => $paramArray[3],
  736. '数量' => $paramArray[4],
  737. 'serial' => $paramArray[6],
  738. 'mod_rq' => null,
  739. ])
  740. ->order('UniqId desc')
  741. ->find();
  742. //如果不存在, 则返回当前扫码小票无效
  743. if (!$res) {
  744. $this->success('当前扫码小票无效', [
  745. 'code' => 1,
  746. 'result' => '',
  747. 'headers' => '',
  748. 'records' => ''
  749. ]);
  750. return;
  751. }
  752. $orderlist = \db('工单_基本资料')
  753. ->where('订单编号', $res['订单编号'])
  754. ->field('订单编号, 生产款号, 款式')
  755. ->find();
  756. $yinjianlist = \db('工单_印件资料')
  757. ->where('订单编号', $res['订单编号'])
  758. ->field('zdtotal, sctotal,颜色,颜色备注')
  759. ->find();
  760. if (empty($res)) {
  761. $ci_num = $paramArray[4];
  762. } else {
  763. $ci_num = $res["ci_num"];
  764. }
  765. $results = [
  766. 'sys_sbID' => $params['sys_sbID'],//设备编号
  767. 'ci_num' => $ci_num,//剩余数量
  768. 'UniqId' => $res['UniqId'],
  769. 'order' => $paramArray[0],//子订单编号
  770. 'zb' => $params['sys_sbID'],//组别
  771. 'gx' => $gongxuncode,//默认固定工序
  772. 'cm' => $paramArray[3],//尺码
  773. 'sl' => $paramArray[4],//数量
  774. 'wb' => $paramArray[5],//尾包
  775. '订单编号' => $orderlist['订单编号'],
  776. '生产款号' => $orderlist['生产款号'],
  777. '款式' => $orderlist['款式'],
  778. 'sctotal' => $yinjianlist['sctotal'],
  779. 'zdtotal' => $yinjianlist['zdtotal'],
  780. '颜色' => $yinjianlist['颜色备注'],
  781. ];
  782. $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  783. $clwhere['订单编号'] = $res['订单编号'];
  784. $clwhere['工序名称'] = $gongxuname;
  785. $clwhere['mod_rq'] = null;
  786. $cljclist = \db('设备_产量计酬')
  787. ->field('订单编号,子订单编号,尺码,s_num as 数量,mod_rq')
  788. ->where($clwhere)
  789. ->select();
  790. $result = [];
  791. $totalQuantity = 0; // 总数量初始化
  792. $orderNumber = ''; // 订单编号初始化
  793. foreach ($cljclist as $item) {
  794. $size = $item['尺码'];
  795. $orderNumber = $item['订单编号'];
  796. if (isset($result[$size])) {
  797. $result[$size] += $item['数量'];
  798. } else {
  799. $result[$size] = $item['数量'];
  800. }
  801. // 累加总数量
  802. $totalQuantity += $item['数量'];
  803. }
  804. foreach ($result as $size => $quantity) {
  805. if ($size !== '订单编号' && $size !== '数量') {
  806. $result[$size] = '合计(' . $quantity . ')';
  807. }
  808. }
  809. $result['订单编号'] = $orderNumber;
  810. $result['数量'] = '合计(' . $totalQuantity . ')';
  811. $where = [];
  812. if (strpos($paramArray[0], '-') !== false) {
  813. $where['c.子订单编号'] = $paramArray[0];
  814. } else {
  815. $where['c.订单编号'] = ['like', '%' . $paramArray[0] . '%'];
  816. }
  817. $where['c.工序编号'] = $gongxuncode;
  818. $where['c.mod_rq'] = null;
  819. $existingRecords = \db('设备_产量计酬')->alias('c')
  820. ->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,
  821. ci_num,s_num,
  822. y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注, CAST(c.serial_num AS SIGNED) as serial_num_numeric')
  823. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  824. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  825. ->where($where)
  826. ->order('c.sys_rq desc')
  827. ->select();
  828. $sizeSummary = [];
  829. $sizes = [];
  830. // 遍历数据库查询结果,根据 UniqId + 尺码 进行分组
  831. foreach ($existingRecords as $record) {
  832. $size = $record['尺码'];
  833. $quantity = $record['数量'];
  834. $serial = $record['serial'];
  835. $uniqId = $record['UniqId'];
  836. // 初始化尺码数据
  837. if (!in_array($size, $sizes)) {
  838. $sizes[] = $size;
  839. }
  840. // 为每条记录创建独立的分组
  841. $groupKey = $uniqId . '_' . $size;
  842. if (!isset($sizeSummary[$groupKey])) {
  843. $sizeSummary[$groupKey] = [
  844. '订单编号' => $record['订单编号'],
  845. '子订单编号' => $record['子订单编号'],
  846. '款号' => $record['款号'],
  847. '款式' => $record['款式'],
  848. '工序编号' => $record['工序编号'],
  849. '工序名称' => $record['工序名称'],
  850. '剩余数量' => $record['ci_num'],
  851. '数量' => $quantity, // 直接使用当前记录的数量
  852. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  853. '颜色' => $record['颜色'],
  854. '颜色备注' => $record['颜色备注'],
  855. '机台号' => $record['sczl_jtbh'],
  856. '组别' => $record['sczl_bh'],
  857. 'serial' => $serial, // 直接使用流水号,不需要数组
  858. 'sys_rq' => $record['sys_rq'],
  859. 'UniqId' => $record['UniqId'],
  860. '尺码数据' => [] // 用于存储尺码对应的数据
  861. ];
  862. }
  863. // 记录尺码数据
  864. if (!isset($sizeSummary[$groupKey]['尺码数据'][$size])) {
  865. $sizeSummary[$groupKey]['尺码数据'][$size] = [];
  866. }
  867. // 添加尺码对应的流水号和数量
  868. $sizeSummary[$groupKey]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  869. }
  870. // 处理最终记录
  871. $finalRecords = [];
  872. foreach ($sizeSummary as &$summary) {
  873. // 合并尺码数据到同一层级
  874. foreach ($summary['尺码数据'] as $size => $serials) {
  875. $summary[$size] = implode(', ', $serials); // 尺码对应的流水号和数量
  876. }
  877. unset($summary['尺码数据']); // 移除尺码数据数组
  878. $finalRecords[] = $summary;
  879. }
  880. // 尺码排序(数字在前,字母在后)
  881. usort($sizes, function ($a, $b) {
  882. $sizeOrder = [
  883. 'XXXS' => 1,
  884. 'XXS' => 2,
  885. 'XS' => 3,
  886. 'S' => 4,
  887. 'M' => 5,
  888. 'L' => 6,
  889. 'XL' => 7,
  890. '2XL' => 8,
  891. 'XXL' => 9,
  892. '3XL' => 10,
  893. 'XXXL' => 11,
  894. '4XL' => 12,
  895. 'XXXXL' => 13,
  896. '5XL' => 14,
  897. 'XXXXXL' => 15
  898. ];
  899. if (is_numeric($a) && is_numeric($b)) return $a - $b;
  900. if (is_numeric($a)) return -1;
  901. if (is_numeric($b)) return 1;
  902. return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  903. });
  904. // 将汇总合计记录添加到最前面
  905. array_unshift($finalRecords, $result);
  906. $this->success('请求成功', [
  907. 'result' => $results,//小票数据
  908. 'headers' => $sizes, // 尺码表头
  909. 'records' => $finalRecords // 查询历史记录
  910. ]);
  911. } else {
  912. // 查询历史记录
  913. $this->success('请求成功', [
  914. 'result' => '',
  915. 'headers' => '',
  916. 'records' => ''
  917. ]);
  918. }
  919. break;
  920. case '大烫':
  921. $gongxuname = '大烫';
  922. $gongxuncode = '5';
  923. //判断是否为标签二维码参数数据
  924. if (strpos($order, ',') !== false) {
  925. //将子订单编号, 机台号, 工序, 尺码, 数量, 尾包, 流水号(包)分割为数组
  926. $paramArray = explode(',', $order);
  927. //通过子订单编号,尺码, 数量,流水号(包)查询数据库是否存在(且未删除数据)
  928. $res = \db('设备_产量计酬')
  929. ->where([
  930. '子订单编号' => $paramArray[0],
  931. '尺码' => $paramArray[3],
  932. '数量' => $paramArray[4],
  933. 'serial' => $paramArray[6],
  934. 'mod_rq' => null,
  935. ])
  936. ->order('UniqId desc')
  937. ->find();
  938. //如果不存在, 则返回当前扫码小票无效
  939. if (!$res) {
  940. $this->success('当前扫码小票无效', [
  941. 'code' => 1,
  942. 'result' => '',
  943. 'headers' => '',
  944. 'records' => ''
  945. ]);
  946. return;
  947. }
  948. $orderlist = \db('工单_基本资料')
  949. ->where('订单编号', $res['订单编号'])
  950. ->field('订单编号, 生产款号, 款式')
  951. ->find();
  952. $yinjianlist = \db('工单_印件资料')
  953. ->where('订单编号', $res['订单编号'])
  954. ->field('zdtotal, sctotal,颜色,颜色备注')
  955. ->find();
  956. // if (empty($res)) {
  957. $ci_num = $paramArray[4];
  958. // } else {
  959. // $ci_num = $res["ci_num"];
  960. // }
  961. $results = [
  962. 'sys_sbID' => $params['sys_sbID'],//设备编号
  963. 'ci_num' => $ci_num,//剩余数量
  964. 'UniqId' => $res['UniqId'],
  965. 'order' => $paramArray[0],//子订单编号
  966. 'zb' => $params['sys_sbID'],//组别
  967. 'gx' => $gongxuncode,//默认固定工序
  968. 'cm' => $paramArray[3],//尺码
  969. 'sl' => $paramArray[4],//数量
  970. 'wb' => $paramArray[5],//尾包
  971. '订单编号' => $orderlist['订单编号'],
  972. '生产款号' => $orderlist['生产款号'],
  973. '款式' => $orderlist['款式'],
  974. 'sctotal' => $yinjianlist['sctotal'],
  975. 'zdtotal' => $yinjianlist['zdtotal'],
  976. '颜色' => $yinjianlist['颜色备注'],
  977. ];
  978. $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  979. $clwhere['订单编号'] = $res['订单编号'];
  980. $clwhere['工序名称'] = $gongxuname;
  981. $clwhere['mod_rq'] = null;
  982. $cljclist = \db('设备_产量计酬')
  983. ->field('订单编号,子订单编号,尺码,s_num as 数量,mod_rq')
  984. ->where($clwhere)
  985. ->select();
  986. $result = [];
  987. $totalQuantity = 0; // 总数量初始化
  988. $orderNumber = ''; // 订单编号初始化
  989. foreach ($cljclist as $item) {
  990. $size = $item['尺码'];
  991. $orderNumber = $item['订单编号'];
  992. if (isset($result[$size])) {
  993. $result[$size] += $item['数量'];
  994. } else {
  995. $result[$size] = $item['数量'];
  996. }
  997. // 累加总数量
  998. $totalQuantity += $item['数量'];
  999. }
  1000. foreach ($result as $size => $quantity) {
  1001. if ($size !== '订单编号' && $size !== '数量') {
  1002. $result[$size] = '合计(' . $quantity . ')';
  1003. }
  1004. }
  1005. $result['订单编号'] = $orderNumber;
  1006. $result['数量'] = '合计(' . $totalQuantity . ')';
  1007. $where = [];
  1008. if (strpos($paramArray[0], '-') !== false) {
  1009. $where['c.子订单编号'] = $paramArray[0];
  1010. } else {
  1011. $where['c.订单编号'] = ['like', '%' . $paramArray[0] . '%'];
  1012. }
  1013. $where['c.工序编号'] = $gongxuncode;
  1014. $where['c.mod_rq'] = null;
  1015. $existingRecords = \db('设备_产量计酬')->alias('c')
  1016. ->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,
  1017. ci_num,s_num,
  1018. y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注, CAST(c.serial_num AS SIGNED) as serial_num_numeric')
  1019. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1020. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1021. ->where($where)
  1022. ->order('c.sys_rq desc')
  1023. ->select();
  1024. $sizeSummary = [];
  1025. $sizes = [];
  1026. // 遍历数据库查询结果,根据 UniqId + 尺码 进行分组
  1027. foreach ($existingRecords as $record) {
  1028. $size = $record['尺码'];
  1029. $quantity = $record['数量'];
  1030. $serial = $record['serial'];
  1031. $uniqId = $record['UniqId'];
  1032. // 初始化尺码数据
  1033. if (!in_array($size, $sizes)) {
  1034. $sizes[] = $size;
  1035. }
  1036. // 为每条记录创建独立的分组
  1037. $groupKey = $uniqId . '_' . $size;
  1038. if (!isset($sizeSummary[$groupKey])) {
  1039. $sizeSummary[$groupKey] = [
  1040. '订单编号' => $record['订单编号'],
  1041. '子订单编号' => $record['子订单编号'],
  1042. '款号' => $record['款号'],
  1043. '款式' => $record['款式'],
  1044. '工序编号' => $record['工序编号'],
  1045. '工序名称' => $record['工序名称'],
  1046. '剩余数量' => $record['ci_num'],
  1047. '数量' => $quantity, // 直接使用当前记录的数量
  1048. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1049. '颜色' => $record['颜色'],
  1050. '颜色备注' => $record['颜色备注'],
  1051. '机台号' => $record['sczl_jtbh'],
  1052. '组别' => $record['sczl_bh'],
  1053. 'serial' => $serial, // 直接使用流水号,不需要数组
  1054. 'sys_rq' => $record['sys_rq'],
  1055. 'UniqId' => $record['UniqId'],
  1056. '尺码数据' => [] // 用于存储尺码对应的数据
  1057. ];
  1058. }
  1059. // 记录尺码数据
  1060. if (!isset($sizeSummary[$groupKey]['尺码数据'][$size])) {
  1061. $sizeSummary[$groupKey]['尺码数据'][$size] = [];
  1062. }
  1063. // 添加尺码对应的流水号和数量
  1064. $sizeSummary[$groupKey]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  1065. }
  1066. // 处理最终记录
  1067. $finalRecords = [];
  1068. foreach ($sizeSummary as &$summary) {
  1069. // 合并尺码数据到同一层级
  1070. foreach ($summary['尺码数据'] as $size => $serials) {
  1071. $summary[$size] = implode(', ', $serials); // 尺码对应的流水号和数量
  1072. }
  1073. unset($summary['尺码数据']); // 移除尺码数据数组
  1074. $finalRecords[] = $summary;
  1075. }
  1076. // 尺码排序(数字在前,字母在后)
  1077. usort($sizes, function ($a, $b) {
  1078. $sizeOrder = [
  1079. 'XXXS' => 1,
  1080. 'XXS' => 2,
  1081. 'XS' => 3,
  1082. 'S' => 4,
  1083. 'M' => 5,
  1084. 'L' => 6,
  1085. 'XL' => 7,
  1086. '2XL' => 8,
  1087. 'XXL' => 9,
  1088. '3XL' => 10,
  1089. 'XXXL' => 11,
  1090. '4XL' => 12,
  1091. 'XXXXL' => 13,
  1092. '5XL' => 14,
  1093. 'XXXXXL' => 15
  1094. ];
  1095. if (is_numeric($a) && is_numeric($b)) return $a - $b;
  1096. if (is_numeric($a)) return -1;
  1097. if (is_numeric($b)) return 1;
  1098. return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  1099. });
  1100. // 将汇总合计记录添加到最前面
  1101. array_unshift($finalRecords, $result);
  1102. $this->success('请求成功', [
  1103. 'result' => $results,//小票数据
  1104. 'headers' => $sizes, // 尺码表头
  1105. 'records' => $finalRecords // 查询历史记录
  1106. ]);
  1107. } else {
  1108. // 查询历史记录
  1109. $this->success('请求成功', [
  1110. 'result' => '',
  1111. 'headers' => '',
  1112. 'records' => ''
  1113. ]);
  1114. }
  1115. break;
  1116. case '总检':
  1117. //判断是否为子订单编号【$order:DC2503291-0703 子订单编号】
  1118. if (strpos($order, '-') !== false) {
  1119. // 定义查询字段
  1120. $field = "
  1121. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  1122. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  1123. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  1124. 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,
  1125. 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,
  1126. 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
  1127. ";
  1128. // 根据订单类型查询数据
  1129. if (preg_match('/-(\d{2})$/', $order, $matches)) {
  1130. $number = (int)$matches[1];
  1131. if ($number > 70) {
  1132. // 船样订单查询
  1133. $data = \db('工单_基本资料')->alias('j')
  1134. ->distinct('y.子订单编号')
  1135. ->field($field)
  1136. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1137. ->where('y.船样',1)
  1138. ->whereNull('y.Mod_rq')
  1139. ->where('y.子订单编号', $order)
  1140. ->select();
  1141. } else {
  1142. // 普通两位数子订单
  1143. $ddbh = explode('-', $order)[0];//获取订单编号
  1144. $data = \db('工单_基本资料')->alias('j')
  1145. ->distinct('y.子订单编号')
  1146. ->field($field)
  1147. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1148. ->where('y.船样',0)->whereNull('y.Mod_rq')
  1149. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  1150. ->select();
  1151. }
  1152. } else {
  1153. // 其他子订单
  1154. $ddbh = explode('-', $order)[0];//获取订单编号
  1155. $data = \db('工单_基本资料')->alias('j')
  1156. ->distinct('y.子订单编号')
  1157. ->field($field)
  1158. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1159. ->where('y.船样',0)->whereNull('y.Mod_rq')
  1160. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  1161. ->select();
  1162. }
  1163. // 处理查询到的数据
  1164. $dataEntry = [];
  1165. $list = [];
  1166. $headers = [];//存放尺码表头
  1167. foreach ($data as $item) {
  1168. // 处理尺码与数量映射
  1169. $cmScslMapping = [];
  1170. for ($i = 1; $i <= 14; $i++) {
  1171. $cmValue = $item['cm' . $i];
  1172. $scslValue = $item['scsl' . $i];
  1173. if (!is_null($cmValue) && $cmValue !== 0) {
  1174. $cmScslMapping[$cmValue] = ($scslValue === 0 || is_null($scslValue)) ? '' : $scslValue;
  1175. if (!in_array($cmValue, $headers)) {
  1176. $headers[] = $cmValue;
  1177. }
  1178. }
  1179. }
  1180. // 构建表格数据
  1181. $orderData = [
  1182. '订单编号' => $item['订单编号'],
  1183. '子订单编号' => $item['子订单编号'],
  1184. '款号' => $item['款号'],
  1185. '款式' => $item['款式'],
  1186. '颜色' => $item['颜色备注'],
  1187. 'zdtotal' => $item['zdtotal'],
  1188. 'sctotal' => $item['sctotal'],
  1189. '单位' => $item['单位'],
  1190. 'Sys_id' => $item['Sys_id'],
  1191. 'Sys_rq' => $item['Sys_rq'],
  1192. ];
  1193. $orderData = array_merge($orderData, $cmScslMapping);
  1194. $dataEntry[] = $orderData;
  1195. // 构建列表数据
  1196. $entry = [];
  1197. for ($i = 1; $i <= 14; $i++) {
  1198. $entry['cm' . $i] = ($item['cm' . $i] === 0 || is_null($item['cm' . $i])) ? '' : $item['cm' . $i];
  1199. $entry['cmsl' . $i] = ($item['cmsl' . $i] === 0 || is_null($item['cmsl' . $i])) ? '' : $item['cmsl' . $i];
  1200. $entry['scsl' . $i] = ($item['scsl' . $i] === 0 || is_null($item['scsl' . $i])) ? '' : $item['scsl' . $i];
  1201. }
  1202. $entry['订单编号'] = $item['订单编号'];
  1203. $entry['子订单编号'] = $item['子订单编号'];
  1204. $entry['UniqId'] = $item['UniqId'];
  1205. $entry['zdtotal'] = $item['zdtotal'];
  1206. $entry['颜色'] = $item['颜色'];
  1207. $entry['款式'] = $item['款式'];
  1208. $entry['款号'] = $item['款号'];
  1209. $entry['color_id'] = $item['color_id'];
  1210. $entry['颜色备注'] = $item['颜色备注'];
  1211. $list[] = $entry;
  1212. }
  1213. // 获取匹配的订单数据
  1214. $order = explode(',', $order)[0];
  1215. $matchedOrder = null;
  1216. foreach ($list as $mergedOrder) {
  1217. if ($mergedOrder['子订单编号'] === $order) {
  1218. $matchedOrder = $mergedOrder;
  1219. break;
  1220. }
  1221. }
  1222. // 检查实裁数是否为空
  1223. $allScslEmpty = true;
  1224. if ($matchedOrder) {
  1225. for ($i = 1; $i <= 14; $i++) {
  1226. if (!empty($matchedOrder["scsl$i"])) {
  1227. $allScslEmpty = false;
  1228. break;
  1229. }
  1230. }
  1231. }
  1232. if ($allScslEmpty) {
  1233. // 当前订单未裁剪 请裁剪后报工
  1234. $this->success('查询失败', [
  1235. 'code' => 1,
  1236. 'serial' => '',
  1237. 'serial_num' => '',
  1238. 'headers' => '',
  1239. 'records' => '',
  1240. ]);
  1241. return;
  1242. }
  1243. // 查询产量计酬记录
  1244. $clwhere = [
  1245. 'sczl_jtbh' => $params['sys_sbID'],
  1246. '子订单编号' => $order,
  1247. 'mod_rq' => null,
  1248. '工序名称' => $params['code']
  1249. ];
  1250. $cljclist = \db('设备_产量计酬')
  1251. ->field('订单编号,子订单编号,尺码,数量,mod_rq')
  1252. ->where($clwhere)
  1253. ->select();
  1254. // 汇总产量记录
  1255. $result = [];
  1256. $totalQuantity = 0;
  1257. $orderNumber = '';
  1258. foreach ($cljclist as $item) {
  1259. $size = $item['尺码'];
  1260. $orderNumber = $item['订单编号'];
  1261. if (isset($result[$size])) {
  1262. $result[$size] += $item['数量'];
  1263. } else {
  1264. $result[$size] = $item['数量'];
  1265. }
  1266. $totalQuantity += $item['数量'];
  1267. }
  1268. $result['订单编号'] = $orderNumber;
  1269. foreach ($result as $size => $quantity) {
  1270. if ($size !== '订单编号' && $size !== '数量') {
  1271. $result[$size] = '合计(' . $quantity . ')';
  1272. }
  1273. }
  1274. $result['数量'] = '合计(' . $totalQuantity . ')';
  1275. // 查询现有工序记录
  1276. $where = [
  1277. 'c.子订单编号' => $order,
  1278. 'c.工序编号' => '6',
  1279. 'c.mod_rq' => null
  1280. ];
  1281. $existingRecords = \db('设备_产量计酬')->alias('c')
  1282. ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码,
  1283. CASE
  1284. WHEN c.工序名称 IN ("裁剪", "车缝") THEN c.数量
  1285. ELSE c.s_num
  1286. END as 数量,
  1287. c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num,c.UniqId,
  1288. y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注')
  1289. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1290. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1291. ->where($where)
  1292. ->order('c.UniqId desc')
  1293. ->group('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num, j.款式')
  1294. ->select();
  1295. // 处理现有记录
  1296. $sizeSummary = [];
  1297. $sizes = [];
  1298. foreach ($existingRecords as $record) {
  1299. $size = $record['尺码'];
  1300. $quantity = $record['数量'];
  1301. $serial = $record['serial'];
  1302. $serialNum = $record['serial_num'];
  1303. // 收集尺码
  1304. if (!in_array($size, $sizes)) {
  1305. $sizes[] = $size;
  1306. }
  1307. // 初始化serial_num分组
  1308. if (!isset($sizeSummary[$serialNum])) {
  1309. $sizeSummary[$serialNum] = [
  1310. '订单编号' => $record['订单编号'],
  1311. '子订单编号' => $record['子订单编号'],
  1312. '款号' => $record['款号'],
  1313. '款式' => $record['款式'],
  1314. '工序编号' => $record['工序编号'],
  1315. '工序名称' => $record['工序名称'],
  1316. '数量' => 0,
  1317. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1318. '颜色' => $record['颜色'],
  1319. '颜色备注' => $record['颜色备注'],
  1320. '机台号' => $record['sczl_jtbh'],
  1321. '组别' => $record['sczl_bh'],
  1322. 'serial' => [],
  1323. 'sys_rq' => $record['sys_rq'],
  1324. 'UniqId' => $record['UniqId'],
  1325. '尺码数据' => []
  1326. ];
  1327. }
  1328. // 添加流水号
  1329. $sizeSummary[$serialNum]['serial'][] = $serial;
  1330. $sizeSummary[$serialNum]['数量'] += $quantity;
  1331. // 记录尺码数据
  1332. if (!isset($sizeSummary[$serialNum]['尺码数据'][$size])) {
  1333. $sizeSummary[$serialNum]['尺码数据'][$size] = [];
  1334. }
  1335. $sizeSummary[$serialNum]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  1336. }
  1337. // 最终记录处理
  1338. $finalRecords = [];
  1339. foreach ($sizeSummary as &$summary) {
  1340. // 排序并合并流水号
  1341. sort($summary['serial'], SORT_NUMERIC);
  1342. $summary['serial'] = implode(',', $summary['serial']);
  1343. // 合并尺码数据
  1344. foreach ($summary['尺码数据'] as $size => $serials) {
  1345. $summary[$size] = implode(', ', $serials);
  1346. }
  1347. unset($summary['尺码数据']);
  1348. $finalRecords[] = $summary;
  1349. }
  1350. // 尺码排序
  1351. $sizeOrder = [
  1352. 'XXXS' => 1, 'XXS' => 2, 'XS' => 3, 'S' => 4, 'M' => 5,
  1353. 'L' => 6, 'XL' => 7, '2XL' => 8, 'XXL' => 9, '3XL' => 10,
  1354. 'XXXL' => 11, '4XL' => 12, 'XXXXL' => 13, '5XL' => 14, 'XXXXXL' => 15
  1355. ];
  1356. usort($sizes, function ($a, $b) use ($sizeOrder) {
  1357. if (is_numeric($a) && is_numeric($b)) return $a - $b;
  1358. if (is_numeric($a)) return -1;
  1359. if (is_numeric($b)) return 1;
  1360. return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  1361. });
  1362. // 添加汇总记录到最前面
  1363. array_unshift($finalRecords, $result);
  1364. // 查询流水号信息
  1365. $res_serial = \db('设备_产量计酬')
  1366. ->field('子订单编号, MAX(CAST(serial AS UNSIGNED)) as serial, MAX(CAST(serial_num AS UNSIGNED)) as serial_num')
  1367. ->where('子订单编号', $order)
  1368. ->whereNull('mod_rq')
  1369. // ->where('sczl_jtbh', $params['sys_sbID'])
  1370. ->group('子订单编号')
  1371. ->find();
  1372. // 处理流水号
  1373. $res_serial['serial'] = empty($res_serial['serial']) ? '000' : str_pad($res_serial['serial'], 3, '0', STR_PAD_LEFT);
  1374. $res_serial['serial_num'] = empty($res_serial['serial_num']) ? '000' : str_pad($res_serial['serial_num'], 3, '0', STR_PAD_LEFT);
  1375. $next_serial = str_pad((int)$res_serial['serial'] + 1, 3, '0', STR_PAD_LEFT);
  1376. $next_serial_num = str_pad((int)$res_serial['serial_num'] + 1, 3, '0', STR_PAD_LEFT);
  1377. $this->success('请求成功', [
  1378. 'serial' => $next_serial,
  1379. 'serial_num' => $next_serial_num,
  1380. 'list' => $matchedOrder,
  1381. 'headers' => $sizes,
  1382. 'records' => $finalRecords
  1383. ]);
  1384. } else {
  1385. // 按照订单编号查询
  1386. $this->success('请求成功', [
  1387. 'serial' => '',
  1388. 'serial_num' => '',
  1389. 'list' => '',
  1390. 'headers' => '',
  1391. 'records' => ''
  1392. ]);
  1393. }
  1394. // //判断是否为子订单编号【$order:DC2503291-0703 子订单编号】
  1395. // if (strpos($order, '-') !== false) {
  1396. // $field = "
  1397. // y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  1398. // y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  1399. // y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  1400. // 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,
  1401. // 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,
  1402. // 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
  1403. // ";
  1404. // if (preg_match('/-(\d{2})$/', $order, $matches)) {
  1405. // $number = (int)$matches[1];
  1406. // if ($number > 70) {
  1407. // $data = \db('工单_基本资料')->alias('j')
  1408. // ->distinct('y.子订单编号')
  1409. // ->field($field)
  1410. // ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1411. // ->where('y.船样',1)
  1412. // ->whereNull('y.Mod_rq')
  1413. // ->where('y.子订单编号', $order)
  1414. // ->select();
  1415. // } else {
  1416. // //echo '两位数子订单';
  1417. // $ddbh = explode('-', $order)[0];//获取订单编号
  1418. // //【查询尺码、制单数、填写报工新增实裁数】
  1419. // $data = \db('工单_基本资料')->alias('j')
  1420. // ->distinct('y.子订单编号')
  1421. // ->field($field)
  1422. // ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1423. // ->where('y.船样',0)->whereNull('y.Mod_rq')
  1424. // ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  1425. // ->select();
  1426. // }
  1427. // }else{
  1428. // $ddbh = explode('-', $order)[0];//获取订单编号
  1429. // //【查询尺码、制单数、填写报工新增实裁数】
  1430. // $data = \db('工单_基本资料')->alias('j')
  1431. // ->distinct('y.子订单编号')
  1432. // ->field($field)
  1433. // ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1434. // ->where('y.船样',0)->whereNull('y.Mod_rq')
  1435. // ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  1436. // ->select();
  1437. // }
  1438. //
  1439. // $dataEntry = [];
  1440. // $list = [];
  1441. // $headers = [];//存放尺码表头
  1442. // foreach ($data as $item) {
  1443. // // 初始化尺码与数量映射数组
  1444. // $cmScslMapping = [];
  1445. // for ($i = 1; $i <= 14; $i++) {
  1446. // $cmValue = $item['cm' . $i];
  1447. // $scslValue = $item['scsl' . $i];
  1448. // if (!is_null($cmValue) && $cmValue !== 0) {
  1449. // $cmScslMapping[$cmValue] = ($scslValue === 0 || is_null($scslValue)) ? '' : $scslValue;
  1450. // if (!in_array($cmValue, $headers)) {
  1451. // $headers[] = $cmValue;
  1452. // }
  1453. // }
  1454. // }
  1455. //
  1456. // // 将尺码数据平铺到主要数据中(表格数据)
  1457. // $orderData = [
  1458. // '订单编号' => $item['订单编号'],
  1459. // '子订单编号' => $item['子订单编号'],
  1460. // '款号' => $item['款号'],
  1461. // '款式' => $item['款式'],
  1462. // '颜色' => $item['颜色备注'],
  1463. // 'zdtotal' => $item['zdtotal'],
  1464. // 'sctotal' => $item['sctotal'],
  1465. // '单位' => $item['单位'],
  1466. // 'Sys_id' => $item['Sys_id'],
  1467. // 'Sys_rq' => $item['Sys_rq'],
  1468. // ];
  1469. // // 平铺尺码映射数据
  1470. // $orderData = array_merge($orderData, $cmScslMapping);
  1471. // $dataEntry[] = $orderData;
  1472. // // list 列表部分
  1473. // $entry = [];
  1474. // for ($i = 1; $i <= 14; $i++) {
  1475. // $entry['cm' . $i] = ($item['cm' . $i] === 0 || is_null($item['cm' . $i])) ? '' : $item['cm' . $i];
  1476. // $entry['cmsl' . $i] = ($item['cmsl' . $i] === 0 || is_null($item['cmsl' . $i])) ? '' : $item['cmsl' . $i];
  1477. // $entry['scsl' . $i] = ($item['scsl' . $i] === 0 || is_null($item['scsl' . $i])) ? '' : $item['scsl' . $i];
  1478. // }
  1479. // $entry['订单编号'] = $item['订单编号'];
  1480. // $entry['子订单编号'] = $item['子订单编号'];
  1481. // $entry['UniqId'] = $item['UniqId'];
  1482. // $entry['zdtotal'] = $item['zdtotal'];
  1483. // $entry['颜色'] = $item['颜色'];
  1484. // $entry['款式'] = $item['款式'];
  1485. // $entry['款号'] = $item['款号'];
  1486. // $entry['color_id'] = $item['color_id'];
  1487. // $entry['颜色备注'] = $item['颜色备注'];
  1488. // $list[] = $entry;
  1489. // }
  1490. //
  1491. //
  1492. // // 如果 $order 含逗号,取第一个逗号前的子订单编号
  1493. // $order = explode(',', $order)[0];
  1494. // $matchedOrder = null;
  1495. // foreach ($list as $mergedOrder) {
  1496. // if ($mergedOrder['子订单编号'] === $order) {
  1497. // $matchedOrder = $mergedOrder;
  1498. // break;
  1499. // }
  1500. // }
  1501. // $allScslEmpty = true;
  1502. // for ($i = 1; $i <= 14; $i++) {
  1503. // if (!empty($matchedOrder["scsl$i"])) {
  1504. // $allScslEmpty = false;
  1505. // break;
  1506. // }
  1507. // }
  1508. //
  1509. // if ($allScslEmpty) {
  1510. // //当前订单未裁剪 请裁剪后报工
  1511. // $this->success('查询失败', [
  1512. // 'code' => 1,
  1513. // 'serial' => '',
  1514. // 'serial_num' => '',
  1515. // 'headers' => '',
  1516. // 'records' => '',
  1517. // ]);
  1518. // return;
  1519. // }
  1520. //
  1521. // $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  1522. // $clwhere['子订单编号'] = $order;
  1523. // $clwhere['mod_rq'] = null;
  1524. // $clwhere['工序名称'] = $params['code'];
  1525. // $cljclist = \db('设备_产量计酬')
  1526. // ->field('订单编号,子订单编号,尺码,数量,mod_rq')
  1527. // ->where($clwhere)
  1528. // ->select();
  1529. // $result = [];
  1530. // $totalQuantity = 0; // 总数量初始化
  1531. // $orderNumber = ''; // 订单编号初始化
  1532. // foreach ($cljclist as $item) {
  1533. // $size = $item['尺码'];
  1534. // $orderNumber = $item['订单编号'];
  1535. // if (isset($result[$size])) {
  1536. // $result[$size] += $item['数量'];
  1537. // } else {
  1538. // $result[$size] = $item['数量'];
  1539. // }
  1540. // // 累加总数量
  1541. // $totalQuantity += $item['数量'];
  1542. // }
  1543. // $result['订单编号'] = $orderNumber;
  1544. // foreach ($result as $size => $quantity) {
  1545. // if ($size !== '订单编号' && $size !== '数量') {
  1546. // $result[$size] = '合计(' . $quantity . ')';
  1547. // }
  1548. // }
  1549. // $result['数量'] = '合计(' . $totalQuantity . ')';
  1550. //
  1551. // $where = [];
  1552. // if (strpos($order, '-') !== false) {
  1553. // $where['c.子订单编号'] = $order;
  1554. // } else {
  1555. // $where['c.订单编号'] = ['like', '%' . $order . '%'];
  1556. // }
  1557. //
  1558. // $where['c.工序编号'] = '6';
  1559. // $where['c.mod_rq'] = null;
  1560. // $existingRecords = \db('设备_产量计酬')->alias('c')
  1561. // ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码,
  1562. // CASE
  1563. // WHEN c.工序名称 IN ("裁剪", "车缝") THEN c.数量
  1564. // ELSE c.s_num
  1565. // END as 数量,
  1566. // c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num,c.UniqId,
  1567. // y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注')
  1568. // ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1569. // ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1570. // ->where($where)
  1571. // ->order('c.UniqId desc')
  1572. // ->group('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num, j.款式')
  1573. // ->select();
  1574. //
  1575. // $sizeSummary = [];
  1576. // $sizes = [];
  1577. // foreach ($existingRecords as $record) {
  1578. // $size = $record['尺码'];
  1579. // $quantity = $record['数量'];
  1580. // $serial = $record['serial'];
  1581. // $serialNum = $record['serial_num']; // serial_num 分组
  1582. // $processNumber = $record['工序编号']; // 工序编号
  1583. // // 初始化尺码
  1584. // if (!in_array($size, $sizes)) {
  1585. // $sizes[] = $size;
  1586. // }
  1587. // // 检查是否已有相同的 serial_num 组合
  1588. // if (!isset($sizeSummary[$serialNum])) {
  1589. // $sizeSummary[$serialNum] = [
  1590. // '订单编号' => $record['订单编号'],
  1591. // '子订单编号' => $record['子订单编号'],
  1592. // '款号' => $record['款号'],
  1593. // '款式' => $record['款式'],
  1594. // '工序编号' => $record['工序编号'],
  1595. // '工序名称' => $record['工序名称'],
  1596. // '数量' => '0', // 记录总数量
  1597. // '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1598. // '颜色' => $record['颜色'],
  1599. // '颜色备注' => $record['颜色备注'],
  1600. // '机台号' => $record['sczl_jtbh'],
  1601. // '组别' => $record['sczl_bh'],
  1602. // 'serial' => [], // 流水号数组
  1603. // 'sys_rq' => $record['sys_rq'],
  1604. // 'UniqId' => $record['UniqId'],
  1605. // '尺码数据' => [] // 用于存储每个尺码对应的流水号和数量
  1606. // ];
  1607. // }
  1608. //
  1609. // // 将流水号添加到 serial 数组
  1610. // $sizeSummary[$serialNum]['serial'][] = $serial;
  1611. //
  1612. // // 累加数量到总数量
  1613. // $sizeSummary[$serialNum]['数量'] += $quantity;
  1614. //
  1615. // // 记录尺码数据
  1616. // if (!isset($sizeSummary[$serialNum]['尺码数据'][$size])) {
  1617. // $sizeSummary[$serialNum]['尺码数据'][$size] = [];
  1618. // }
  1619. //
  1620. // // 添加尺码对应的流水号和数量
  1621. // $sizeSummary[$serialNum]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  1622. // }
  1623. //
  1624. // // 对每个工序的 serial 进行处理,并转换为逗号分隔的字符串
  1625. // $finalRecords = [];
  1626. // foreach ($sizeSummary as &$summary) {
  1627. // // 将 serial 数组按数值降序排序
  1628. // sort($summary['serial'], SORT_NUMERIC);
  1629. // $summary['serial'] = implode(',', $summary['serial']);
  1630. //
  1631. // // 合并尺码数据到同一层级
  1632. // foreach ($summary['尺码数据'] as $size => $serials) {
  1633. // $summary[$size] = implode(', ', $serials); // 尺码对应的流水号和数量
  1634. // }
  1635. //
  1636. // unset($summary['尺码数据']); // 移除尺码数据数组,直接将数据合并到 summary
  1637. // $finalRecords[] = $summary;
  1638. // }
  1639. //
  1640. // // 尺码排序(数字在前,字母在后)
  1641. // usort($sizes, function ($a, $b) {
  1642. // $sizeOrder = [
  1643. // 'XXXS' => 1,
  1644. // 'XXS' => 2,
  1645. // 'XS' => 3,
  1646. // 'S' => 4,
  1647. // 'M' => 5,
  1648. // 'L' => 6,
  1649. // 'XL' => 7,
  1650. // '2XL' => 8,
  1651. // 'XXL' => 9,
  1652. // '3XL' => 10,
  1653. // 'XXXL' => 11,
  1654. // '4XL' => 12,
  1655. // 'XXXXL' => 13,
  1656. // '5XL' => 14,
  1657. // 'XXXXXL' => 15
  1658. // ];
  1659. // if (is_numeric($a) && is_numeric($b)) return $a - $b;
  1660. // if (is_numeric($a)) return -1;
  1661. // if (is_numeric($b)) return 1;
  1662. // return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  1663. // });
  1664. //
  1665. // // 将汇总合计记录添加到最前面
  1666. // array_unshift($finalRecords, $result);
  1667. //
  1668. // // 查询流水号(包数)序号
  1669. // $res_serial = \db('设备_产量计酬')
  1670. // ->field('子订单编号, MAX(CAST(serial AS UNSIGNED)) as serial, MAX(CAST(serial_num AS UNSIGNED)) as serial_num')
  1671. // ->where('子订单编号', $order)
  1672. // ->whereNull('mod_rq')// 查询未删除数据
  1673. // ->where('sczl_jtbh', $params['sys_sbID'])
  1674. // ->group('子订单编号')
  1675. // ->find();
  1676. // // 默认值填充(字符串带前导零)
  1677. // $res_serial['serial'] = empty($res_serial['serial']) ? '000' : str_pad($res_serial['serial'], 3, '0', STR_PAD_LEFT);
  1678. // $res_serial['serial_num'] = empty($res_serial['serial_num']) ? '000' : str_pad($res_serial['serial_num'], 3, '0', STR_PAD_LEFT);
  1679. // // 先加 1,再格式化为 3 位
  1680. // $next_serial = str_pad((int)$res_serial['serial'] + 1, 3, '0', STR_PAD_LEFT);
  1681. // $next_serial_num = str_pad((int)$res_serial['serial_num'] + 1, 3, '0', STR_PAD_LEFT);
  1682. //
  1683. // $this->success('请求成功', [
  1684. // 'serial' => $next_serial, // 流水号
  1685. // 'serial_num' => $next_serial_num, // 序号
  1686. // 'list' => $matchedOrder, // 报工前需要的数据
  1687. // 'headers' => $sizes, // 尺码表头
  1688. // 'records' =>$finalRecords // 查询报工后历史记录
  1689. // ]);
  1690. // }else{
  1691. // //按照订单编号查询
  1692. // $this->success('请求成功', [
  1693. // 'serial' => '', // // 流水号(包数)
  1694. // 'serial_num' => '', // 序号
  1695. // 'list' => '', // 报工
  1696. // 'headers' => '',// 尺码表头
  1697. // 'records' => '' // 查询历史记录
  1698. // ]);
  1699. // }
  1700. break;
  1701. case '包装':
  1702. //判断是否为子订单编号【$order:DC2503291-0703 子订单编号】
  1703. if (strpos($order, '-') !== false) {
  1704. // 定义查询字段
  1705. $field = "
  1706. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  1707. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  1708. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  1709. 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,
  1710. 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,
  1711. 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
  1712. ";
  1713. // 根据订单类型查询数据
  1714. if (preg_match('/-(\d{2})$/', $order, $matches)) {
  1715. $number = (int)$matches[1];
  1716. if ($number > 70) {
  1717. // 船样订单查询
  1718. $data = \db('工单_基本资料')->alias('j')
  1719. ->distinct('y.子订单编号')
  1720. ->field($field)
  1721. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1722. ->where('y.船样',1)
  1723. ->whereNull('y.Mod_rq')
  1724. ->where('y.子订单编号', $order)
  1725. ->select();
  1726. } else {
  1727. // 普通两位数子订单
  1728. $ddbh = explode('-', $order)[0];//获取订单编号
  1729. $data = \db('工单_基本资料')->alias('j')
  1730. ->distinct('y.子订单编号')
  1731. ->field($field)
  1732. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1733. ->where('y.船样',0)->whereNull('y.Mod_rq')
  1734. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  1735. ->select();
  1736. }
  1737. } else {
  1738. // 其他子订单
  1739. $ddbh = explode('-', $order)[0];//获取订单编号
  1740. $data = \db('工单_基本资料')->alias('j')
  1741. ->distinct('y.子订单编号')
  1742. ->field($field)
  1743. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  1744. ->where('y.船样',0)->whereNull('y.Mod_rq')
  1745. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  1746. ->select();
  1747. }
  1748. // 处理查询到的数据
  1749. $dataEntry = [];
  1750. $list = [];
  1751. $headers = [];//存放尺码表头
  1752. foreach ($data as $item) {
  1753. // 处理尺码与数量映射
  1754. $cmScslMapping = [];
  1755. for ($i = 1; $i <= 14; $i++) {
  1756. $cmValue = $item['cm' . $i];
  1757. $scslValue = $item['scsl' . $i];
  1758. if (!is_null($cmValue) && $cmValue !== 0) {
  1759. $cmScslMapping[$cmValue] = ($scslValue === 0 || is_null($scslValue)) ? '' : $scslValue;
  1760. if (!in_array($cmValue, $headers)) {
  1761. $headers[] = $cmValue;
  1762. }
  1763. }
  1764. }
  1765. // 构建表格数据
  1766. $orderData = [
  1767. '订单编号' => $item['订单编号'],
  1768. '子订单编号' => $item['子订单编号'],
  1769. '款号' => $item['款号'],
  1770. '款式' => $item['款式'],
  1771. '颜色' => $item['颜色备注'],
  1772. 'zdtotal' => $item['zdtotal'],
  1773. 'sctotal' => $item['sctotal'],
  1774. '单位' => $item['单位'],
  1775. 'Sys_id' => $item['Sys_id'],
  1776. 'Sys_rq' => $item['Sys_rq'],
  1777. ];
  1778. $orderData = array_merge($orderData, $cmScslMapping);
  1779. $dataEntry[] = $orderData;
  1780. // 构建列表数据
  1781. $entry = [];
  1782. for ($i = 1; $i <= 14; $i++) {
  1783. $entry['cm' . $i] = ($item['cm' . $i] === 0 || is_null($item['cm' . $i])) ? '' : $item['cm' . $i];
  1784. $entry['cmsl' . $i] = ($item['cmsl' . $i] === 0 || is_null($item['cmsl' . $i])) ? '' : $item['cmsl' . $i];
  1785. $entry['scsl' . $i] = ($item['scsl' . $i] === 0 || is_null($item['scsl' . $i])) ? '' : $item['scsl' . $i];
  1786. }
  1787. $entry['订单编号'] = $item['订单编号'];
  1788. $entry['子订单编号'] = $item['子订单编号'];
  1789. $entry['UniqId'] = $item['UniqId'];
  1790. $entry['zdtotal'] = $item['zdtotal'];
  1791. $entry['颜色'] = $item['颜色'];
  1792. $entry['款式'] = $item['款式'];
  1793. $entry['款号'] = $item['款号'];
  1794. $entry['color_id'] = $item['color_id'];
  1795. $entry['颜色备注'] = $item['颜色备注'];
  1796. $list[] = $entry;
  1797. }
  1798. // 获取匹配的订单数据
  1799. $order = explode(',', $order)[0];
  1800. $matchedOrder = null;
  1801. foreach ($list as $mergedOrder) {
  1802. if ($mergedOrder['子订单编号'] === $order) {
  1803. $matchedOrder = $mergedOrder;
  1804. break;
  1805. }
  1806. }
  1807. // 检查实裁数是否为空
  1808. $allScslEmpty = true;
  1809. if ($matchedOrder) {
  1810. for ($i = 1; $i <= 14; $i++) {
  1811. if (!empty($matchedOrder["scsl$i"])) {
  1812. $allScslEmpty = false;
  1813. break;
  1814. }
  1815. }
  1816. }
  1817. if ($allScslEmpty) {
  1818. // 当前子订单未裁剪 请裁剪后报工
  1819. $this->success('当前子订单未裁剪 请裁剪后报工', [
  1820. 'code' => 1,
  1821. 'serial' => '',
  1822. 'serial_num' => '',
  1823. 'headers' => '',
  1824. 'records' => '',
  1825. ]);
  1826. return;
  1827. }
  1828. // 查询产量计酬记录
  1829. $clwhere = [
  1830. 'sczl_jtbh' => $params['sys_sbID'],
  1831. '子订单编号' => $order,
  1832. 'mod_rq' => null,
  1833. '工序名称' => $params['code']
  1834. ];
  1835. $cljclist = \db('设备_产量计酬')
  1836. ->field('订单编号,子订单编号,尺码,数量,mod_rq')
  1837. ->where($clwhere)
  1838. ->select();
  1839. // 汇总产量记录
  1840. $result = [];
  1841. $totalQuantity = 0;
  1842. $orderNumber = '';
  1843. foreach ($cljclist as $item) {
  1844. $size = $item['尺码'];
  1845. $orderNumber = $item['订单编号'];
  1846. if (isset($result[$size])) {
  1847. $result[$size] += $item['数量'];
  1848. } else {
  1849. $result[$size] = $item['数量'];
  1850. }
  1851. $totalQuantity += $item['数量'];
  1852. }
  1853. $result['订单编号'] = $orderNumber;
  1854. foreach ($result as $size => $quantity) {
  1855. if ($size !== '订单编号' && $size !== '数量') {
  1856. $result[$size] = '合计(' . $quantity . ')';
  1857. }
  1858. }
  1859. $result['数量'] = '合计(' . $totalQuantity . ')';
  1860. // 查询现有工序记录
  1861. $where = [
  1862. 'c.子订单编号' => $order,
  1863. 'c.工序编号' => '7',
  1864. 'c.mod_rq' => null
  1865. ];
  1866. $existingRecords = \db('设备_产量计酬')->alias('c')
  1867. ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码,
  1868. CASE
  1869. WHEN c.工序名称 IN ("裁剪", "车缝") THEN c.数量
  1870. ELSE c.s_num
  1871. END as 数量,
  1872. c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num,c.UniqId,
  1873. y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注')
  1874. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1875. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1876. ->where($where)
  1877. ->order('c.UniqId desc')
  1878. ->group('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num, j.款式')
  1879. ->select();
  1880. // 处理现有记录
  1881. $sizeSummary = [];
  1882. $sizes = [];
  1883. foreach ($existingRecords as $record) {
  1884. $size = $record['尺码'];
  1885. $quantity = $record['数量'];
  1886. $serial = $record['serial'];
  1887. $serialNum = $record['serial_num'];
  1888. // 收集尺码
  1889. if (!in_array($size, $sizes)) {
  1890. $sizes[] = $size;
  1891. }
  1892. // 初始化serial_num分组
  1893. if (!isset($sizeSummary[$serialNum])) {
  1894. $sizeSummary[$serialNum] = [
  1895. '订单编号' => $record['订单编号'],
  1896. '子订单编号' => $record['子订单编号'],
  1897. '款号' => $record['款号'],
  1898. '款式' => $record['款式'],
  1899. '工序编号' => $record['工序编号'],
  1900. '工序名称' => $record['工序名称'],
  1901. '数量' => 0,
  1902. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1903. '颜色' => $record['颜色'],
  1904. '颜色备注' => $record['颜色备注'],
  1905. '机台号' => $record['sczl_jtbh'],
  1906. '组别' => $record['sczl_bh'],
  1907. 'serial' => [],
  1908. 'sys_rq' => $record['sys_rq'],
  1909. 'UniqId' => $record['UniqId'],
  1910. '尺码数据' => []
  1911. ];
  1912. }
  1913. // 添加流水号
  1914. $sizeSummary[$serialNum]['serial'][] = $serial;
  1915. $sizeSummary[$serialNum]['数量'] += $quantity;
  1916. // 记录尺码数据
  1917. if (!isset($sizeSummary[$serialNum]['尺码数据'][$size])) {
  1918. $sizeSummary[$serialNum]['尺码数据'][$size] = [];
  1919. }
  1920. $sizeSummary[$serialNum]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  1921. }
  1922. // 最终记录处理
  1923. $finalRecords = [];
  1924. foreach ($sizeSummary as &$summary) {
  1925. // 排序并合并流水号
  1926. sort($summary['serial'], SORT_NUMERIC);
  1927. $summary['serial'] = implode(',', $summary['serial']);
  1928. // 合并尺码数据
  1929. foreach ($summary['尺码数据'] as $size => $serials) {
  1930. $summary[$size] = implode(', ', $serials);
  1931. }
  1932. unset($summary['尺码数据']);
  1933. $finalRecords[] = $summary;
  1934. }
  1935. // 尺码排序
  1936. $sizeOrder = [
  1937. 'XXXS' => 1, 'XXS' => 2, 'XS' => 3, 'S' => 4, 'M' => 5,
  1938. 'L' => 6, 'XL' => 7, '2XL' => 8, 'XXL' => 9, '3XL' => 10,
  1939. 'XXXL' => 11, '4XL' => 12, 'XXXXL' => 13, '5XL' => 14, 'XXXXXL' => 15
  1940. ];
  1941. usort($sizes, function ($a, $b) use ($sizeOrder) {
  1942. if (is_numeric($a) && is_numeric($b)) return $a - $b;
  1943. if (is_numeric($a)) return -1;
  1944. if (is_numeric($b)) return 1;
  1945. return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  1946. });
  1947. // 添加汇总记录到最前面
  1948. array_unshift($finalRecords, $result);
  1949. // 查询流水号信息
  1950. $res_serial = \db('设备_产量计酬')
  1951. ->field('子订单编号, MAX(CAST(serial AS UNSIGNED)) as serial, MAX(CAST(serial_num AS UNSIGNED)) as serial_num')
  1952. ->where('子订单编号', $order)
  1953. ->whereNull('mod_rq')
  1954. ->where('sczl_jtbh', $params['sys_sbID'])
  1955. ->group('子订单编号')
  1956. ->find();
  1957. // 处理流水号
  1958. $res_serial['serial'] = empty($res_serial['serial']) ? '000' : str_pad($res_serial['serial'], 3, '0', STR_PAD_LEFT);
  1959. $res_serial['serial_num'] = empty($res_serial['serial_num']) ? '000' : str_pad($res_serial['serial_num'], 3, '0', STR_PAD_LEFT);
  1960. $next_serial = str_pad((int)$res_serial['serial'] + 1, 3, '0', STR_PAD_LEFT);
  1961. $next_serial_num = str_pad((int)$res_serial['serial_num'] + 1, 3, '0', STR_PAD_LEFT);
  1962. $this->success('请求成功', [
  1963. 'serial' => $next_serial,
  1964. 'serial_num' => $next_serial_num,
  1965. 'list' => $matchedOrder,
  1966. 'headers' => $sizes,
  1967. 'records' => $finalRecords
  1968. ]);
  1969. } else {
  1970. // 按照订单编号查询
  1971. $this->success('请求成功', [
  1972. 'serial' => '',
  1973. 'serial_num' => '',
  1974. 'list' => '',
  1975. 'headers' => '',
  1976. 'records' => ''
  1977. ]);
  1978. }
  1979. break;
  1980. default:
  1981. $this->error('无效的 code 参数');
  1982. break;
  1983. }
  1984. }
  1985. /**
  1986. * 出库报工日志
  1987. * 确认出库 恢复出库状态
  1988. */
  1989. public function getTab() {
  1990. if (!$this->request->isPost()) {
  1991. $this->error('非法请求');
  1992. }
  1993. $params = $this->request->param();
  1994. // 校验子订单编号和出库日期
  1995. if (empty($params['order_id'])) {
  1996. $this->error('子订单编号不能为空');
  1997. }
  1998. // 处理出库逻辑
  1999. if ($params['code'] == '出库') {
  2000. if (strpos($params['order_id'], '-') !== false) {
  2001. // 查询传入的子订单编号的记录,获取其颜色备注
  2002. $cklist = \db('工单_印件资料')
  2003. ->field('子订单编号, ck_rq, 颜色备注')
  2004. ->where('子订单编号', $params['order_id'])
  2005. ->find();
  2006. if ($cklist) {
  2007. if (!empty($cklist['ck_rq'])) {
  2008. $this->error('该子订单已完成出库操作,请勿重复操作。');
  2009. } else {
  2010. // 获取相同颜色备注的所有子订单记录
  2011. $sameColorList = \db('工单_印件资料')
  2012. ->field('子订单编号, ck_rq')
  2013. ->where('颜色备注', $cklist['颜色备注'])
  2014. ->whereNull('ck_rq') // 只选择未出库的记录
  2015. ->select();
  2016. // 开始出库操作
  2017. $updateCount1 = 0;
  2018. foreach ($sameColorList as $item) {
  2019. // 更新 '工单_印件资料' 表中所有相同颜色备注的子订单的出库日期
  2020. $result1 = \db('工单_印件资料')
  2021. ->where('子订单编号', $item['子订单编号'])
  2022. ->update(['ck_rq' => date("Y-m-d H:i:s")]);
  2023. if ($result1 !== false) {
  2024. $updateCount1 += $result1;
  2025. }
  2026. }
  2027. // 检查 '工单_基本资料' 表的 '出库日期' 是否为空
  2028. $order = explode('-', $params['order_id'])[0];
  2029. $basicInfo = \db('工单_基本资料')
  2030. ->field('出库日期')
  2031. ->where('订单编号', $order)
  2032. ->find();
  2033. $updateCount2 = 0;
  2034. if (empty($basicInfo['出库日期'])) {
  2035. // 如果 '出库日期' 为空,则更新
  2036. $jbzl = \db('工单_基本资料')
  2037. ->where('订单编号', $order)
  2038. ->fetchSql(true)
  2039. ->update(['出库日期' => date("Y-m-d H:i:s")]);
  2040. $result2 = \db()->query($jbzl);
  2041. if ($result2 !== false) {
  2042. $updateCount2 = $result2;
  2043. }
  2044. }
  2045. // if ($updateCount1 > 0) {
  2046. // // 插入日志
  2047. // foreach ($sameColorList as $item) {
  2048. // $addlist = [
  2049. // 'order_id' => $item['子订单编号'],
  2050. // 'rq' => date("Y-m-d H:i:s"),
  2051. // 'name' => $params['code'],
  2052. // 'sys_id' => $params['sys_id']
  2053. // ];
  2054. // \db('库存_出入库明细')->insertGetId($addlist);
  2055. // }
  2056. // $this->success("出库成功");
  2057. // } else {
  2058. // $this->error('出库失败');
  2059. // }
  2060. }
  2061. } else {
  2062. $this->error('未找到对应的出库订单');
  2063. }
  2064. } else {
  2065. $this->error('请扫描子订单编号进行出库');
  2066. }
  2067. }
  2068. // 处理恢复逻辑
  2069. else if ($params['code'] == '恢复') {
  2070. $cklist = \db('工单_印件资料')
  2071. ->field('子订单编号, ck_rq')
  2072. ->where('子订单编号', $params['order_id'])
  2073. ->find();
  2074. if (!empty($cklist['ck_rq'])) {
  2075. $result1 = \db('工单_印件资料')
  2076. ->where('子订单编号', $params['order_id'])
  2077. ->fetchSql(true)
  2078. ->update(['ck_rq' => null]);
  2079. $updateCount1 = \db()->query($result1);
  2080. // 更新 '工单_基本资料' 表
  2081. $order = explode('-', $params['order_id'])[0];
  2082. $result2 = \db('工单_基本资料')
  2083. ->where('订单编号', $order)
  2084. ->fetchSql(true)
  2085. ->update(['出库日期' => null,'gd_statu'=>'1-计划中']);
  2086. $updateCount2 = \db()->query($result2);
  2087. // if ($updateCount1 > 0 && $updateCount2 > 0) {
  2088. // // 插入日志
  2089. // $addlist = [
  2090. // 'order_id' => $params['order_id'],
  2091. // 'rq' => date("Y-m-d H:i:s"),
  2092. // 'name' => $params['code'],
  2093. // 'sys_id' => $params['sys_id']
  2094. // ];
  2095. // \db('库存_出入库明细')->insertGetId($addlist);
  2096. // $this->success($params['order_id'] . "恢复未出库");
  2097. // } else {
  2098. // $this->error($params['order_id'] . '该子订单是未出库状态,无需再次恢复');
  2099. // }
  2100. } else {
  2101. $this->error('该子订单是未出库状态,无需再次恢复');
  2102. }
  2103. }
  2104. }
  2105. /**
  2106. * 裁剪完工报工修改工单_印件资料表一条数据
  2107. */
  2108. public function getInfo(){
  2109. if (!$this->request->isPost()) {
  2110. $this->error('非法请求');
  2111. }
  2112. $params = $this->request->param();
  2113. // 检查子订单编号和日期是否都存在
  2114. if (!isset($params['order_id']) || empty($params['order_id'])) {
  2115. $this->error('参数错误,子订单编号不能为空');
  2116. }
  2117. if (!isset($params['rq']) || empty($params['rq'])) {
  2118. $this->error('参数错误,日期不能为空');
  2119. }
  2120. function setIfNotEmpty($value) {
  2121. return (!empty($value) && $value != 0) ? $value : '';
  2122. }
  2123. // 先查询当前记录的原始数据
  2124. $existingData = \db('工单_印件资料')
  2125. ->where('子订单编号', $params['order_id'])
  2126. // ->where(function($query) {
  2127. // $query->whereNull('Mod_rq')->whereOr('Mod_rq', '');
  2128. // }) // 查询未删除数据
  2129. // ->order('子订单编号 desc')
  2130. ->find();
  2131. if ($existingData) {
  2132. // 计算新的累加值
  2133. $updateData = [
  2134. 'sc_rq' => date('Y-m-d H:i:s'),//生产报工日期
  2135. 'scsl1' => isset($params['scsl1']) && is_numeric($params['scsl1']) ? $existingData['scsl1'] + $params['scsl1'] : $existingData['scsl1'] + 0,
  2136. 'scsl2' => isset($params['scsl2']) && is_numeric($params['scsl2']) ? $existingData['scsl2'] + $params['scsl2'] : $existingData['scsl2'] + 0,
  2137. 'scsl3' => isset($params['scsl3']) && is_numeric($params['scsl3']) ? $existingData['scsl3'] + $params['scsl3'] : $existingData['scsl3'] + 0,
  2138. 'scsl4' => isset($params['scsl4']) && is_numeric($params['scsl4']) ? $existingData['scsl4'] + $params['scsl4'] : $existingData['scsl4'] + 0,
  2139. 'scsl5' => isset($params['scsl5']) && is_numeric($params['scsl5']) ? $existingData['scsl5'] + $params['scsl5'] : $existingData['scsl5'] + 0,
  2140. 'scsl6' => isset($params['scsl6']) && is_numeric($params['scsl6']) ? $existingData['scsl6'] + $params['scsl6'] : $existingData['scsl6'] + 0,
  2141. 'scsl7' => isset($params['scsl7']) && is_numeric($params['scsl7']) ? $existingData['scsl7'] + $params['scsl7'] : $existingData['scsl7'] + 0,
  2142. 'scsl8' => isset($params['scsl8']) && is_numeric($params['scsl8']) ? $existingData['scsl8'] + $params['scsl8'] : $existingData['scsl8'] + 0,
  2143. 'scsl9' => isset($params['scsl9']) && is_numeric($params['scsl9']) ? $existingData['scsl9'] + $params['scsl9'] : $existingData['scsl9'] + 0,
  2144. 'scsl10' => isset($params['scsl10']) && is_numeric($params['scsl10']) ? $existingData['scsl10'] + $params['scsl10'] : $existingData['scsl10'] + 0,
  2145. 'scsl11' => isset($params['scsl11']) && is_numeric($params['scsl11']) ? $existingData['scsl11'] + $params['scsl11'] : $existingData['scsl11'] + 0,
  2146. 'scsl12' => isset($params['scsl12']) && is_numeric($params['scsl12']) ? $existingData['scsl12'] + $params['scsl12'] : $existingData['scsl12'] + 0,
  2147. 'scsl13' => isset($params['scsl13']) && is_numeric($params['scsl13']) ? $existingData['scsl13'] + $params['scsl13'] : $existingData['scsl13'] + 0,
  2148. 'scsl14' => isset($params['scsl14']) && is_numeric($params['scsl14']) ? $existingData['scsl14'] + $params['scsl14'] : $existingData['scsl14'] + 0,
  2149. 'sctotal' => isset($params['sctotal']) && is_numeric($params['sctotal']) ? $existingData['sctotal'] + $params['sctotal'] : $existingData['sctotal'] + 0,
  2150. // 你可以在这里继续添加其他字段
  2151. ];
  2152. $updateCount = \db('工单_印件资料')
  2153. ->where('子订单编号', $params['order_id'])
  2154. ->fetchSql(true)
  2155. // ->where(function($query) {
  2156. // $query->whereNull('Mod_rq')->whereOr('Mod_rq', '');
  2157. // }) // 查询未删除数据
  2158. ->update($updateData);
  2159. $list_gd = \db()->query($updateCount);
  2160. } else {
  2161. echo "没有找到相关记录";
  2162. }
  2163. // 检查更新结果
  2164. if ($list_gd > 0) {
  2165. $this->success("更新成功");
  2166. } else {
  2167. // 查询数据库,是否能找到对应的子订单编号
  2168. $existingRecord = \db('工单_印件资料')
  2169. ->where('子订单编号', $params['order_id'])
  2170. // ->where('UniqId', $params['UniqId'])
  2171. ->find();
  2172. if ($existingRecord) {
  2173. $this->error('更新失败,可能是提供的更新数据与现有数据相同,未进行任何更改');
  2174. } else {
  2175. $this->error('更新失败,未找到对应的子订单编号');
  2176. }
  2177. }
  2178. }
  2179. /**
  2180. * 手工、大烫、总检、包装完工报工【通用的报工操作函数】
  2181. */
  2182. function reportWork($params) {
  2183. // 查询设备_产量计酬表的数据
  2184. $bglist = \db('设备_产量计酬')->field('订单编号,子订单编号,数量,ci_num,款号,sys_rq,serial,serial_num')
  2185. ->where('UniqId', $params['UniqId'])->fetchSql(true)->find();
  2186. $list_gd = \db()->query($bglist);
  2187. // 查询工单基本资料表的数据
  2188. $orderlist = \db('工单_基本资料')->where('订单编号', $list_gd[0]['订单编号'])->find();
  2189. // 检查code是否为 "包装"
  2190. if ($params['code'] == '包装') {
  2191. // 如果是 "包装",则增加工单基本资料表中的入仓数量
  2192. $addorder['工单入仓数量'] = $orderlist['工单入仓数量'] + $params['sl'];
  2193. $result = \db('工单_基本资料')->where('订单编号', $list_gd[0]['订单编号'])->fetchSql(true)->update($addorder);
  2194. $sql = \db()->query($result);
  2195. }
  2196. // 构建单条记录的函数,传递 $list_gd 作为参数
  2197. function buildRecords($item, $list_gd) {
  2198. return [
  2199. '订单编号' => $item['order'],
  2200. '子订单编号' => $item['order_id'],
  2201. '款号' => $list_gd[0]['款号'],
  2202. 'serial' => $list_gd[0]['serial'],
  2203. 'serial_num' => $list_gd[0]['serial_num'],
  2204. '工序编号' => $item['gx'],
  2205. '工序名称' => $item['gxmc'],
  2206. '尺码' => $item['cm'],
  2207. '数量' => $item['sl'],
  2208. 'sczl_jtbh' => $item['sczl_jtbh'],
  2209. 'sczl_bh' => $item['sczl_bh'],
  2210. '尾包' => $item['wb'],
  2211. 'sys_id' => $item['sys_id'],
  2212. 'sys_rq' => date('Y-m-d H:i:s'),
  2213. 'sczl_rq' => date('Y-m-d'),
  2214. 'ci_num' => $item['ci_num'] - $item['s_num'],
  2215. 's_num' => $item['s_num'],
  2216. ];
  2217. }
  2218. // 检查是否是索引数组(多条记录)
  2219. if (isset($params[0]) && is_array($params[0])) {
  2220. foreach ($params as $item) {
  2221. $list[] = buildRecords($item, $list_gd);
  2222. }
  2223. } else {
  2224. // 处理单条记录
  2225. $list[] = buildRecords($params, $list_gd);
  2226. }
  2227. // 插入数据到设备_产量计酬表
  2228. $result = \db('设备_产量计酬')->insertAll($list);
  2229. if ($result) {
  2230. $this->success('数据插入成功 : ' . date('H:i:s'));
  2231. } else {
  2232. $this->error('数据插入失败');
  2233. }
  2234. }
  2235. /**
  2236. * 报工:裁剪、车缝、手工、大烫、总检、包装
  2237. */
  2238. public function search(){
  2239. if (!$this->request->isPost()) {
  2240. $this->error('非法请求');
  2241. }
  2242. $params = $this->request->param();
  2243. if($params['order_id'] == ''){
  2244. $this->error('请扫描订单后再报工');
  2245. }
  2246. // 根据不同的 code 值执行不同的逻辑
  2247. switch ($params['code']) {
  2248. case '裁剪':
  2249. $order_id = explode('-', $params['order_id']);
  2250. $order = $order_id[0];
  2251. //先查询这条订单是否存在
  2252. $sql = \db('工单_基本资料')->where('订单编号', $order)->fetchSql(true)->find();
  2253. $order_list = \db()->query($sql);
  2254. if (empty($order_list)) {
  2255. $this->error('没有找到对应的订单信息');
  2256. return;
  2257. }
  2258. if($order_list[0]['gd_statu'] == '1-计划中' || $order_list[0]['gd_statu'] == '生产中' || $order_list[0]['gd_statu'] == '2-生产中'){
  2259. DB::name('工单_基本资料')
  2260. ->where('订单编号', $order)
  2261. ->update(['gd_statu' => '已裁剪']);
  2262. }
  2263. //将前端传过来的数据存到数据库
  2264. $insertData = [];
  2265. $dataList = isset($params[0]) && is_array($params[0]) ? $params : [$params];
  2266. foreach ($dataList as $item) {
  2267. $list = [
  2268. '订单编号' => $order_list[0]['订单编号'],
  2269. '款号' => $order_list[0]['生产款号'],
  2270. '子订单编号' => $item['order_id'],
  2271. '工序编号' => '2',
  2272. '工序名称' => '裁剪',
  2273. '尺码' => $item['cm'],
  2274. '数量' => $item['sl'],
  2275. 'sczl_jtbh' => $item['sczl_jtbh'],
  2276. 'sys_id' => $item['sys_id'],
  2277. 'sczl_bh' => $item['sczl_bh'],
  2278. 'sczl_rq' => date('Y-m-d 00:00:00'),
  2279. 'sys_rq' => date('Y-m-d H:i:s')
  2280. ];
  2281. $insertData[] = $list;
  2282. }
  2283. $result = \db('设备_产量计酬')->insertAll($insertData);
  2284. $result ? $this->success('数据插入成功 : ' . date('H:i:s')) : $this->error('数据插入失败');
  2285. break;
  2286. case '车缝':
  2287. $list = [];
  2288. $order_id = explode('-', $params['order_id']);
  2289. $order = $order_id[0];
  2290. $sql = \db('工单_基本资料')->where('订单编号', $order)->fetchSql(true)->find();
  2291. $list_gd = \db()->query($sql); // 查询的结果可能是多条记录,确保是数组
  2292. if($list_gd[0]['gd_statu'] == '1-计划中' || $list_gd[0]['gd_statu'] == '已裁剪' || $list_gd[0]['gd_statu'] == '2-生产中'){
  2293. DB::name('工单_基本资料')
  2294. ->where('订单编号', $order)
  2295. ->update(['gd_statu' => '已车缝']);
  2296. }
  2297. // 查询子订单编号对应的最大 serial 值
  2298. $list_order_id = \db('设备_产量计酬')
  2299. ->field('子订单编号, MAX(serial) as serial')
  2300. ->where('子订单编号', $params['order_id'])
  2301. ->where(function($query) {
  2302. $query->whereNull('mod_rq')->whereOr('mod_rq', '');
  2303. }) // 查询未删除数据
  2304. ->group('子订单编号')
  2305. ->find();
  2306. function buildRecord($item, $list_gd, $list_order_id) {
  2307. return [
  2308. '订单编号' => $list_gd[0]['订单编号'],
  2309. '款号' => $list_gd[0]['生产款号'],
  2310. '子订单编号' => $item['order_id'],
  2311. '工序编号' => 3,
  2312. '工序名称' => '车缝',
  2313. 'serial' => $item['serial'],
  2314. 'ci_num' => (int)$item['sl'],
  2315. 'serial_num' => $item['serial_num'],
  2316. '尺码' => $item['cm'],
  2317. '数量' => (int)$item['sl'],
  2318. 'sczl_jtbh' => $item['sczl_jtbh'],
  2319. 'sys_rq' => date('Y-m-d H:i:s'),
  2320. 'sczl_rq' => date('Y-m-d'),
  2321. '尾包' => $item['wb'],
  2322. 'sys_id' => $item['sys_id'],
  2323. 'sczl_bh' => $item['sczl_bh']
  2324. ];
  2325. }
  2326. if (isset($params[0]) && is_array($params[0])) {
  2327. foreach ($params as $item) {
  2328. $list[] = buildRecord($item, $list_gd, $list_order_id); // 所有记录使用相同的 serial
  2329. }
  2330. } else {
  2331. // 处理单条记录
  2332. $list[] = buildRecord($params, $list_gd, $list_order_id); // 单条记录
  2333. }
  2334. $result = \db('设备_产量计酬')->insertAll($list);
  2335. if ($result) {
  2336. $this->success('数据插入成功 : ' . date('H:i:s'));
  2337. } else {
  2338. $this->error('数据插入失败');
  2339. }
  2340. break;
  2341. case '手工':
  2342. $this->reportWork($params);
  2343. break;
  2344. case '大烫':
  2345. $this->reportWork($params);
  2346. break;
  2347. case '总检':
  2348. $order_id_parts = explode('-', $params['order_id']);
  2349. $order = $order_id_parts[0];
  2350. //查询订单信息
  2351. $list_gd = Db::name('工单_基本资料')
  2352. ->where('订单编号', $order)
  2353. ->whereNull('Mod_rq')
  2354. ->find();
  2355. if (!$list_gd) {
  2356. $this->error('订单不存在');
  2357. }
  2358. $insert_cp_list = [];
  2359. $insert_cj_list = [];
  2360. $isBatch = isset($params[0]) && is_array($params[0]);
  2361. $dataList = $isBatch ? $params : [$params];
  2362. foreach ($dataList as $item) {
  2363. // ---------------- 入仓数据 ----------------
  2364. $insert_cp_list[] = [
  2365. 'order' => $list_gd['订单编号'],
  2366. 'customer' => $list_gd['客户编号'],
  2367. 'order_id' => $item['order_id'],
  2368. 'sl' => (int)$item['sl'],
  2369. 'cm' => $item['cm'],
  2370. 'wb' => $item['wb'],
  2371. 'sys_id' => $item['sys_id'],
  2372. 'sys_rq' => date('Y-m-d H:i:s')
  2373. ];
  2374. // ---------------- 产量计酬数据 ----------------
  2375. $insert_cj_list[] = [
  2376. '订单编号' => $list_gd['订单编号'],
  2377. '款号' => $list_gd['生产款号'],
  2378. '子订单编号' => $item['order_id'],
  2379. '工序编号' => 6,
  2380. '工序名称' => '总检',
  2381. 'serial' => $item['serial'],
  2382. 'ci_num' => (int)$item['sl'],
  2383. 'serial_num' => $item['serial_num'],
  2384. '尺码' => $item['cm'],
  2385. '数量' => (int)$item['sl'],
  2386. 's_num' => (int)$item['sl'],
  2387. 'sczl_jtbh' => $item['sczl_jtbh'],
  2388. 'sys_rq' => date('Y-m-d H:i:s'),
  2389. 'sczl_rq' => date('Y-m-d'),
  2390. '尾包' => $item['wb'],
  2391. 'sys_id' => $item['sys_id'],
  2392. 'sczl_bh' => $item['sczl_bh']
  2393. ];
  2394. }
  2395. $res1 = \db('成品入仓')->insertAll($insert_cp_list);
  2396. $res2 = \db('设备_产量计酬')->insertAll($insert_cj_list);
  2397. $orderNo = $list_gd['订单编号'];
  2398. // 累计入仓(所有时间)
  2399. $cumulativeData = Db::name('成品入仓')
  2400. ->alias('a')
  2401. ->field('a.order as 订单编号, SUM(a.sl) as 累计入仓数量')
  2402. ->where('a.order', $orderNo)
  2403. ->whereNull('a.mod_rq')
  2404. ->group('a.order')
  2405. ->select();
  2406. $cumulativeMap = [];
  2407. foreach ($cumulativeData as $row) {
  2408. $cumulativeMap[$row['订单编号']] = (int)$row['累计入仓数量'];
  2409. }
  2410. // 裁剪产量(所有时间)
  2411. $prodRecords = Db::name('设备_产量计酬')
  2412. ->field('订单编号, SUM(数量) as 裁剪数量')
  2413. ->where('订单编号', $orderNo)
  2414. ->where('工序名称', '裁剪')
  2415. ->group('订单编号')
  2416. ->select();
  2417. $cutMap = [];
  2418. foreach ($prodRecords as $row) {
  2419. $cutMap[$row['订单编号']] = (int)$row['裁剪数量'];
  2420. }
  2421. $cutQty = $cutMap[$orderNo] ?? 0;
  2422. $cumulativeQty = $cumulativeMap[$orderNo] ?? 0;
  2423. $passRate = $cutQty > 0 ? round($cumulativeQty / $cutQty * 100, 2) : 0;
  2424. $passRateDisplay = $cutQty > 0 ? $passRate . '%' : '0%';
  2425. // 根据合格率决定显示状态
  2426. if($passRate > 95) {
  2427. $status = '已完工';
  2428. } else {
  2429. $status = '已总检' . $passRateDisplay;
  2430. }
  2431. if (in_array($list_gd['gd_statu'], ['1-计划中','已车缝', '2-生产中']) || strpos($list_gd['gd_statu'], '已总检') !== false) {
  2432. DB::name('工单_基本资料')
  2433. ->where('订单编号', $order)
  2434. ->update(['gd_statu' => $status]);
  2435. }
  2436. if ($res1 && $res2) {
  2437. $this->success('数据报工成功 : ' . date('H:i:s'));
  2438. } else {
  2439. $this->error('数据插入失败');
  2440. }
  2441. break;
  2442. case '包装':
  2443. $order_id_parts = explode('-', $params['order_id']);
  2444. $order = $order_id_parts[0];
  2445. $list_gd = \db('工单_基本资料')->where('订单编号', $order)
  2446. ->whereNull('Mod_rq')
  2447. ->find();
  2448. $insert_cj_list = [];
  2449. $isBatch = isset($params[0]) && is_array($params[0]);
  2450. $dataList = $isBatch ? $params : [$params];
  2451. foreach ($dataList as $item) {
  2452. // ---------------- 产量计酬数据 ----------------
  2453. $insert_cj_list[] = [
  2454. '订单编号' => $list_gd['订单编号'],
  2455. '款号' => $list_gd['生产款号'],
  2456. '子订单编号' => $item['order_id'],
  2457. '工序编号' => 7,
  2458. '工序名称' => '包装',
  2459. 'serial' => $item['serial'],
  2460. 'ci_num' => (int)$item['sl'],
  2461. 'serial_num' => $item['serial_num'],
  2462. '尺码' => $item['cm'],
  2463. '数量' => (int)$item['sl'],
  2464. 's_num' => (int)$item['sl'],
  2465. 'sczl_jtbh' => $item['sczl_jtbh'],
  2466. 'sys_rq' => date('Y-m-d H:i:s'),
  2467. 'sczl_rq' => date('Y-m-d'),
  2468. '尾包' => $item['wb'],
  2469. 'sys_id' => $item['sys_id'],
  2470. 'sczl_bh' => $item['sczl_bh']
  2471. ];
  2472. }
  2473. $res2 = \db('设备_产量计酬')->insertAll($insert_cj_list);
  2474. if ($res2) {
  2475. $this->success('数据报工成功 : ' . date('H:i:s'));
  2476. } else {
  2477. $this->error('数据插入失败');
  2478. }
  2479. break;
  2480. default:
  2481. $this->error('无效的 code 参数');
  2482. break;
  2483. }
  2484. }
  2485. /**
  2486. * 物理地址获取机台编号
  2487. * /work_order_spot_check/getMachineMac
  2488. */
  2489. public function getMachineMac(){
  2490. if ($this->request->isGet() == false){$this->error('非法请求');}
  2491. $params = $this->request->get();
  2492. if (empty($params['sys_sbID'])){$this->error('物理地址参数不能为空');}
  2493. $data = \db('设备_基本资料')->where('sys_sbID', $params['sys_sbID'])
  2494. ->field('设备编号 as 机台号,生产工序,工序,rtrim(设备编组) as 组别,组长,sys_sbID')
  2495. // ->where('生产工序',$params['code'])
  2496. ->find();
  2497. $this->success('请求成功', $data);
  2498. }
  2499. /**
  2500. * 删除报工记录
  2501. * /work_order_spot_check/del
  2502. */
  2503. public function del() {
  2504. // 判断请求方式是否为 GET
  2505. if (!$this->request->isGet()) {
  2506. return $this->error('请求错误');
  2507. }
  2508. // 获取请求参数
  2509. $param = $this->request->param();
  2510. // 检查 UniqId 参数是否存在
  2511. if (empty($param['UniqId'])) {
  2512. return $this->error('UniqId 参数错误');
  2513. }
  2514. // 将 UniqId 的值按逗号分割成数组
  2515. $printIdArray = explode(',', $param['UniqId']);
  2516. // 初始化失败计数器
  2517. $failureCount = 0;
  2518. // 开启事务
  2519. \db()->startTrans();
  2520. try {
  2521. foreach ($printIdArray as $uniqId) {
  2522. // 根据 UniqId 查询设备_产量计酬表中的数据
  2523. $result = \db('设备_产量计酬')->where('UniqId', $uniqId)
  2524. ->field('子订单编号, 订单编号, 尺码, 数量, sczl_bh, 工序名称')
  2525. ->find();
  2526. // 如果查询不到数据,跳过当前处理
  2527. if (!$result) {
  2528. $failureCount++;
  2529. continue;
  2530. }
  2531. // 判断工序名称是否是“裁剪”
  2532. if (isset($param['sys_id']) && strpos($param['sys_id'], '裁剪') !== false) {
  2533. // 如果工序名称包含“裁剪”,更新工单_印件资料表
  2534. $yresult = \db('工单_印件资料')->where('子订单编号', $result['子订单编号'])
  2535. ->whereNull('Mod_rq') // 仅更新没有修改日期的记录
  2536. ->find();
  2537. // 如果没有查询到工单印件资料表的数据,跳过
  2538. if (!$yresult) {
  2539. $failureCount++;
  2540. continue;
  2541. }
  2542. // 处理尺码和数量更新
  2543. $size = $result['尺码'];
  2544. $quantity = $result['数量'];
  2545. $updateData = [];
  2546. // 遍历工单中的尺码字段(cm1 到 cm10),找到匹配的尺码
  2547. for ($i = 1; $i <= 14; $i++) {
  2548. $cmField = 'cm' . $i;
  2549. $scslField = 'scsl' . $i;
  2550. if (isset($yresult[$cmField]) && $yresult[$cmField] === $size) {
  2551. // 找到匹配的尺码后,更新对应的数量字段
  2552. if (isset($yresult[$scslField])) {
  2553. $yresult[$scslField] -= $quantity;
  2554. if ($yresult[$scslField] < 0) {
  2555. $yresult[$scslField] = 0; // 防止数量为负数
  2556. }
  2557. $updateData[$scslField] = $yresult[$scslField];
  2558. }
  2559. break;
  2560. }
  2561. }
  2562. // 计算更新后的总数量
  2563. $newTotal = 0;
  2564. for ($i = 1; $i <= 14; $i++) {
  2565. $scslField = 'scsl' . $i;
  2566. if (isset($yresult[$scslField])) {
  2567. $newTotal += $yresult[$scslField];
  2568. }
  2569. }
  2570. // 更新工单_印件资料表
  2571. \db('工单_印件资料')
  2572. ->where('子订单编号', $result['子订单编号'])
  2573. ->update(['sctotal' => $newTotal] + $updateData);
  2574. }
  2575. // 更新设备_产量计酬表
  2576. $updateData = [
  2577. 'mod_rq' => date('Y-m-d H:i:s'),
  2578. 'delsys_id' => $param['sys_id']
  2579. ];
  2580. // 执行更新操作
  2581. $updateResult = \db('设备_产量计酬')->where('UniqId', $uniqId)->update($updateData);
  2582. // 如果更新失败,记录失败
  2583. if ($updateResult === false) {
  2584. $failureCount++;
  2585. }
  2586. }
  2587. // 提交事务
  2588. \db()->commit();
  2589. } catch (\Exception $e) {
  2590. // 发生异常时回滚事务
  2591. \db()->rollback();
  2592. return $this->error('操作失败:' . $e->getMessage());
  2593. }
  2594. // 返回操作结果
  2595. if ($failureCount === 0) {
  2596. return $this->success('删除成功');
  2597. } else {
  2598. return $this->error("操作完成,但有 {$failureCount} 条记录更新失败");
  2599. }
  2600. }
  2601. /**
  2602. * 面料出库报工
  2603. */
  2604. public function outReport()
  2605. {
  2606. if ($this->request->isPost() === false){
  2607. $this->error('请求错误');
  2608. }
  2609. $param = Request::instance()->post();
  2610. if (empty($param)){
  2611. $this->error('参数错误');
  2612. }
  2613. foreach ($param as $item) {
  2614. // 更新库存数据
  2615. $Inventory = \db('物料_库存')
  2616. ->where('批次号',$item['批次号'])
  2617. ->field('库存数量,领用数量')
  2618. ->find();
  2619. $InventoryData = [
  2620. '批次号' => $item['批次号'],
  2621. '关联号' => $item['关联编号'],
  2622. '物料编号' => $item['物料编码'],
  2623. '物料名称' => $item['物料名称'],
  2624. '领用数量' => (float)$Inventory['领用数量'] + (float)$item['number'],
  2625. '库存数量' => (float)$Inventory['库存数量'] - (float)$item['number'],
  2626. '实际门幅' => $item['实际门幅'],
  2627. '单位' => $item['单位'],
  2628. '状态' => 1,
  2629. 'departname' => $item['departname'],
  2630. 'remark' => isset($item['remark']) ? $item['remark'] : '',
  2631. 'sys_id' => $item['sys_id'],
  2632. 'sys_rq' => date('Y-m-d H:i:s'),
  2633. ];
  2634. $sql = \db('物料_库存')->where('批次号',$item['批次号'])->fetchSql(true)->update($InventoryData);
  2635. \db()->query($sql);
  2636. // 记录入库报工日志
  2637. $ReportData = [
  2638. '批次号' => $item['批次号'],
  2639. 'order_id' => $item['order_id'],
  2640. '款号' => $item['款号'],
  2641. '物料编码' => $item['物料编码'],
  2642. '物料名称' => $item['物料名称'],
  2643. '库存数量' => $item['number'],
  2644. 'number' => $item['number'],
  2645. 'rq' => $item['rq'],
  2646. 'name' => $item['name'],
  2647. 'sys_id' => $item['sys_id'],
  2648. 'receipt_number' => $item['receipt_number'],
  2649. '客户编号' => $item['客户编号'],
  2650. 'departname' => $item['departname'],
  2651. 'remark' => isset($item['remark']) ? $item['remark'] : '',
  2652. '款式' => $item['款式'],
  2653. 'sys_rq' => date('Y-m-d H:i:s'),
  2654. ];
  2655. $sql = \db('库存_出入库明细')->fetchSql(true)->insert($ReportData);
  2656. \db()->query($sql);
  2657. }
  2658. $this->success('成功');
  2659. }
  2660. /**
  2661. * 入库订单信息模糊查询
  2662. */
  2663. public function InputOrderSearch()
  2664. {
  2665. if ($this->request->isGet() === false){
  2666. $this->error('请求错误');
  2667. }
  2668. $param = $this->request->param();
  2669. if (empty($param)){
  2670. $this->error('参数错误');
  2671. }
  2672. $list = \db('工单_基本资料')
  2673. ->alias('a')
  2674. ->join('工单_面料资料 b','a.订单编号 = b.BOM_工单编号')
  2675. ->join('工单关联表 c','a.订单编号 = c.订单编号 AND b.BOM_物料编码 = c.物料编号')
  2676. ->where('a.生产款号|a.订单编号|b.BOM_物料编码','like','%'.$param['search'].'%')
  2677. ->where('a.Mod_rq',null)
  2678. ->where('b.Mod_rq',null)
  2679. ->where('c.Mod_rq',null)
  2680. ->field('a.订单编号,a.生产款号,a.客户编号,a.款式,a.订单数量,a.落货日期,a.接单日期,a.Sys_id as 创建人员,
  2681. a.Sys_rq as 创建时间,b.BOM_颜色 as 颜色,b.BOM_物料编码 as 物料编码,b.BOM_物料名称 as 物料名称,GROUP_CONCAT(DISTINCT c.订单编号 SEPARATOR ",") AS 关联订单,c.关联编号')
  2682. ->group('物料编码')
  2683. ->order('a.订单编号 desc')
  2684. ->select();
  2685. if (empty($list)){
  2686. $res = \db('工单_基本资料')->alias('a')
  2687. ->join('工单_印件资料 b','a.订单编号 = b.订单编号')
  2688. ->join('工单_bom资料 c','a.订单编号 = c.BOM_工单编号')
  2689. ->where('b.订单编号','like','%'.$param['search'].'%')
  2690. ->field('a.订单编号,a.生产款号,b.颜色备注,c.BOM_物料名称,a.Sys_id')
  2691. ->where('a.Mod_rq',null)
  2692. ->where('b.Mod_rq',null)
  2693. ->where('c.Mod_rq',null)
  2694. ->group('c.BOM_物料名称')
  2695. ->select();
  2696. foreach ($res as $index => &$item) {
  2697. $item['物料编码'] = $item['生产款号'] . '-' . $item['颜色备注'] . ($index + 1);
  2698. }
  2699. unset($item);
  2700. $arr = \db('工单关联表')
  2701. ->orderRaw("CAST(SUBSTRING(关联编号, 5) AS UNSIGNED) DESC")
  2702. ->find();
  2703. // 2. 提取数字部分并加一
  2704. $num = intval(substr($arr['关联编号'], 4)) + 1;
  2705. // 3. 重新拼接新的编号(保留 GDGL 前缀)
  2706. $newCode = 'GDGL' . $num;
  2707. // 3. 定义当前日期
  2708. $today = date('Y-m-d');
  2709. // 4. 循环插入到两个表
  2710. foreach ($res as $row) {
  2711. // 插入到 工单_面料资料
  2712. $sql = \db('工单_面料资料')->fetchSql(true)->insert([
  2713. 'BOM_工单编号' => $row['订单编号'],
  2714. 'BOM_颜色' => $row['颜色备注'],
  2715. 'BOM_物料编码' => $row['物料编码'],
  2716. 'BOM_物料名称' => $row['BOM_物料名称'],
  2717. 'Sys_ID' => $row['Sys_id'],
  2718. 'Sys_rq' => $today,
  2719. ]);
  2720. \db()->query($sql);
  2721. // 插入到 工单关联表
  2722. $sqll = \db('工单关联表')->fetchSql(true)->insert([
  2723. '关联编号' => $newCode,
  2724. '订单编号' => $row['订单编号'],
  2725. '生产款号' => $row['生产款号'],
  2726. '颜色' => $row['颜色备注'],
  2727. '物料编号' => $row['物料编码'],
  2728. '物料名称' => $row['BOM_物料名称'],
  2729. 'Sys_id' => $row['Sys_id'],
  2730. 'Sys_rq' => $today,
  2731. ]);
  2732. \db()->query($sqll);
  2733. }
  2734. $list = \db('工单_基本资料')
  2735. ->alias('a')
  2736. ->join('工单_面料资料 b','a.订单编号 = b.BOM_工单编号')
  2737. ->join('工单关联表 c','a.订单编号 = c.订单编号 AND b.BOM_物料编码 = c.物料编号')
  2738. ->where('a.生产款号|a.订单编号|b.BOM_物料编码','like','%'.$param['search'].'%')
  2739. ->where('a.Mod_rq',null)
  2740. ->where('b.Mod_rq',null)
  2741. ->where('c.Mod_rq',null)
  2742. ->field('a.订单编号,a.生产款号,a.客户编号,a.款式,a.订单数量,a.落货日期,a.接单日期,a.Sys_id as 创建人员,
  2743. a.Sys_rq as 创建时间,b.BOM_颜色 as 颜色,b.BOM_物料编码 as 物料编码,b.BOM_物料名称 as 物料名称,GROUP_CONCAT(DISTINCT c.订单编号 SEPARATOR ",") AS 关联订单,c.关联编号')
  2744. ->group('物料编码')
  2745. ->order('a.订单编号 desc')
  2746. ->select();
  2747. $this->success('成功',$list);
  2748. }else{
  2749. $this->success('成功',$list);
  2750. }
  2751. }
  2752. /**
  2753. * 面料入库报工
  2754. */
  2755. public function inputStash()
  2756. {
  2757. if (!$this->request->isPost()) {
  2758. $this->error('请求错误');
  2759. }
  2760. $param = Request::instance()->post();
  2761. if (empty($param)) {
  2762. $this->error('参数错误');
  2763. }
  2764. // 获取当前最大批次号
  2765. $lastBatch = \db('物料_库存')
  2766. ->field("批次号")
  2767. ->orderRaw("CAST(SUBSTRING(批次号, 4) AS UNSIGNED) DESC")
  2768. ->limit(1)
  2769. ->value('批次号');
  2770. $currentNumber = 0;
  2771. if (!empty($lastBatch) && preg_match('/^PCH(\d+)$/i', $lastBatch, $match)) {
  2772. $currentNumber = intval($match[1]);
  2773. }
  2774. foreach ($param as $item) {
  2775. // 查找是否已有相同物料编号 + 关联号的库存记录
  2776. $existingBatch = \db('物料_库存')
  2777. ->where('物料编号', $item['物料编码'])
  2778. ->where('物料名称', $item['物料名称'])
  2779. ->whereNull('Mod_rq')
  2780. ->where('关联号', $item['关联编号'])
  2781. ->value('批次号');
  2782. // 更新库存数据
  2783. $Inventory = \db('物料_库存')
  2784. ->where('批次号',$item['批次号'])
  2785. ->field('库存数量,入仓总量')
  2786. ->find();
  2787. if ($existingBatch) {
  2788. $BatchNumber = $existingBatch; // 已入库,使用原批次号
  2789. $InventoryData = [
  2790. '批次号' => $item['批次号'],
  2791. '关联号' => $item['关联编号'],
  2792. '物料编号' => $item['物料编码'],
  2793. '物料名称' => $item['物料名称'],
  2794. '入仓总量' => $Inventory['入仓总量'] + $item['number'],
  2795. '库存数量' => $Inventory['库存数量'] + $item['number'],
  2796. '实际门幅' => $item['实际门幅'],
  2797. '单位' => $item['单位'],
  2798. '状态' => 1,
  2799. 'departname' => $item['departname'],
  2800. 'remark' => isset($item['remark']) ? $item['remark'] : '',
  2801. 'sys_id' => $item['sys_id'],
  2802. 'sys_rq' => date('Y-m-d H:i:s'),
  2803. ];
  2804. $sql = \db('物料_库存')->fetchSql(true)->where('批次号',$BatchNumber)->update($InventoryData);
  2805. \db()->query($sql);
  2806. } else {
  2807. // 生成新批次号
  2808. $currentNumber++;
  2809. $BatchNumber = 'PCH' . $currentNumber;
  2810. // 插入库存表
  2811. $InventoryData = [
  2812. '批次号' => $BatchNumber,
  2813. '关联号' => $item['关联编号'],
  2814. '物料编号' => $item['物料编码'],
  2815. '物料名称' => $item['物料名称'],
  2816. '入仓总量' => $item['number'],
  2817. '库存数量' => $item['number'],
  2818. '单位' => $item['单位'],
  2819. '实际门幅' => $item['实际门幅'],
  2820. '状态' => 1,
  2821. 'departname' => $item['departname'],
  2822. 'remark' => isset($item['remark']) ? $item['remark'] : '',
  2823. 'sys_id' => $item['sys_id'],
  2824. 'sys_rq' => date('Y-m-d H:i:s'),
  2825. ];
  2826. $sql = \db('物料_库存')->fetchSql(true)->insert($InventoryData);
  2827. \db()->query($sql);
  2828. }
  2829. // 每条都写一条报工日志
  2830. $ReportData = [
  2831. '批次号' => $BatchNumber,
  2832. 'order_id' => $item['order_id'],
  2833. '款号' => $item['款号'],
  2834. '物料编码' => $item['物料编码'],
  2835. '物料名称' => $item['物料名称'],
  2836. '库存数量' => $item['number'],
  2837. 'number' => $item['number'],
  2838. 'rq' => $item['rq'],
  2839. 'name' => $item['name'],
  2840. 'sys_id' => $item['sys_id'],
  2841. 'receipt_number' => $item['receipt_number'],
  2842. 'departname' => $item['departname'],
  2843. 'remark' => isset($item['remark']) ? $item['remark'] : '',
  2844. '客户编号' => $item['客户编号'],
  2845. '款式' => $item['款式'],
  2846. 'sys_rq' => date('Y-m-d H:i:s'),
  2847. ];
  2848. $sql = \db('库存_出入库明细')->fetchSql(true)->insert($ReportData);
  2849. \db()->query($sql);
  2850. }
  2851. $this->success('成功');
  2852. }
  2853. /**
  2854. * 出库订单信息查询
  2855. */
  2856. public function OutOrderSearch()
  2857. {
  2858. if ($this->request->isGet() === false){
  2859. $this->error('请求错误');
  2860. }
  2861. $param = $this->request->param();
  2862. if (empty($param)){
  2863. $this->error('参数错误');
  2864. }
  2865. $list = \db('工单_基本资料')
  2866. ->alias('a')
  2867. ->join('工单_面料资料 b','a.订单编号 = b.BOM_工单编号')
  2868. ->join('物料_库存 c','b.BOM_物料编码 = c.物料编号','left')
  2869. ->join('工单关联表 d','a.订单编号 = d.订单编号 AND d.物料编号 = b.BOM_物料编码')
  2870. ->where('a.生产款号|a.订单编号|b.BOM_物料编码','like','%'.$param['search'].'%')
  2871. ->where('a.Mod_rq',null)
  2872. ->where('b.Mod_rq',null)
  2873. ->where('c.状态',1)
  2874. ->field('a.订单编号,a.生产款号,a.客户编号,a.款式,a.订单数量,a.落货日期,a.接单日期,a.Sys_id as 创建人员,
  2875. a.Sys_rq as 创建时间,b.BOM_颜色 as 颜色,b.BOM_物料编码 as 物料编码,b.BOM_物料名称 as 物料名称,c.批次号,d.关联编号')
  2876. ->order('a.订单编号 desc')
  2877. ->select();
  2878. if (empty($list)){
  2879. $this->success('未找到相关订单信息');
  2880. }else{
  2881. $this->success('成功',$list);
  2882. }
  2883. }
  2884. /**
  2885. * 获取入库面料信息
  2886. */
  2887. public function getInputDetail()
  2888. {
  2889. if ($this->request->isGet() === false){
  2890. $this->error('请求错误');
  2891. }
  2892. $param = $this->request->param();
  2893. if (empty($param)){
  2894. $this->error('参数错误');
  2895. }
  2896. $list = \db('工单_面料资料')
  2897. ->alias('b')
  2898. ->join('物料_库存 a', 'a.物料编号 = b.BOM_物料编码 AND a.物料名称 = b.BOM_物料名称 AND a.Mod_rq IS NULL', 'left')
  2899. ->join('工单关联表 c', 'b.BOM_物料编码 = c.物料编号 AND b.BOM_工单编号 = c.订单编号')
  2900. ->field('a.Mod_rq, a.批次号, c.关联编号, b.BOM_物料编码, b.BOM_物料名称, a.入仓总量, a.库存数量, a.领用数量, a.退还数量, a.单位, a.实际门幅,
  2901. a.departname, b.BOM_工单编号, b.BOM_颜色, b.BOM_计划门幅 as 计划门幅, b.BOM_定额门幅, b.BOM_计划用量 as 计划用料, b.BOM_标准用量 as 定额用料')
  2902. ->where('c.关联编号', $param['关联编号'])
  2903. ->where('c.订单编号', $param['订单编号'])
  2904. ->where('b.BOM_物料编码', $param['物料编码'])
  2905. ->where('a.Mod_rq',null)
  2906. ->where('b.Mod_rq',null)
  2907. ->where('c.Mod_rq',null)
  2908. ->find();
  2909. // if (empty($param['Mod_rq'])) { // 如果 Mod_rq 为空,才关联物料库存表
  2910. // $list = $list->join('物料_库存 a', 'a.物料编号 = b.BOM_物料编码 AND a.物料名称 = b.BOM_物料名称 AND a.Mod_rq IS NULL', 'left')
  2911. // ->field('a.批次号, a.入仓总量, a.库存数量, a.领用数量, a.退还数量, a.单位, a.实际门幅, a.departname');
  2912. // }
  2913. // $list = $list->where('c.关联编号', $param['关联编号'])
  2914. // ->where('c.订单编号', $param['订单编号'])
  2915. // ->where('b.BOM_物料编码', $param['物料编码'])
  2916. // ->find();
  2917. $orderList = \db('工单关联表')
  2918. ->where('关联编号',$param['关联编号'])
  2919. ->column('GROUP_CONCAT(DISTINCT 订单编号 SEPARATOR ",") AS 关联订单');
  2920. $list['关联订单'] = $orderList[0];
  2921. $this->success('成功',$list);
  2922. }
  2923. /**
  2924. * 裁剪面料退货包工
  2925. * @return void
  2926. * @throws \think\db\exception\DataNotFoundException
  2927. * @throws \think\db\exception\ModelNotFoundException
  2928. * @throws \think\exception\DbException
  2929. * @throws \think\exception\PDOException
  2930. */
  2931. public function TuihuoReport()
  2932. {
  2933. if ($this->request->isPost() === false){
  2934. $this->error('请求错误');
  2935. }
  2936. $param = Request::instance()->post();
  2937. if (empty($param)){
  2938. $this->error('参数错误');
  2939. }
  2940. foreach ($param as $item) {
  2941. // 查询库存数据
  2942. $number = \db('物料_库存')
  2943. ->where('批次号', $item['批次号'])
  2944. ->where('关联号', $item['关联编号'])
  2945. ->where('物料编号', $item['物料编码'])
  2946. ->whereNull('Mod_rq')
  2947. ->field('库存数量, 退还数量')
  2948. ->find();
  2949. // 根据类型调整库存
  2950. if ($item['type'] === '退仓') {
  2951. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  2952. $stockQuantity = (float)$number['库存数量'] + (float)$item['number'];
  2953. } elseif ($item['type'] === '退厂商') {
  2954. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  2955. $stockQuantity = (float)$number['库存数量'] - (float)$item['number'];
  2956. } elseif ($item['type'] === '销售') {
  2957. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  2958. $stockQuantity = (float)$number['库存数量'] - (float)$item['number'];
  2959. }elseif ($item['type'] === '退客户') {
  2960. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  2961. $stockQuantity = (float)$number['库存数量'] - (float)$item['number'];
  2962. // 检查库存是否足够
  2963. if ($stockQuantity < 0) {
  2964. $this->error('库存不足,无法进行退客户操作。当前库存:' . $number['库存数量']);
  2965. }
  2966. }else{
  2967. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  2968. $stockQuantity = (float)$number['库存数量'] ;
  2969. }
  2970. // 构建库存数据
  2971. $inventoryData = [
  2972. '批次号' => $item['批次号'],
  2973. '关联号' => $item['关联编号'],
  2974. '物料编号' => $item['物料编码'],
  2975. '物料名称' => $item['物料名称'],
  2976. '实际门幅' => $item['实际门幅'],
  2977. '单位' => $item['单位'],
  2978. '状态' => 1,
  2979. 'sys_id' => $item['sys_id'],
  2980. 'departname' => $item['departname'],
  2981. 'remark' => isset($item['remark']) ? $item['remark'] : '',
  2982. 'sys_rq' => date('Y-m-d H:i:s'),
  2983. '退还数量' => $returnQuantity,
  2984. '库存数量' => $stockQuantity,
  2985. ];
  2986. $InventorySql = \db('物料_库存')
  2987. ->where('批次号',$item['批次号'])
  2988. ->where('关联号',$item['关联编号'])
  2989. ->where('物料编号',$item['物料编码'])
  2990. ->whereNull('Mod_rq')
  2991. ->fetchSql(true)
  2992. ->update($inventoryData);
  2993. \db()->query($InventorySql);
  2994. // 记录入库报工日志
  2995. $ReportData = [
  2996. '批次号' => $item['批次号'],
  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. 'type' => $item['type'],
  3012. 'sys_rq' => date('Y-m-d H:i:s'),
  3013. ];
  3014. $sql = \db('库存_出入库明细')->fetchSql(true)->insert($ReportData);
  3015. \db()->query($sql);
  3016. }
  3017. $this->success('成功');
  3018. }
  3019. /**
  3020. * 总检日产量汇总
  3021. * code:工序名称
  3022. */
  3023. public function DailyProductionReport(){
  3024. if ($this->request->isGet() === false) {
  3025. $this->error('请求错误');
  3026. }
  3027. $param = $this->request->param();
  3028. if (!empty($param['code'])) {
  3029. $threeMonthsAgo = date('Y-m-d H:i:s', strtotime('-1 months'));
  3030. // 先获取每个分组的最新日期和汇总数据
  3031. $subQuery = Db::name('设备_产量计酬')->alias('c')
  3032. ->field('c.订单编号, c.子订单编号, c.尺码, SUM(c.数量) as 总数量, MAX(c.sys_rq) as rq')
  3033. ->where('工序名称', $param['code'])
  3034. ->where('c.sys_rq', '>=', $threeMonthsAgo)
  3035. ->group('c.子订单编号, c.尺码')
  3036. ->buildSql();
  3037. // 主查询关联工单信息并排序
  3038. $res = Db::table($subQuery . ' t')
  3039. ->field('t.订单编号, t.子订单编号, t.总数量, t.尺码,j.生产款号, j.款式')
  3040. ->join('工单_基本资料 j', 't.订单编号 = j.订单编号', 'left')
  3041. ->order('t.rq desc, t.订单编号, t.子订单编号, t.尺码')
  3042. ->select();
  3043. $this->success('成功', [
  3044. 'riqi' => date('Y-m-d'),
  3045. 'list' => $res,
  3046. 'total' => count($res)
  3047. ]);
  3048. }else{
  3049. $this->error('缺少工序,参数错误');
  3050. }
  3051. }
  3052. }