WorkOrderSpotCheck.php 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\db\Query;
  5. use \think\Request;
  6. use \think\Db;
  7. use const http\Client\Curl\IPRESOLVE_V4;
  8. class WorkOrderSpotCheck extends Api{
  9. protected $noNeedLogin = ['*'];
  10. protected $noNeedRight = ['*'];
  11. /**
  12. * 首页
  13. */
  14. public function index()
  15. {
  16. $this->success('请求成功');
  17. }
  18. /**
  19. * 报工查询
  20. *
  21. */
  22. public function getList() {
  23. // 判断请求是否为 GET 请求
  24. if (!$this->request->isGet()) {
  25. $this->error('请求方式错误');
  26. }
  27. $params = $this->request->param();
  28. // 校验参数是否存在且不为空
  29. $requiredParams = ['order', 'code'];
  30. foreach ($requiredParams as $param) {
  31. if (!isset($params[$param]) || empty($params[$param])) {
  32. $this->error("$param 参数错误");
  33. }
  34. }
  35. // 如果订单编号没有 'DC' 前缀,自动添加 'DC'
  36. if (strpos($params['order'], 'DC') !== 0) {
  37. $params['order'] = 'DC' . $params['order'];
  38. }
  39. $order = $params['order'];
  40. // 根据不同的 code 值执行不同的逻辑
  41. switch ($params['code']) {
  42. case '出库':
  43. $query = \db('工单_基本资料')->alias('j')
  44. ->field('y.订单编号, y.子订单编号, y.颜色, y.款号, j.款式, y.ck_rq,y.颜色备注')
  45. ->distinct('y.子订单编号')
  46. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'inner');
  47. if (strpos($order, '-') !== false) {
  48. $query->where('y.子订单编号', $order);
  49. } else {
  50. $query->where('y.订单编号', 'like', '%' . $order . '%');
  51. }
  52. $query->where(function($query) {
  53. $query->whereNull('y.Mod_rq')
  54. ->whereOr('y.Mod_rq', '');
  55. });
  56. $data = $query->select();
  57. if (isset($data)) {
  58. usort($data, function($a, $b) {
  59. // 判断是否出库
  60. $a_status = !empty($a['ck_rq']) ? 1 : 0;
  61. $b_status = !empty($b['ck_rq']) ? 1 : 0;
  62. if ($a_status == $b_status) {
  63. // 如果出库状态相同,则按 ck_rq 倒序排序
  64. return strcmp($b['ck_rq'], $a['ck_rq']);
  65. } else {
  66. // 先显示未出库(0),再显示已出库(1)
  67. return $a_status - $b_status;
  68. }
  69. });
  70. foreach ($data as &$item) {
  71. $item['是否出库'] = !empty($item['ck_rq']) ? '已出库' : '未出库';
  72. $item['ck_rq'] = substr($item['ck_rq'], 0, 10);
  73. }
  74. $result = [
  75. 'data' => $data,
  76. 'rq' => date("Y-m-d"),
  77. 'total' => count($data)
  78. ];
  79. $this->success('请求成功', $result);
  80. }
  81. break;
  82. case '裁剪':
  83. //订单编号
  84. $ddh = explode('-', $order)[0];
  85. $currentDateTime = date("Y-m-d H:i:s");
  86. // 查询未出库的记录,若存在则进行批量出库和日志记录
  87. $data = \db('工单_印件资料')
  88. ->where(['子订单编号' => $order, 'ck_rq' => null])
  89. ->find();
  90. if ($data) {
  91. // 检查并更新工单_基本资料表的出库日期
  92. $sql = \db('工单_基本资料')
  93. ->where('订单编号', $ddh)
  94. ->whereNull('出库日期')
  95. ->fetchSql(true)
  96. ->update(['出库日期' => $currentDateTime]);
  97. \db()->query($sql);
  98. // 获取相同颜色备注的未出库子订单编号列表
  99. $sameColorList = \db('工单_印件资料')
  100. ->where(['订单编号' => $ddh, 'Mod_rq' => null, 'ck_rq' => null])
  101. ->field('子订单编号')
  102. ->select();
  103. // 更新工单_印件资料表的出库日期
  104. $sql = \db('工单_印件资料')
  105. ->where('订单编号', $ddh)
  106. ->whereNull('Mod_rq')
  107. ->whereNull('ck_rq')
  108. ->fetchSql(true)
  109. ->update(['ck_rq' => $currentDateTime]);
  110. $updateCount1 = \db()->query($sql);
  111. if ($updateCount1 > 0) {
  112. // 批量生成并插入日志数据
  113. $logData = array_map(function ($item) use ($currentDateTime, $params) {
  114. return [
  115. 'order_id' => $item['子订单编号'],
  116. 'rq' => $currentDateTime,
  117. 'name' => $params['code'],
  118. 'sys_id' => '裁剪自动出库'
  119. ];
  120. }, $sameColorList);
  121. \db('设备_报工日志')->insertAll($logData);
  122. } else {
  123. return $this->error('网络异常,重新尝试');
  124. }
  125. }
  126. //查询子订单编号对应数据【条码查询】
  127. if (strpos($order, '-') !== false) {
  128. if (preg_match('/-(\d{2})$/', $order, $matches)) {
  129. $number = (int)$matches[1];
  130. //船样单子
  131. if ($number > 70) {
  132. $data = \db('工单_基本资料')->alias('j')
  133. ->distinct('y.子订单编号')
  134. ->field('
  135. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  136. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  137. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  138. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,
  139. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,
  140. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10'
  141. )
  142. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  143. ->where('y.船样',1)
  144. ->where(function($query) {
  145. $query->whereNull('y.Mod_rq')->whereOr('y.Mod_rq', '');
  146. }) // 查询未删除数据
  147. ->where('y.子订单编号', $order)
  148. ->select();
  149. } else {
  150. //获取原始子订单编号【-01】
  151. $ddbh = explode('-', $order)[0];//获取订单编号
  152. //【查询尺码、制单数、填写报工新增实裁数】
  153. $data = \db('工单_基本资料')->alias('j')
  154. ->distinct('y.子订单编号')
  155. ->field('
  156. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  157. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  158. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  159. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,
  160. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,
  161. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10'
  162. )
  163. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  164. ->where('y.船样',0)
  165. ->where(function($query) {
  166. $query->whereNull('y.Mod_rq')->whereOr('y.Mod_rq', '');
  167. }) // 查询未删除数据
  168. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  169. ->select();
  170. }
  171. }else{
  172. $ddbh = explode('-', $order)[0];//获取订单编号
  173. //【查询尺码、制单数、填写报工新增实裁数】
  174. $data = \db('工单_基本资料')->alias('j')
  175. ->distinct('y.子订单编号')
  176. ->field('
  177. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  178. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  179. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  180. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,
  181. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,
  182. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10'
  183. )
  184. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  185. ->where('y.船样',0)
  186. ->where(function($query) {
  187. $query->whereNull('y.Mod_rq')->whereOr('y.Mod_rq', '');
  188. }) // 查询未删除数据
  189. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  190. ->select();
  191. }
  192. $dataEntry = [];
  193. $list = [];
  194. $headers = [];//存放尺码表头
  195. foreach ($data as $item) {
  196. // 初始化尺码与数量映射数组
  197. $cmScslMapping = [];
  198. for ($i = 1; $i <= 10; $i++) {
  199. $cmValue = $item['cm' . $i];
  200. $scslValue = $item['scsl' . $i];
  201. if (!is_null($cmValue) && $cmValue !== 0) {
  202. $cmScslMapping[$cmValue] = ($scslValue === 0 || is_null($scslValue)) ? '' : $scslValue;
  203. if (!in_array($cmValue, $headers)) {
  204. $headers[] = $cmValue;
  205. }
  206. }
  207. }
  208. // 将尺码数据平铺到主要数据中(表格数据)
  209. $orderData = [
  210. '订单编号' => $item['订单编号'],
  211. '子订单编号' => $item['子订单编号'],
  212. '款号' => $item['款号'],
  213. '款式' => $item['款式'],
  214. '颜色' => $item['颜色备注'],
  215. 'zdtotal' => $item['zdtotal'],
  216. 'sctotal' => $item['sctotal'],
  217. '单位' => $item['单位'],
  218. 'Sys_id' => $item['Sys_id'],
  219. 'Sys_rq' => $item['Sys_rq'],
  220. ];
  221. // 平铺尺码映射数据
  222. $orderData = array_merge($orderData, $cmScslMapping);
  223. $dataEntry[] = $orderData;
  224. // list 列表部分
  225. $entry = [];
  226. for ($i = 1; $i <= 10; $i++) {
  227. $entry['cm' . $i] = ($item['cm' . $i] === 0 || is_null($item['cm' . $i])) ? '' : $item['cm' . $i];
  228. $entry['cmsl' . $i] = ($item['cmsl' . $i] === 0 || is_null($item['cmsl' . $i])) ? '' : $item['cmsl' . $i];
  229. $entry['scsl' . $i] = ($item['scsl' . $i] === 0 || is_null($item['scsl' . $i])) ? '' : $item['scsl' . $i];
  230. }
  231. $entry['订单编号'] = $item['订单编号'];
  232. $entry['子订单编号'] = $item['子订单编号'];
  233. $entry['UniqId'] = $item['UniqId'];
  234. $entry['zdtotal'] = $item['zdtotal'];
  235. $entry['颜色'] = $item['颜色'];
  236. $entry['款式'] = $item['款式'];
  237. $entry['款号'] = $item['款号'];
  238. $entry['color_id'] = $item['color_id'];
  239. $entry['颜色备注'] = $item['颜色备注'];
  240. $list[] = $entry;
  241. }
  242. //打印当前订单所有尺码数据
  243. // echo "<pre>";print_r($list);echo "<pre>";die;
  244. // 合并子订单
  245. $merged = [];
  246. foreach ($list as $item) {
  247. $key = $item['子订单编号'];
  248. // 生成合并的唯一键,以 color_id 和 颜色备注 为依据
  249. $mergeKey = $item['color_id'] . '-' . $item['颜色备注'];
  250. if (!isset($merged[$mergeKey])) {
  251. // 如果不存在,则初始化
  252. $merged[$mergeKey] = $item;
  253. } else {
  254. // 如果已经存在,则合并数量
  255. for ($i = 1; $i <= 10; $i++) {
  256. $sizeKey = 'cm' . $i;
  257. $qtyKey = 'cmsl' . $i;
  258. if (!empty($item[$sizeKey]) && $item[$sizeKey] === $merged[$mergeKey][$sizeKey]) {
  259. $merged[$mergeKey][$qtyKey] = (int)$merged[$mergeKey][$qtyKey] + (int)$item[$qtyKey];
  260. }
  261. }
  262. // 也可以累加其他字段,如总数量等
  263. $merged[$mergeKey]['zdtotal'] += $item['zdtotal'];
  264. }
  265. }
  266. $mergeds = array_values($merged); // 重置索引为连续数字索引
  267. //打印输出合并后订单数据
  268. // echo "<pre>";print_r($mergeds);echo "</pre>";die;
  269. // 在 $mergeds 中查找匹配的子订单编号 对应的一条数据
  270. $matchedOrder = null;
  271. foreach ($mergeds as $mergedOrder) {
  272. if ($mergedOrder['子订单编号'] === $order) {
  273. $matchedOrder = $mergedOrder;
  274. break; // 找到后可以退出循环
  275. }
  276. }
  277. // echo "<pre>";print_r($matchedOrder);echo "</pre>";die;
  278. //产量进度汇总记录
  279. $rows = db()->table('设备_产量计酬')->alias('c')
  280. // ->join('工单_印件资料 y', 'c.订单编号 = y.订单编号', 'left')
  281. ->field('c.款号, c.订单编号, c.子订单编号, c.sys_rq as 上报时间, c.尺码, c.数量')
  282. ->where([
  283. 'c.子订单编号' => $params['order'],
  284. 'c.mod_rq' => null,
  285. 'c.工序名称' => $params['code']
  286. ])
  287. ->select();
  288. $formattedRows = [];
  289. foreach ($rows as $item) {
  290. if (!empty($item['尺码'])) {
  291. $key = $item['款号'];
  292. if (!isset($formattedRows[$key])) {
  293. $formattedRows[$key] = [
  294. '订单编号' => $item['订单编号'],
  295. '款号' => $item['款号'],
  296. '上报时间' => $item['上报时间'],
  297. '子订单编号' => $item['子订单编号'],
  298. '颜色' => $list[0]['颜色备注'],
  299. '裁剪数' => 0
  300. ];
  301. }
  302. // 累加尺码数量
  303. if (isset($formattedRows[$key][$item['尺码']])) {
  304. $formattedRows[$key][$item['尺码']] += $item['数量'];
  305. } else {
  306. $formattedRows[$key][$item['尺码']] = $item['数量'];
  307. }
  308. // 累加当前尺码的数量【裁剪数】
  309. $formattedRows[$key]['裁剪数'] += $item['数量'];
  310. }
  311. }
  312. //重置索引为连续数字索引
  313. $finalResult = array_values($formattedRows);
  314. // 报工历史记录
  315. $existingRecords = \db('设备_产量计酬')->alias('c')
  316. ->field('
  317. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  318. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  319. j.款式,
  320. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  321. ')
  322. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  323. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  324. ->where('c.子订单编号', $params['order'])
  325. ->where('c.工序名称', $params['code'])
  326. // ->where('c.sczl_jtbh', $params['sys_sbID'])
  327. ->whereNull('c.mod_rq') // 查询未删除数据
  328. ->where(function($query) {
  329. $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
  330. }) // 查询出库数据
  331. ->order('c.UniqId', 'desc')
  332. ->distinct('c.子订单编号')
  333. ->select();
  334. $sizeSummary = [];
  335. $sizes = [];
  336. foreach ($existingRecords as $record) {
  337. $size = $record['尺码'];
  338. $quantity = $record['数量'];
  339. $serial = $record['serial'];
  340. // 如果尺码还没有出现过,加入尺码列表
  341. if (!in_array($size, $sizes)) {
  342. $sizes[] = $size;
  343. }
  344. if (!isset($sizeSummary[$record['UniqId']])) {
  345. $sizeSummary[$record['UniqId']] = [
  346. 'UniqId' => $record['UniqId'],
  347. '订单编号' => $record['订单编号'],
  348. '子订单编号' => $record['子订单编号'],
  349. '上报数量' => $record['s_num'],
  350. '剩余数量' => $record['ci_num'],
  351. '次品数量' => $record['数量'] - $record['ci_num'],
  352. '生产款号' => $record['款号'],
  353. '尺码' => $record['尺码'],
  354. '工序编号' => $record['工序编号'],
  355. '工序名称' => $record['工序名称'],
  356. '数量' => $quantity,
  357. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  358. '颜色' => $record['颜色备注'],
  359. '组别' => $record['sczl_jtbh'],
  360. '款式' => $record['款式'],
  361. 'serial' => '第('.$serial.')包',
  362. 'sys_rq' => $record['sys_rq']
  363. ];
  364. }
  365. }
  366. // 将 $sizeSummary 转换为索引数组
  367. $sizeSummary = array_values($sizeSummary);
  368. // 对 $sizeSummary 按 sys_rq 降序排序
  369. usort($sizeSummary, function ($a, $b) {
  370. return strtotime($b['UniqId']) - strtotime($a['UniqId']);
  371. });
  372. $result = [
  373. 'list' => $matchedOrder,
  374. 'headers' => $headers,
  375. 'table' => $finalResult,
  376. 'bgtable' => $existingRecords
  377. ];
  378. }else {
  379. //【查询订单编号对应的数据】
  380. $data = \db('工单_基本资料')->alias('j')
  381. ->distinct('y.子订单编号')
  382. ->field('
  383. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  384. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  385. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  386. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,
  387. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,
  388. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10'
  389. )
  390. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  391. // ->where('y.船样',1)
  392. ->where(function($query) {
  393. $query->whereNull('y.Mod_rq')->whereOr('y.Mod_rq', '');
  394. }) // 查询未删除数据
  395. // ->where($where)
  396. ->where('y.订单编号', 'like', '%' . $order . '%')
  397. ->select();
  398. $headers = [];//存放尺码表头
  399. foreach ($data as $item) {
  400. // 初始化尺码与数量映射数组
  401. $cmScslMapping = [];
  402. for ($i = 1; $i <= 10; $i++) {
  403. $cmValue = $item['cm' . $i];
  404. $scslValue = $item['scsl' . $i];
  405. if (!is_null($cmValue) && $cmValue !== 0) {
  406. $cmScslMapping[$cmValue] = ($scslValue === 0 || is_null($scslValue)) ? '' : $scslValue;
  407. if (!in_array($cmValue, $headers)) {
  408. $headers[] = $cmValue;
  409. }
  410. }
  411. }
  412. }
  413. //【查询产量计酬历史记录】
  414. $where['c.订单编号'] = ['like','%'.explode('C',$order)[1].'%'];
  415. $where['c.工序名称'] = $params['code'];
  416. $rows = db()->table('设备_产量计酬')->alias('c')
  417. ->field('c.款号, c.订单编号, c.子订单编号, c.sys_rq as 上报时间, c.尺码, c.数量')
  418. ->where([
  419. 'c.mod_rq' => null,
  420. // 'c.订单编号' => $order,
  421. // 'c.工序名称' => $params['code']
  422. ])
  423. ->where($where)
  424. ->select();
  425. $formattedRows = [];
  426. foreach ($rows as $item) {
  427. if (!empty($item['尺码'])) {
  428. $key = $item['上报时间'];
  429. if (!isset($formattedRows[$key])) {
  430. $formattedRows[$key] = [
  431. '订单编号' => $item['订单编号'],
  432. '款号' => $item['款号'],
  433. '上报时间' => $item['上报时间'],
  434. '子订单编号' => $item['子订单编号'],
  435. '裁剪数' => 0
  436. ];
  437. }
  438. // 累加尺码数量
  439. if (isset($formattedRows[$key][$item['尺码']])) {
  440. $formattedRows[$key][$item['尺码']] += $item['数量'];
  441. } else {
  442. $formattedRows[$key][$item['尺码']] = $item['数量'];
  443. }
  444. // 累加当前尺码的数量【裁剪数】
  445. $formattedRows[$key]['裁剪数'] += $item['数量'];
  446. }
  447. }
  448. // 重置索引为连续数字索引
  449. $finalResult = array_values($formattedRows);
  450. $result = [
  451. 'table' => $finalResult,
  452. 'headers' => $headers,
  453. 'list' => '',
  454. 'total' => '',
  455. ];
  456. }
  457. $this->success('请求成功', $result);
  458. break;
  459. case '车缝':
  460. // 查询历史记录并生成尺寸汇总
  461. function getSizeSummary($order) {
  462. $where = [];
  463. if (strpos($order, '-') !== false) {
  464. $where['c.子订单编号'] = $order;
  465. } else {
  466. $where['c.订单编号'] = ['like', '%' . $order . '%'];
  467. }
  468. $where['c.工序编号'] = '3';
  469. $where['c.mod_rq'] = null;
  470. $existingRecords = \db('设备_产量计酬')->alias('c')
  471. ->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,
  472. y.zdtotal, y.sctotal, j.款式, y.颜色,y.颜色备注, CAST(c.serial_num AS SIGNED) as serial_num_numeric')
  473. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  474. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  475. ->where($where)
  476. ->order('c.sys_rq desc')
  477. ->group('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num, j.款式')
  478. ->select();
  479. $sizeSummary = [];
  480. $sizes = [];
  481. foreach ($existingRecords as $record) {
  482. $size = $record['尺码'];
  483. $quantity = $record['数量'];
  484. $serial = $record['serial'];
  485. $serialNum = $record['serial_num']; // serial_num 分组
  486. $processNumber = $record['工序编号']; // 工序编号
  487. // 初始化尺码
  488. if (!in_array($size, $sizes)) {
  489. $sizes[] = $size;
  490. }
  491. // 检查是否已有相同的 serial_num 组合
  492. if (!isset($sizeSummary[$serialNum])) {
  493. $sizeSummary[$serialNum] = [
  494. '订单编号' => $record['订单编号'],
  495. '子订单编号' => $record['子订单编号'],
  496. '款号' => $record['款号'],
  497. '款式' => $record['款式'],
  498. '工序编号' => $record['工序编号'],
  499. '工序名称' => $record['工序名称'],
  500. '数量' => '0', // 记录总数量
  501. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  502. '颜色' => $record['颜色'],
  503. '颜色备注' => $record['颜色备注'],
  504. '机台号' => $record['sczl_jtbh'],
  505. '组别' => $record['sczl_bh'],
  506. 'serial' => [], // 流水号数组
  507. 'sys_rq' => $record['sys_rq'],
  508. 'UniqId' => $record['UniqId'],
  509. '尺码数据' => [] // 用于存储每个尺码对应的流水号和数量
  510. ];
  511. }
  512. // 将流水号添加到 serial 数组
  513. $sizeSummary[$serialNum]['serial'][] = $serial;
  514. // 累加数量到总数量
  515. $sizeSummary[$serialNum]['数量'] += $quantity;
  516. // 记录尺码数据
  517. if (!isset($sizeSummary[$serialNum]['尺码数据'][$size])) {
  518. $sizeSummary[$serialNum]['尺码数据'][$size] = [];
  519. }
  520. // 添加尺码对应的流水号和数量
  521. $sizeSummary[$serialNum]['尺码数据'][$size][] = $serial . ' (' . $quantity . ')';
  522. }
  523. // 对每个工序的 serial 进行处理,并转换为逗号分隔的字符串
  524. $finalRecords = [];
  525. foreach ($sizeSummary as &$summary) {
  526. // 将 serial 数组按数值降序排序
  527. sort($summary['serial'], SORT_NUMERIC);
  528. $summary['serial'] = implode(',', $summary['serial']);
  529. // 合并尺码数据到同一层级
  530. foreach ($summary['尺码数据'] as $size => $serials) {
  531. $summary[$size] = implode(', ', $serials); // 尺码对应的流水号和数量
  532. }
  533. unset($summary['尺码数据']); // 移除尺码数据数组,直接将数据合并到 summary
  534. $finalRecords[] = $summary;
  535. }
  536. // 尺码排序(数字在前,字母在后)
  537. usort($sizes, function ($a, $b) {
  538. $sizeOrder = ['XXS' => 1,'XS' => 2,'S' => 3, 'M' => 4, 'L' => 5, 'XL' => 6, 'XXL' => 7, 'XXXL' => 8];
  539. if (is_numeric($a) && is_numeric($b)) return $a - $b;
  540. if (is_numeric($a)) return -1;
  541. if (is_numeric($b)) return 1;
  542. return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
  543. });
  544. return ['headers' => $sizes, 'records' => $finalRecords];
  545. }
  546. if (strpos($order, '-') !== false) {
  547. if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
  548. $this->error('sys_sbID 参数错误');
  549. }
  550. if (preg_match('/-(\d{2})$/', $order, $matches)) {
  551. $number = (int)$matches[1];
  552. if ($number > 70) {
  553. //echo '船样';
  554. //获取订单编号
  555. // $ddbh = explode('-', $order)[0];
  556. //1.查询尺码、制单数、填写报工新增实裁数
  557. $data = \db('工单_基本资料')->alias('j')
  558. ->distinct('y.子订单编号')
  559. ->field('
  560. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  561. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  562. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  563. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,
  564. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,
  565. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10'
  566. )
  567. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  568. ->where('y.船样',1)
  569. ->where(function($query) {
  570. $query->whereNull('y.Mod_rq')->whereOr('y.Mod_rq', '');
  571. }) // 查询未删除数据
  572. ->where('y.子订单编号', $order)
  573. ->select();
  574. } else {
  575. //echo '两位数子订单';
  576. $ddbh = explode('-', $order)[0];//获取订单编号
  577. //【查询尺码、制单数、填写报工新增实裁数】
  578. $data = \db('工单_基本资料')->alias('j')
  579. ->distinct('y.子订单编号')
  580. ->field('
  581. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  582. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  583. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  584. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,
  585. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,
  586. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10'
  587. )
  588. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  589. ->where('y.船样',0)
  590. ->where(function($query) {
  591. $query->whereNull('y.Mod_rq')->whereOr('y.Mod_rq', '');
  592. }) // 查询未删除数据
  593. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  594. ->select();
  595. }
  596. }else{
  597. $ddbh = explode('-', $order)[0];//获取订单编号
  598. //【查询尺码、制单数、填写报工新增实裁数】
  599. $data = \db('工单_基本资料')->alias('j')
  600. ->distinct('y.子订单编号')
  601. ->field('
  602. y.订单编号, y.子订单编号, y.款号, j.款式, y.颜色,
  603. y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq,
  604. y.UniqId, y.sc_rq, y.颜色备注, y.color_id,y.船样,
  605. y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,
  606. y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,
  607. y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10'
  608. )
  609. ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
  610. ->where('y.船样',0)
  611. ->where(function($query) {
  612. $query->whereNull('y.Mod_rq')->whereOr('y.Mod_rq', '');
  613. }) // 查询未删除数据
  614. ->where('y.子订单编号', 'like', '%' . $ddbh . '%')
  615. ->select();
  616. }
  617. $dataEntry = [];
  618. $list = [];
  619. $headers = [];//存放尺码表头
  620. foreach ($data as $item) {
  621. // 初始化尺码与数量映射数组
  622. $cmScslMapping = [];
  623. for ($i = 1; $i <= 10; $i++) {
  624. $cmValue = $item['cm' . $i];
  625. $scslValue = $item['scsl' . $i];
  626. if (!is_null($cmValue) && $cmValue !== 0) {
  627. $cmScslMapping[$cmValue] = ($scslValue === 0 || is_null($scslValue)) ? '' : $scslValue;
  628. if (!in_array($cmValue, $headers)) {
  629. $headers[] = $cmValue;
  630. }
  631. }
  632. }
  633. // 将尺码数据平铺到主要数据中(表格数据)
  634. $orderData = [
  635. '订单编号' => $item['订单编号'],
  636. '子订单编号' => $item['子订单编号'],
  637. '款号' => $item['款号'],
  638. '款式' => $item['款式'],
  639. '颜色' => $item['颜色备注'],
  640. 'zdtotal' => $item['zdtotal'],
  641. 'sctotal' => $item['sctotal'],
  642. '单位' => $item['单位'],
  643. 'Sys_id' => $item['Sys_id'],
  644. 'Sys_rq' => $item['Sys_rq'],
  645. ];
  646. // 平铺尺码映射数据
  647. $orderData = array_merge($orderData, $cmScslMapping);
  648. $dataEntry[] = $orderData;
  649. // list 列表部分
  650. $entry = [];
  651. for ($i = 1; $i <= 10; $i++) {
  652. $entry['cm' . $i] = ($item['cm' . $i] === 0 || is_null($item['cm' . $i])) ? '' : $item['cm' . $i];
  653. $entry['cmsl' . $i] = ($item['cmsl' . $i] === 0 || is_null($item['cmsl' . $i])) ? '' : $item['cmsl' . $i];
  654. $entry['scsl' . $i] = ($item['scsl' . $i] === 0 || is_null($item['scsl' . $i])) ? '' : $item['scsl' . $i];
  655. }
  656. $entry['订单编号'] = $item['订单编号'];
  657. $entry['子订单编号'] = $item['子订单编号'];
  658. $entry['UniqId'] = $item['UniqId'];
  659. $entry['zdtotal'] = $item['zdtotal'];
  660. $entry['颜色'] = $item['颜色'];
  661. $entry['款式'] = $item['款式'];
  662. $entry['款号'] = $item['款号'];
  663. $entry['color_id'] = $item['color_id'];
  664. $entry['颜色备注'] = $item['颜色备注'];
  665. $list[] = $entry;
  666. }
  667. //打印当前订单所有尺码数据
  668. // echo "<pre>";print_r($list);echo "<pre>";die;
  669. // 合并子订单
  670. $merged = [];
  671. foreach ($list as $item) {
  672. $key = $item['子订单编号'];
  673. // 生成合并的唯一键,以 color_id 和 颜色备注 为依据
  674. $mergeKey = $item['color_id'] . '-' . $item['颜色备注'];
  675. if (!isset($merged[$mergeKey])) {
  676. // 如果不存在,则初始化
  677. $merged[$mergeKey] = $item;
  678. } else {
  679. // 如果已经存在,则合并数量
  680. for ($i = 1; $i <= 10; $i++) {
  681. $sizeKey = 'cm' . $i;
  682. $qtyKey = 'cmsl' . $i;
  683. if (!empty($item[$sizeKey]) && $item[$sizeKey] === $merged[$mergeKey][$sizeKey]) {
  684. $merged[$mergeKey][$qtyKey] = (int)$merged[$mergeKey][$qtyKey] + (int)$item[$qtyKey];
  685. }
  686. }
  687. // 也可以累加其他字段,如总数量等
  688. $merged[$mergeKey]['zdtotal'] += $item['zdtotal'];
  689. }
  690. }
  691. $mergeds = array_values($merged); // 重置索引为连续数字索引
  692. //打印输出合并后订单数据
  693. // echo "<pre>";print_r($mergeds);echo "</pre>";die;
  694. // 在 $mergeds 中查找匹配的子订单编号 对应的一条数据
  695. $matchedOrder = null;
  696. foreach ($mergeds as $mergedOrder) {
  697. if ($mergedOrder['子订单编号'] === $order) {
  698. $matchedOrder = $mergedOrder;
  699. break; // 找到后可以退出循环
  700. }
  701. }
  702. $allScslEmpty = true;
  703. for ($i = 1; $i <= 10; $i++) {
  704. if (!empty($matchedOrder["scsl$i"])) {
  705. $allScslEmpty = false;
  706. break;
  707. }
  708. }
  709. if ($allScslEmpty) {
  710. $this->error('当前订单还未进行裁剪,请裁剪后再报工');
  711. }
  712. // echo "<pre>";print_r($matchedOrder);echo "</pre>";die;
  713. $sizeData = getSizeSummary($order);
  714. // 查询流水号(包数)和序号
  715. $serial = \db('设备_产量计酬')
  716. ->field('子订单编号, MAX(CAST(serial AS UNSIGNED)) as serial, MAX(CAST(serial_num AS UNSIGNED)) as serial_num')
  717. ->where('子订单编号', $order)
  718. ->where(function($query) {
  719. $query->whereNull('mod_rq')->whereOr('mod_rq', '');
  720. }) // 查询未删除数据
  721. // ->where('sczl_jtbh', $params['sys_sbID'])
  722. ->group('子订单编号')
  723. ->find();
  724. $serial['serial'] = empty($serial['serial']) ? '000' : str_pad($serial['serial'], 3, '0', STR_PAD_LEFT);
  725. $serial['serial_num'] = empty($serial['serial_num']) ? '0' : $serial['serial_num'];
  726. $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  727. $clwhere['子订单编号'] = $order;
  728. $clwhere['mod_rq'] = null;
  729. $clwhere['工序名称'] = $params['code'];
  730. $cljclist = \db('设备_产量计酬')
  731. ->field('订单编号,子订单编号,尺码,数量,mod_rq')
  732. ->where($clwhere)
  733. ->select();
  734. $result = [];
  735. $totalQuantity = 0; // 总数量初始化
  736. $orderNumber = ''; // 订单编号初始化
  737. foreach ($cljclist as $item) {
  738. $size = $item['尺码'];
  739. $orderNumber = $item['订单编号'];
  740. if (isset($result[$size])) {
  741. $result[$size] += $item['数量'];
  742. } else {
  743. $result[$size] = $item['数量'];
  744. }
  745. // 累加总数量
  746. $totalQuantity += $item['数量'];
  747. }
  748. $result['订单编号'] = $orderNumber;
  749. foreach ($result as $size => $quantity) {
  750. if ($size !== '订单编号' && $size !== '数量') {
  751. $result[$size] = '合计(' . $quantity . ')';
  752. }
  753. }
  754. $result['数量'] = '合计(' . $totalQuantity . ')';
  755. // 初始化 $sizeData['records'],以防万一它不存在
  756. if (!isset($sizeData['records'])) {
  757. $sizeData['records'] = [];
  758. }
  759. // 将 $result 插入到 $sizeData['records'] 的最前面
  760. array_unshift($sizeData['records'], $result);
  761. $this->success('请求成功', [
  762. 'serial' => $serial['serial'], // 流水号
  763. 'serial_num' => $serial['serial_num'], // 序号
  764. 'list' => $matchedOrder, // 报工
  765. 'headers' => $sizeData['headers'], // 尺码表头
  766. 'records' => $sizeData['records'] // 查询历史记录
  767. ]);
  768. }else{
  769. $sizeData = getSizeSummary($order);
  770. // 查询流水号(包数)和序号
  771. $serial = \db('设备_产量计酬')
  772. ->field('子订单编号, MAX(CAST(serial AS UNSIGNED)) as serial, MAX(CAST(serial_num AS UNSIGNED)) as serial_num')
  773. ->where('订单编号', $order)
  774. ->group('订单编号')
  775. ->find();
  776. $serial['serial'] = empty($serial['serial']) ? '000' : str_pad($serial['serial'], 3, '0', STR_PAD_LEFT);
  777. $serial['serial_num'] = empty($serial['serial_num']) ? '0' : $serial['serial_num'];
  778. $clwhere['sczl_jtbh'] = $params['sys_sbID'];
  779. $clwhere['订单编号'] = $order;
  780. $clwhere['工序名称'] = $params['code'];
  781. $clwhere['mod_rq'] = null;
  782. $cljclist = \db('设备_产量计酬')
  783. ->field('订单编号,子订单编号,尺码,数量,mod_rq')
  784. ->where($clwhere)
  785. ->select();
  786. $result = [];
  787. $totalQuantity = 0; // 总数量初始化
  788. $orderNumber = ''; // 订单编号初始化
  789. foreach ($cljclist as $item) {
  790. $size = $item['尺码'];
  791. $orderNumber = $item['订单编号'];
  792. if (isset($result[$size])) {
  793. $result[$size] += $item['数量'];
  794. } else {
  795. $result[$size] = $item['数量'];
  796. }
  797. // 累加总数量
  798. $totalQuantity += $item['数量'];
  799. }
  800. foreach ($result as $size => $quantity) {
  801. if ($size !== '订单编号' && $size !== '数量') {
  802. $result[$size] = '合计(' . $quantity . ')';
  803. }
  804. }
  805. $result['订单编号'] = $orderNumber;
  806. $result['数量'] = '合计(' . $totalQuantity . ')';
  807. // 初始化 $sizeData['records'],以防万一它不存在
  808. if (!isset($sizeData['records'])) {
  809. $sizeData['records'] = [];
  810. }
  811. // 将 $result 插入到 $sizeData['records'] 的最前面
  812. array_unshift($sizeData['records'], $result);
  813. $this->success('请求成功', [
  814. 'serial' => $serial['serial'], // 流水号
  815. 'serial_num' => $serial['serial_num'], // 序号
  816. // 'list' => $matchedOrder, // 报工
  817. 'headers' => $sizeData['headers'], // 尺码表头
  818. 'records' => $sizeData['records'] // 查询历史记录
  819. ]);
  820. // $this->success('请求成功');
  821. }
  822. break;
  823. case '手工':
  824. //判断是否为标签参数数据
  825. if (strpos($order, ',') !== false) {
  826. if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
  827. $this->error('未获取到机台号');
  828. }
  829. // 将子订单编号, 机台号, 工序, 尺码, 数量, 尾包, 流水号(包)分割为数组
  830. $paramArray = explode(',', $order);
  831. // 获取车缝报工生成小票的数据
  832. $Uniqid = \db('设备_产量计酬')
  833. ->where([
  834. '子订单编号' => $paramArray[0],
  835. '尺码' => $paramArray[3],
  836. '数量' => $paramArray[4],
  837. 'serial' => $paramArray[6],
  838. ])
  839. ->find();
  840. // 判断是否成功获取到数据
  841. if ($Uniqid) {
  842. $orderlist = \db('工单_基本资料')
  843. ->where('订单编号', $Uniqid['订单编号'])
  844. ->field('订单编号, 生产款号, 款式')
  845. ->find();
  846. $yinjianlist = \db('工单_印件资料')
  847. ->where('订单编号', $orderlist['订单编号'])
  848. ->field('zdtotal, sctotal,颜色,颜色备注')
  849. ->find();
  850. } else {
  851. $this->error('当前扫码小票无效');
  852. }
  853. // 获取当前手工机台上报的数据
  854. $res = \db('设备_产量计酬')
  855. ->where([
  856. '子订单编号' => $paramArray[0],
  857. '尺码' => $paramArray[3],
  858. '数量' => $paramArray[4],
  859. 'serial' => $paramArray[6],
  860. 'sczl_jtbh' => $params['sys_sbID'],
  861. 'mod_rq' => null,
  862. ])
  863. ->order('UniqId desc')
  864. ->find();
  865. if (empty($res)) {
  866. $ci_num = $paramArray[4];
  867. } else {
  868. $ci_num = $res["ci_num"];
  869. // // 累加 s_num
  870. // $total_s_num = 0;
  871. // foreach ($res as $item) {
  872. // $total_s_num += $item['s_num'];
  873. // }
  874. // if ($total_s_num >= $paramArray[4]) {
  875. // $ci_num = 0;
  876. // } else {
  877. // $ci_num = $res[0]['ci_num'];
  878. // }
  879. }
  880. // 判断是否已报工并给出提示
  881. // if (!empty($res) && $res["ci_num"] == 0) {
  882. // $this->error('已上报,无需再次报工');
  883. // }
  884. $result = [
  885. 'sys_sbID' => $params['sys_sbID'],//设备编号
  886. 'ci_num' => $ci_num,//剩余数量
  887. 'UniqId' => $Uniqid['UniqId'],
  888. 'order' => $paramArray[0],//子订单编号
  889. 'zb' => $params['sys_sbID'],//组别
  890. 'gx' => 4,//默认固定工序
  891. 'cm' => $paramArray[3],//尺码
  892. 'sl' => $paramArray[4],//数量
  893. 'wb' => $paramArray[5],//尾包
  894. '订单编号' => $orderlist['订单编号'],
  895. '生产款号' => $orderlist['生产款号'],
  896. '款式' => $orderlist['款式'],
  897. 'sctotal' => $yinjianlist['sctotal'],
  898. 'zdtotal' => $yinjianlist['zdtotal'],
  899. '颜色' => $yinjianlist['颜色备注'],
  900. ];
  901. // 查询历史记录
  902. $existingRecords = \db('设备_产量计酬')->alias('c')
  903. ->field('
  904. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  905. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  906. j.款式,
  907. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  908. ')
  909. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  910. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  911. ->where('c.子订单编号', $paramArray[0])
  912. ->where('c.工序名称', '手工')
  913. ->where('c.sczl_jtbh', $params['sys_sbID'])
  914. ->whereNull('c.mod_rq') // 查询未删除数据
  915. ->where(function($query) {
  916. $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
  917. }) // 查询出库数据
  918. ->order('c.UniqId', 'desc')
  919. ->distinct('c.子订单编号')
  920. ->select();
  921. $sizeSummary = [];
  922. $sizes = [];
  923. foreach ($existingRecords as $record) {
  924. $size = $record['尺码'];
  925. $quantity = $record['数量'];
  926. $serial = $record['serial'];
  927. // 如果尺码还没有出现过,加入尺码列表
  928. if (!in_array($size, $sizes)) {
  929. $sizes[] = $size;
  930. }
  931. if (!isset($sizeSummary[$record['UniqId']])) {
  932. $sizeSummary[$record['UniqId']] = [
  933. 'UniqId' => $record['UniqId'],
  934. '订单编号' => $record['订单编号'],
  935. '子订单编号' => $record['子订单编号'],
  936. '上报数量' => $record['s_num'],
  937. '剩余数量' => $record['ci_num'],
  938. '次品数量' => $record['数量'] - $record['ci_num'],
  939. '生产款号' => $record['款号'],
  940. '尺码' => $record['尺码'],
  941. '工序编号' => $record['工序编号'],
  942. '工序名称' => $record['工序名称'],
  943. '数量' => $quantity,
  944. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  945. '颜色' => $record['颜色备注'],
  946. '组别' => $record['sczl_jtbh'],
  947. '款式' => $record['款式'],
  948. 'serial' => '第('.$serial.')包',
  949. 'sys_rq' => $record['sys_rq']
  950. ];
  951. }
  952. }
  953. // 将 $sizeSummary 转换为索引数组
  954. $sizeSummary = array_values($sizeSummary);
  955. // 对 $sizeSummary 按 sys_rq 降序排序
  956. usort($sizeSummary, function ($a, $b) {
  957. return strtotime($b['UniqId']) - strtotime($a['UniqId']);
  958. });
  959. $this->success('请求成功', [
  960. 'result' => $result,//小票数据
  961. 'headers' => $sizes, // 尺码表头
  962. 'records' => $sizeSummary // 历史记录
  963. ]);
  964. }else{
  965. // 查询历史记录
  966. $res = \db('设备_产量计酬')->alias('c')
  967. ->field('
  968. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  969. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  970. j.款式,
  971. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  972. ')
  973. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  974. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  975. ->where('c.订单编号', 'like', '%' . explode('DC',$order)[1] . '%')
  976. ->where('c.工序名称', $params['code'])
  977. ->where('c.sczl_jtbh', $params['sys_sbID'])
  978. ->whereNull('c.mod_rq') // 查询未删除数据
  979. ->order('UniqId desc')
  980. ->select();
  981. // 初始化返回数据的结构
  982. $reslist = [];
  983. $sizes = [];
  984. // 遍历查询结果,构造需要的结构化数据
  985. foreach ($res as $record) {
  986. $size = $record['尺码']; // 记录的尺码
  987. $quantity = $record['数量']; // 每个记录的数量
  988. $serial = $record['serial']; // 记录的serial
  989. // 记录所有不同的尺码,避免重复
  990. if (!in_array($size, $sizes)) {
  991. $sizes[] = $size;
  992. }
  993. // 构造每条记录的详细信息
  994. $reslist[] = [
  995. 'UniqId' => $record['UniqId'],
  996. '订单编号' => $record['订单编号'],
  997. '子订单编号' => $record['子订单编号'],
  998. '上报数量' => $record['s_num'],
  999. '剩余数量' => $record['ci_num'],
  1000. '次品数量' => $record['数量'] - $record['ci_num'],
  1001. '生产款号' => $record['款号'],
  1002. '尺码' => $record['尺码'],
  1003. '工序编号' => $record['工序编号'],
  1004. '工序名称' => $record['工序名称'],
  1005. '数量' => $quantity,
  1006. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1007. '颜色' => $record['颜色备注'],
  1008. '组别' => $record['sczl_jtbh'],
  1009. '款式' => $record['款式'],
  1010. 'serial' => '第('.$serial.')包',
  1011. 'sys_rq' => $record['sys_rq']
  1012. ];
  1013. }
  1014. // 按 sys_rq (时间) 或 UniqId 排序(按实际需求选)
  1015. usort($reslist, function ($a, $b) {
  1016. return strtotime($b['sys_rq']) - strtotime($a['sys_rq']);
  1017. });
  1018. // 返回结构化的查询结果
  1019. $this->success('请求成功', [
  1020. 'result' => $res, // 原始查询结果
  1021. 'headers' => $sizes, // 尺码表头
  1022. 'records' => $reslist // 处理后的记录列表
  1023. ]);
  1024. }
  1025. break;
  1026. case '大烫':
  1027. if (strpos($order, ',') !== false) {
  1028. if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
  1029. $this->error('未获取到机台号');
  1030. }
  1031. // 将子订单编号, 机台号, 工序, 尺码, 数量, 尾包, 流水号(包)分割为数组
  1032. $paramArray = explode(',', $order);
  1033. // 获取车缝报工生成小票的数据
  1034. $Uniqid = \db('设备_产量计酬')
  1035. ->where([
  1036. '子订单编号' => $paramArray[0],
  1037. '尺码' => $paramArray[3],
  1038. '数量' => $paramArray[4],
  1039. 'serial' => $paramArray[6],
  1040. ])
  1041. ->find();
  1042. // 获取上一个机台是否已上报过
  1043. $baogonglist = \db('设备_产量计酬')
  1044. ->where([
  1045. '子订单编号' => $paramArray[0],
  1046. '尺码' => $paramArray[3],
  1047. '数量' => $paramArray[4],
  1048. 'serial' => $paramArray[6],
  1049. 'Mod_rq' => null,
  1050. ])
  1051. ->where('sczl_jtbh', 'like', 'HD%')
  1052. ->select();
  1053. if(empty($baogonglist)){
  1054. $this->error('当前小票'.'【手工报工】'.'未上报,请上报后再操作');
  1055. }
  1056. // 判断是否成功获取到数据
  1057. if ($Uniqid) {
  1058. $orderlist = \db('工单_基本资料')
  1059. ->where('订单编号', $Uniqid['订单编号'])
  1060. ->field('订单编号, 生产款号, 款式')
  1061. ->find();
  1062. $yinjianlist = \db('工单_印件资料')
  1063. ->where('订单编号', $orderlist['订单编号'])
  1064. ->field('zdtotal, sctotal,颜色,颜色备注')
  1065. ->find();
  1066. } else {
  1067. $this->error('当前扫码小票无效');
  1068. }
  1069. // 获取当前手工机台上报的数据
  1070. $res = \db('设备_产量计酬')
  1071. ->where([
  1072. '子订单编号' => $paramArray[0],
  1073. '尺码' => $paramArray[3],
  1074. '数量' => $paramArray[4],
  1075. 'serial' => $paramArray[6],
  1076. 'sczl_jtbh' => $params['sys_sbID'],
  1077. 'mod_rq' => null,
  1078. ])
  1079. ->order('UniqId desc')
  1080. ->find();
  1081. if (empty($res)) {
  1082. $ci_num = $paramArray[4];
  1083. } else {
  1084. $ci_num = $res["ci_num"];
  1085. // // 累加 s_num
  1086. // $total_s_num = 0;
  1087. // foreach ($res as $item) {
  1088. // $total_s_num += $item['s_num'];
  1089. // }
  1090. // if ($total_s_num >= $paramArray[4]) {
  1091. // $ci_num = 0;
  1092. // } else {
  1093. // $ci_num = $res[0]['ci_num'];
  1094. // }
  1095. }
  1096. // 判断是否已报工并给出提示
  1097. // if (!empty($res) && $paramArray[4] == $res[0]['s_num'] || $ci_num === '' || $ci_num === 0) {
  1098. // $this->error('已上报,无需再次报工');
  1099. // }
  1100. // if (!empty($res) && $res["ci_num"] == 0) {
  1101. // $this->success('已上报,无需再次报工');
  1102. // }
  1103. $result = [
  1104. 'sys_sbID' => $params['sys_sbID'],//设备编号
  1105. 'ci_num' => $ci_num,//剩余数量
  1106. 'UniqId' => $Uniqid['UniqId'],
  1107. 'order' => $paramArray[0],//子订单编号
  1108. 'zb' => $params['sys_sbID'],//组别
  1109. 'gx' => 4,//默认固定工序
  1110. 'cm' => $paramArray[3],//尺码
  1111. 'sl' => $paramArray[4],//数量
  1112. 'wb' => $paramArray[5],//尾包
  1113. '订单编号' => $orderlist['订单编号'],
  1114. '生产款号' => $orderlist['生产款号'],
  1115. '款式' => $orderlist['款式'],
  1116. 'sctotal' => $yinjianlist['sctotal'],
  1117. 'zdtotal' => $yinjianlist['zdtotal'],
  1118. '颜色' => $yinjianlist['颜色备注'],
  1119. ];
  1120. // 查询历史记录
  1121. $existingRecords = \db('设备_产量计酬')->alias('c')
  1122. ->field('
  1123. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  1124. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1125. j.款式,
  1126. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1127. ')
  1128. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1129. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1130. ->where('c.子订单编号', $paramArray[0])
  1131. ->where('c.工序名称', '大烫')
  1132. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1133. ->whereNull('c.mod_rq') // 查询未删除数据
  1134. ->where(function($query) {
  1135. $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
  1136. }) // 查询出库数据
  1137. ->order('c.UniqId', 'desc')
  1138. ->distinct('c.子订单编号')
  1139. ->select();
  1140. $sizeSummary = [];
  1141. $sizes = [];
  1142. foreach ($existingRecords as $record) {
  1143. $size = $record['尺码'];
  1144. $quantity = $record['数量'];
  1145. $serial = $record['serial'];
  1146. // 如果尺码还没有出现过,加入尺码列表
  1147. if (!in_array($size, $sizes)) {
  1148. $sizes[] = $size;
  1149. }
  1150. if (!isset($sizeSummary[$record['UniqId']])) {
  1151. $sizeSummary[$record['UniqId']] = [
  1152. 'UniqId' => $record['UniqId'],
  1153. '订单编号' => $record['订单编号'],
  1154. '子订单编号' => $record['子订单编号'],
  1155. '上报数量' => $record['s_num'],
  1156. '剩余数量' => $record['ci_num'],
  1157. '次品数量' => $record['数量'] - $record['ci_num'],
  1158. '生产款号' => $record['款号'],
  1159. '尺码' => $record['尺码'],
  1160. '工序编号' => $record['工序编号'],
  1161. '工序名称' => $record['工序名称'],
  1162. '数量' => $quantity,
  1163. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1164. '颜色' => $record['颜色备注'],
  1165. '组别' => $record['sczl_jtbh'],
  1166. '款式' => $record['款式'],
  1167. 'serial' => '第('.$serial.')包',
  1168. 'sys_rq' => $record['sys_rq']
  1169. ];
  1170. }
  1171. }
  1172. // 将 $sizeSummary 转换为索引数组
  1173. $sizeSummary = array_values($sizeSummary);
  1174. // 对 $sizeSummary 按 sys_rq 降序排序
  1175. usort($sizeSummary, function ($a, $b) {
  1176. return strtotime($b['UniqId']) - strtotime($a['UniqId']);
  1177. });
  1178. $this->success('请求成功', [
  1179. 'result' => $result,//小票数据
  1180. 'headers' => $sizes, // 尺码表头
  1181. 'records' => $sizeSummary // 历史记录
  1182. ]);
  1183. }else{
  1184. // 查询历史记录
  1185. $res = \db('设备_产量计酬')->alias('c')
  1186. ->field('
  1187. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  1188. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1189. j.款式,
  1190. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1191. ')
  1192. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1193. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1194. ->where('c.订单编号', 'like', '%' . explode('DC',$order)[1] . '%')
  1195. ->where('c.工序名称', $params['code'])
  1196. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1197. ->whereNull('c.mod_rq') // 查询未删除数据
  1198. ->order('UniqId desc')
  1199. ->select();
  1200. // 初始化返回数据的结构
  1201. $reslist = [];
  1202. $sizes = [];
  1203. // 遍历查询结果,构造需要的结构化数据
  1204. foreach ($res as $record) {
  1205. $size = $record['尺码']; // 记录的尺码
  1206. $quantity = $record['数量']; // 每个记录的数量
  1207. $serial = $record['serial']; // 记录的serial
  1208. // 记录所有不同的尺码,避免重复
  1209. if (!in_array($size, $sizes)) {
  1210. $sizes[] = $size;
  1211. }
  1212. // 构造每条记录的详细信息
  1213. $reslist[] = [
  1214. 'UniqId' => $record['UniqId'],
  1215. '订单编号' => $record['订单编号'],
  1216. '子订单编号' => $record['子订单编号'],
  1217. '上报数量' => $record['s_num'],
  1218. '剩余数量' => $record['ci_num'],
  1219. '次品数量' => $record['数量'] - $record['ci_num'],
  1220. '生产款号' => $record['款号'],
  1221. '尺码' => $record['尺码'],
  1222. '工序编号' => $record['工序编号'],
  1223. '工序名称' => $record['工序名称'],
  1224. '数量' => $quantity,
  1225. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1226. '颜色' => $record['颜色备注'],
  1227. '组别' => $record['sczl_jtbh'],
  1228. '款式' => $record['款式'],
  1229. 'serial' => '第('.$serial.')包',
  1230. 'sys_rq' => $record['sys_rq']
  1231. ];
  1232. }
  1233. // 按 sys_rq (时间) 或 UniqId 排序(按实际需求选)
  1234. usort($reslist, function ($a, $b) {
  1235. return strtotime($b['sys_rq']) - strtotime($a['sys_rq']);
  1236. });
  1237. // 返回结构化的查询结果
  1238. $this->success('请求成功', [
  1239. 'result' => $res, // 原始查询结果
  1240. 'headers' => $sizes, // 尺码表头
  1241. 'records' => $reslist // 处理后的记录列表
  1242. ]);
  1243. }
  1244. break;
  1245. case '总检':
  1246. if (strpos($order, ',') !== false) {
  1247. if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
  1248. $this->error('未获取到机台号');
  1249. }
  1250. // 将子订单编号, 机台号, 工序, 尺码, 数量, 尾包, 流水号(包)分割为数组
  1251. $paramArray = explode(',', $order);
  1252. // 获取车缝报工生成小票的数据
  1253. $Uniqid = \db('设备_产量计酬')
  1254. ->where([
  1255. '子订单编号' => $paramArray[0],
  1256. '尺码' => $paramArray[3],
  1257. '数量' => $paramArray[4],
  1258. 'serial' => $paramArray[6],
  1259. ])
  1260. ->find();
  1261. // 获取上一个机台是否已上报过
  1262. $baogonglist = \db('设备_产量计酬')
  1263. ->where([
  1264. '子订单编号' => $paramArray[0],
  1265. '尺码' => $paramArray[3],
  1266. '数量' => $paramArray[4],
  1267. 'serial' => $paramArray[6],
  1268. 'Mod_rq' => null,
  1269. ])
  1270. ->where('sczl_jtbh', 'like', 'DT%')
  1271. ->select();
  1272. if(empty($baogonglist)){
  1273. $this->error('当前小票'.'【大烫报工】'.'未上报,请上报后再操作');
  1274. }
  1275. // 判断是否成功获取到数据
  1276. if ($Uniqid) {
  1277. $orderlist = \db('工单_基本资料')
  1278. ->where('订单编号', $Uniqid['订单编号'])
  1279. ->field('订单编号, 生产款号, 款式')
  1280. ->find();
  1281. $yinjianlist = \db('工单_印件资料')
  1282. ->where('订单编号', $orderlist['订单编号'])
  1283. ->field('zdtotal, sctotal,颜色,颜色备注')
  1284. ->find();
  1285. } else {
  1286. $this->error('当前扫码小票无效');
  1287. }
  1288. // 获取当前手工机台上报的数据
  1289. $res = \db('设备_产量计酬')
  1290. ->where([
  1291. '子订单编号' => $paramArray[0],
  1292. '尺码' => $paramArray[3],
  1293. '数量' => $paramArray[4],
  1294. 'serial' => $paramArray[6],
  1295. 'sczl_jtbh' => $params['sys_sbID'],
  1296. 'mod_rq' => null,
  1297. ])
  1298. ->order('UniqId desc')
  1299. ->find();
  1300. if (empty($res)) {
  1301. $ci_num = $paramArray[4];
  1302. } else {
  1303. $ci_num = $res["ci_num"];
  1304. // // 累加 s_num
  1305. // $total_s_num = 0;
  1306. // foreach ($res as $item) {
  1307. // $total_s_num += $item['s_num'];
  1308. // }
  1309. // if ($total_s_num >= $paramArray[4]) {
  1310. // $ci_num = 0;
  1311. // } else {
  1312. // $ci_num = $res[0]['ci_num'];
  1313. // }
  1314. }
  1315. // 判断是否已报工并给出提示
  1316. // if (!empty($res) && $paramArray[4] == $res[0]['s_num'] || $ci_num === '' || $ci_num === 0) {
  1317. // $this->error('已上报,无需再次报工');
  1318. // }
  1319. // if (!empty($res) && $res["ci_num"] == 0) {
  1320. // $this->error('已上报,无需再次报工');
  1321. // }
  1322. $result = [
  1323. 'sys_sbID' => $params['sys_sbID'],//设备编号
  1324. 'ci_num' => $ci_num,//剩余数量
  1325. 'UniqId' => $Uniqid['UniqId'],
  1326. 'order' => $paramArray[0],//子订单编号
  1327. 'zb' => $params['sys_sbID'],//组别
  1328. 'gx' => 4,//默认固定工序
  1329. 'cm' => $paramArray[3],//尺码
  1330. 'sl' => $paramArray[4],//数量
  1331. 'wb' => $paramArray[5],//尾包
  1332. '订单编号' => $orderlist['订单编号'],
  1333. '生产款号' => $orderlist['生产款号'],
  1334. '款式' => $orderlist['款式'],
  1335. 'sctotal' => $yinjianlist['sctotal'],
  1336. 'zdtotal' => $yinjianlist['zdtotal'],
  1337. '颜色' => $yinjianlist['颜色备注'],
  1338. ];
  1339. // 查询历史记录
  1340. $existingRecords = \db('设备_产量计酬')->alias('c')
  1341. ->field('
  1342. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  1343. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1344. j.款式,
  1345. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1346. ')
  1347. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1348. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1349. ->where('c.子订单编号', $paramArray[0])
  1350. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1351. ->whereNull('c.mod_rq') // 查询未删除数据
  1352. ->where(function($query) {
  1353. $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
  1354. }) // 查询出库数据
  1355. ->order('c.UniqId', 'desc')
  1356. ->distinct('c.子订单编号')
  1357. ->select();
  1358. $sizeSummary = [];
  1359. $sizes = [];
  1360. foreach ($existingRecords as $record) {
  1361. $size = $record['尺码'];
  1362. $quantity = $record['数量'];
  1363. $serial = $record['serial'];
  1364. // 如果尺码还没有出现过,加入尺码列表
  1365. if (!in_array($size, $sizes)) {
  1366. $sizes[] = $size;
  1367. }
  1368. if (!isset($sizeSummary[$record['UniqId']])) {
  1369. $sizeSummary[$record['UniqId']] = [
  1370. 'UniqId' => $record['UniqId'],
  1371. '订单编号' => $record['订单编号'],
  1372. '子订单编号' => $record['子订单编号'],
  1373. '上报数量' => $record['s_num'],
  1374. '剩余数量' => $record['ci_num'],
  1375. '次品数量' => $record['数量'] - $record['ci_num'],
  1376. '生产款号' => $record['款号'],
  1377. '尺码' => $record['尺码'],
  1378. '工序编号' => $record['工序编号'],
  1379. '工序名称' => $record['工序名称'],
  1380. '数量' => $quantity,
  1381. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1382. '颜色' => $record['颜色备注'],
  1383. '组别' => $record['sczl_jtbh'],
  1384. '款式' => $record['款式'],
  1385. 'serial' => '第('.$serial.')包',
  1386. 'sys_rq' => $record['sys_rq']
  1387. ];
  1388. }
  1389. }
  1390. // 将 $sizeSummary 转换为索引数组
  1391. $sizeSummary = array_values($sizeSummary);
  1392. // 对 $sizeSummary 按 sys_rq 降序排序
  1393. usort($sizeSummary, function ($a, $b) {
  1394. return strtotime($b['UniqId']) - strtotime($a['UniqId']);
  1395. });
  1396. $this->success('请求成功', [
  1397. 'result' => $result,//小票数据
  1398. 'headers' => $sizes, // 尺码表头
  1399. 'records' => $sizeSummary // 历史记录
  1400. ]);
  1401. }else{
  1402. // 查询历史记录
  1403. $res = \db('设备_产量计酬')->alias('c')
  1404. ->field('
  1405. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  1406. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1407. j.款式,
  1408. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1409. ')
  1410. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1411. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1412. ->where('c.订单编号', 'like', '%' . explode('DC',$order)[1] . '%')
  1413. ->where('c.工序名称', $params['code'])
  1414. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1415. ->whereNull('c.mod_rq') // 查询未删除数据
  1416. ->order('UniqId desc')
  1417. ->select();
  1418. // 初始化返回数据的结构
  1419. $reslist = [];
  1420. $sizes = [];
  1421. // 遍历查询结果,构造需要的结构化数据
  1422. foreach ($res as $record) {
  1423. $size = $record['尺码']; // 记录的尺码
  1424. $quantity = $record['数量']; // 每个记录的数量
  1425. $serial = $record['serial']; // 记录的serial
  1426. // 记录所有不同的尺码,避免重复
  1427. if (!in_array($size, $sizes)) {
  1428. $sizes[] = $size;
  1429. }
  1430. // 构造每条记录的详细信息
  1431. $reslist[] = [
  1432. 'UniqId' => $record['UniqId'],
  1433. '订单编号' => $record['订单编号'],
  1434. '子订单编号' => $record['子订单编号'],
  1435. '上报数量' => $record['s_num'],
  1436. '次品数量' => $record['数量'] - $record['ci_num'],
  1437. '生产款号' => $record['款号'],
  1438. '尺码' => $record['尺码'],
  1439. '工序编号' => $record['工序编号'],
  1440. '工序名称' => $record['工序名称'],
  1441. '数量' => $quantity,
  1442. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1443. '颜色' => $record['颜色备注'],
  1444. '组别' => $record['sczl_jtbh'],
  1445. '款式' => $record['款式'],
  1446. 'serial' => '第('.$serial.')包',
  1447. 'sys_rq' => $record['sys_rq']
  1448. ];
  1449. }
  1450. // 按 sys_rq (时间) 或 UniqId 排序(按实际需求选)
  1451. usort($reslist, function ($a, $b) {
  1452. return strtotime($b['sys_rq']) - strtotime($a['sys_rq']);
  1453. });
  1454. // 返回结构化的查询结果
  1455. $this->success('请求成功', [
  1456. 'result' => $res, // 原始查询结果
  1457. 'headers' => $sizes, // 尺码表头
  1458. 'records' => $reslist // 处理后的记录列表
  1459. ]);
  1460. }
  1461. break;
  1462. case '包装':
  1463. if (strpos($order, ',') !== false) {
  1464. if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
  1465. $this->error('未获取到机台号');
  1466. }
  1467. // 将子订单编号, 机台号, 工序, 尺码, 数量, 尾包, 流水号(包)分割为数组
  1468. $paramArray = explode(',', $order);
  1469. // 获取车缝报工生成小票的数据
  1470. $Uniqid = \db('设备_产量计酬')
  1471. ->where([
  1472. '子订单编号' => $paramArray[0],
  1473. '尺码' => $paramArray[3],
  1474. '数量' => $paramArray[4],
  1475. 'serial' => $paramArray[6],
  1476. ])
  1477. ->find();
  1478. // 获取上一个机台是否已上报过
  1479. $baogonglist = \db('设备_产量计酬')
  1480. ->where([
  1481. '子订单编号' => $paramArray[0],
  1482. '尺码' => $paramArray[3],
  1483. '数量' => $paramArray[4],
  1484. 'serial' => $paramArray[6],
  1485. 'Mod_rq' => null,
  1486. ])
  1487. ->where('sczl_jtbh', 'like', 'ZJ%')
  1488. ->select();
  1489. if(empty($baogonglist)){
  1490. $this->error('当前小票'.'【总检报工】'.'未上报,请上报后再操作');
  1491. }
  1492. // 判断是否成功获取到数据
  1493. if ($Uniqid) {
  1494. $orderlist = \db('工单_基本资料')
  1495. ->where('订单编号', $Uniqid['订单编号'])
  1496. ->field('订单编号, 生产款号, 款式')
  1497. ->find();
  1498. $yinjianlist = \db('工单_印件资料')
  1499. ->where('订单编号', $orderlist['订单编号'])
  1500. ->field('zdtotal, sctotal,颜色,颜色备注')
  1501. ->find();
  1502. } else {
  1503. $this->error('当前扫码小票无效');
  1504. }
  1505. // 获取当前手工机台上报的数据
  1506. $res = \db('设备_产量计酬')
  1507. ->where([
  1508. '子订单编号' => $paramArray[0],
  1509. '尺码' => $paramArray[3],
  1510. '数量' => $paramArray[4],
  1511. 'serial' => $paramArray[6],
  1512. 'sczl_jtbh' => $params['sys_sbID'],
  1513. 'mod_rq' => null,
  1514. ])
  1515. ->order('UniqId desc')
  1516. ->find();
  1517. if (empty($res)) {
  1518. $ci_num = $paramArray[4];
  1519. } else {
  1520. $ci_num = $res["ci_num"];
  1521. // // 累加 s_num
  1522. // $total_s_num = 0;
  1523. // foreach ($res as $item) {
  1524. // $total_s_num += $item['s_num'];
  1525. // }
  1526. // if ($total_s_num >= $paramArray[4]) {
  1527. // $ci_num = 0;
  1528. // } else {
  1529. // $ci_num = $res[0]['ci_num'];
  1530. // }
  1531. }
  1532. // 判断是否已报工并给出提示
  1533. // if (!empty($res) && $paramArray[4] == $res[0]['s_num'] || $ci_num === '' || $ci_num === 0) {
  1534. // $this->error('已上报,无需再次报工');
  1535. // }
  1536. // if (!empty($res) && $res["ci_num"] == 0) {
  1537. // $this->error('已上报,无需再次报工');
  1538. // }
  1539. $result = [
  1540. 'sys_sbID' => $params['sys_sbID'],//设备编号
  1541. 'ci_num' => $ci_num,//剩余数量
  1542. 'UniqId' => $Uniqid['UniqId'],
  1543. 'order' => $paramArray[0],//子订单编号
  1544. 'zb' => $params['sys_sbID'],//组别
  1545. 'gx' => 4,//默认固定工序
  1546. 'cm' => $paramArray[3],//尺码
  1547. 'sl' => $paramArray[4],//数量
  1548. 'wb' => $paramArray[5],//尾包
  1549. '订单编号' => $orderlist['订单编号'],
  1550. '生产款号' => $orderlist['生产款号'],
  1551. '款式' => $orderlist['款式'],
  1552. 'sctotal' => $yinjianlist['sctotal'],
  1553. 'zdtotal' => $yinjianlist['zdtotal'],
  1554. '颜色' => $yinjianlist['颜色备注'],
  1555. ];
  1556. // 查询历史记录
  1557. $existingRecords = \db('设备_产量计酬')->alias('c')
  1558. ->field('
  1559. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  1560. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1561. j.款式,
  1562. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1563. ')
  1564. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1565. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1566. ->where('c.子订单编号', $paramArray[0])
  1567. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1568. ->whereNull('c.mod_rq') // 查询未删除数据
  1569. ->where(function($query) {
  1570. $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
  1571. }) // 查询出库数据
  1572. ->order('c.UniqId', 'desc')
  1573. ->distinct('c.子订单编号')
  1574. ->select();
  1575. $sizeSummary = [];
  1576. $sizes = [];
  1577. foreach ($existingRecords as $record) {
  1578. $size = $record['尺码'];
  1579. $quantity = $record['数量'];
  1580. $serial = $record['serial'];
  1581. // 如果尺码还没有出现过,加入尺码列表
  1582. if (!in_array($size, $sizes)) {
  1583. $sizes[] = $size;
  1584. }
  1585. if (!isset($sizeSummary[$record['UniqId']])) {
  1586. $sizeSummary[$record['UniqId']] = [
  1587. 'UniqId' => $record['UniqId'],
  1588. '订单编号' => $record['订单编号'],
  1589. '子订单编号' => $record['子订单编号'],
  1590. '上报数量' => $record['s_num'],
  1591. '剩余数量' => $record['ci_num'],
  1592. '次品数量' => $record['数量'] - $record['ci_num'],
  1593. '生产款号' => $record['款号'],
  1594. '尺码' => $record['尺码'],
  1595. '工序编号' => $record['工序编号'],
  1596. '工序名称' => $record['工序名称'],
  1597. '数量' => $quantity,
  1598. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1599. '颜色' => $record['颜色备注'],
  1600. '组别' => $record['sczl_jtbh'],
  1601. '款式' => $record['款式'],
  1602. 'serial' => '第('.$serial.')包',
  1603. 'sys_rq' => $record['sys_rq']
  1604. ];
  1605. }
  1606. }
  1607. // 将 $sizeSummary 转换为索引数组
  1608. $sizeSummary = array_values($sizeSummary);
  1609. // 对 $sizeSummary 按 sys_rq 降序排序
  1610. usort($sizeSummary, function ($a, $b) {
  1611. return strtotime($b['UniqId']) - strtotime($a['UniqId']);
  1612. });
  1613. $this->success('请求成功', [
  1614. 'result' => $result,//小票数据
  1615. 'headers' => $sizes, // 尺码表头
  1616. 'records' => $sizeSummary // 历史记录
  1617. ]);
  1618. }else{
  1619. // 查询历史记录
  1620. $res = \db('设备_产量计酬')->alias('c')
  1621. ->field('
  1622. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  1623. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1624. j.款式,
  1625. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1626. ')
  1627. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1628. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1629. ->where('c.订单编号', 'like', '%' . explode('DC',$order)[1] . '%')
  1630. ->where('c.工序名称', $params['code'])
  1631. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1632. ->whereNull('c.mod_rq') // 查询未删除数据
  1633. ->order('UniqId desc')
  1634. ->select();
  1635. // 初始化返回数据的结构
  1636. $reslist = [];
  1637. $sizes = [];
  1638. // 遍历查询结果,构造需要的结构化数据
  1639. foreach ($res as $record) {
  1640. $size = $record['尺码']; // 记录的尺码
  1641. $quantity = $record['数量']; // 每个记录的数量
  1642. $serial = $record['serial']; // 记录的serial
  1643. // 记录所有不同的尺码,避免重复
  1644. if (!in_array($size, $sizes)) {
  1645. $sizes[] = $size;
  1646. }
  1647. // 构造每条记录的详细信息
  1648. $reslist[] = [
  1649. 'UniqId' => $record['UniqId'],
  1650. '订单编号' => $record['订单编号'],
  1651. '子订单编号' => $record['子订单编号'],
  1652. '上报数量' => $record['s_num'],
  1653. '次品数量' => $record['数量'] - $record['ci_num'],
  1654. '生产款号' => $record['款号'],
  1655. '尺码' => $record['尺码'],
  1656. '工序编号' => $record['工序编号'],
  1657. '工序名称' => $record['工序名称'],
  1658. '数量' => $quantity,
  1659. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1660. '颜色' => $record['颜色备注'],
  1661. '组别' => $record['sczl_jtbh'],
  1662. '款式' => $record['款式'],
  1663. 'serial' => '第('.$serial.')包',
  1664. 'sys_rq' => $record['sys_rq']
  1665. ];
  1666. }
  1667. // 按 sys_rq (时间) 或 UniqId 排序(按实际需求选)
  1668. usort($reslist, function ($a, $b) {
  1669. return strtotime($b['sys_rq']) - strtotime($a['sys_rq']);
  1670. });
  1671. // 返回结构化的查询结果
  1672. $this->success('请求成功', [
  1673. 'result' => $res, // 原始查询结果
  1674. 'headers' => $sizes, // 尺码表头
  1675. 'records' => $reslist // 处理后的记录列表
  1676. ]);
  1677. }
  1678. break;
  1679. default:
  1680. $this->error('无效的 code 参数');
  1681. break;
  1682. }
  1683. }
  1684. /**
  1685. * 出库报工日志
  1686. * 确认出库 恢复出库状态
  1687. */
  1688. public function getTab() {
  1689. if (!$this->request->isPost()) {
  1690. $this->error('非法请求');
  1691. }
  1692. $params = $this->request->param();
  1693. // 校验子订单编号和出库日期
  1694. if (empty($params['order_id'])) {
  1695. $this->error('子订单编号不能为空');
  1696. }
  1697. // 处理出库逻辑
  1698. if ($params['code'] == '出库') {
  1699. if (strpos($params['order_id'], '-') !== false) {
  1700. // 查询传入的子订单编号的记录,获取其颜色备注
  1701. $cklist = \db('工单_印件资料')
  1702. ->field('子订单编号, ck_rq, 颜色备注')
  1703. ->where('子订单编号', $params['order_id'])
  1704. ->find();
  1705. if ($cklist) {
  1706. if (!empty($cklist['ck_rq'])) {
  1707. $this->error('该子订单已完成出库操作,请勿重复操作。');
  1708. } else {
  1709. // 获取相同颜色备注的所有子订单记录
  1710. $sameColorList = \db('工单_印件资料')
  1711. ->field('子订单编号, ck_rq')
  1712. ->where('颜色备注', $cklist['颜色备注'])
  1713. ->whereNull('ck_rq') // 只选择未出库的记录
  1714. ->select();
  1715. // 开始出库操作
  1716. $updateCount1 = 0;
  1717. foreach ($sameColorList as $item) {
  1718. // 更新 '工单_印件资料' 表中所有相同颜色备注的子订单的出库日期
  1719. $result1 = \db('工单_印件资料')
  1720. ->where('子订单编号', $item['子订单编号'])
  1721. ->update(['ck_rq' => date("Y-m-d H:i:s")]);
  1722. if ($result1 !== false) {
  1723. $updateCount1 += $result1;
  1724. }
  1725. }
  1726. // 检查 '工单_基本资料' 表的 '出库日期' 是否为空
  1727. $order = explode('-', $params['order_id'])[0];
  1728. $basicInfo = \db('工单_基本资料')
  1729. ->field('出库日期')
  1730. ->where('订单编号', $order)
  1731. ->find();
  1732. $updateCount2 = 0;
  1733. if (empty($basicInfo['出库日期'])) {
  1734. // 如果 '出库日期' 为空,则更新
  1735. $jbzl = \db('工单_基本资料')
  1736. ->where('订单编号', $order)
  1737. ->fetchSql(true)
  1738. ->update(['出库日期' => date("Y-m-d H:i:s")]);
  1739. $result2 = \db()->query($jbzl);
  1740. if ($result2 !== false) {
  1741. $updateCount2 = $result2;
  1742. }
  1743. }
  1744. if ($updateCount1 > 0) {
  1745. // 插入日志
  1746. foreach ($sameColorList as $item) {
  1747. $addlist = [
  1748. 'order_id' => $item['子订单编号'],
  1749. 'rq' => date("Y-m-d H:i:s"),
  1750. 'name' => $params['code'],
  1751. 'sys_id' => $params['sys_id']
  1752. ];
  1753. \db('设备_报工日志')->insertGetId($addlist);
  1754. }
  1755. $this->success("出库成功");
  1756. } else {
  1757. $this->error('出库失败');
  1758. }
  1759. }
  1760. } else {
  1761. $this->error('未找到对应的出库订单');
  1762. }
  1763. } else {
  1764. $this->error('请扫描子订单编号进行出库');
  1765. }
  1766. }
  1767. // // 处理出库逻辑
  1768. // if ($params['code'] == '出库') {
  1769. // if (strpos($params['order_id'], '-') !== false) {
  1770. // $cklist = \db('工单_印件资料')
  1771. // ->field('子订单编号, ck_rq,颜色备注')
  1772. // ->where('子订单编号', $params['order_id'])
  1773. // ->find();
  1774. //
  1775. // if ($cklist) {
  1776. // if (!empty($cklist['ck_rq'])) {
  1777. // $this->error('该子订单已完成出库操作,请勿重复操作。');
  1778. // } else {
  1779. // // 更新 '工单_印件资料' 表
  1780. // $result1 = \db('工单_印件资料')
  1781. // ->where('子订单编号', $params['order_id'])
  1782. // ->fetchSql(true)
  1783. // ->update(['ck_rq' => date("Y-m-d 00:00:00")]);
  1784. // $updateCount1 = \db()->query($result1);
  1785. //
  1786. // // 更新 '工单_基本资料' 表
  1787. // $order = explode('-', $params['order_id'])[0];
  1788. // $result2 = \db('工单_基本资料')
  1789. // ->where('订单编号', $order)
  1790. // ->fetchSql(true)
  1791. // ->update(['出库日期' => date("Y-m-d 00:00:00"),'gd_statu'=>'2-生产中']);
  1792. // $updateCount2 = \db()->query($result2);
  1793. //
  1794. // if ($updateCount1 > 0 && $updateCount2 > 0) {
  1795. // // 插入日志
  1796. // $addlist = [
  1797. // 'order_id' => $params['order_id'],
  1798. // 'rq' => date("Y-m-d H:i:s"),
  1799. // 'name' => $params['code'],
  1800. // 'sys_id' => $params['sys_id']
  1801. // ];
  1802. // \db('设备_报工日志')->insertGetId($addlist);
  1803. // $this->success($params['order_id'] . "已出库");
  1804. // } else {
  1805. // $this->error('出库失败');
  1806. // }
  1807. // }
  1808. // } else {
  1809. // $this->error('未找到对应的子订单');
  1810. // }
  1811. // } else {
  1812. // $this->error('请扫描子订单编号进行出库');
  1813. // }
  1814. // }
  1815. // 处理恢复逻辑
  1816. else if ($params['code'] == '恢复') {
  1817. $cklist = \db('工单_印件资料')
  1818. ->field('子订单编号, ck_rq')
  1819. ->where('子订单编号', $params['order_id'])
  1820. ->find();
  1821. if (!empty($cklist['ck_rq'])) {
  1822. $result1 = \db('工单_印件资料')
  1823. ->where('子订单编号', $params['order_id'])
  1824. ->fetchSql(true)
  1825. ->update(['ck_rq' => null]);
  1826. $updateCount1 = \db()->query($result1);
  1827. // 更新 '工单_基本资料' 表
  1828. $order = explode('-', $params['order_id'])[0];
  1829. $result2 = \db('工单_基本资料')
  1830. ->where('订单编号', $order)
  1831. ->fetchSql(true)
  1832. ->update(['出库日期' => null,'gd_statu'=>'1-计划中']);
  1833. $updateCount2 = \db()->query($result2);
  1834. if ($updateCount1 > 0 && $updateCount2 > 0) {
  1835. // 插入日志
  1836. $addlist = [
  1837. 'order_id' => $params['order_id'],
  1838. 'rq' => date("Y-m-d H:i:s"),
  1839. 'name' => $params['code'],
  1840. 'sys_id' => $params['sys_id']
  1841. ];
  1842. \db('设备_报工日志')->insertGetId($addlist);
  1843. $this->success($params['order_id'] . "恢复未出库");
  1844. } else {
  1845. $this->error($params['order_id'] . '该子订单是未出库状态,无需再次恢复');
  1846. }
  1847. } else {
  1848. $this->error('该子订单是未出库状态,无需再次恢复');
  1849. }
  1850. }
  1851. }
  1852. /**
  1853. * 裁剪完工报工修改工单_印件资料表一条数据
  1854. *
  1855. */
  1856. public function getInfo(){
  1857. if (!$this->request->isPost()) {
  1858. $this->error('非法请求');
  1859. }
  1860. $params = $this->request->param();
  1861. // 检查子订单编号和日期是否都存在
  1862. if (!isset($params['order_id']) || empty($params['order_id'])) {
  1863. $this->error('参数错误,子订单编号不能为空');
  1864. }
  1865. if (!isset($params['rq']) || empty($params['rq'])) {
  1866. $this->error('参数错误,日期不能为空');
  1867. }
  1868. function setIfNotEmpty($value) {
  1869. return (!empty($value) && $value != 0) ? $value : '';
  1870. }
  1871. // 先查询当前记录的原始数据
  1872. $existingData = \db('工单_印件资料')
  1873. ->where('子订单编号', $params['order_id'])
  1874. ->find();
  1875. if ($existingData) {
  1876. // 计算新的累加值
  1877. $updateData = [
  1878. 'sc_rq' => date('Y-m-d H:i:s'),
  1879. 'scsl1' => isset($params['scsl1']) && is_numeric($params['scsl1']) ? $existingData['scsl1'] + $params['scsl1'] : $existingData['scsl1'] + 0,
  1880. 'scsl2' => isset($params['scsl2']) && is_numeric($params['scsl2']) ? $existingData['scsl2'] + $params['scsl2'] : $existingData['scsl2'] + 0,
  1881. 'scsl3' => isset($params['scsl3']) && is_numeric($params['scsl3']) ? $existingData['scsl3'] + $params['scsl3'] : $existingData['scsl3'] + 0,
  1882. 'scsl4' => isset($params['scsl4']) && is_numeric($params['scsl4']) ? $existingData['scsl4'] + $params['scsl4'] : $existingData['scsl4'] + 0,
  1883. 'scsl5' => isset($params['scsl5']) && is_numeric($params['scsl5']) ? $existingData['scsl5'] + $params['scsl5'] : $existingData['scsl5'] + 0,
  1884. 'scsl6' => isset($params['scsl6']) && is_numeric($params['scsl6']) ? $existingData['scsl6'] + $params['scsl6'] : $existingData['scsl6'] + 0,
  1885. 'scsl7' => isset($params['scsl7']) && is_numeric($params['scsl7']) ? $existingData['scsl7'] + $params['scsl7'] : $existingData['scsl7'] + 0,
  1886. 'scsl8' => isset($params['scsl8']) && is_numeric($params['scsl8']) ? $existingData['scsl8'] + $params['scsl8'] : $existingData['scsl8'] + 0,
  1887. 'scsl9' => isset($params['scsl9']) && is_numeric($params['scsl9']) ? $existingData['scsl9'] + $params['scsl9'] : $existingData['scsl9'] + 0,
  1888. 'scsl10' => isset($params['scsl10']) && is_numeric($params['scsl10']) ? $existingData['scsl10'] + $params['scsl10'] : $existingData['scsl10'] + 0,
  1889. 'sctotal' => isset($params['sctotal']) && is_numeric($params['sctotal']) ? $existingData['sctotal'] + $params['sctotal'] : $existingData['sctotal'] + 0,
  1890. // 你可以在这里继续添加其他字段
  1891. ];
  1892. $updateCount = \db('工单_印件资料')
  1893. ->where('子订单编号', $params['order_id'])
  1894. ->where('UniqId', $params['UniqId'])
  1895. ->update($updateData);
  1896. } else {
  1897. echo "没有找到相关记录";
  1898. }
  1899. // 检查更新结果
  1900. if ($updateCount > 0) {
  1901. $this->success("更新成功");
  1902. } else {
  1903. // 查询数据库,是否能找到对应的子订单编号
  1904. $existingRecord = \db('工单_印件资料')
  1905. ->where('子订单编号', $params['order_id'])
  1906. ->where('UniqId', $params['UniqId'])
  1907. ->find();
  1908. if ($existingRecord) {
  1909. $this->error('更新失败,可能是提供的更新数据与现有数据相同,未进行任何更改');
  1910. } else {
  1911. $this->error('更新失败,未找到对应的子订单编号');
  1912. }
  1913. }
  1914. }
  1915. /**
  1916. * 手工、大烫、总检、包装完工报工【通用的报工操作函数】
  1917. */
  1918. // function reportWork($params) {
  1919. // $bglist = \db('设备_产量计酬')->field('订单编号,子订单编号,数量,ci_num,款号,sys_rq,serial,serial_num')->where('UniqId', $params['UniqId'])->fetchSql(true)->find();
  1920. // $list_gd = \db()->query($bglist);
  1921. //
  1922. // $orderlist = \db('工单_基本资料')->where('订单编号', $list_gd[0]['订单编号'])->find();
  1923. // echo "<pre>";
  1924. // print_r($orderlist);
  1925. // echo "<pre>";die;
  1926. // $addorder['工单入仓数量'] = $params['sl'];
  1927. //
  1928. // $orderlist = \db('工单_基本资料')->where('订单编号', $list_gd[0]['订单编号'])->update($addorder);
  1929. //
  1930. // // 构建单条记录的函数,传递 $list_gd 作为参数
  1931. // function buildRecords($item, $list_gd) {
  1932. // return [
  1933. // '订单编号' => $item['order'],
  1934. // '子订单编号' => $item['order_id'],
  1935. // '款号' => $list_gd[0]['款号'],
  1936. // 'serial' => $list_gd[0]['serial'],
  1937. // 'serial_num' => $list_gd[0]['serial_num'],
  1938. // '工序编号' => $item['gx'],
  1939. // '工序名称' => $item['gxmc'],
  1940. // '尺码' => $item['cm'],
  1941. // '数量' => $item['sl'],
  1942. // 'sczl_jtbh' => $item['sczl_jtbh'],
  1943. // 'sczl_bh' => $item['sczl_bh'],
  1944. // '尾包' => $item['wb'],
  1945. // 'sys_id' => $item['sys_id'],
  1946. // 'sys_rq' => date('Y-m-d H:i:s'),
  1947. // 'sczl_rq' => date('Y-m-d'),
  1948. // 'ci_num' => $item['ci_num'] - $item['s_num'],
  1949. // 's_num' => $item['s_num'],
  1950. // ];
  1951. // }
  1952. // // 检查是否是索引数组(多条记录)
  1953. // if (isset($params[0]) && is_array($params[0])) {
  1954. // foreach ($params as $item) {
  1955. // $list[] = buildRecords($item, $list_gd);
  1956. // }
  1957. // } else {
  1958. // //处理单条记录
  1959. // $list[] = buildRecords($params, $list_gd);
  1960. // }
  1961. // $result = \db('设备_产量计酬')->insertAll($list);
  1962. // if ($result) {
  1963. // $this->success('数据插入成功 : ' . date('H:i:s'));
  1964. // } else {
  1965. // $this->error('数据插入失败');
  1966. // }
  1967. // }
  1968. function reportWork($params) {
  1969. // 查询设备_产量计酬表的数据
  1970. $bglist = \db('设备_产量计酬')->field('订单编号,子订单编号,数量,ci_num,款号,sys_rq,serial,serial_num')
  1971. ->where('UniqId', $params['UniqId'])->fetchSql(true)->find();
  1972. $list_gd = \db()->query($bglist);
  1973. // 查询工单基本资料表的数据
  1974. $orderlist = \db('工单_基本资料')->where('订单编号', $list_gd[0]['订单编号'])->find();
  1975. // 检查code是否为 "包装"
  1976. if ($params['code'] == '包装') {
  1977. // 如果是 "包装",则增加工单基本资料表中的入仓数量
  1978. $addorder['工单入仓数量'] = $orderlist['工单入仓数量'] + $params['sl'];
  1979. $result = \db('工单_基本资料')->where('订单编号', $list_gd[0]['订单编号'])->fetchSql(true)->update($addorder);
  1980. $sql = \db()->query($result);
  1981. }
  1982. // 构建单条记录的函数,传递 $list_gd 作为参数
  1983. function buildRecords($item, $list_gd) {
  1984. return [
  1985. '订单编号' => $item['order'],
  1986. '子订单编号' => $item['order_id'],
  1987. '款号' => $list_gd[0]['款号'],
  1988. 'serial' => $list_gd[0]['serial'],
  1989. 'serial_num' => $list_gd[0]['serial_num'],
  1990. '工序编号' => $item['gx'],
  1991. '工序名称' => $item['gxmc'],
  1992. '尺码' => $item['cm'],
  1993. '数量' => $item['sl'],
  1994. 'sczl_jtbh' => $item['sczl_jtbh'],
  1995. 'sczl_bh' => $item['sczl_bh'],
  1996. '尾包' => $item['wb'],
  1997. 'sys_id' => $item['sys_id'],
  1998. 'sys_rq' => date('Y-m-d H:i:s'),
  1999. 'sczl_rq' => date('Y-m-d'),
  2000. 'ci_num' => $item['ci_num'] - $item['s_num'],
  2001. 's_num' => $item['s_num'],
  2002. ];
  2003. }
  2004. // 检查是否是索引数组(多条记录)
  2005. if (isset($params[0]) && is_array($params[0])) {
  2006. foreach ($params as $item) {
  2007. $list[] = buildRecords($item, $list_gd);
  2008. }
  2009. } else {
  2010. // 处理单条记录
  2011. $list[] = buildRecords($params, $list_gd);
  2012. }
  2013. // 插入数据到设备_产量计酬表
  2014. $result = \db('设备_产量计酬')->insertAll($list);
  2015. if ($result) {
  2016. $this->success('数据插入成功 : ' . date('H:i:s'));
  2017. } else {
  2018. $this->error('数据插入失败');
  2019. }
  2020. }
  2021. /**
  2022. * 裁剪新增、车缝新增、手工新增、大烫新增、总检新增、包装新增
  2023. * /work_order_spot_check/search
  2024. */
  2025. public function search(){
  2026. if (!$this->request->isPost()) {
  2027. $this->error('非法请求');
  2028. }
  2029. $params = $this->request->param();
  2030. // 根据不同的 code 值执行不同的逻辑
  2031. switch ($params['code']) {
  2032. case '裁剪':
  2033. $order_id = explode('-', $params['order_id']);
  2034. $order = $order_id[0];
  2035. $sql = \db('工单_基本资料')->where('订单编号', $order)->fetchSql(true)->find();
  2036. $order_list = \db()->query($sql);
  2037. if (empty($order_list)) {
  2038. $this->error('没有找到对应的订单信息');
  2039. return;
  2040. }
  2041. $insertData = [];
  2042. $dataList = isset($params[0]) && is_array($params[0]) ? $params : [$params];
  2043. foreach ($dataList as $item) {
  2044. $list = [
  2045. '订单编号' => $order_list[0]['订单编号'],
  2046. '款号' => $order_list[0]['生产款号'],
  2047. '子订单编号' => $item['order_id'],
  2048. '工序编号' => '2',
  2049. '工序名称' => '裁剪',
  2050. '尺码' => $item['cm'],
  2051. '数量' => $item['sl'],
  2052. 'sczl_jtbh' => $item['sczl_jtbh'],
  2053. 'sys_id' => $item['sys_id'],
  2054. 'sczl_rq' => date('Y-m-d 00:00:00'),
  2055. 'sys_rq' => date('Y-m-d H:i:s')
  2056. ];
  2057. $insertData[] = $list;
  2058. }
  2059. $result = \db('设备_产量计酬')->insertAll($insertData);
  2060. $result ? $this->success('数据批量插入成功 : ' . date('H:i:s')) : $this->error('数据插入失败');
  2061. break;
  2062. case '车缝':
  2063. $list = [];
  2064. $order_id = explode('-', $params['order_id']);
  2065. $order = $order_id[0];
  2066. $sql = \db('工单_基本资料')->where('订单编号', $order)->fetchSql(true)->find();
  2067. $list_gd = \db()->query($sql); // 查询的结果可能是多条记录,确保是数组
  2068. // 查询子订单编号对应的最大 serial 值
  2069. $list_order_id = \db('设备_产量计酬')
  2070. ->field('子订单编号, MAX(serial) as serial')
  2071. ->where('子订单编号', $params['order_id'])
  2072. ->where(function($query) {
  2073. $query->whereNull('mod_rq')->whereOr('mod_rq', '');
  2074. }) // 查询未删除数据
  2075. ->group('子订单编号')
  2076. ->find();
  2077. function buildRecord($item, $list_gd, $list_order_id) {
  2078. return [
  2079. '订单编号' => $list_gd[0]['订单编号'],
  2080. '款号' => $list_gd[0]['生产款号'],
  2081. '子订单编号' => $item['order_id'],
  2082. '工序编号' => 3,
  2083. '工序名称' => '车缝',
  2084. 'serial' => $item['serial'],
  2085. 'ci_num' => $item['sl'],
  2086. 'serial_num' => $item['serial_num'],
  2087. '尺码' => $item['cm'],
  2088. '数量' => $item['sl'],
  2089. 'sczl_jtbh' => $item['sczl_jtbh'],
  2090. 'sys_rq' => date('Y-m-d H:i:s'),
  2091. 'sczl_rq' => date('Y-m-d'),
  2092. '尾包' => $item['wb'],
  2093. 'sys_id' => $item['sys_id'],
  2094. 'sczl_bh' => $item['sczl_bh']
  2095. ];
  2096. }
  2097. if (isset($params[0]) && is_array($params[0])) {
  2098. foreach ($params as $item) {
  2099. $list[] = buildRecord($item, $list_gd, $list_order_id); // 所有记录使用相同的 serial
  2100. }
  2101. } else {
  2102. // 处理单条记录
  2103. $list[] = buildRecord($params, $list_gd, $list_order_id); // 单条记录
  2104. }
  2105. $result = \db('设备_产量计酬')->insertAll($list);
  2106. if ($result) {
  2107. $this->success('数据插入成功 : ' . date('H:i:s'));
  2108. } else {
  2109. $this->error('数据插入失败');
  2110. }
  2111. break;
  2112. case '手工':
  2113. case '大烫':
  2114. case '总检':
  2115. case '包装':
  2116. //调用reportWork方法名函数
  2117. $this->reportWork($params);
  2118. break;
  2119. default:
  2120. $this->error('无效的 code 参数');
  2121. break;
  2122. }
  2123. }
  2124. /**
  2125. * 物理地址获取机台编号
  2126. * /work_order_spot_check/getMachineMac
  2127. */
  2128. public function getMachineMac(){
  2129. if ($this->request->isGet() == false){$this->error('非法请求');}
  2130. $params = $this->request->get();
  2131. if (empty($params['sys_sbID'])){$this->error('物理地址参数不能为空');}
  2132. $data = \db('设备_基本资料')->where('sys_sbID', $params['sys_sbID'])
  2133. ->field('设备编号 as 机台号,生产工序,工序,rtrim(设备编组) as 组别,组长,sys_sbID')
  2134. // ->where('生产工序',$params['code'])
  2135. ->find();
  2136. $this->success('请求成功', $data);
  2137. }
  2138. /**
  2139. * 删除报工记录
  2140. * /work_order_spot_check/del
  2141. */
  2142. public function del() {
  2143. if (!$this->request->isGet()) {
  2144. return $this->error('请求错误');
  2145. }
  2146. $param = $this->request->param();
  2147. if (empty($param['UniqId'])) {
  2148. return $this->error('UniqId参数错误');
  2149. }
  2150. // 将 $param['UniqId'] 的值按逗号分割成数组
  2151. $printIdArray = explode(',', $param['UniqId']);
  2152. // 初始化失败计数器
  2153. $failureCount = 0;
  2154. foreach ($printIdArray as $uniqId) {
  2155. // 根据 UniqId 查询 设备_产量计酬 表中的订单编号、尺码和数量
  2156. $result = \db('设备_产量计酬')->where('UniqId', $uniqId)
  2157. ->field('订单编号,尺码,数量')
  2158. ->find();
  2159. // 检查是否找到结果
  2160. if (!$result) {
  2161. $failureCount++;
  2162. continue; // 跳过当前循环,处理下一个 UniqId
  2163. }
  2164. // 根据订单编号查询工单_印件资料表,并排除有修改日期的记录
  2165. $yresult = \db('工单_印件资料')->where('订单编号', $result['订单编号'])
  2166. ->whereNull('Mod_rq')
  2167. ->find();
  2168. // 检查是否找到结果
  2169. if (!$yresult) {
  2170. $failureCount++;
  2171. continue; // 跳过当前循环,处理下一个 UniqId
  2172. }
  2173. // 获取 result 中的尺码和数量
  2174. $size = $result['尺码'];
  2175. $quantity = $result['数量'];
  2176. // 遍历 yresult 中的 cm1 到 cm10 字段,查找匹配的尺码
  2177. for ($i = 1; $i <= 10; $i++) {
  2178. $cmField = 'cm' . $i;
  2179. $scslField = 'scsl' . $i;
  2180. // 检查 yresult 中是否存在对应的尺码字段,且值等于 result 中的尺码
  2181. if (isset($yresult[$cmField]) && $yresult[$cmField] === $size) {
  2182. // 找到对应 scsl 字段并进行数量减法
  2183. if (isset($yresult[$scslField])) {
  2184. $yresult[$scslField] -= $quantity;
  2185. // 防止数量变成负数
  2186. if ($yresult[$scslField] < 0) {
  2187. $yresult[$scslField] = 0;
  2188. }
  2189. // 更新数据库中的 scsl 字段
  2190. \db('工单_印件资料')
  2191. ->where('订单编号', $result['订单编号'])
  2192. ->update([$scslField => $yresult[$scslField]]);
  2193. }
  2194. break; // 找到并处理后,跳出循环
  2195. }
  2196. }
  2197. // 重新计算 scsl1 到 scsl10 的总和并更新 sctotal 字段
  2198. $newTotal = 0;
  2199. for ($i = 1; $i <= 10; $i++) {
  2200. $scslField = 'scsl' . $i;
  2201. if (isset($yresult[$scslField])) {
  2202. $newTotal += $yresult[$scslField];
  2203. }
  2204. }
  2205. // 更新 sctotal 的最新值到数据库
  2206. \db('工单_印件资料')
  2207. ->where('订单编号', $result['订单编号'])
  2208. ->update(['sctotal' => $newTotal]);
  2209. // 更新设备_产量计酬表的mod_rq和delsys_id字段
  2210. $updateData = [
  2211. 'mod_rq' => date('Y-m-d H:i:s'),
  2212. 'delsys_id' => $param['sys_id']
  2213. ];
  2214. $result = \db('设备_产量计酬')->where('UniqId', $uniqId)->fetchSql(true)->update($updateData);
  2215. $sql = \db()->query($result);
  2216. // 检查更新是否成功
  2217. if ($sql === false) {
  2218. $failureCount++;
  2219. }
  2220. }
  2221. // 如果没有失败记录,则表示删除成功
  2222. if ($failureCount === 0) {
  2223. return $this->success('删除成功');
  2224. } else {
  2225. return $this->error("操作完成,但有 {$failureCount} 条记录更新失败");
  2226. }
  2227. }
  2228. }