WorkOrderSpotCheck.php 171 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418
  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('工单_基本资料')->alias('a')
  2673. ->field('
  2674. a.订单编号,
  2675. a.生产款号,
  2676. a.客户编号,
  2677. a.款式,
  2678. a.订单数量,
  2679. a.落货日期,
  2680. a.接单日期,
  2681. a.Sys_id as 创建人员,
  2682. a.Sys_rq as 创建时间,
  2683. b.BOM_颜色 as 颜色,
  2684. b.BOM_物料编码 as 物料编码,
  2685. b.BOM_物料名称 as 物料名称,
  2686. GROUP_CONCAT(DISTINCT c.订单编号 SEPARATOR ",") AS 关联订单,
  2687. c.关联编号')
  2688. ->group('物料编码')
  2689. ->join('工单_面料资料 b','a.订单编号 = b.BOM_工单编号')
  2690. ->join('工单关联表 c','a.订单编号 = c.订单编号 AND b.BOM_物料编码 = c.物料编号')
  2691. ->where('a.生产款号|a.订单编号|b.BOM_物料编码','like','%'.$param['search'].'%')
  2692. ->where('a.Mod_rq',null)
  2693. ->where('b.Mod_rq',null)
  2694. ->where('c.Mod_rq',null)
  2695. ->order('a.订单编号 desc')
  2696. ->select();
  2697. if (empty($list)){
  2698. $res = \db('工单_基本资料')->alias('a')
  2699. ->join('工单_印件资料 b','a.订单编号 = b.订单编号')
  2700. ->join('工单_bom资料 c','a.订单编号 = c.BOM_工单编号')
  2701. ->where('b.订单编号','like','%'.$param['search'].'%')
  2702. ->field('a.订单编号,a.生产款号,b.颜色备注,c.BOM_物料名称,a.Sys_id')
  2703. ->where('a.Mod_rq',null)
  2704. ->where('b.Mod_rq',null)
  2705. ->where('c.Mod_rq',null)
  2706. ->group('c.BOM_物料名称')
  2707. ->select();
  2708. foreach ($res as $index => &$item) {
  2709. $item['物料编码'] = $item['生产款号'] . '-' . $item['颜色备注'] . ($index + 1);
  2710. }
  2711. unset($item);
  2712. $arr = \db('工单关联表')
  2713. ->orderRaw("CAST(SUBSTRING(关联编号, 5) AS UNSIGNED) DESC")
  2714. ->find();
  2715. // 2. 提取数字部分并加一
  2716. $num = intval(substr($arr['关联编号'], 4)) + 1;
  2717. // 3. 重新拼接新的编号(保留 GDGL 前缀)
  2718. $newCode = 'GDGL' . $num;
  2719. // 3. 定义当前日期
  2720. $today = date('Y-m-d');
  2721. // 4. 循环插入到两个表
  2722. foreach ($res as $row) {
  2723. // 插入到 工单_面料资料
  2724. $sql = \db('工单_面料资料')->fetchSql(true)->insert([
  2725. 'BOM_工单编号' => $row['订单编号'],
  2726. 'BOM_颜色' => $row['颜色备注'],
  2727. 'BOM_物料编码' => $row['物料编码'],
  2728. 'BOM_物料名称' => $row['BOM_物料名称'],
  2729. 'Sys_ID' => $row['Sys_id'],
  2730. 'Sys_rq' => $today,
  2731. ]);
  2732. \db()->query($sql);
  2733. // 插入到 工单关联表
  2734. $sqll = \db('工单关联表')->fetchSql(true)->insert([
  2735. '关联编号' => $newCode,
  2736. '订单编号' => $row['订单编号'],
  2737. '生产款号' => $row['生产款号'],
  2738. '颜色' => $row['颜色备注'],
  2739. '物料编号' => $row['物料编码'],
  2740. '物料名称' => $row['BOM_物料名称'],
  2741. 'Sys_id' => $row['Sys_id'],
  2742. 'Sys_rq' => $today,
  2743. ]);
  2744. \db()->query($sqll);
  2745. }
  2746. $list = \db('工单_基本资料')
  2747. ->alias('a')
  2748. ->join('工单_面料资料 b','a.订单编号 = b.BOM_工单编号')
  2749. ->join('工单关联表 c','a.订单编号 = c.订单编号 AND b.BOM_物料编码 = c.物料编号')
  2750. ->where('a.生产款号|a.订单编号|b.BOM_物料编码','like','%'.$param['search'].'%')
  2751. ->where('a.Mod_rq',null)
  2752. ->where('b.Mod_rq',null)
  2753. ->where('c.Mod_rq',null)
  2754. ->field('a.订单编号,a.生产款号,a.客户编号,a.款式,a.订单数量,a.落货日期,a.接单日期,a.Sys_id as 创建人员,
  2755. a.Sys_rq as 创建时间,b.BOM_颜色 as 颜色,b.BOM_物料编码 as 物料编码,b.BOM_物料名称 as 物料名称,GROUP_CONCAT(DISTINCT c.订单编号 SEPARATOR ",") AS 关联订单,c.关联编号')
  2756. ->group('物料编码')
  2757. ->order('a.订单编号 desc')
  2758. ->select();
  2759. $this->success('成功',$list);
  2760. }else{
  2761. if(count($list) > 800){
  2762. $this->success('成功','');
  2763. }else{
  2764. $this->success('成功',$list);
  2765. }
  2766. // $this->success('成功',$list);
  2767. }
  2768. }
  2769. /**
  2770. * 面料入库报工
  2771. */
  2772. public function inputStash()
  2773. {
  2774. if (!$this->request->isPost()) {
  2775. $this->error('请求错误');
  2776. }
  2777. $param = Request::instance()->post();
  2778. if (empty($param)) {
  2779. $this->error('参数错误');
  2780. }
  2781. // 开启事务
  2782. Db::startTrans();
  2783. try {
  2784. //获取最大批次号(兼容 GDGL 前缀)
  2785. $lastBatch = Db::name('物料_库存')
  2786. ->where('批次号', 'like', 'GDGL%')
  2787. ->orderRaw('CAST(SUBSTRING(批次号, 5) AS UNSIGNED) DESC')
  2788. ->value('批次号');
  2789. $currentNumber = $lastBatch ? intval(substr($lastBatch, 4)) : 0;
  2790. $insertInventory = []; // 新入库
  2791. $updateInventory = []; // 更新库存
  2792. $insertLogs = []; // 出入库日志
  2793. foreach ($param as $item) {
  2794. if (empty($item['物料编码']) || empty($item['关联编号']) || !isset($item['number']) || !is_numeric($item['number']) || $item['number'] <= 0) {
  2795. continue;
  2796. }
  2797. // 唯一KEY:物料编码 + 物料名称 + 关联号 + 未删除
  2798. $existingBatch = Db::name('物料_库存')
  2799. ->where('物料编号', $item['物料编码'])
  2800. ->where('物料名称', $item['物料名称'])
  2801. ->where('关联号', $item['关联编号'])
  2802. ->whereNull('Mod_rq')
  2803. ->value('批次号');
  2804. if ($existingBatch) {
  2805. //更新库存
  2806. $updateData = [
  2807. '入仓总量' => Db::raw('入仓总量 + ' . intval($item['number'])),
  2808. '库存数量' => Db::raw('库存数量 + ' . intval($item['number'])),
  2809. '实际门幅' => $item['实际门幅'],
  2810. 'departname' => $item['departname'],
  2811. 'remark' => $item['remark'] ?? '',
  2812. 'sys_rq' => date('Y-m-d H:i:s'),
  2813. ];
  2814. Db::name('物料_库存')
  2815. ->where('批次号', $existingBatch)
  2816. ->update($updateData);
  2817. $BatchNumber = $existingBatch;
  2818. } else {
  2819. //新增库存
  2820. $currentNumber++;
  2821. $BatchNumber = 'GDGL' . $currentNumber;
  2822. $insertInventory[] = [
  2823. '批次号' => $BatchNumber,
  2824. '关联号' => $item['关联编号'],
  2825. '物料编号' => $item['物料编码'],
  2826. '物料名称' => $item['物料名称'],
  2827. '入仓总量' => $item['number'],
  2828. '库存数量' => $item['number'],
  2829. '单位' => $item['单位'],
  2830. '实际门幅' => $item['实际门幅'],
  2831. '状态' => 1,
  2832. 'departname' => $item['departname'],
  2833. 'remark' => $item['remark'] ?? '',
  2834. 'sys_id' => $item['sys_id'],
  2835. 'sys_rq' => date('Y-m-d H:i:s'),
  2836. ];
  2837. }
  2838. //日志
  2839. $insertLogs[] = [
  2840. '批次号' => $BatchNumber,
  2841. 'order_id' => $item['order_id'],
  2842. '款号' => $item['款号'],
  2843. '物料编码' => $item['物料编码'],
  2844. '物料名称' => $item['物料名称'],
  2845. '库存数量' => $item['number'],
  2846. 'number' => $item['number'],
  2847. 'rq' => $item['rq'],
  2848. 'name' => $item['name'],
  2849. 'sys_id' => $item['sys_id'],
  2850. 'receipt_number' => $item['receipt_number'],
  2851. 'departname' => $item['departname'],
  2852. 'remark' => $item['remark'] ?? '',
  2853. '客户编号' => $item['客户编号'],
  2854. '款式' => $item['款式'],
  2855. 'sys_rq' => date('Y-m-d H:i:s'),
  2856. ];
  2857. }
  2858. // 批量新增
  2859. if (!empty($insertInventory)) {
  2860. Db::name('物料_库存')->fetchSql(true)->insert($insertInventory);
  2861. }
  2862. // 批量日志
  2863. if (!empty($insertLogs)) {
  2864. Db::name('库存_出入库明细')->fetchSql(true)->insert($insertLogs);
  2865. }
  2866. Db::commit();
  2867. $this->success('入库成功');
  2868. } catch (\Exception $e) {
  2869. Db::rollback();
  2870. $this->error('操作失败:' . $e->getMessage());
  2871. }
  2872. }
  2873. // public function inputStash()
  2874. // {
  2875. // if (!$this->request->isPost()) {
  2876. // $this->error('请求错误');
  2877. // }
  2878. //
  2879. // $param = Request::instance()->post();
  2880. // if (empty($param)) {
  2881. // $this->error('参数错误');
  2882. // }
  2883. //
  2884. // // 获取当前最大批次号
  2885. // $lastBatch = \db('物料_库存')
  2886. // ->field("批次号")
  2887. // ->orderRaw("CAST(SUBSTRING(批次号, 4) AS UNSIGNED) DESC")
  2888. // ->limit(1)
  2889. // ->value('批次号');
  2890. //
  2891. // $currentNumber = 0;
  2892. // if (!empty($lastBatch) && preg_match('/^PCH(\d+)$/i', $lastBatch, $match)) {
  2893. // $currentNumber = intval($match[1]);
  2894. // }
  2895. //
  2896. // foreach ($param as $item) {
  2897. // // 查找是否已有相同物料编号 + 关联号的库存记录
  2898. // $existingBatch = \db('物料_库存')
  2899. // ->where('物料编号', $item['物料编码'])
  2900. // ->where('物料名称', $item['物料名称'])
  2901. // ->whereNull('Mod_rq')
  2902. // ->where('关联号', $item['关联编号'])
  2903. // ->value('批次号');
  2904. //
  2905. //
  2906. // // 更新库存数据
  2907. // $Inventory = \db('物料_库存')
  2908. // ->where('批次号',$item['批次号'])
  2909. // ->field('库存数量,入仓总量')
  2910. // ->find();
  2911. //
  2912. //
  2913. // if ($existingBatch) {
  2914. // $BatchNumber = $existingBatch; // 已入库,使用原批次号
  2915. // $InventoryData = [
  2916. // '批次号' => $item['批次号'],
  2917. // '关联号' => $item['关联编号'],
  2918. // '物料编号' => $item['物料编码'],
  2919. // '物料名称' => $item['物料名称'],
  2920. // '入仓总量' => $Inventory['入仓总量'] + $item['number'],
  2921. // '库存数量' => $Inventory['库存数量'] + $item['number'],
  2922. // '实际门幅' => $item['实际门幅'],
  2923. // '单位' => $item['单位'],
  2924. // '状态' => 1,
  2925. // 'departname' => $item['departname'],
  2926. // 'remark' => isset($item['remark']) ? $item['remark'] : '',
  2927. // 'sys_id' => $item['sys_id'],
  2928. // 'sys_rq' => date('Y-m-d H:i:s'),
  2929. // ];
  2930. // $sql = \db('物料_库存')->fetchSql(true)->where('批次号',$BatchNumber)->update($InventoryData);
  2931. // \db()->query($sql);
  2932. // } else {
  2933. // // 生成新批次号
  2934. // $currentNumber++;
  2935. // $BatchNumber = 'PCH' . $currentNumber;
  2936. //
  2937. // // 插入库存表
  2938. // $InventoryData = [
  2939. // '批次号' => $BatchNumber,
  2940. // '关联号' => $item['关联编号'],
  2941. // '物料编号' => $item['物料编码'],
  2942. // '物料名称' => $item['物料名称'],
  2943. // '入仓总量' => $item['number'],
  2944. // '库存数量' => $item['number'],
  2945. // '单位' => $item['单位'],
  2946. // '实际门幅' => $item['实际门幅'],
  2947. // '状态' => 1,
  2948. // 'departname' => $item['departname'],
  2949. // 'remark' => isset($item['remark']) ? $item['remark'] : '',
  2950. // 'sys_id' => $item['sys_id'],
  2951. // 'sys_rq' => date('Y-m-d H:i:s'),
  2952. // ];
  2953. // $sql = \db('物料_库存')->fetchSql(true)->insert($InventoryData);
  2954. // \db()->query($sql);
  2955. // }
  2956. //
  2957. // // 每条都写一条报工日志
  2958. // $ReportData = [
  2959. // '批次号' => $BatchNumber,
  2960. // 'order_id' => $item['order_id'],
  2961. // '款号' => $item['款号'],
  2962. // '物料编码' => $item['物料编码'],
  2963. // '物料名称' => $item['物料名称'],
  2964. // '库存数量' => $item['number'],
  2965. // 'number' => $item['number'],
  2966. // 'rq' => $item['rq'],
  2967. // 'name' => $item['name'],
  2968. // 'sys_id' => $item['sys_id'],
  2969. // 'receipt_number' => $item['receipt_number'],
  2970. // 'departname' => $item['departname'],
  2971. // 'remark' => isset($item['remark']) ? $item['remark'] : '',
  2972. // '客户编号' => $item['客户编号'],
  2973. // '款式' => $item['款式'],
  2974. // 'sys_rq' => date('Y-m-d H:i:s'),
  2975. // ];
  2976. // $sql = \db('库存_出入库明细')->fetchSql(true)->insert($ReportData);
  2977. // \db()->query($sql);
  2978. // }
  2979. //
  2980. // $this->success('成功');
  2981. // }
  2982. /**
  2983. * 出库订单信息查询
  2984. */
  2985. public function OutOrderSearch()
  2986. {
  2987. if ($this->request->isGet() === false){
  2988. $this->error('请求错误');
  2989. }
  2990. $param = $this->request->param();
  2991. if (empty($param)){
  2992. $this->error('参数错误');
  2993. }
  2994. $list = \db('工单_基本资料')
  2995. ->alias('a')
  2996. ->join('工单_面料资料 b','a.订单编号 = b.BOM_工单编号')
  2997. ->join('物料_库存 c','b.BOM_物料编码 = c.物料编号','left')
  2998. ->join('工单关联表 d','a.订单编号 = d.订单编号 AND d.物料编号 = b.BOM_物料编码')
  2999. ->where('a.生产款号|a.订单编号|b.BOM_物料编码','like','%'.$param['search'].'%')
  3000. ->where('a.Mod_rq',null)
  3001. ->where('b.Mod_rq',null)
  3002. ->where('c.状态',1)
  3003. ->field('a.订单编号,a.生产款号,a.客户编号,a.款式,a.订单数量,a.落货日期,a.接单日期,a.Sys_id as 创建人员,
  3004. a.Sys_rq as 创建时间,b.BOM_颜色 as 颜色,b.BOM_物料编码 as 物料编码,b.BOM_物料名称 as 物料名称,c.批次号,d.关联编号')
  3005. ->order('a.订单编号 desc')
  3006. ->select();
  3007. if (empty($list)){
  3008. $this->success('未找到相关订单信息');
  3009. }else{
  3010. $this->success('成功',$list);
  3011. }
  3012. }
  3013. /**
  3014. * 获取入库面料信息
  3015. */
  3016. public function getInputDetail()
  3017. {
  3018. if ($this->request->isGet() === false){
  3019. $this->error('请求错误');
  3020. }
  3021. $param = $this->request->param();
  3022. if (empty($param)){
  3023. $this->error('参数错误');
  3024. }
  3025. $list = \db('工单_面料资料')
  3026. ->alias('b')
  3027. ->join('物料_库存 a', 'a.物料编号 = b.BOM_物料编码 AND a.物料名称 = b.BOM_物料名称 AND a.Mod_rq IS NULL', 'left')
  3028. ->join('工单关联表 c', 'b.BOM_物料编码 = c.物料编号 AND b.BOM_工单编号 = c.订单编号')
  3029. ->field('a.Mod_rq, a.批次号, c.关联编号, b.BOM_物料编码, b.BOM_物料名称, a.入仓总量, a.库存数量, a.领用数量, a.退还数量, a.单位, a.实际门幅,
  3030. a.departname, b.BOM_工单编号, b.BOM_颜色, b.BOM_计划门幅 as 计划门幅, b.BOM_定额门幅, b.BOM_计划用量 as 计划用料, b.BOM_标准用量 as 定额用料')
  3031. ->where('c.关联编号', $param['关联编号'])
  3032. ->where('c.订单编号', $param['订单编号'])
  3033. ->where('b.BOM_物料编码', $param['物料编码'])
  3034. ->where('a.Mod_rq',null)
  3035. ->where('b.Mod_rq',null)
  3036. ->where('c.Mod_rq',null)
  3037. ->find();
  3038. // if (empty($param['Mod_rq'])) { // 如果 Mod_rq 为空,才关联物料库存表
  3039. // $list = $list->join('物料_库存 a', 'a.物料编号 = b.BOM_物料编码 AND a.物料名称 = b.BOM_物料名称 AND a.Mod_rq IS NULL', 'left')
  3040. // ->field('a.批次号, a.入仓总量, a.库存数量, a.领用数量, a.退还数量, a.单位, a.实际门幅, a.departname');
  3041. // }
  3042. // $list = $list->where('c.关联编号', $param['关联编号'])
  3043. // ->where('c.订单编号', $param['订单编号'])
  3044. // ->where('b.BOM_物料编码', $param['物料编码'])
  3045. // ->find();
  3046. $orderList = \db('工单关联表')
  3047. ->where('关联编号',$param['关联编号'])
  3048. ->column('GROUP_CONCAT(DISTINCT 订单编号 SEPARATOR ",") AS 关联订单');
  3049. $list['关联订单'] = $orderList[0];
  3050. $this->success('成功',$list);
  3051. }
  3052. /**
  3053. * 裁剪面料退货包工
  3054. * @return void
  3055. * @throws \think\db\exception\DataNotFoundException
  3056. * @throws \think\db\exception\ModelNotFoundException
  3057. * @throws \think\exception\DbException
  3058. * @throws \think\exception\PDOException
  3059. */
  3060. public function TuihuoReport()
  3061. {
  3062. if ($this->request->isPost() === false){
  3063. $this->error('请求错误');
  3064. }
  3065. $param = Request::instance()->post();
  3066. if (empty($param)){
  3067. $this->error('参数错误');
  3068. }
  3069. foreach ($param as $item) {
  3070. // 查询库存数据
  3071. $number = \db('物料_库存')
  3072. ->where('批次号', $item['批次号'])
  3073. ->where('关联号', $item['关联编号'])
  3074. ->where('物料编号', $item['物料编码'])
  3075. ->whereNull('Mod_rq')
  3076. ->field('库存数量, 退还数量')
  3077. ->find();
  3078. // 根据类型调整库存
  3079. if ($item['type'] === '退仓') {
  3080. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  3081. $stockQuantity = (float)$number['库存数量'] + (float)$item['number'];
  3082. } elseif ($item['type'] === '退厂商') {
  3083. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  3084. $stockQuantity = (float)$number['库存数量'] - (float)$item['number'];
  3085. } elseif ($item['type'] === '销售') {
  3086. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  3087. $stockQuantity = (float)$number['库存数量'] - (float)$item['number'];
  3088. }elseif ($item['type'] === '退客户') {
  3089. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  3090. $stockQuantity = (float)$number['库存数量'] - (float)$item['number'];
  3091. // 检查库存是否足够
  3092. if ($stockQuantity < 0) {
  3093. $this->error('库存不足,无法进行退客户操作。当前库存:' . $number['库存数量']);
  3094. }
  3095. }else{
  3096. $returnQuantity = (float)$number['退还数量'] + (float)$item['number'];
  3097. $stockQuantity = (float)$number['库存数量'] ;
  3098. }
  3099. // 构建库存数据
  3100. $inventoryData = [
  3101. '批次号' => $item['批次号'],
  3102. '关联号' => $item['关联编号'],
  3103. '物料编号' => $item['物料编码'],
  3104. '物料名称' => $item['物料名称'],
  3105. '实际门幅' => $item['实际门幅'],
  3106. '单位' => $item['单位'],
  3107. '状态' => 1,
  3108. 'sys_id' => $item['sys_id'],
  3109. 'departname' => $item['departname'],
  3110. 'remark' => isset($item['remark']) ? $item['remark'] : '',
  3111. 'sys_rq' => date('Y-m-d H:i:s'),
  3112. '退还数量' => $returnQuantity,
  3113. '库存数量' => $stockQuantity,
  3114. ];
  3115. $InventorySql = \db('物料_库存')
  3116. ->where('批次号',$item['批次号'])
  3117. ->where('关联号',$item['关联编号'])
  3118. ->where('物料编号',$item['物料编码'])
  3119. ->whereNull('Mod_rq')
  3120. ->fetchSql(true)
  3121. ->update($inventoryData);
  3122. \db()->query($InventorySql);
  3123. // 记录入库报工日志
  3124. $ReportData = [
  3125. '批次号' => $item['批次号'],
  3126. 'order_id' => $item['order_id'],
  3127. '款号' => $item['款号'],
  3128. '物料编码' => $item['物料编码'],
  3129. '物料名称' => $item['物料名称'],
  3130. '库存数量' => $item['number'],
  3131. 'number' => $item['number'],
  3132. 'rq' => $item['rq'],
  3133. 'name' => $item['name'],
  3134. 'sys_id' => $item['sys_id'],
  3135. 'receipt_number' => $item['receipt_number'],
  3136. 'departname' => $item['departname'],
  3137. 'remark' => isset($item['remark']) ? $item['remark'] : '',
  3138. '客户编号' => $item['客户编号'],
  3139. '款式' => $item['款式'],
  3140. 'type' => $item['type'],
  3141. 'sys_rq' => date('Y-m-d H:i:s'),
  3142. ];
  3143. $sql = \db('库存_出入库明细')->fetchSql(true)->insert($ReportData);
  3144. \db()->query($sql);
  3145. }
  3146. $this->success('成功');
  3147. }
  3148. /**
  3149. * 总检日产量汇总
  3150. * code:工序名称
  3151. */
  3152. public function DailyProductionReport(){
  3153. if ($this->request->isGet() === false) {
  3154. $this->error('请求错误');
  3155. }
  3156. $param = $this->request->param();
  3157. if (!empty($param['code'])) {
  3158. $threeMonthsAgo = date('Y-m-d H:i:s', strtotime('-1 months'));
  3159. // 先获取每个分组的最新日期和汇总数据
  3160. $subQuery = Db::name('设备_产量计酬')->alias('c')
  3161. ->field('c.订单编号, c.子订单编号, c.尺码, SUM(c.数量) as 总数量, MAX(c.sys_rq) as rq')
  3162. ->where('工序名称', $param['code'])
  3163. ->where('c.sys_rq', '>=', $threeMonthsAgo)
  3164. ->group('c.子订单编号, c.尺码')
  3165. ->buildSql();
  3166. // 主查询关联工单信息并排序
  3167. $res = Db::table($subQuery . ' t')
  3168. ->field('t.订单编号, t.子订单编号, t.总数量, t.尺码,j.生产款号, j.款式')
  3169. ->join('工单_基本资料 j', 't.订单编号 = j.订单编号', 'left')
  3170. ->order('t.rq desc, t.订单编号, t.子订单编号, t.尺码')
  3171. ->select();
  3172. $this->success('成功', [
  3173. 'riqi' => date('Y-m-d'),
  3174. 'list' => $res,
  3175. 'total' => count($res)
  3176. ]);
  3177. }else{
  3178. $this->error('缺少工序,参数错误');
  3179. }
  3180. }
  3181. }