WorkOrderSpotCheck.php 162 KB

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