WorkOrderSpotCheck.php 148 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896
  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. $ddorder = explode('-', $paramArray[0]);
  948. $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  949. $clwhere['订单编号'] = $ddorder[0];
  950. $clwhere['工序名称'] = $gongxuname;
  951. $clwhere['mod_rq'] = null;
  952. $cljclist = \db('设备_产量计酬')
  953. ->field('订单编号,子订单编号,尺码,s_num as 数量,mod_rq')
  954. ->where($clwhere)
  955. ->select();
  956. $result = [];
  957. $totalQuantity = 0; // 总数量初始化
  958. $orderNumber = ''; // 订单编号初始化
  959. foreach ($cljclist as $item) {
  960. $size = $item['尺码'];
  961. $orderNumber = $item['订单编号'];
  962. if (isset($result[$size])) {
  963. $result[$size] += $item['数量'];
  964. } else {
  965. $result[$size] = $item['数量'];
  966. }
  967. // 累加总数量
  968. $totalQuantity += $item['数量'];
  969. }
  970. foreach ($result as $size => $quantity) {
  971. if ($size !== '订单编号' && $size !== '数量') {
  972. $result[$size] = '合计(' . $quantity . ')';
  973. }
  974. }
  975. $result['订单编号'] = $orderNumber;
  976. $result['数量'] = '合计(' . $totalQuantity . ')';
  977. $sizeData = getSizeSummary($ddorder[0]);
  978. // 初始化 $sizeData['records'],以防万一它不存在
  979. if (!isset($sizeData['records'])) {
  980. $sizeData['records'] = [];
  981. }
  982. // 将 $result 插入到 $sizeData['records'] 的最前面
  983. array_unshift($sizeData['records'], $result);
  984. $this->success('请求成功', [
  985. 'result' => $results,//小票数据
  986. 'headers' => $sizeData['headers'], // 尺码表头
  987. 'records' => $sizeData['records'] // 查询历史记录
  988. ]);
  989. }else{
  990. // 查询历史记录
  991. $res = \db('设备_产量计酬')->alias('c')
  992. ->field('
  993. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.s_num as 数量, c.sczl_jtbh,
  994. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  995. j.款式,
  996. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  997. ')
  998. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  999. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1000. ->where('c.订单编号', 'like', '%' . explode('DC',$order)[1] . '%')
  1001. ->where('c.工序名称', $params['code'])
  1002. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1003. ->whereNull('c.mod_rq') // 查询未删除数据
  1004. ->order('UniqId desc')
  1005. ->select();
  1006. // 初始化返回数据的结构
  1007. $reslist = [];
  1008. $sizes = [];
  1009. // 遍历查询结果,构造需要的结构化数据
  1010. foreach ($res as $record) {
  1011. $size = $record['尺码']; // 记录的尺码
  1012. $quantity = $record['数量']; // 每个记录的数量
  1013. $serial = $record['serial']; // 记录的serial
  1014. // 记录所有不同的尺码,避免重复
  1015. if (!in_array($size, $sizes)) {
  1016. $sizes[] = $size;
  1017. }
  1018. // 构造每条记录的详细信息
  1019. $reslist[] = [
  1020. 'UniqId' => $record['UniqId'],
  1021. '订单编号' => $record['订单编号'],
  1022. '子订单编号' => $record['子订单编号'],
  1023. '上报数量' => $record['s_num'],
  1024. '剩余数量' => $record['ci_num'],
  1025. '次品数量' => $record['数量'] - $record['ci_num'],
  1026. '款号' => $record['款号'],
  1027. '尺码' => $record['尺码'],
  1028. '工序编号' => $record['工序编号'],
  1029. '工序名称' => $record['工序名称'],
  1030. '数量' => $quantity,
  1031. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1032. '颜色' => $record['颜色备注'],
  1033. '组别' => $record['sczl_jtbh'],
  1034. '款式' => $record['款式'],
  1035. 'serial' => '第('.$serial.')包',
  1036. 'sys_rq' => $record['sys_rq']
  1037. ];
  1038. }
  1039. // 按 sys_rq (时间) 或 UniqId 排序(按实际需求选)
  1040. usort($reslist, function ($a, $b) {
  1041. return strtotime($b['sys_rq']) - strtotime($a['sys_rq']);
  1042. });
  1043. // 返回结构化的查询结果
  1044. $this->success('请求成功', [
  1045. 'result' => $res, // 原始查询结果
  1046. 'headers' => $sizes, // 尺码表头
  1047. 'records' => $reslist // 处理后的记录列表
  1048. ]);
  1049. }
  1050. break;
  1051. case '大烫':
  1052. $gongxuname = '大烫';
  1053. $gongxuncode = '5';
  1054. // 查询历史记录并生成尺寸汇总
  1055. function getSizeSummary($order) {
  1056. $where = [];
  1057. if (strpos($order, '-') !== false) {
  1058. $where['c.子订单编号'] = $order;
  1059. } else {
  1060. $where['c.订单编号'] = ['like', '%' . $order . '%'];
  1061. }
  1062. $where['c.工序编号'] = '5';
  1063. $where['c.mod_rq'] = null;
  1064. $existingRecords = \db('设备_产量计酬')->alias('c')
  1065. ->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,
  1066. ci_num,s_num,
  1067. y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注, CAST(c.serial_num AS SIGNED) as serial_num_numeric')
  1068. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1069. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1070. ->where($where)
  1071. ->order('c.sys_rq desc')
  1072. ->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.款式')
  1073. ->select();
  1074. $sizeSummary = [];
  1075. $sizes = [];
  1076. foreach ($existingRecords as $record) {
  1077. $size = $record['尺码'];
  1078. $quantity = $record['数量'];
  1079. $serial = $record['serial'];
  1080. $serialNum = $record['serial_num']; // serial_num 分组
  1081. $processNumber = $record['工序编号']; // 工序编号
  1082. // 初始化尺码
  1083. if (!in_array($size, $sizes)) {
  1084. $sizes[] = $size;
  1085. }
  1086. // 检查是否已有相同的 serial_num 组合
  1087. if (!isset($sizeSummary[$serialNum])) {
  1088. $sizeSummary[$serialNum] = [
  1089. '订单编号' => $record['订单编号'],
  1090. '子订单编号' => $record['子订单编号'],
  1091. '款号' => $record['款号'],
  1092. '款式' => $record['款式'],
  1093. '工序编号' => $record['工序编号'],
  1094. '工序名称' => $record['工序名称'],
  1095. // '上报数量' => $record['s_num'],
  1096. '剩余数量' => $record['ci_num'],
  1097. '数量' => '0', // 记录总数量
  1098. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1099. '颜色' => $record['颜色'],
  1100. '颜色备注' => $record['颜色备注'],
  1101. '机台号' => $record['sczl_jtbh'],
  1102. '组别' => $record['sczl_bh'],
  1103. 'serial' => [], // 流水号数组
  1104. 'sys_rq' => $record['sys_rq'],
  1105. 'UniqId' => $record['UniqId'],
  1106. '尺码数据' => [] // 用于存储每个尺码对应的流水号和数量
  1107. ];
  1108. }
  1109. // 将流水号添加到 serial 数组
  1110. $sizeSummary[$serialNum]['serial'][] = $serial;
  1111. // 累加数量到总数量
  1112. $sizeSummary[$serialNum]['数量'] += $quantity;
  1113. // 记录尺码数据
  1114. if (!isset($sizeSummary[$serialNum]['尺码数据'][$size])) {
  1115. $sizeSummary[$serialNum]['尺码数据'][$size] = [];
  1116. }
  1117. // 添加尺码对应的流水号和数量
  1118. $sizeSummary[$serialNum]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  1119. }
  1120. // 对每个工序的 serial 进行处理,并转换为逗号分隔的字符串
  1121. $finalRecords = [];
  1122. foreach ($sizeSummary as &$summary) {
  1123. // 将 serial 数组按数值降序排序
  1124. sort($summary['serial'], SORT_NUMERIC);
  1125. $summary['serial'] = implode(',', $summary['serial']);
  1126. // 合并尺码数据到同一层级
  1127. foreach ($summary['尺码数据'] as $size => $serials) {
  1128. $summary[$size] = implode(', ', $serials); // 尺码对应的流水号和数量
  1129. }
  1130. unset($summary['尺码数据']); // 移除尺码数据数组,直接将数据合并到 summary
  1131. $finalRecords[] = $summary;
  1132. }
  1133. // 尺码排序(数字在前,字母在后)
  1134. usort($sizes, function ($a, $b) {
  1135. $sizeOrder = ['XXXS' => 1,'XXS' => 2,'XS' => 3,'S' => 4, 'M' => 5, 'L' => 6, 'XL' => 7, 'XXL' => 8, 'XXXL' => 9, 'XXXXL' => 10];
  1136. if (is_numeric($a) && is_numeric($b)) return $a - $b;
  1137. if (is_numeric($a)) return -1;
  1138. if (is_numeric($b)) return 1;
  1139. return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  1140. });
  1141. return ['headers' => $sizes, 'records' => $finalRecords];
  1142. }
  1143. //判断是否为标签参数数据
  1144. if (strpos($order, ',') !== false) {
  1145. if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
  1146. $this->error('未获取到机台号');
  1147. }
  1148. // 将子订单编号, 机台号, 工序, 尺码, 数量, 尾包, 流水号(包)分割为数组
  1149. $paramArray = explode(',', $order);
  1150. // 获取车缝报工生成小票的数据
  1151. $Uniqid = \db('设备_产量计酬')
  1152. ->where([
  1153. '子订单编号' => $paramArray[0],
  1154. '尺码' => $paramArray[3],
  1155. '数量' => $paramArray[4],
  1156. 'serial' => $paramArray[6],
  1157. ])
  1158. ->find();
  1159. // 判断是否成功获取到数据
  1160. if ($Uniqid) {
  1161. $orderlist = \db('工单_基本资料')
  1162. ->where('订单编号', $Uniqid['订单编号'])
  1163. ->field('订单编号, 生产款号, 款式')
  1164. ->find();
  1165. $yinjianlist = \db('工单_印件资料')
  1166. ->where('订单编号', $orderlist['订单编号'])
  1167. ->field('zdtotal, sctotal,颜色,颜色备注')
  1168. ->find();
  1169. } else {
  1170. $this->error('当前扫码小票无效');
  1171. }
  1172. // 获取当前手工机台上报的数据
  1173. $res = \db('设备_产量计酬')
  1174. ->where([
  1175. '子订单编号' => $paramArray[0],
  1176. '尺码' => $paramArray[3],
  1177. '数量' => $paramArray[4],
  1178. 'serial' => $paramArray[6],
  1179. 'sczl_jtbh' => $params['sys_sbID'],
  1180. 'mod_rq' => null,
  1181. ])
  1182. ->order('UniqId desc')
  1183. ->find();
  1184. if (empty($res)) {
  1185. $ci_num = $paramArray[4];
  1186. } else {
  1187. $ci_num = $res["ci_num"];
  1188. }
  1189. $results = [
  1190. 'sys_sbID' => $params['sys_sbID'],//设备编号
  1191. 'ci_num' => $ci_num,//剩余数量
  1192. 'UniqId' => $Uniqid['UniqId'],
  1193. 'order' => $paramArray[0],//子订单编号
  1194. 'zb' => $params['sys_sbID'],//组别
  1195. 'gx' => $gongxuncode,//默认固定工序
  1196. 'cm' => $paramArray[3],//尺码
  1197. 'sl' => $paramArray[4],//数量
  1198. 'wb' => $paramArray[5],//尾包
  1199. '订单编号' => $orderlist['订单编号'],
  1200. '生产款号' => $orderlist['生产款号'],
  1201. '款式' => $orderlist['款式'],
  1202. 'sctotal' => $yinjianlist['sctotal'],
  1203. 'zdtotal' => $yinjianlist['zdtotal'],
  1204. '颜色' => $yinjianlist['颜色备注'],
  1205. ];
  1206. $ddorder = explode('-', $paramArray[0]);
  1207. $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  1208. $clwhere['订单编号'] = $ddorder[0];
  1209. $clwhere['工序名称'] = $gongxuname;
  1210. $clwhere['mod_rq'] = null;
  1211. $cljclist = \db('设备_产量计酬')
  1212. ->field('订单编号,子订单编号,尺码,s_num as 数量,mod_rq')
  1213. ->where($clwhere)
  1214. ->select();
  1215. $result = [];
  1216. $totalQuantity = 0; // 总数量初始化
  1217. $orderNumber = ''; // 订单编号初始化
  1218. foreach ($cljclist as $item) {
  1219. $size = $item['尺码'];
  1220. $orderNumber = $item['订单编号'];
  1221. if (isset($result[$size])) {
  1222. $result[$size] += $item['数量'];
  1223. } else {
  1224. $result[$size] = $item['数量'];
  1225. }
  1226. // 累加总数量
  1227. $totalQuantity += $item['数量'];
  1228. }
  1229. foreach ($result as $size => $quantity) {
  1230. if ($size !== '订单编号' && $size !== '数量') {
  1231. $result[$size] = '合计(' . $quantity . ')';
  1232. }
  1233. }
  1234. $result['订单编号'] = $orderNumber;
  1235. $result['数量'] = '合计(' . $totalQuantity . ')';
  1236. $sizeData = getSizeSummary($ddorder[0]);
  1237. // 初始化 $sizeData['records'],以防万一它不存在
  1238. if (!isset($sizeData['records'])) {
  1239. $sizeData['records'] = [];
  1240. }
  1241. // 将 $result 插入到 $sizeData['records'] 的最前面
  1242. array_unshift($sizeData['records'], $result);
  1243. $this->success('请求成功', [
  1244. 'result' => $results,//小票数据
  1245. 'headers' => $sizeData['headers'], // 尺码表头
  1246. 'records' => $sizeData['records'] // 查询历史记录
  1247. ]);
  1248. }else{
  1249. // 查询历史记录
  1250. $res = \db('设备_产量计酬')->alias('c')
  1251. ->field('
  1252. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.s_num as 数量, c.sczl_jtbh,
  1253. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1254. j.款式,
  1255. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1256. ')
  1257. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1258. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1259. ->where('c.订单编号', 'like', '%' . explode('DC',$order)[1] . '%')
  1260. ->where('c.工序名称', $params['code'])
  1261. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1262. ->whereNull('c.mod_rq') // 查询未删除数据
  1263. ->order('UniqId desc')
  1264. ->select();
  1265. // 初始化返回数据的结构
  1266. $reslist = [];
  1267. $sizes = [];
  1268. // 遍历查询结果,构造需要的结构化数据
  1269. foreach ($res as $record) {
  1270. $size = $record['尺码']; // 记录的尺码
  1271. $quantity = $record['数量']; // 每个记录的数量
  1272. $serial = $record['serial']; // 记录的serial
  1273. // 记录所有不同的尺码,避免重复
  1274. if (!in_array($size, $sizes)) {
  1275. $sizes[] = $size;
  1276. }
  1277. // 构造每条记录的详细信息
  1278. $reslist[] = [
  1279. 'UniqId' => $record['UniqId'],
  1280. '订单编号' => $record['订单编号'],
  1281. '子订单编号' => $record['子订单编号'],
  1282. '上报数量' => $record['s_num'],
  1283. '剩余数量' => $record['ci_num'],
  1284. '次品数量' => $record['数量'] - $record['ci_num'],
  1285. '款号' => $record['款号'],
  1286. '尺码' => $record['尺码'],
  1287. '工序编号' => $record['工序编号'],
  1288. '工序名称' => $record['工序名称'],
  1289. '数量' => $quantity,
  1290. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1291. '颜色' => $record['颜色备注'],
  1292. '组别' => $record['sczl_jtbh'],
  1293. '款式' => $record['款式'],
  1294. 'serial' => '第('.$serial.')包',
  1295. 'sys_rq' => $record['sys_rq']
  1296. ];
  1297. }
  1298. // 按 sys_rq (时间) 或 UniqId 排序(按实际需求选)
  1299. usort($reslist, function ($a, $b) {
  1300. return strtotime($b['sys_rq']) - strtotime($a['sys_rq']);
  1301. });
  1302. // 返回结构化的查询结果
  1303. $this->success('请求成功', [
  1304. 'result' => $res, // 原始查询结果
  1305. 'headers' => $sizes, // 尺码表头
  1306. 'records' => $reslist // 处理后的记录列表
  1307. ]);
  1308. }
  1309. break;
  1310. case '总检':
  1311. $gongxuname = '总检';
  1312. $gongxuncode = '6';
  1313. // 查询历史记录并生成尺寸汇总
  1314. function getSizeSummary($order) {
  1315. $where = [];
  1316. if (strpos($order, '-') !== false) {
  1317. $where['c.子订单编号'] = $order;
  1318. } else {
  1319. $where['c.订单编号'] = ['like', '%' . $order . '%'];
  1320. }
  1321. $where['c.工序编号'] = '6';
  1322. $where['c.mod_rq'] = null;
  1323. $existingRecords = \db('设备_产量计酬')->alias('c')
  1324. ->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,
  1325. ci_num,s_num,
  1326. y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注, CAST(c.serial_num AS SIGNED) as serial_num_numeric')
  1327. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1328. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1329. ->where($where)
  1330. ->order('c.sys_rq desc')
  1331. ->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.款式')
  1332. ->select();
  1333. $sizeSummary = [];
  1334. $sizes = [];
  1335. foreach ($existingRecords as $record) {
  1336. $size = $record['尺码'];
  1337. $quantity = $record['数量'];
  1338. $serial = $record['serial'];
  1339. $serialNum = $record['serial_num']; // serial_num 分组
  1340. $processNumber = $record['工序编号']; // 工序编号
  1341. // 初始化尺码
  1342. if (!in_array($size, $sizes)) {
  1343. $sizes[] = $size;
  1344. }
  1345. // 检查是否已有相同的 serial_num 组合
  1346. if (!isset($sizeSummary[$serialNum])) {
  1347. $sizeSummary[$serialNum] = [
  1348. '订单编号' => $record['订单编号'],
  1349. '子订单编号' => $record['子订单编号'],
  1350. '款号' => $record['款号'],
  1351. '款式' => $record['款式'],
  1352. '工序编号' => $record['工序编号'],
  1353. '工序名称' => $record['工序名称'],
  1354. // '上报数量' => $record['s_num'],
  1355. '剩余数量' => $record['ci_num'],
  1356. '数量' => '0', // 记录总数量
  1357. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1358. '颜色' => $record['颜色'],
  1359. '颜色备注' => $record['颜色备注'],
  1360. '机台号' => $record['sczl_jtbh'],
  1361. '组别' => $record['sczl_bh'],
  1362. 'serial' => [], // 流水号数组
  1363. 'sys_rq' => $record['sys_rq'],
  1364. 'UniqId' => $record['UniqId'],
  1365. '尺码数据' => [] // 用于存储每个尺码对应的流水号和数量
  1366. ];
  1367. }
  1368. // 将流水号添加到 serial 数组
  1369. $sizeSummary[$serialNum]['serial'][] = $serial;
  1370. // 累加数量到总数量
  1371. $sizeSummary[$serialNum]['数量'] += $quantity;
  1372. // 记录尺码数据
  1373. if (!isset($sizeSummary[$serialNum]['尺码数据'][$size])) {
  1374. $sizeSummary[$serialNum]['尺码数据'][$size] = [];
  1375. }
  1376. // 添加尺码对应的流水号和数量
  1377. $sizeSummary[$serialNum]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  1378. }
  1379. // 对每个工序的 serial 进行处理,并转换为逗号分隔的字符串
  1380. $finalRecords = [];
  1381. foreach ($sizeSummary as &$summary) {
  1382. // 将 serial 数组按数值降序排序
  1383. sort($summary['serial'], SORT_NUMERIC);
  1384. $summary['serial'] = implode(',', $summary['serial']);
  1385. // 合并尺码数据到同一层级
  1386. foreach ($summary['尺码数据'] as $size => $serials) {
  1387. $summary[$size] = implode(', ', $serials); // 尺码对应的流水号和数量
  1388. }
  1389. unset($summary['尺码数据']); // 移除尺码数据数组,直接将数据合并到 summary
  1390. $finalRecords[] = $summary;
  1391. }
  1392. // 尺码排序(数字在前,字母在后)
  1393. usort($sizes, function ($a, $b) {
  1394. $sizeOrder = ['XXXS' => 1,'XXS' => 2,'XS' => 3,'S' => 4, 'M' => 5, 'L' => 6, 'XL' => 7, 'XXL' => 8, 'XXXL' => 9, 'XXXXL' => 10];
  1395. if (is_numeric($a) && is_numeric($b)) return $a - $b;
  1396. if (is_numeric($a)) return -1;
  1397. if (is_numeric($b)) return 1;
  1398. return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  1399. });
  1400. return ['headers' => $sizes, 'records' => $finalRecords];
  1401. }
  1402. //判断是否为标签参数数据
  1403. if (strpos($order, ',') !== false) {
  1404. if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
  1405. $this->error('未获取到机台号');
  1406. }
  1407. // 将子订单编号, 机台号, 工序, 尺码, 数量, 尾包, 流水号(包)分割为数组
  1408. $paramArray = explode(',', $order);
  1409. // 获取车缝报工生成小票的数据
  1410. $Uniqid = \db('设备_产量计酬')
  1411. ->where([
  1412. '子订单编号' => $paramArray[0],
  1413. '尺码' => $paramArray[3],
  1414. '数量' => $paramArray[4],
  1415. 'serial' => $paramArray[6],
  1416. ])
  1417. ->find();
  1418. // 判断是否成功获取到数据
  1419. if ($Uniqid) {
  1420. $orderlist = \db('工单_基本资料')
  1421. ->where('订单编号', $Uniqid['订单编号'])
  1422. ->field('订单编号, 生产款号, 款式')
  1423. ->find();
  1424. $yinjianlist = \db('工单_印件资料')
  1425. ->where('订单编号', $orderlist['订单编号'])
  1426. ->field('zdtotal, sctotal,颜色,颜色备注')
  1427. ->find();
  1428. } else {
  1429. $this->error('当前扫码小票无效');
  1430. }
  1431. // 获取当前手工机台上报的数据
  1432. $res = \db('设备_产量计酬')
  1433. ->where([
  1434. '子订单编号' => $paramArray[0],
  1435. '尺码' => $paramArray[3],
  1436. '数量' => $paramArray[4],
  1437. 'serial' => $paramArray[6],
  1438. 'sczl_jtbh' => $params['sys_sbID'],
  1439. 'mod_rq' => null,
  1440. ])
  1441. ->order('UniqId desc')
  1442. ->find();
  1443. if (empty($res)) {
  1444. $ci_num = $paramArray[4];
  1445. } else {
  1446. $ci_num = $res["ci_num"];
  1447. }
  1448. $results = [
  1449. 'sys_sbID' => $params['sys_sbID'],//设备编号
  1450. 'ci_num' => $ci_num,//剩余数量
  1451. 'UniqId' => $Uniqid['UniqId'],
  1452. 'order' => $paramArray[0],//子订单编号
  1453. 'zb' => $params['sys_sbID'],//组别
  1454. 'gx' => $gongxuncode,//默认固定工序
  1455. 'cm' => $paramArray[3],//尺码
  1456. 'sl' => $paramArray[4],//数量
  1457. 'wb' => $paramArray[5],//尾包
  1458. '订单编号' => $orderlist['订单编号'],
  1459. '生产款号' => $orderlist['生产款号'],
  1460. '款式' => $orderlist['款式'],
  1461. 'sctotal' => $yinjianlist['sctotal'],
  1462. 'zdtotal' => $yinjianlist['zdtotal'],
  1463. '颜色' => $yinjianlist['颜色备注'],
  1464. ];
  1465. $ddorder = explode('-', $paramArray[0]);
  1466. $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  1467. $clwhere['订单编号'] = $ddorder[0];
  1468. $clwhere['工序名称'] = $gongxuname;
  1469. $clwhere['mod_rq'] = null;
  1470. $cljclist = \db('设备_产量计酬')
  1471. ->field('订单编号,子订单编号,尺码,s_num as 数量,mod_rq')
  1472. ->where($clwhere)
  1473. ->select();
  1474. $result = [];
  1475. $totalQuantity = 0; // 总数量初始化
  1476. $orderNumber = ''; // 订单编号初始化
  1477. foreach ($cljclist as $item) {
  1478. $size = $item['尺码'];
  1479. $orderNumber = $item['订单编号'];
  1480. if (isset($result[$size])) {
  1481. $result[$size] += $item['数量'];
  1482. } else {
  1483. $result[$size] = $item['数量'];
  1484. }
  1485. // 累加总数量
  1486. $totalQuantity += $item['数量'];
  1487. }
  1488. foreach ($result as $size => $quantity) {
  1489. if ($size !== '订单编号' && $size !== '数量') {
  1490. $result[$size] = '合计(' . $quantity . ')';
  1491. }
  1492. }
  1493. $result['订单编号'] = $orderNumber;
  1494. $result['数量'] = '合计(' . $totalQuantity . ')';
  1495. $sizeData = getSizeSummary($ddorder[0]);
  1496. // 初始化 $sizeData['records'],以防万一它不存在
  1497. if (!isset($sizeData['records'])) {
  1498. $sizeData['records'] = [];
  1499. }
  1500. // 将 $result 插入到 $sizeData['records'] 的最前面
  1501. array_unshift($sizeData['records'], $result);
  1502. $this->success('请求成功', [
  1503. 'result' => $results,//小票数据
  1504. 'headers' => $sizeData['headers'], // 尺码表头
  1505. 'records' => $sizeData['records'] // 查询历史记录
  1506. ]);
  1507. }else{
  1508. // 查询历史记录
  1509. $res = \db('设备_产量计酬')->alias('c')
  1510. ->field('
  1511. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.s_num as 数量, c.sczl_jtbh,
  1512. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1513. j.款式,
  1514. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1515. ')
  1516. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1517. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1518. ->where('c.订单编号', 'like', '%' . explode('DC',$order)[1] . '%')
  1519. ->where('c.工序名称', $params['code'])
  1520. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1521. ->whereNull('c.mod_rq') // 查询未删除数据
  1522. ->order('UniqId desc')
  1523. ->select();
  1524. // 初始化返回数据的结构
  1525. $reslist = [];
  1526. $sizes = [];
  1527. // 遍历查询结果,构造需要的结构化数据
  1528. foreach ($res as $record) {
  1529. $size = $record['尺码']; // 记录的尺码
  1530. $quantity = $record['数量']; // 每个记录的数量
  1531. $serial = $record['serial']; // 记录的serial
  1532. // 记录所有不同的尺码,避免重复
  1533. if (!in_array($size, $sizes)) {
  1534. $sizes[] = $size;
  1535. }
  1536. // 构造每条记录的详细信息
  1537. $reslist[] = [
  1538. 'UniqId' => $record['UniqId'],
  1539. '订单编号' => $record['订单编号'],
  1540. '子订单编号' => $record['子订单编号'],
  1541. '上报数量' => $record['s_num'],
  1542. '剩余数量' => $record['ci_num'],
  1543. '次品数量' => $record['数量'] - $record['ci_num'],
  1544. '款号' => $record['款号'],
  1545. '尺码' => $record['尺码'],
  1546. '工序编号' => $record['工序编号'],
  1547. '工序名称' => $record['工序名称'],
  1548. '数量' => $quantity,
  1549. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1550. '颜色' => $record['颜色备注'],
  1551. '组别' => $record['sczl_jtbh'],
  1552. '款式' => $record['款式'],
  1553. 'serial' => '第('.$serial.')包',
  1554. 'sys_rq' => $record['sys_rq']
  1555. ];
  1556. }
  1557. // 按 sys_rq (时间) 或 UniqId 排序(按实际需求选)
  1558. usort($reslist, function ($a, $b) {
  1559. return strtotime($b['sys_rq']) - strtotime($a['sys_rq']);
  1560. });
  1561. // 返回结构化的查询结果
  1562. $this->success('请求成功', [
  1563. 'result' => $res, // 原始查询结果
  1564. 'headers' => $sizes, // 尺码表头
  1565. 'records' => $reslist // 处理后的记录列表
  1566. ]);
  1567. }
  1568. break;
  1569. case '包装':
  1570. $gongxuname = '包装';
  1571. $gongxuncode = '7';
  1572. // 查询历史记录并生成尺寸汇总
  1573. function getSizeSummary($order) {
  1574. $where = [];
  1575. if (strpos($order, '-') !== false) {
  1576. $where['c.子订单编号'] = $order;
  1577. } else {
  1578. $where['c.订单编号'] = ['like', '%' . $order . '%'];
  1579. }
  1580. $where['c.工序编号'] = '7';
  1581. $where['c.mod_rq'] = null;
  1582. $existingRecords = \db('设备_产量计酬')->alias('c')
  1583. ->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,
  1584. ci_num,s_num,
  1585. y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注, CAST(c.serial_num AS SIGNED) as serial_num_numeric')
  1586. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1587. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1588. ->where($where)
  1589. ->order('c.sys_rq desc')
  1590. ->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.款式')
  1591. ->select();
  1592. $sizeSummary = [];
  1593. $sizes = [];
  1594. foreach ($existingRecords as $record) {
  1595. $size = $record['尺码'];
  1596. $quantity = $record['数量'];
  1597. $serial = $record['serial'];
  1598. $serialNum = $record['serial_num']; // serial_num 分组
  1599. $processNumber = $record['工序编号']; // 工序编号
  1600. // 初始化尺码
  1601. if (!in_array($size, $sizes)) {
  1602. $sizes[] = $size;
  1603. }
  1604. // 检查是否已有相同的 serial_num 组合
  1605. if (!isset($sizeSummary[$serialNum])) {
  1606. $sizeSummary[$serialNum] = [
  1607. '订单编号' => $record['订单编号'],
  1608. '子订单编号' => $record['子订单编号'],
  1609. '款号' => $record['款号'],
  1610. '款式' => $record['款式'],
  1611. '工序编号' => $record['工序编号'],
  1612. '工序名称' => $record['工序名称'],
  1613. // '上报数量' => $record['s_num'],
  1614. '剩余数量' => $record['ci_num'],
  1615. '数量' => '0', // 记录总数量
  1616. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1617. '颜色' => $record['颜色'],
  1618. '颜色备注' => $record['颜色备注'],
  1619. '机台号' => $record['sczl_jtbh'],
  1620. '组别' => $record['sczl_bh'],
  1621. 'serial' => [], // 流水号数组
  1622. 'sys_rq' => $record['sys_rq'],
  1623. 'UniqId' => $record['UniqId'],
  1624. '尺码数据' => [] // 用于存储每个尺码对应的流水号和数量
  1625. ];
  1626. }
  1627. // 将流水号添加到 serial 数组
  1628. $sizeSummary[$serialNum]['serial'][] = $serial;
  1629. // 累加数量到总数量
  1630. $sizeSummary[$serialNum]['数量'] += $quantity;
  1631. // 记录尺码数据
  1632. if (!isset($sizeSummary[$serialNum]['尺码数据'][$size])) {
  1633. $sizeSummary[$serialNum]['尺码数据'][$size] = [];
  1634. }
  1635. // 添加尺码对应的流水号和数量
  1636. $sizeSummary[$serialNum]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  1637. }
  1638. // 对每个工序的 serial 进行处理,并转换为逗号分隔的字符串
  1639. $finalRecords = [];
  1640. foreach ($sizeSummary as &$summary) {
  1641. // 将 serial 数组按数值降序排序
  1642. sort($summary['serial'], SORT_NUMERIC);
  1643. $summary['serial'] = implode(',', $summary['serial']);
  1644. // 合并尺码数据到同一层级
  1645. foreach ($summary['尺码数据'] as $size => $serials) {
  1646. $summary[$size] = implode(', ', $serials); // 尺码对应的流水号和数量
  1647. }
  1648. unset($summary['尺码数据']); // 移除尺码数据数组,直接将数据合并到 summary
  1649. $finalRecords[] = $summary;
  1650. }
  1651. // 尺码排序(数字在前,字母在后)
  1652. usort($sizes, function ($a, $b) {
  1653. $sizeOrder = ['XXXS' => 1,'XXS' => 2,'XS' => 3,'S' => 4, 'M' => 5, 'L' => 6, 'XL' => 7, 'XXL' => 8, 'XXXL' => 9, 'XXXXL' => 10];
  1654. if (is_numeric($a) && is_numeric($b)) return $a - $b;
  1655. if (is_numeric($a)) return -1;
  1656. if (is_numeric($b)) return 1;
  1657. return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  1658. });
  1659. return ['headers' => $sizes, 'records' => $finalRecords];
  1660. }
  1661. //判断是否为标签参数数据
  1662. if (strpos($order, ',') !== false) {
  1663. if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
  1664. $this->error('未获取到机台号');
  1665. }
  1666. // 将子订单编号, 机台号, 工序, 尺码, 数量, 尾包, 流水号(包)分割为数组
  1667. $paramArray = explode(',', $order);
  1668. // 获取车缝报工生成小票的数据
  1669. $Uniqid = \db('设备_产量计酬')
  1670. ->where([
  1671. '子订单编号' => $paramArray[0],
  1672. '尺码' => $paramArray[3],
  1673. '数量' => $paramArray[4],
  1674. 'serial' => $paramArray[6],
  1675. ])
  1676. ->find();
  1677. // 判断是否成功获取到数据
  1678. if ($Uniqid) {
  1679. $orderlist = \db('工单_基本资料')
  1680. ->where('订单编号', $Uniqid['订单编号'])
  1681. ->field('订单编号, 生产款号, 款式')
  1682. ->find();
  1683. $yinjianlist = \db('工单_印件资料')
  1684. ->where('订单编号', $orderlist['订单编号'])
  1685. ->field('zdtotal, sctotal,颜色,颜色备注')
  1686. ->find();
  1687. } else {
  1688. $this->error('当前扫码小票无效');
  1689. }
  1690. // 获取当前手工机台上报的数据
  1691. $res = \db('设备_产量计酬')
  1692. ->where([
  1693. '子订单编号' => $paramArray[0],
  1694. '尺码' => $paramArray[3],
  1695. '数量' => $paramArray[4],
  1696. 'serial' => $paramArray[6],
  1697. 'sczl_jtbh' => $params['sys_sbID'],
  1698. 'mod_rq' => null,
  1699. ])
  1700. ->order('UniqId desc')
  1701. ->find();
  1702. if (empty($res)) {
  1703. $ci_num = $paramArray[4];
  1704. } else {
  1705. $ci_num = $res["ci_num"];
  1706. }
  1707. $results = [
  1708. 'sys_sbID' => $params['sys_sbID'],//设备编号
  1709. 'ci_num' => $ci_num,//剩余数量
  1710. 'UniqId' => $Uniqid['UniqId'],
  1711. 'order' => $paramArray[0],//子订单编号
  1712. 'zb' => $params['sys_sbID'],//组别
  1713. 'gx' => $gongxuncode,//默认固定工序
  1714. 'cm' => $paramArray[3],//尺码
  1715. 'sl' => $paramArray[4],//数量
  1716. 'wb' => $paramArray[5],//尾包
  1717. '订单编号' => $orderlist['订单编号'],
  1718. '生产款号' => $orderlist['生产款号'],
  1719. '款式' => $orderlist['款式'],
  1720. 'sctotal' => $yinjianlist['sctotal'],
  1721. 'zdtotal' => $yinjianlist['zdtotal'],
  1722. '颜色' => $yinjianlist['颜色备注'],
  1723. ];
  1724. $ddorder = explode('-', $paramArray[0]);
  1725. $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  1726. $clwhere['订单编号'] = $ddorder[0];
  1727. $clwhere['工序名称'] = $gongxuname;
  1728. $clwhere['mod_rq'] = null;
  1729. $cljclist = \db('设备_产量计酬')
  1730. ->field('订单编号,子订单编号,尺码,s_num as 数量,mod_rq')
  1731. ->where($clwhere)
  1732. ->select();
  1733. $result = [];
  1734. $totalQuantity = 0; // 总数量初始化
  1735. $orderNumber = ''; // 订单编号初始化
  1736. foreach ($cljclist as $item) {
  1737. $size = $item['尺码'];
  1738. $orderNumber = $item['订单编号'];
  1739. if (isset($result[$size])) {
  1740. $result[$size] += $item['数量'];
  1741. } else {
  1742. $result[$size] = $item['数量'];
  1743. }
  1744. // 累加总数量
  1745. $totalQuantity += $item['数量'];
  1746. }
  1747. foreach ($result as $size => $quantity) {
  1748. if ($size !== '订单编号' && $size !== '数量') {
  1749. $result[$size] = '合计(' . $quantity . ')';
  1750. }
  1751. }
  1752. $result['订单编号'] = $orderNumber;
  1753. $result['数量'] = '合计(' . $totalQuantity . ')';
  1754. $sizeData = getSizeSummary($ddorder[0]);
  1755. // 初始化 $sizeData['records'],以防万一它不存在
  1756. if (!isset($sizeData['records'])) {
  1757. $sizeData['records'] = [];
  1758. }
  1759. // 将 $result 插入到 $sizeData['records'] 的最前面
  1760. array_unshift($sizeData['records'], $result);
  1761. $this->success('请求成功', [
  1762. 'result' => $results,//小票数据
  1763. 'headers' => $sizeData['headers'], // 尺码表头
  1764. 'records' => $sizeData['records'] // 查询历史记录
  1765. ]);
  1766. }else{
  1767. // 查询历史记录
  1768. $res = \db('设备_产量计酬')->alias('c')
  1769. ->field('
  1770. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.s_num as 数量, c.sczl_jtbh,
  1771. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1772. j.款式,
  1773. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1774. ')
  1775. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1776. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1777. ->where('c.订单编号', 'like', '%' . explode('DC',$order)[1] . '%')
  1778. ->where('c.工序名称', $params['code'])
  1779. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1780. ->whereNull('c.mod_rq') // 查询未删除数据
  1781. ->order('UniqId desc')
  1782. ->select();
  1783. // 初始化返回数据的结构
  1784. $reslist = [];
  1785. $sizes = [];
  1786. // 遍历查询结果,构造需要的结构化数据
  1787. foreach ($res as $record) {
  1788. $size = $record['尺码']; // 记录的尺码
  1789. $quantity = $record['数量']; // 每个记录的数量
  1790. $serial = $record['serial']; // 记录的serial
  1791. // 记录所有不同的尺码,避免重复
  1792. if (!in_array($size, $sizes)) {
  1793. $sizes[] = $size;
  1794. }
  1795. // 构造每条记录的详细信息
  1796. $reslist[] = [
  1797. 'UniqId' => $record['UniqId'],
  1798. '订单编号' => $record['订单编号'],
  1799. '子订单编号' => $record['子订单编号'],
  1800. '上报数量' => $record['s_num'],
  1801. '剩余数量' => $record['ci_num'],
  1802. '次品数量' => $record['数量'] - $record['ci_num'],
  1803. '款号' => $record['款号'],
  1804. '尺码' => $record['尺码'],
  1805. '工序编号' => $record['工序编号'],
  1806. '工序名称' => $record['工序名称'],
  1807. '数量' => $quantity,
  1808. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1809. '颜色' => $record['颜色备注'],
  1810. '组别' => $record['sczl_jtbh'],
  1811. '款式' => $record['款式'],
  1812. 'serial' => '第('.$serial.')包',
  1813. 'sys_rq' => $record['sys_rq']
  1814. ];
  1815. }
  1816. // 按 sys_rq (时间) 或 UniqId 排序(按实际需求选)
  1817. usort($reslist, function ($a, $b) {
  1818. return strtotime($b['sys_rq']) - strtotime($a['sys_rq']);
  1819. });
  1820. // 返回结构化的查询结果
  1821. $this->success('请求成功', [
  1822. 'result' => $res, // 原始查询结果
  1823. 'headers' => $sizes, // 尺码表头
  1824. 'records' => $reslist // 处理后的记录列表
  1825. ]);
  1826. }
  1827. break;
  1828. default:
  1829. $this->error('无效的 code 参数');
  1830. break;
  1831. }
  1832. }
  1833. /**
  1834. * 出库报工日志
  1835. * 确认出库 恢复出库状态
  1836. */
  1837. public function getTab() {
  1838. if (!$this->request->isPost()) {
  1839. $this->error('非法请求');
  1840. }
  1841. $params = $this->request->param();
  1842. // 校验子订单编号和出库日期
  1843. if (empty($params['order_id'])) {
  1844. $this->error('子订单编号不能为空');
  1845. }
  1846. // 处理出库逻辑
  1847. if ($params['code'] == '出库') {
  1848. if (strpos($params['order_id'], '-') !== false) {
  1849. // 查询传入的子订单编号的记录,获取其颜色备注
  1850. $cklist = \db('工单_印件资料')
  1851. ->field('子订单编号, ck_rq, 颜色备注')
  1852. ->where('子订单编号', $params['order_id'])
  1853. ->find();
  1854. if ($cklist) {
  1855. if (!empty($cklist['ck_rq'])) {
  1856. $this->error('该子订单已完成出库操作,请勿重复操作。');
  1857. } else {
  1858. // 获取相同颜色备注的所有子订单记录
  1859. $sameColorList = \db('工单_印件资料')
  1860. ->field('子订单编号, ck_rq')
  1861. ->where('颜色备注', $cklist['颜色备注'])
  1862. ->whereNull('ck_rq') // 只选择未出库的记录
  1863. ->select();
  1864. // 开始出库操作
  1865. $updateCount1 = 0;
  1866. foreach ($sameColorList as $item) {
  1867. // 更新 '工单_印件资料' 表中所有相同颜色备注的子订单的出库日期
  1868. $result1 = \db('工单_印件资料')
  1869. ->where('子订单编号', $item['子订单编号'])
  1870. ->update(['ck_rq' => date("Y-m-d H:i:s")]);
  1871. if ($result1 !== false) {
  1872. $updateCount1 += $result1;
  1873. }
  1874. }
  1875. // 检查 '工单_基本资料' 表的 '出库日期' 是否为空
  1876. $order = explode('-', $params['order_id'])[0];
  1877. $basicInfo = \db('工单_基本资料')
  1878. ->field('出库日期')
  1879. ->where('订单编号', $order)
  1880. ->find();
  1881. $updateCount2 = 0;
  1882. if (empty($basicInfo['出库日期'])) {
  1883. // 如果 '出库日期' 为空,则更新
  1884. $jbzl = \db('工单_基本资料')
  1885. ->where('订单编号', $order)
  1886. ->fetchSql(true)
  1887. ->update(['出库日期' => date("Y-m-d H:i:s")]);
  1888. $result2 = \db()->query($jbzl);
  1889. if ($result2 !== false) {
  1890. $updateCount2 = $result2;
  1891. }
  1892. }
  1893. // if ($updateCount1 > 0) {
  1894. // // 插入日志
  1895. // foreach ($sameColorList as $item) {
  1896. // $addlist = [
  1897. // 'order_id' => $item['子订单编号'],
  1898. // 'rq' => date("Y-m-d H:i:s"),
  1899. // 'name' => $params['code'],
  1900. // 'sys_id' => $params['sys_id']
  1901. // ];
  1902. // \db('设备_报工日志')->insertGetId($addlist);
  1903. // }
  1904. // $this->success("出库成功");
  1905. // } else {
  1906. // $this->error('出库失败');
  1907. // }
  1908. }
  1909. } else {
  1910. $this->error('未找到对应的出库订单');
  1911. }
  1912. } else {
  1913. $this->error('请扫描子订单编号进行出库');
  1914. }
  1915. }
  1916. // 处理恢复逻辑
  1917. else if ($params['code'] == '恢复') {
  1918. $cklist = \db('工单_印件资料')
  1919. ->field('子订单编号, ck_rq')
  1920. ->where('子订单编号', $params['order_id'])
  1921. ->find();
  1922. if (!empty($cklist['ck_rq'])) {
  1923. $result1 = \db('工单_印件资料')
  1924. ->where('子订单编号', $params['order_id'])
  1925. ->fetchSql(true)
  1926. ->update(['ck_rq' => null]);
  1927. $updateCount1 = \db()->query($result1);
  1928. // 更新 '工单_基本资料' 表
  1929. $order = explode('-', $params['order_id'])[0];
  1930. $result2 = \db('工单_基本资料')
  1931. ->where('订单编号', $order)
  1932. ->fetchSql(true)
  1933. ->update(['出库日期' => null,'gd_statu'=>'1-计划中']);
  1934. $updateCount2 = \db()->query($result2);
  1935. // if ($updateCount1 > 0 && $updateCount2 > 0) {
  1936. // // 插入日志
  1937. // $addlist = [
  1938. // 'order_id' => $params['order_id'],
  1939. // 'rq' => date("Y-m-d H:i:s"),
  1940. // 'name' => $params['code'],
  1941. // 'sys_id' => $params['sys_id']
  1942. // ];
  1943. // \db('设备_报工日志')->insertGetId($addlist);
  1944. // $this->success($params['order_id'] . "恢复未出库");
  1945. // } else {
  1946. // $this->error($params['order_id'] . '该子订单是未出库状态,无需再次恢复');
  1947. // }
  1948. } else {
  1949. $this->error('该子订单是未出库状态,无需再次恢复');
  1950. }
  1951. }
  1952. }
  1953. /**
  1954. * 裁剪完工报工修改工单_印件资料表一条数据
  1955. *
  1956. */
  1957. public function getInfo(){
  1958. if (!$this->request->isPost()) {
  1959. $this->error('非法请求');
  1960. }
  1961. $params = $this->request->param();
  1962. // 检查子订单编号和日期是否都存在
  1963. if (!isset($params['order_id']) || empty($params['order_id'])) {
  1964. $this->error('参数错误,子订单编号不能为空');
  1965. }
  1966. if (!isset($params['rq']) || empty($params['rq'])) {
  1967. $this->error('参数错误,日期不能为空');
  1968. }
  1969. function setIfNotEmpty($value) {
  1970. return (!empty($value) && $value != 0) ? $value : '';
  1971. }
  1972. // 先查询当前记录的原始数据
  1973. $existingData = \db('工单_印件资料')
  1974. ->where('子订单编号', $params['order_id'])
  1975. // ->where(function($query) {
  1976. // $query->whereNull('Mod_rq')->whereOr('Mod_rq', '');
  1977. // }) // 查询未删除数据
  1978. // ->order('子订单编号 desc')
  1979. ->find();
  1980. if ($existingData) {
  1981. // 计算新的累加值
  1982. $updateData = [
  1983. 'sc_rq' => date('Y-m-d H:i:s'),//生产报工日期
  1984. 'scsl1' => isset($params['scsl1']) && is_numeric($params['scsl1']) ? $existingData['scsl1'] + $params['scsl1'] : $existingData['scsl1'] + 0,
  1985. 'scsl2' => isset($params['scsl2']) && is_numeric($params['scsl2']) ? $existingData['scsl2'] + $params['scsl2'] : $existingData['scsl2'] + 0,
  1986. 'scsl3' => isset($params['scsl3']) && is_numeric($params['scsl3']) ? $existingData['scsl3'] + $params['scsl3'] : $existingData['scsl3'] + 0,
  1987. 'scsl4' => isset($params['scsl4']) && is_numeric($params['scsl4']) ? $existingData['scsl4'] + $params['scsl4'] : $existingData['scsl4'] + 0,
  1988. 'scsl5' => isset($params['scsl5']) && is_numeric($params['scsl5']) ? $existingData['scsl5'] + $params['scsl5'] : $existingData['scsl5'] + 0,
  1989. 'scsl6' => isset($params['scsl6']) && is_numeric($params['scsl6']) ? $existingData['scsl6'] + $params['scsl6'] : $existingData['scsl6'] + 0,
  1990. 'scsl7' => isset($params['scsl7']) && is_numeric($params['scsl7']) ? $existingData['scsl7'] + $params['scsl7'] : $existingData['scsl7'] + 0,
  1991. 'scsl8' => isset($params['scsl8']) && is_numeric($params['scsl8']) ? $existingData['scsl8'] + $params['scsl8'] : $existingData['scsl8'] + 0,
  1992. 'scsl9' => isset($params['scsl9']) && is_numeric($params['scsl9']) ? $existingData['scsl9'] + $params['scsl9'] : $existingData['scsl9'] + 0,
  1993. 'scsl10' => isset($params['scsl10']) && is_numeric($params['scsl10']) ? $existingData['scsl10'] + $params['scsl10'] : $existingData['scsl10'] + 0,
  1994. 'sctotal' => isset($params['sctotal']) && is_numeric($params['sctotal']) ? $existingData['sctotal'] + $params['sctotal'] : $existingData['sctotal'] + 0,
  1995. // 你可以在这里继续添加其他字段
  1996. ];
  1997. $updateCount = \db('工单_印件资料')
  1998. ->where('子订单编号', $params['order_id'])
  1999. ->fetchSql(true)
  2000. // ->where(function($query) {
  2001. // $query->whereNull('Mod_rq')->whereOr('Mod_rq', '');
  2002. // }) // 查询未删除数据
  2003. ->update($updateData);
  2004. $list_gd = \db()->query($updateCount);
  2005. } else {
  2006. echo "没有找到相关记录";
  2007. }
  2008. // 检查更新结果
  2009. if ($list_gd > 0) {
  2010. $this->success("更新成功");
  2011. } else {
  2012. // 查询数据库,是否能找到对应的子订单编号
  2013. $existingRecord = \db('工单_印件资料')
  2014. ->where('子订单编号', $params['order_id'])
  2015. // ->where('UniqId', $params['UniqId'])
  2016. ->find();
  2017. if ($existingRecord) {
  2018. $this->error('更新失败,可能是提供的更新数据与现有数据相同,未进行任何更改');
  2019. } else {
  2020. $this->error('更新失败,未找到对应的子订单编号');
  2021. }
  2022. }
  2023. }
  2024. /**
  2025. * 手工、大烫、总检、包装完工报工【通用的报工操作函数】
  2026. */
  2027. function reportWork($params) {
  2028. // 查询设备_产量计酬表的数据
  2029. $bglist = \db('设备_产量计酬')->field('订单编号,子订单编号,数量,ci_num,款号,sys_rq,serial,serial_num')
  2030. ->where('UniqId', $params['UniqId'])->fetchSql(true)->find();
  2031. $list_gd = \db()->query($bglist);
  2032. // 查询工单基本资料表的数据
  2033. $orderlist = \db('工单_基本资料')->where('订单编号', $list_gd[0]['订单编号'])->find();
  2034. // 检查code是否为 "包装"
  2035. if ($params['code'] == '包装') {
  2036. // 如果是 "包装",则增加工单基本资料表中的入仓数量
  2037. $addorder['工单入仓数量'] = $orderlist['工单入仓数量'] + $params['sl'];
  2038. $result = \db('工单_基本资料')->where('订单编号', $list_gd[0]['订单编号'])->fetchSql(true)->update($addorder);
  2039. $sql = \db()->query($result);
  2040. }
  2041. // 构建单条记录的函数,传递 $list_gd 作为参数
  2042. function buildRecords($item, $list_gd) {
  2043. return [
  2044. '订单编号' => $item['order'],
  2045. '子订单编号' => $item['order_id'],
  2046. '款号' => $list_gd[0]['款号'],
  2047. 'serial' => $list_gd[0]['serial'],
  2048. 'serial_num' => $list_gd[0]['serial_num'],
  2049. '工序编号' => $item['gx'],
  2050. '工序名称' => $item['gxmc'],
  2051. '尺码' => $item['cm'],
  2052. '数量' => $item['sl'],
  2053. 'sczl_jtbh' => $item['sczl_jtbh'],
  2054. 'sczl_bh' => $item['sczl_bh'],
  2055. '尾包' => $item['wb'],
  2056. 'sys_id' => $item['sys_id'],
  2057. 'sys_rq' => date('Y-m-d H:i:s'),
  2058. 'sczl_rq' => date('Y-m-d'),
  2059. 'ci_num' => $item['ci_num'] - $item['s_num'],
  2060. 's_num' => $item['s_num'],
  2061. ];
  2062. }
  2063. // 检查是否是索引数组(多条记录)
  2064. if (isset($params[0]) && is_array($params[0])) {
  2065. foreach ($params as $item) {
  2066. $list[] = buildRecords($item, $list_gd);
  2067. }
  2068. } else {
  2069. // 处理单条记录
  2070. $list[] = buildRecords($params, $list_gd);
  2071. }
  2072. // 插入数据到设备_产量计酬表
  2073. $result = \db('设备_产量计酬')->insertAll($list);
  2074. if ($result) {
  2075. $this->success('数据插入成功 : ' . date('H:i:s'));
  2076. } else {
  2077. $this->error('数据插入失败');
  2078. }
  2079. }
  2080. /**
  2081. * 裁剪新增、车缝新增、手工新增、大烫新增、总检新增、包装新增
  2082. * /work_order_spot_check/search
  2083. */
  2084. public function search(){
  2085. if (!$this->request->isPost()) {
  2086. $this->error('非法请求');
  2087. }
  2088. $params = $this->request->param();
  2089. if($params['order_id'] == ''){
  2090. $this->error('请扫描订单后再报工');
  2091. }
  2092. // 根据不同的 code 值执行不同的逻辑
  2093. switch ($params['code']) {
  2094. case '裁剪':
  2095. $order_id = explode('-', $params['order_id']);
  2096. $order = $order_id[0];
  2097. //先查询这条订单是否存在
  2098. $sql = \db('工单_基本资料')->where('订单编号', $order)->fetchSql(true)->find();
  2099. $order_list = \db()->query($sql);
  2100. if (empty($order_list)) {
  2101. $this->error('没有找到对应的订单信息');
  2102. return;
  2103. }
  2104. //将前端传过来的数据存到数据库
  2105. $insertData = [];
  2106. $dataList = isset($params[0]) && is_array($params[0]) ? $params : [$params];
  2107. foreach ($dataList as $item) {
  2108. $list = [
  2109. '订单编号' => $order_list[0]['订单编号'],
  2110. '款号' => $order_list[0]['生产款号'],
  2111. '子订单编号' => $item['order_id'],
  2112. '工序编号' => '2',
  2113. '工序名称' => '裁剪',
  2114. '尺码' => $item['cm'],
  2115. '数量' => $item['sl'],
  2116. 'sczl_jtbh' => $item['sczl_jtbh'],
  2117. 'sys_id' => $item['sys_id'],
  2118. 'sczl_bh' => $item['sczl_bh'],
  2119. 'sczl_rq' => date('Y-m-d 00:00:00'),
  2120. 'sys_rq' => date('Y-m-d H:i:s')
  2121. ];
  2122. $insertData[] = $list;
  2123. }
  2124. $result = \db('设备_产量计酬')->insertAll($insertData);
  2125. $result ? $this->success('数据批量插入成功 : ' . date('H:i:s')) : $this->error('数据插入失败');
  2126. break;
  2127. case '车缝':
  2128. $list = [];
  2129. $order_id = explode('-', $params['order_id']);
  2130. $order = $order_id[0];
  2131. $sql = \db('工单_基本资料')->where('订单编号', $order)->fetchSql(true)->find();
  2132. $list_gd = \db()->query($sql); // 查询的结果可能是多条记录,确保是数组
  2133. // 查询子订单编号对应的最大 serial 值
  2134. $list_order_id = \db('设备_产量计酬')
  2135. ->field('子订单编号, MAX(serial) as serial')
  2136. ->where('子订单编号', $params['order_id'])
  2137. ->where(function($query) {
  2138. $query->whereNull('mod_rq')->whereOr('mod_rq', '');
  2139. }) // 查询未删除数据
  2140. ->group('子订单编号')
  2141. ->find();
  2142. function buildRecord($item, $list_gd, $list_order_id) {
  2143. return [
  2144. '订单编号' => $list_gd[0]['订单编号'],
  2145. '款号' => $list_gd[0]['生产款号'],
  2146. '子订单编号' => $item['order_id'],
  2147. '工序编号' => 3,
  2148. '工序名称' => '车缝',
  2149. 'serial' => $item['serial'],
  2150. 'ci_num' => (int)$item['sl'],
  2151. 'serial_num' => $item['serial_num'],
  2152. '尺码' => $item['cm'],
  2153. '数量' => (int)$item['sl'],
  2154. 'sczl_jtbh' => $item['sczl_jtbh'],
  2155. 'sys_rq' => date('Y-m-d H:i:s'),
  2156. 'sczl_rq' => date('Y-m-d'),
  2157. '尾包' => $item['wb'],
  2158. 'sys_id' => $item['sys_id'],
  2159. 'sczl_bh' => $item['sczl_bh']
  2160. ];
  2161. }
  2162. if (isset($params[0]) && is_array($params[0])) {
  2163. foreach ($params as $item) {
  2164. $list[] = buildRecord($item, $list_gd, $list_order_id); // 所有记录使用相同的 serial
  2165. }
  2166. } else {
  2167. // 处理单条记录
  2168. $list[] = buildRecord($params, $list_gd, $list_order_id); // 单条记录
  2169. }
  2170. $result = \db('设备_产量计酬')->insertAll($list);
  2171. if ($result) {
  2172. $this->success('数据插入成功 : ' . date('H:i:s'));
  2173. } else {
  2174. $this->error('数据插入失败');
  2175. }
  2176. break;
  2177. case '手工':
  2178. case '大烫':
  2179. case '总检':
  2180. case '包装':
  2181. //调用reportWork方法名函数
  2182. $this->reportWork($params);
  2183. break;
  2184. default:
  2185. $this->error('无效的 code 参数');
  2186. break;
  2187. }
  2188. }
  2189. /**
  2190. * 物理地址获取机台编号
  2191. * /work_order_spot_check/getMachineMac
  2192. */
  2193. public function getMachineMac(){
  2194. if ($this->request->isGet() == false){$this->error('非法请求');}
  2195. $params = $this->request->get();
  2196. if (empty($params['sys_sbID'])){$this->error('物理地址参数不能为空');}
  2197. $data = \db('设备_基本资料')->where('sys_sbID', $params['sys_sbID'])
  2198. ->field('设备编号 as 机台号,生产工序,工序,rtrim(设备编组) as 组别,组长,sys_sbID')
  2199. // ->where('生产工序',$params['code'])
  2200. ->find();
  2201. $this->success('请求成功', $data);
  2202. }
  2203. /**
  2204. * 删除报工记录
  2205. * /work_order_spot_check/del
  2206. */
  2207. // public function del() {
  2208. // if (!$this->request->isGet()) {
  2209. // return $this->error('请求错误');
  2210. // }
  2211. //
  2212. // $param = $this->request->param();
  2213. // if (empty($param['UniqId'])) {
  2214. // return $this->error('UniqId参数错误');
  2215. // }
  2216. //
  2217. // // 将 $param['UniqId'] 的值按逗号分割成数组
  2218. // $printIdArray = explode(',', $param['UniqId']);
  2219. //
  2220. // // 如果是裁剪工序
  2221. // if (isset($param['sys_id']) && strpos($param['sys_id'], '裁剪') !== false) {
  2222. // // 初始化失败计数器
  2223. // $failureCount = 0;
  2224. //
  2225. // // 开启事务
  2226. // \db()->startTrans();
  2227. // try {
  2228. // foreach ($printIdArray as $uniqId) {
  2229. // // 根据 UniqId 查询 设备_产量计酬 表中的订单编号、尺码和数量
  2230. // $result = \db('设备_产量计酬')->where('UniqId', $uniqId)
  2231. // ->field('子订单编号,订单编号,尺码,数量,sczl_bh')
  2232. // ->find();
  2233. //
  2234. // // 如果未查询到数据,跳过
  2235. // if (!$result) {
  2236. // $failureCount++;
  2237. // continue;
  2238. // }
  2239. //
  2240. // // 根据订单编号查询工单_印件资料表,并排除有修改日期的记录
  2241. // $yresult = \db('工单_印件资料')->where('子订单编号', $result['子订单编号'])
  2242. // ->whereNull('Mod_rq')
  2243. // ->find();
  2244. //
  2245. // // 处理 yresult 中的尺码
  2246. // $size = $result['尺码'];
  2247. // $quantity = $result['数量'];
  2248. // $updateData = [];
  2249. // for ($i = 1; $i <= 10; $i++) {
  2250. // $cmField = 'cm' . $i;
  2251. // $scslField = 'scsl' . $i;
  2252. // if (isset($yresult[$cmField]) && $yresult[$cmField] === $size) {
  2253. // if (isset($yresult[$scslField])) {
  2254. // $yresult[$scslField] -= $quantity;
  2255. // if ($yresult[$scslField] < 0) {
  2256. // $yresult[$scslField] = 0;
  2257. // }
  2258. // $updateData[$scslField] = $yresult[$scslField];
  2259. // }
  2260. // break;
  2261. // }
  2262. // }
  2263. //
  2264. // // 更新工单_印件资料表
  2265. // $newTotal = 0;
  2266. // for ($i = 1; $i <= 10; $i++) {
  2267. // $scslField = 'scsl' . $i;
  2268. // if (isset($yresult[$scslField])) {
  2269. // $newTotal += $yresult[$scslField];
  2270. // }
  2271. // }
  2272. //
  2273. // \db('工单_印件资料')
  2274. // ->where('子订单编号', $result['子订单编号'])
  2275. // ->update(['sctotal' => $newTotal] + $updateData);
  2276. //
  2277. // // 更新设备_产量计酬表
  2278. // $updateData = [
  2279. // 'mod_rq' => date('Y-m-d H:i:s'),
  2280. // 'delsys_id' => $param['sys_id']
  2281. // ];
  2282. // $updateResult = \db('设备_产量计酬')->where('UniqId', $uniqId)->update($updateData);
  2283. //
  2284. // // 如果更新失败,记录失败
  2285. // if ($updateResult === false) {
  2286. // $failureCount++;
  2287. // }
  2288. // }
  2289. //
  2290. // // 提交事务
  2291. // \db()->commit();
  2292. //
  2293. // } catch (\Exception $e) {
  2294. // // 发生异常时回滚
  2295. // \db()->rollback();
  2296. // return $this->error('操作失败:' . $e->getMessage());
  2297. // }
  2298. //
  2299. // // 返回操作结果
  2300. // if ($failureCount === 0) {
  2301. // return $this->success('删除成功');
  2302. // } else {
  2303. // return $this->error("操作完成,但有 {$failureCount} 条记录更新失败");
  2304. // }
  2305. // } else {
  2306. //
  2307. // // 处理其他情况,跟上面的逻辑类似
  2308. // }
  2309. // }
  2310. public function del() {
  2311. // 判断请求方式是否为 GET
  2312. if (!$this->request->isGet()) {
  2313. return $this->error('请求错误');
  2314. }
  2315. // 获取请求参数
  2316. $param = $this->request->param();
  2317. // 检查 UniqId 参数是否存在
  2318. if (empty($param['UniqId'])) {
  2319. return $this->error('UniqId 参数错误');
  2320. }
  2321. // 将 UniqId 的值按逗号分割成数组
  2322. $printIdArray = explode(',', $param['UniqId']);
  2323. // 初始化失败计数器
  2324. $failureCount = 0;
  2325. // 开启事务
  2326. \db()->startTrans();
  2327. try {
  2328. foreach ($printIdArray as $uniqId) {
  2329. // 根据 UniqId 查询设备_产量计酬表中的数据
  2330. $result = \db('设备_产量计酬')->where('UniqId', $uniqId)
  2331. ->field('子订单编号, 订单编号, 尺码, 数量, sczl_bh, 工序名称')
  2332. ->find();
  2333. // 如果查询不到数据,跳过当前处理
  2334. if (!$result) {
  2335. $failureCount++;
  2336. continue;
  2337. }
  2338. // 判断工序名称是否是“裁剪”
  2339. if (isset($param['sys_id']) && strpos($param['sys_id'], '裁剪') !== false) {
  2340. // 如果工序名称包含“裁剪”,更新工单_印件资料表
  2341. $yresult = \db('工单_印件资料')->where('子订单编号', $result['子订单编号'])
  2342. ->whereNull('Mod_rq') // 仅更新没有修改日期的记录
  2343. ->find();
  2344. // 如果没有查询到工单印件资料表的数据,跳过
  2345. if (!$yresult) {
  2346. $failureCount++;
  2347. continue;
  2348. }
  2349. // 处理尺码和数量更新
  2350. $size = $result['尺码'];
  2351. $quantity = $result['数量'];
  2352. $updateData = [];
  2353. // 遍历工单中的尺码字段(cm1 到 cm10),找到匹配的尺码
  2354. for ($i = 1; $i <= 10; $i++) {
  2355. $cmField = 'cm' . $i;
  2356. $scslField = 'scsl' . $i;
  2357. if (isset($yresult[$cmField]) && $yresult[$cmField] === $size) {
  2358. // 找到匹配的尺码后,更新对应的数量字段
  2359. if (isset($yresult[$scslField])) {
  2360. $yresult[$scslField] -= $quantity;
  2361. if ($yresult[$scslField] < 0) {
  2362. $yresult[$scslField] = 0; // 防止数量为负数
  2363. }
  2364. $updateData[$scslField] = $yresult[$scslField];
  2365. }
  2366. break;
  2367. }
  2368. }
  2369. // 计算更新后的总数量
  2370. $newTotal = 0;
  2371. for ($i = 1; $i <= 10; $i++) {
  2372. $scslField = 'scsl' . $i;
  2373. if (isset($yresult[$scslField])) {
  2374. $newTotal += $yresult[$scslField];
  2375. }
  2376. }
  2377. // 更新工单_印件资料表
  2378. \db('工单_印件资料')
  2379. ->where('子订单编号', $result['子订单编号'])
  2380. ->update(['sctotal' => $newTotal] + $updateData);
  2381. }
  2382. // 更新设备_产量计酬表
  2383. $updateData = [
  2384. 'mod_rq' => date('Y-m-d H:i:s'),
  2385. 'delsys_id' => $param['sys_id']
  2386. ];
  2387. // 执行更新操作
  2388. $updateResult = \db('设备_产量计酬')->where('UniqId', $uniqId)->update($updateData);
  2389. // 如果更新失败,记录失败
  2390. if ($updateResult === false) {
  2391. $failureCount++;
  2392. }
  2393. }
  2394. // 提交事务
  2395. \db()->commit();
  2396. } catch (\Exception $e) {
  2397. // 发生异常时回滚事务
  2398. \db()->rollback();
  2399. return $this->error('操作失败:' . $e->getMessage());
  2400. }
  2401. // 返回操作结果
  2402. if ($failureCount === 0) {
  2403. return $this->success('删除成功');
  2404. } else {
  2405. return $this->error("操作完成,但有 {$failureCount} 条记录更新失败");
  2406. }
  2407. }
  2408. /**
  2409. * 出库报工
  2410. * @return void
  2411. * @throws \think\db\exception\DataNotFoundException
  2412. * @throws \think\db\exception\ModelNotFoundException
  2413. * @throws \think\exception\DbException
  2414. */
  2415. public function outReport()
  2416. {
  2417. if ($this->request->isPost() === false){
  2418. $this->error('请求错误');
  2419. }
  2420. $param = Request::instance()->post();
  2421. if (empty($param)){
  2422. $this->error('参数错误');
  2423. }
  2424. foreach ($param as $item){
  2425. //查询库存表中的数量
  2426. $Inventory = \db('物料_库存')
  2427. ->where('批次号',$item['批次号'])
  2428. ->field('库存数量,领用数量')
  2429. ->find();
  2430. //构建物料库存数据
  2431. $InventoryData = [
  2432. '库存数量' => $Inventory['库存数量'] - (int)$item['number'],
  2433. '领用数量' => $Inventory['领用数量'] + (int)$item['number']
  2434. ];
  2435. $ReportData = $item;
  2436. unset($ReportData['关联编号']);
  2437. $ReportData['批次号'] = $item['批次号'];
  2438. $ReportData['rq'] = date('Y-m-d H:i:s',time());
  2439. $ReportData['库存数量'] = $InventoryData['库存数量'];
  2440. //创建事务,存储库存表数据和入库报工表数据
  2441. Db::startTrans();
  2442. try {
  2443. //存储库存表数据
  2444. $InventorySql = \db('物料_库存')->where('批次号',$item['批次号'])->fetchSql(true)->update($InventoryData);
  2445. \db()->query($InventorySql);
  2446. //存储入库报工表数据
  2447. $ReportSql = \db('设备_报工日志')->fetchSql(true)->insert($ReportData);
  2448. \db()->query($ReportSql);
  2449. //提交事务
  2450. Db::commit();
  2451. }catch (\Exception $e){
  2452. Db::rollback();
  2453. }
  2454. }
  2455. $this->success('成功');
  2456. }
  2457. /**
  2458. * 入库订单信息模糊查询
  2459. * @return void
  2460. * @throws \think\db\exception\DataNotFoundException
  2461. * @throws \think\db\exception\ModelNotFoundException
  2462. * @throws \think\exception\DbException
  2463. */
  2464. public function InputOrderSearch()
  2465. {
  2466. if ($this->request->isGet() === false){
  2467. $this->error('请求错误');
  2468. }
  2469. $param = $this->request->param();
  2470. if (empty($param)){
  2471. $this->error('参数错误');
  2472. }
  2473. $list = \db('工单_基本资料')
  2474. ->alias('a')
  2475. ->join('工单_面料资料 b','a.订单编号 = b.BOM_工单编号')
  2476. ->join('工单关联表 c','a.订单编号 = c.订单编号 AND b.BOM_物料编码 = c.物料编号')
  2477. ->where('a.订单编号|a.生产款号','like','%'.$param['search'].'%')
  2478. ->where('a.Mod_rq',null)
  2479. ->where('b.Mod_rq',null)
  2480. ->field('a.订单编号,a.生产款号,a.客户编号,a.款式,a.订单数量,a.落货日期,a.接单日期,a.Sys_id as 创建人员,
  2481. a.Sys_rq as 创建时间,b.BOM_颜色 as 颜色,b.BOM_物料编码 as 物料编码,b.BOM_物料名称 as 物料名称,GROUP_CONCAT(DISTINCT c.订单编号 SEPARATOR ",") AS 关联订单,c.关联编号')
  2482. ->group('物料编码')
  2483. ->order('a.订单编号 desc')
  2484. ->select();
  2485. if (empty($list)){
  2486. $this->success('未找到相关订单信息');
  2487. }else{
  2488. $this->success('成功',$list);
  2489. }
  2490. }
  2491. /**
  2492. * 面料入库报工
  2493. * @return void
  2494. * @throws \think\db\exception\DataNotFoundException
  2495. * @throws \think\db\exception\ModelNotFoundException
  2496. * @throws \think\exception\DbException
  2497. */
  2498. public function inputStash()
  2499. {
  2500. if ($this->request->isPost() === false){
  2501. $this->error('请求错误');
  2502. }
  2503. $param = Request::instance()->post();
  2504. if (empty($param)){
  2505. $this->error('参数错误');
  2506. }
  2507. foreach ($param as $item){
  2508. //查询批次号是否存在
  2509. $BatchNumber = \db('物料_库存')
  2510. ->where('物料编号',$item['物料编码'])
  2511. ->where('关联号',$item['关联编号'])
  2512. ->column('批次号');
  2513. if (empty($BatchNumber)){
  2514. $LastNumber = \db('物料_库存')
  2515. ->order('批次号 desc')
  2516. ->value('批次号');
  2517. if (!empty($LastNumber)){
  2518. $BatchNumber[0] = 'PCH'.((int)substr($LastNumber,3) + 1);
  2519. }else{
  2520. $BatchNumber[0] = 'PCH1';
  2521. }
  2522. //构建库存表数据
  2523. $InventoryData = [
  2524. '批次号' => $BatchNumber[0],
  2525. '关联号' => $item['关联编号'],
  2526. '物料编号' => $item['物料编码'],
  2527. '物料名称' => $item['物料名称'],
  2528. '入仓总量' => $item['number'],
  2529. '库存数量' => $item['number'],
  2530. '单位' => '米',
  2531. '实际门幅' => $item['实际门幅'],
  2532. '状态' => 1,
  2533. 'sys_id' => $item['sys_id'],
  2534. 'sys_rq' => date('Y-m-d H:i:s',time())
  2535. ];
  2536. $ReportData = $item;
  2537. unset($ReportData['关联编号']);
  2538. unset($ReportData['实际门幅']);
  2539. $ReportData['批次号'] = $BatchNumber[0];
  2540. $ReportData['rq'] = date('Y-m-d H:i:s',time());
  2541. $ReportData['库存数量'] = $InventoryData['库存数量'];
  2542. //创建事务,存储库存表数据和入库报工表数据
  2543. db()->startTrans();
  2544. try {
  2545. //存储库存表数据
  2546. $InventorySql = \db('物料_库存')->fetchSql(true)->insert($InventoryData);
  2547. \db()->query($InventorySql);
  2548. //存储入库报工表数据
  2549. $ReportSql = \db('设备_报工日志')->fetchSql(true)->insert($ReportData);
  2550. \db()->query($ReportSql);
  2551. //提交事务
  2552. \db()->commit();
  2553. }catch (\Exception $e){
  2554. \db()->rollback();
  2555. $this->error($e->getMessage());
  2556. }
  2557. }else{
  2558. //查询库存表中的数量
  2559. $Inventory = \db('物料_库存')
  2560. ->where('批次号',$BatchNumber[0])
  2561. ->field('入仓总量,库存数量')
  2562. ->find();
  2563. //构建物料库存数据
  2564. $InventoryData = [
  2565. '实际门幅' => $item['实际门幅'],
  2566. '入仓总量' => $Inventory['入仓总量'] + $item['number'],
  2567. '库存数量' => $Inventory['库存数量'] + $item['number']
  2568. ];
  2569. $ReportData = $item;
  2570. unset($ReportData['关联编号']);
  2571. unset($ReportData['实际门幅']);
  2572. $ReportData['批次号'] = $BatchNumber[0];
  2573. $ReportData['rq'] = date('Y-m-d H:i:s',time());
  2574. $ReportData['库存数量'] = $InventoryData['库存数量'];
  2575. //创建事务,存储库存表数据和入库报工表数据
  2576. \db()->startTrans();
  2577. try {
  2578. //存储库存表数据
  2579. $InventorySql = \db('物料_库存')->where('批次号',$BatchNumber[0])->fetchSql(true)->update($InventoryData);
  2580. \db()->query($InventorySql);
  2581. //存储入库报工表数据
  2582. $ReportSql = \db('设备_报工日志')->fetchSql(true)->insert($ReportData);
  2583. \db()->query($ReportSql);
  2584. //提交事务
  2585. \db()->commit();
  2586. }catch (\Exception $e){
  2587. \db()->rollback();
  2588. }
  2589. }
  2590. }
  2591. $this->success('成功');
  2592. }
  2593. /**
  2594. * 出库订单信息查询
  2595. * @return void
  2596. * @throws \think\db\exception\DataNotFoundException
  2597. * @throws \think\db\exception\ModelNotFoundException
  2598. * @throws \think\exception\DbException
  2599. */
  2600. public function OutOrderSearch()
  2601. {
  2602. if ($this->request->isGet() === false){
  2603. $this->error('请求错误');
  2604. }
  2605. $param = $this->request->param();
  2606. if (empty($param)){
  2607. $this->error('参数错误');
  2608. }
  2609. $list = \db('工单_基本资料')
  2610. ->alias('a')
  2611. ->join('工单_面料资料 b','a.订单编号 = b.BOM_工单编号')
  2612. ->join('物料_库存 c','b.BOM_物料编码 = c.物料编号','left')
  2613. ->join('工单关联表 d','a.订单编号 = d.订单编号 AND d.物料编号 = b.BOM_物料编码')
  2614. ->where('a.订单编号|a.生产款号','like','%'.$param['search'].'%')
  2615. ->where('a.Mod_rq',null)
  2616. ->where('b.Mod_rq',null)
  2617. ->where('c.状态',1)
  2618. ->field('a.订单编号,a.生产款号,a.客户编号,a.款式,a.订单数量,a.落货日期,a.接单日期,a.Sys_id as 创建人员,
  2619. a.Sys_rq as 创建时间,b.BOM_颜色 as 颜色,b.BOM_物料编码 as 物料编码,b.BOM_物料名称 as 物料名称,c.批次号,d.关联编号')
  2620. ->order('a.订单编号 desc')
  2621. ->select();
  2622. if (empty($list)){
  2623. $this->success('未找到相关订单信息');
  2624. }else{
  2625. $this->success('成功',$list);
  2626. }
  2627. }
  2628. /**
  2629. * 获取入库面料信息
  2630. * @return void
  2631. * @throws \think\db\exception\DataNotFoundException
  2632. * @throws \think\db\exception\ModelNotFoundException
  2633. * @throws \think\exception\DbException
  2634. */
  2635. public function getInputDetail()
  2636. {
  2637. if ($this->request->isGet() === false){
  2638. $this->error('请求错误');
  2639. }
  2640. $param = $this->request->param();
  2641. if (empty($param)){
  2642. $this->error('参数错误');
  2643. }
  2644. $list = \db('工单_面料资料')
  2645. ->alias('b')
  2646. ->join('物料_库存 a','a.物料编号 = b.BOM_物料编码 AND a.物料名称 = b.BOM_物料名称','left')
  2647. ->join('工单关联表 c','b.BOM_物料编码 = c.物料编号 AND b.BOM_工单编号 = c.订单编号')
  2648. ->field('a.批次号,c.关联编号,b.BOM_物料编码,b.BOM_物料名称,a.入仓总量,a.库存数量,a.领用数量,a.领用数量,a.单位,a.实际门幅,
  2649. b.BOM_工单编号,b.BOM_颜色,b.BOM_计划门幅,b.BOM_定额门幅,b.BOM_计划用量 as 计划用料,b.BOM_标准用量 as 定额用料')
  2650. ->where('c.关联编号',$param['关联编号'])
  2651. ->where('c.订单编号',$param['订单编号'])
  2652. ->where('b.BOM_物料编码',$param['物料编码'])
  2653. ->find();
  2654. $orderList = \db('工单关联表')
  2655. ->where('关联编号',$param['关联编号'])
  2656. ->column('GROUP_CONCAT(DISTINCT 订单编号 SEPARATOR ",") AS 关联订单');
  2657. $list['关联订单'] = $orderList[0];
  2658. $this->success('成功',$list);
  2659. }
  2660. /**
  2661. * 裁剪面料退货包工
  2662. * @return void
  2663. * @throws \think\db\exception\DataNotFoundException
  2664. * @throws \think\db\exception\ModelNotFoundException
  2665. * @throws \think\exception\DbException
  2666. * @throws \think\exception\PDOException
  2667. */
  2668. public function TuihuoReport()
  2669. {
  2670. if ($this->request->isPost() === false){
  2671. $this->error('请求错误');
  2672. }
  2673. $param = Request::instance()->post();
  2674. if (empty($param)){
  2675. $this->error('参数错误');
  2676. }
  2677. foreach ($param as $item){
  2678. //搜索批次号库存表中中数据
  2679. $number = \db('物料_库存')
  2680. ->where('批次号',$item['批次号'])
  2681. ->where('关联号',$item['关联编号'])
  2682. ->where('物料编号',$item['物料编码'])
  2683. ->field('库存数量,退还数量')
  2684. ->find();
  2685. if ($item['type'] === '退面料'){
  2686. //构建库存表数据
  2687. $InventoryData = [
  2688. '退还数量' => $number['退还数量'] + (int)$item['number'],
  2689. '库存数量' => $number['库存数量'] + (int)$item['number'],
  2690. ];
  2691. }elseif ($item['type'] === '退厂商'){
  2692. $InventoryData = [
  2693. '退还数量' => $number['退还数量'],
  2694. '库存数量' => $number['库存数量'] - (int)$item['number'],
  2695. ];
  2696. }else{
  2697. $InventoryData = [
  2698. '退还数量' => $number['退还数量'],
  2699. '库存数量' => $number['库存数量'],
  2700. ];
  2701. }
  2702. $ReportData = $item;
  2703. unset($ReportData['关联编号']);
  2704. unset($ReportData['实际门幅']);
  2705. $ReportData['批次号'] = $item['批次号'];
  2706. $ReportData['rq'] = date('Y-m-d H:i:s',time());
  2707. $ReportData['库存数量'] = $InventoryData['库存数量'];
  2708. //创建事务,存储库存表数据和入库报工表数据
  2709. db()->startTrans();
  2710. try {
  2711. //存储库存表数据
  2712. $InventorySql = \db('物料_库存')
  2713. ->where('批次号',$item['批次号'])
  2714. ->where('关联号',$item['关联编号'])
  2715. ->where('物料编号',$item['物料编码'])
  2716. ->fetchSql(true)
  2717. ->update($InventoryData);
  2718. \db()->query($InventorySql);
  2719. //存储入库报工表数据
  2720. $ReportSql = \db('设备_报工日志')->fetchSql(true)->insert($ReportData);
  2721. \db()->query($ReportSql);
  2722. //提交事务
  2723. \db()->commit();
  2724. }catch (\Exception $e){
  2725. \db()->rollback();
  2726. $this->error($e->getMessage());
  2727. }
  2728. }
  2729. $this->success('成功');
  2730. }
  2731. }