WorkOrderSpotCheck.php 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258
  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. }
  870. $result = [
  871. 'sys_sbID' => $params['sys_sbID'],//设备编号
  872. 'ci_num' => $ci_num,//剩余数量
  873. 'UniqId' => $Uniqid['UniqId'],
  874. 'order' => $paramArray[0],//子订单编号
  875. 'zb' => $params['sys_sbID'],//组别
  876. 'gx' => 4,//默认固定工序
  877. 'cm' => $paramArray[3],//尺码
  878. 'sl' => $paramArray[4],//数量
  879. 'wb' => $paramArray[5],//尾包
  880. '订单编号' => $orderlist['订单编号'],
  881. '生产款号' => $orderlist['生产款号'],
  882. '款式' => $orderlist['款式'],
  883. 'sctotal' => $yinjianlist['sctotal'],
  884. 'zdtotal' => $yinjianlist['zdtotal'],
  885. '颜色' => $yinjianlist['颜色备注'],
  886. ];
  887. // 查询历史记录
  888. $existingRecords = \db('设备_产量计酬')->alias('c')
  889. ->field('
  890. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  891. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  892. j.款式,
  893. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  894. ')
  895. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  896. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  897. ->where('c.子订单编号', $paramArray[0])
  898. ->where('c.工序名称', '手工')
  899. ->where('c.sczl_jtbh', $params['sys_sbID'])
  900. ->whereNull('c.mod_rq') // 查询未删除数据
  901. ->where(function($query) {
  902. $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
  903. }) // 查询出库数据
  904. ->order('c.UniqId', 'desc')
  905. ->distinct('c.子订单编号')
  906. ->select();
  907. $sizeSummary = [];
  908. $sizes = [];
  909. foreach ($existingRecords as $record) {
  910. $size = $record['尺码'];
  911. $quantity = $record['数量'];
  912. $serial = $record['serial'];
  913. // 如果尺码还没有出现过,加入尺码列表
  914. if (!in_array($size, $sizes)) {
  915. $sizes[] = $size;
  916. }
  917. if (!isset($sizeSummary[$record['UniqId']])) {
  918. $sizeSummary[$record['UniqId']] = [
  919. 'UniqId' => $record['UniqId'],
  920. '订单编号' => $record['订单编号'],
  921. '子订单编号' => $record['子订单编号'],
  922. '上报数量' => $record['s_num'],
  923. '剩余数量' => $record['ci_num'],
  924. '次品数量' => $record['数量'] - $record['ci_num'],
  925. '生产款号' => $record['款号'],
  926. '尺码' => $record['尺码'],
  927. '工序编号' => $record['工序编号'],
  928. '工序名称' => $record['工序名称'],
  929. '数量' => $quantity,
  930. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  931. '颜色' => $record['颜色备注'],
  932. '组别' => $record['sczl_jtbh'],
  933. '款式' => $record['款式'],
  934. 'serial' => '第('.$serial.')包',
  935. 'sys_rq' => $record['sys_rq']
  936. ];
  937. }
  938. }
  939. // 将 $sizeSummary 转换为索引数组
  940. $sizeSummary = array_values($sizeSummary);
  941. // 对 $sizeSummary 按 sys_rq 降序排序
  942. usort($sizeSummary, function ($a, $b) {
  943. return strtotime($b['UniqId']) - strtotime($a['UniqId']);
  944. });
  945. $this->success('请求成功', [
  946. 'result' => $result,//小票数据
  947. 'headers' => $sizes, // 尺码表头
  948. 'records' => $sizeSummary // 历史记录
  949. ]);
  950. }else{
  951. // 查询历史记录
  952. $res = \db('设备_产量计酬')->alias('c')
  953. ->field('
  954. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  955. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  956. j.款式,
  957. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  958. ')
  959. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  960. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  961. ->where('c.订单编号', 'like', '%' . explode('DC',$order)[1] . '%')
  962. ->where('c.工序名称', $params['code'])
  963. ->where('c.sczl_jtbh', $params['sys_sbID'])
  964. ->whereNull('c.mod_rq') // 查询未删除数据
  965. ->order('UniqId desc')
  966. ->select();
  967. // 初始化返回数据的结构
  968. $reslist = [];
  969. $sizes = [];
  970. // 遍历查询结果,构造需要的结构化数据
  971. foreach ($res as $record) {
  972. $size = $record['尺码']; // 记录的尺码
  973. $quantity = $record['数量']; // 每个记录的数量
  974. $serial = $record['serial']; // 记录的serial
  975. // 记录所有不同的尺码,避免重复
  976. if (!in_array($size, $sizes)) {
  977. $sizes[] = $size;
  978. }
  979. // 构造每条记录的详细信息
  980. $reslist[] = [
  981. 'UniqId' => $record['UniqId'],
  982. '订单编号' => $record['订单编号'],
  983. '子订单编号' => $record['子订单编号'],
  984. '上报数量' => $record['s_num'],
  985. '剩余数量' => $record['ci_num'],
  986. '次品数量' => $record['数量'] - $record['ci_num'],
  987. '生产款号' => $record['款号'],
  988. '尺码' => $record['尺码'],
  989. '工序编号' => $record['工序编号'],
  990. '工序名称' => $record['工序名称'],
  991. '数量' => $quantity,
  992. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  993. '颜色' => $record['颜色备注'],
  994. '组别' => $record['sczl_jtbh'],
  995. '款式' => $record['款式'],
  996. 'serial' => '第('.$serial.')包',
  997. 'sys_rq' => $record['sys_rq']
  998. ];
  999. }
  1000. // 按 sys_rq (时间) 或 UniqId 排序(按实际需求选)
  1001. usort($reslist, function ($a, $b) {
  1002. return strtotime($b['sys_rq']) - strtotime($a['sys_rq']);
  1003. });
  1004. // 返回结构化的查询结果
  1005. $this->success('请求成功', [
  1006. 'result' => $res, // 原始查询结果
  1007. 'headers' => $sizes, // 尺码表头
  1008. 'records' => $reslist // 处理后的记录列表
  1009. ]);
  1010. }
  1011. break;
  1012. case '大烫':
  1013. if (strpos($order, ',') !== false) {
  1014. if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
  1015. $this->error('未获取到机台号');
  1016. }
  1017. // 将子订单编号, 机台号, 工序, 尺码, 数量, 尾包, 流水号(包)分割为数组
  1018. $paramArray = explode(',', $order);
  1019. // 获取车缝报工生成小票的数据
  1020. $Uniqid = \db('设备_产量计酬')
  1021. ->where([
  1022. '子订单编号' => $paramArray[0],
  1023. '尺码' => $paramArray[3],
  1024. '数量' => $paramArray[4],
  1025. 'serial' => $paramArray[6],
  1026. ])
  1027. ->find();
  1028. // 获取上一个机台是否已上报过
  1029. $baogonglist = \db('设备_产量计酬')
  1030. ->where([
  1031. '子订单编号' => $paramArray[0],
  1032. '尺码' => $paramArray[3],
  1033. '数量' => $paramArray[4],
  1034. 'serial' => $paramArray[6],
  1035. 'Mod_rq' => null,
  1036. ])
  1037. ->where('sczl_jtbh', 'like', 'HD%')
  1038. ->select();
  1039. if(empty($baogonglist)){
  1040. $this->error('当前小票'.'【手工报工】'.'未上报,请上报后再操作');
  1041. }
  1042. // 判断是否成功获取到数据
  1043. if ($Uniqid) {
  1044. $orderlist = \db('工单_基本资料')
  1045. ->where('订单编号', $Uniqid['订单编号'])
  1046. ->field('订单编号, 生产款号, 款式')
  1047. ->find();
  1048. $yinjianlist = \db('工单_印件资料')
  1049. ->where('订单编号', $orderlist['订单编号'])
  1050. ->field('zdtotal, sctotal,颜色,颜色备注')
  1051. ->find();
  1052. } else {
  1053. $this->error('当前扫码小票无效');
  1054. }
  1055. // 获取当前手工机台上报的数据
  1056. $res = \db('设备_产量计酬')
  1057. ->where([
  1058. '子订单编号' => $paramArray[0],
  1059. '尺码' => $paramArray[3],
  1060. '数量' => $paramArray[4],
  1061. 'serial' => $paramArray[6],
  1062. 'sczl_jtbh' => $params['sys_sbID'],
  1063. 'mod_rq' => null,
  1064. ])
  1065. ->order('UniqId desc')
  1066. ->find();
  1067. if (empty($res)) {
  1068. $ci_num = $paramArray[4];
  1069. } else {
  1070. $ci_num = $res["ci_num"];
  1071. // // 累加 s_num
  1072. // $total_s_num = 0;
  1073. // foreach ($res as $item) {
  1074. // $total_s_num += $item['s_num'];
  1075. // }
  1076. // if ($total_s_num >= $paramArray[4]) {
  1077. // $ci_num = 0;
  1078. // } else {
  1079. // $ci_num = $res[0]['ci_num'];
  1080. // }
  1081. }
  1082. // 判断是否已报工并给出提示
  1083. // if (!empty($res) && $paramArray[4] == $res[0]['s_num'] || $ci_num === '' || $ci_num === 0) {
  1084. // $this->error('已上报,无需再次报工');
  1085. // }
  1086. // if (!empty($res) && $res["ci_num"] == 0) {
  1087. // $this->success('已上报,无需再次报工');
  1088. // }
  1089. $result = [
  1090. 'sys_sbID' => $params['sys_sbID'],//设备编号
  1091. 'ci_num' => $ci_num,//剩余数量
  1092. 'UniqId' => $Uniqid['UniqId'],
  1093. 'order' => $paramArray[0],//子订单编号
  1094. 'zb' => $params['sys_sbID'],//组别
  1095. 'gx' => 4,//默认固定工序
  1096. 'cm' => $paramArray[3],//尺码
  1097. 'sl' => $paramArray[4],//数量
  1098. 'wb' => $paramArray[5],//尾包
  1099. '订单编号' => $orderlist['订单编号'],
  1100. '生产款号' => $orderlist['生产款号'],
  1101. '款式' => $orderlist['款式'],
  1102. 'sctotal' => $yinjianlist['sctotal'],
  1103. 'zdtotal' => $yinjianlist['zdtotal'],
  1104. '颜色' => $yinjianlist['颜色备注'],
  1105. ];
  1106. // 查询历史记录
  1107. $existingRecords = \db('设备_产量计酬')->alias('c')
  1108. ->field('
  1109. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  1110. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1111. j.款式,
  1112. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1113. ')
  1114. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1115. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1116. ->where('c.子订单编号', $paramArray[0])
  1117. ->where('c.工序名称', '大烫')
  1118. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1119. ->whereNull('c.mod_rq') // 查询未删除数据
  1120. ->where(function($query) {
  1121. $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
  1122. }) // 查询出库数据
  1123. ->order('c.UniqId', 'desc')
  1124. ->distinct('c.子订单编号')
  1125. ->select();
  1126. $sizeSummary = [];
  1127. $sizes = [];
  1128. foreach ($existingRecords as $record) {
  1129. $size = $record['尺码'];
  1130. $quantity = $record['数量'];
  1131. $serial = $record['serial'];
  1132. // 如果尺码还没有出现过,加入尺码列表
  1133. if (!in_array($size, $sizes)) {
  1134. $sizes[] = $size;
  1135. }
  1136. if (!isset($sizeSummary[$record['UniqId']])) {
  1137. $sizeSummary[$record['UniqId']] = [
  1138. 'UniqId' => $record['UniqId'],
  1139. '订单编号' => $record['订单编号'],
  1140. '子订单编号' => $record['子订单编号'],
  1141. '上报数量' => $record['s_num'],
  1142. '剩余数量' => $record['ci_num'],
  1143. '次品数量' => $record['数量'] - $record['ci_num'],
  1144. '生产款号' => $record['款号'],
  1145. '尺码' => $record['尺码'],
  1146. '工序编号' => $record['工序编号'],
  1147. '工序名称' => $record['工序名称'],
  1148. '数量' => $quantity,
  1149. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1150. '颜色' => $record['颜色备注'],
  1151. '组别' => $record['sczl_jtbh'],
  1152. '款式' => $record['款式'],
  1153. 'serial' => '第('.$serial.')包',
  1154. 'sys_rq' => $record['sys_rq']
  1155. ];
  1156. }
  1157. }
  1158. // 将 $sizeSummary 转换为索引数组
  1159. $sizeSummary = array_values($sizeSummary);
  1160. // 对 $sizeSummary 按 sys_rq 降序排序
  1161. usort($sizeSummary, function ($a, $b) {
  1162. return strtotime($b['UniqId']) - strtotime($a['UniqId']);
  1163. });
  1164. $this->success('请求成功', [
  1165. 'result' => $result,//小票数据
  1166. 'headers' => $sizes, // 尺码表头
  1167. 'records' => $sizeSummary // 历史记录
  1168. ]);
  1169. }else{
  1170. // 查询历史记录
  1171. $res = \db('设备_产量计酬')->alias('c')
  1172. ->field('
  1173. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  1174. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1175. j.款式,
  1176. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1177. ')
  1178. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1179. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1180. ->where('c.订单编号', 'like', '%' . explode('DC',$order)[1] . '%')
  1181. ->where('c.工序名称', $params['code'])
  1182. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1183. ->whereNull('c.mod_rq') // 查询未删除数据
  1184. ->order('UniqId desc')
  1185. ->select();
  1186. // 初始化返回数据的结构
  1187. $reslist = [];
  1188. $sizes = [];
  1189. // 遍历查询结果,构造需要的结构化数据
  1190. foreach ($res as $record) {
  1191. $size = $record['尺码']; // 记录的尺码
  1192. $quantity = $record['数量']; // 每个记录的数量
  1193. $serial = $record['serial']; // 记录的serial
  1194. // 记录所有不同的尺码,避免重复
  1195. if (!in_array($size, $sizes)) {
  1196. $sizes[] = $size;
  1197. }
  1198. // 构造每条记录的详细信息
  1199. $reslist[] = [
  1200. 'UniqId' => $record['UniqId'],
  1201. '订单编号' => $record['订单编号'],
  1202. '子订单编号' => $record['子订单编号'],
  1203. '上报数量' => $record['s_num'],
  1204. '剩余数量' => $record['ci_num'],
  1205. '次品数量' => $record['数量'] - $record['ci_num'],
  1206. '生产款号' => $record['款号'],
  1207. '尺码' => $record['尺码'],
  1208. '工序编号' => $record['工序编号'],
  1209. '工序名称' => $record['工序名称'],
  1210. '数量' => $quantity,
  1211. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1212. '颜色' => $record['颜色备注'],
  1213. '组别' => $record['sczl_jtbh'],
  1214. '款式' => $record['款式'],
  1215. 'serial' => '第('.$serial.')包',
  1216. 'sys_rq' => $record['sys_rq']
  1217. ];
  1218. }
  1219. // 按 sys_rq (时间) 或 UniqId 排序(按实际需求选)
  1220. usort($reslist, function ($a, $b) {
  1221. return strtotime($b['sys_rq']) - strtotime($a['sys_rq']);
  1222. });
  1223. // 返回结构化的查询结果
  1224. $this->success('请求成功', [
  1225. 'result' => $res, // 原始查询结果
  1226. 'headers' => $sizes, // 尺码表头
  1227. 'records' => $reslist // 处理后的记录列表
  1228. ]);
  1229. }
  1230. break;
  1231. case '总检':
  1232. if (strpos($order, ',') !== false) {
  1233. if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
  1234. $this->error('未获取到机台号');
  1235. }
  1236. // 将子订单编号, 机台号, 工序, 尺码, 数量, 尾包, 流水号(包)分割为数组
  1237. $paramArray = explode(',', $order);
  1238. // 获取车缝报工生成小票的数据
  1239. $Uniqid = \db('设备_产量计酬')
  1240. ->where([
  1241. '子订单编号' => $paramArray[0],
  1242. '尺码' => $paramArray[3],
  1243. '数量' => $paramArray[4],
  1244. 'serial' => $paramArray[6],
  1245. ])
  1246. ->find();
  1247. // 获取上一个机台是否已上报过
  1248. $baogonglist = \db('设备_产量计酬')
  1249. ->where([
  1250. '子订单编号' => $paramArray[0],
  1251. '尺码' => $paramArray[3],
  1252. '数量' => $paramArray[4],
  1253. 'serial' => $paramArray[6],
  1254. 'Mod_rq' => null,
  1255. ])
  1256. ->where('sczl_jtbh', 'like', 'DT%')
  1257. ->select();
  1258. if(empty($baogonglist)){
  1259. $this->error('当前小票'.'【大烫报工】'.'未上报,请上报后再操作');
  1260. }
  1261. // 判断是否成功获取到数据
  1262. if ($Uniqid) {
  1263. $orderlist = \db('工单_基本资料')
  1264. ->where('订单编号', $Uniqid['订单编号'])
  1265. ->field('订单编号, 生产款号, 款式')
  1266. ->find();
  1267. $yinjianlist = \db('工单_印件资料')
  1268. ->where('订单编号', $orderlist['订单编号'])
  1269. ->field('zdtotal, sctotal,颜色,颜色备注')
  1270. ->find();
  1271. } else {
  1272. $this->error('当前扫码小票无效');
  1273. }
  1274. // 获取当前手工机台上报的数据
  1275. $res = \db('设备_产量计酬')
  1276. ->where([
  1277. '子订单编号' => $paramArray[0],
  1278. '尺码' => $paramArray[3],
  1279. '数量' => $paramArray[4],
  1280. 'serial' => $paramArray[6],
  1281. 'sczl_jtbh' => $params['sys_sbID'],
  1282. 'mod_rq' => null,
  1283. ])
  1284. ->order('UniqId desc')
  1285. ->find();
  1286. if (empty($res)) {
  1287. $ci_num = $paramArray[4];
  1288. } else {
  1289. $ci_num = $res["ci_num"];
  1290. // // 累加 s_num
  1291. // $total_s_num = 0;
  1292. // foreach ($res as $item) {
  1293. // $total_s_num += $item['s_num'];
  1294. // }
  1295. // if ($total_s_num >= $paramArray[4]) {
  1296. // $ci_num = 0;
  1297. // } else {
  1298. // $ci_num = $res[0]['ci_num'];
  1299. // }
  1300. }
  1301. // 判断是否已报工并给出提示
  1302. // if (!empty($res) && $paramArray[4] == $res[0]['s_num'] || $ci_num === '' || $ci_num === 0) {
  1303. // $this->error('已上报,无需再次报工');
  1304. // }
  1305. // if (!empty($res) && $res["ci_num"] == 0) {
  1306. // $this->error('已上报,无需再次报工');
  1307. // }
  1308. $result = [
  1309. 'sys_sbID' => $params['sys_sbID'],//设备编号
  1310. 'ci_num' => $ci_num,//剩余数量
  1311. 'UniqId' => $Uniqid['UniqId'],
  1312. 'order' => $paramArray[0],//子订单编号
  1313. 'zb' => $params['sys_sbID'],//组别
  1314. 'gx' => 4,//默认固定工序
  1315. 'cm' => $paramArray[3],//尺码
  1316. 'sl' => $paramArray[4],//数量
  1317. 'wb' => $paramArray[5],//尾包
  1318. '订单编号' => $orderlist['订单编号'],
  1319. '生产款号' => $orderlist['生产款号'],
  1320. '款式' => $orderlist['款式'],
  1321. 'sctotal' => $yinjianlist['sctotal'],
  1322. 'zdtotal' => $yinjianlist['zdtotal'],
  1323. '颜色' => $yinjianlist['颜色备注'],
  1324. ];
  1325. // 查询历史记录
  1326. $existingRecords = \db('设备_产量计酬')->alias('c')
  1327. ->field('
  1328. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  1329. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1330. j.款式,
  1331. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1332. ')
  1333. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1334. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1335. ->where('c.子订单编号', $paramArray[0])
  1336. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1337. ->whereNull('c.mod_rq') // 查询未删除数据
  1338. ->where(function($query) {
  1339. $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
  1340. }) // 查询出库数据
  1341. ->order('c.UniqId', 'desc')
  1342. ->distinct('c.子订单编号')
  1343. ->select();
  1344. $sizeSummary = [];
  1345. $sizes = [];
  1346. foreach ($existingRecords as $record) {
  1347. $size = $record['尺码'];
  1348. $quantity = $record['数量'];
  1349. $serial = $record['serial'];
  1350. // 如果尺码还没有出现过,加入尺码列表
  1351. if (!in_array($size, $sizes)) {
  1352. $sizes[] = $size;
  1353. }
  1354. if (!isset($sizeSummary[$record['UniqId']])) {
  1355. $sizeSummary[$record['UniqId']] = [
  1356. 'UniqId' => $record['UniqId'],
  1357. '订单编号' => $record['订单编号'],
  1358. '子订单编号' => $record['子订单编号'],
  1359. '上报数量' => $record['s_num'],
  1360. '剩余数量' => $record['ci_num'],
  1361. '次品数量' => $record['数量'] - $record['ci_num'],
  1362. '生产款号' => $record['款号'],
  1363. '尺码' => $record['尺码'],
  1364. '工序编号' => $record['工序编号'],
  1365. '工序名称' => $record['工序名称'],
  1366. '数量' => $quantity,
  1367. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1368. '颜色' => $record['颜色备注'],
  1369. '组别' => $record['sczl_jtbh'],
  1370. '款式' => $record['款式'],
  1371. 'serial' => '第('.$serial.')包',
  1372. 'sys_rq' => $record['sys_rq']
  1373. ];
  1374. }
  1375. }
  1376. // 将 $sizeSummary 转换为索引数组
  1377. $sizeSummary = array_values($sizeSummary);
  1378. // 对 $sizeSummary 按 sys_rq 降序排序
  1379. usort($sizeSummary, function ($a, $b) {
  1380. return strtotime($b['UniqId']) - strtotime($a['UniqId']);
  1381. });
  1382. $this->success('请求成功', [
  1383. 'result' => $result,//小票数据
  1384. 'headers' => $sizes, // 尺码表头
  1385. 'records' => $sizeSummary // 历史记录
  1386. ]);
  1387. }else{
  1388. // 查询历史记录
  1389. $res = \db('设备_产量计酬')->alias('c')
  1390. ->field('
  1391. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  1392. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1393. j.款式,
  1394. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1395. ')
  1396. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1397. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1398. ->where('c.订单编号', 'like', '%' . explode('DC',$order)[1] . '%')
  1399. ->where('c.工序名称', $params['code'])
  1400. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1401. ->whereNull('c.mod_rq') // 查询未删除数据
  1402. ->order('UniqId desc')
  1403. ->select();
  1404. // 初始化返回数据的结构
  1405. $reslist = [];
  1406. $sizes = [];
  1407. // 遍历查询结果,构造需要的结构化数据
  1408. foreach ($res as $record) {
  1409. $size = $record['尺码']; // 记录的尺码
  1410. $quantity = $record['数量']; // 每个记录的数量
  1411. $serial = $record['serial']; // 记录的serial
  1412. // 记录所有不同的尺码,避免重复
  1413. if (!in_array($size, $sizes)) {
  1414. $sizes[] = $size;
  1415. }
  1416. // 构造每条记录的详细信息
  1417. $reslist[] = [
  1418. 'UniqId' => $record['UniqId'],
  1419. '订单编号' => $record['订单编号'],
  1420. '子订单编号' => $record['子订单编号'],
  1421. '上报数量' => $record['s_num'],
  1422. '次品数量' => $record['数量'] - $record['ci_num'],
  1423. '生产款号' => $record['款号'],
  1424. '尺码' => $record['尺码'],
  1425. '工序编号' => $record['工序编号'],
  1426. '工序名称' => $record['工序名称'],
  1427. '数量' => $quantity,
  1428. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1429. '颜色' => $record['颜色备注'],
  1430. '组别' => $record['sczl_jtbh'],
  1431. '款式' => $record['款式'],
  1432. 'serial' => '第('.$serial.')包',
  1433. 'sys_rq' => $record['sys_rq']
  1434. ];
  1435. }
  1436. // 按 sys_rq (时间) 或 UniqId 排序(按实际需求选)
  1437. usort($reslist, function ($a, $b) {
  1438. return strtotime($b['sys_rq']) - strtotime($a['sys_rq']);
  1439. });
  1440. // 返回结构化的查询结果
  1441. $this->success('请求成功', [
  1442. 'result' => $res, // 原始查询结果
  1443. 'headers' => $sizes, // 尺码表头
  1444. 'records' => $reslist // 处理后的记录列表
  1445. ]);
  1446. }
  1447. break;
  1448. case '包装':
  1449. if (strpos($order, ',') !== false) {
  1450. if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
  1451. $this->error('未获取到机台号');
  1452. }
  1453. // 将子订单编号, 机台号, 工序, 尺码, 数量, 尾包, 流水号(包)分割为数组
  1454. $paramArray = explode(',', $order);
  1455. // 获取车缝报工生成小票的数据
  1456. $Uniqid = \db('设备_产量计酬')
  1457. ->where([
  1458. '子订单编号' => $paramArray[0],
  1459. '尺码' => $paramArray[3],
  1460. '数量' => $paramArray[4],
  1461. 'serial' => $paramArray[6],
  1462. ])
  1463. ->find();
  1464. // 获取上一个机台是否已上报过
  1465. $baogonglist = \db('设备_产量计酬')
  1466. ->where([
  1467. '子订单编号' => $paramArray[0],
  1468. '尺码' => $paramArray[3],
  1469. '数量' => $paramArray[4],
  1470. 'serial' => $paramArray[6],
  1471. 'Mod_rq' => null,
  1472. ])
  1473. ->where('sczl_jtbh', 'like', 'ZJ%')
  1474. ->select();
  1475. if(empty($baogonglist)){
  1476. $this->error('当前小票'.'【总检报工】'.'未上报,请上报后再操作');
  1477. }
  1478. // 判断是否成功获取到数据
  1479. if ($Uniqid) {
  1480. $orderlist = \db('工单_基本资料')
  1481. ->where('订单编号', $Uniqid['订单编号'])
  1482. ->field('订单编号, 生产款号, 款式')
  1483. ->find();
  1484. $yinjianlist = \db('工单_印件资料')
  1485. ->where('订单编号', $orderlist['订单编号'])
  1486. ->field('zdtotal, sctotal,颜色,颜色备注')
  1487. ->find();
  1488. } else {
  1489. $this->error('当前扫码小票无效');
  1490. }
  1491. // 获取当前手工机台上报的数据
  1492. $res = \db('设备_产量计酬')
  1493. ->where([
  1494. '子订单编号' => $paramArray[0],
  1495. '尺码' => $paramArray[3],
  1496. '数量' => $paramArray[4],
  1497. 'serial' => $paramArray[6],
  1498. 'sczl_jtbh' => $params['sys_sbID'],
  1499. 'mod_rq' => null,
  1500. ])
  1501. ->order('UniqId desc')
  1502. ->find();
  1503. if (empty($res)) {
  1504. $ci_num = $paramArray[4];
  1505. } else {
  1506. $ci_num = $res["ci_num"];
  1507. // // 累加 s_num
  1508. // $total_s_num = 0;
  1509. // foreach ($res as $item) {
  1510. // $total_s_num += $item['s_num'];
  1511. // }
  1512. // if ($total_s_num >= $paramArray[4]) {
  1513. // $ci_num = 0;
  1514. // } else {
  1515. // $ci_num = $res[0]['ci_num'];
  1516. // }
  1517. }
  1518. // 判断是否已报工并给出提示
  1519. // if (!empty($res) && $paramArray[4] == $res[0]['s_num'] || $ci_num === '' || $ci_num === 0) {
  1520. // $this->error('已上报,无需再次报工');
  1521. // }
  1522. // if (!empty($res) && $res["ci_num"] == 0) {
  1523. // $this->error('已上报,无需再次报工');
  1524. // }
  1525. $result = [
  1526. 'sys_sbID' => $params['sys_sbID'],//设备编号
  1527. 'ci_num' => $ci_num,//剩余数量
  1528. 'UniqId' => $Uniqid['UniqId'],
  1529. 'order' => $paramArray[0],//子订单编号
  1530. 'zb' => $params['sys_sbID'],//组别
  1531. 'gx' => 4,//默认固定工序
  1532. 'cm' => $paramArray[3],//尺码
  1533. 'sl' => $paramArray[4],//数量
  1534. 'wb' => $paramArray[5],//尾包
  1535. '订单编号' => $orderlist['订单编号'],
  1536. '生产款号' => $orderlist['生产款号'],
  1537. '款式' => $orderlist['款式'],
  1538. 'sctotal' => $yinjianlist['sctotal'],
  1539. 'zdtotal' => $yinjianlist['zdtotal'],
  1540. '颜色' => $yinjianlist['颜色备注'],
  1541. ];
  1542. // 查询历史记录
  1543. $existingRecords = \db('设备_产量计酬')->alias('c')
  1544. ->field('
  1545. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  1546. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1547. j.款式,
  1548. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1549. ')
  1550. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1551. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1552. ->where('c.子订单编号', $paramArray[0])
  1553. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1554. ->whereNull('c.mod_rq') // 查询未删除数据
  1555. ->where(function($query) {
  1556. $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
  1557. }) // 查询出库数据
  1558. ->order('c.UniqId', 'desc')
  1559. ->distinct('c.子订单编号')
  1560. ->select();
  1561. $sizeSummary = [];
  1562. $sizes = [];
  1563. foreach ($existingRecords as $record) {
  1564. $size = $record['尺码'];
  1565. $quantity = $record['数量'];
  1566. $serial = $record['serial'];
  1567. // 如果尺码还没有出现过,加入尺码列表
  1568. if (!in_array($size, $sizes)) {
  1569. $sizes[] = $size;
  1570. }
  1571. if (!isset($sizeSummary[$record['UniqId']])) {
  1572. $sizeSummary[$record['UniqId']] = [
  1573. 'UniqId' => $record['UniqId'],
  1574. '订单编号' => $record['订单编号'],
  1575. '子订单编号' => $record['子订单编号'],
  1576. '上报数量' => $record['s_num'],
  1577. '剩余数量' => $record['ci_num'],
  1578. '次品数量' => $record['数量'] - $record['ci_num'],
  1579. '生产款号' => $record['款号'],
  1580. '尺码' => $record['尺码'],
  1581. '工序编号' => $record['工序编号'],
  1582. '工序名称' => $record['工序名称'],
  1583. '数量' => $quantity,
  1584. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1585. '颜色' => $record['颜色备注'],
  1586. '组别' => $record['sczl_jtbh'],
  1587. '款式' => $record['款式'],
  1588. 'serial' => '第('.$serial.')包',
  1589. 'sys_rq' => $record['sys_rq']
  1590. ];
  1591. }
  1592. }
  1593. // 将 $sizeSummary 转换为索引数组
  1594. $sizeSummary = array_values($sizeSummary);
  1595. // 对 $sizeSummary 按 sys_rq 降序排序
  1596. usort($sizeSummary, function ($a, $b) {
  1597. return strtotime($b['UniqId']) - strtotime($a['UniqId']);
  1598. });
  1599. $this->success('请求成功', [
  1600. 'result' => $result,//小票数据
  1601. 'headers' => $sizes, // 尺码表头
  1602. 'records' => $sizeSummary // 历史记录
  1603. ]);
  1604. }else{
  1605. // 查询历史记录
  1606. $res = \db('设备_产量计酬')->alias('c')
  1607. ->field('
  1608. c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh,
  1609. c.尾包,c.UniqId,c.ci_num,c.s_num,c.sys_rq, c.serial,
  1610. j.款式,
  1611. y.zdtotal, y.sctotal, y.颜色,y.颜色备注
  1612. ')
  1613. ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
  1614. ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
  1615. ->where('c.订单编号', 'like', '%' . explode('DC',$order)[1] . '%')
  1616. ->where('c.工序名称', $params['code'])
  1617. ->where('c.sczl_jtbh', $params['sys_sbID'])
  1618. ->whereNull('c.mod_rq') // 查询未删除数据
  1619. ->order('UniqId desc')
  1620. ->select();
  1621. // 初始化返回数据的结构
  1622. $reslist = [];
  1623. $sizes = [];
  1624. // 遍历查询结果,构造需要的结构化数据
  1625. foreach ($res as $record) {
  1626. $size = $record['尺码']; // 记录的尺码
  1627. $quantity = $record['数量']; // 每个记录的数量
  1628. $serial = $record['serial']; // 记录的serial
  1629. // 记录所有不同的尺码,避免重复
  1630. if (!in_array($size, $sizes)) {
  1631. $sizes[] = $size;
  1632. }
  1633. // 构造每条记录的详细信息
  1634. $reslist[] = [
  1635. 'UniqId' => $record['UniqId'],
  1636. '订单编号' => $record['订单编号'],
  1637. '子订单编号' => $record['子订单编号'],
  1638. '上报数量' => $record['s_num'],
  1639. '次品数量' => $record['数量'] - $record['ci_num'],
  1640. '生产款号' => $record['款号'],
  1641. '尺码' => $record['尺码'],
  1642. '工序编号' => $record['工序编号'],
  1643. '工序名称' => $record['工序名称'],
  1644. '数量' => $quantity,
  1645. '尾包' => $record['尾包'] == 1 ? '是' : '否',
  1646. '颜色' => $record['颜色备注'],
  1647. '组别' => $record['sczl_jtbh'],
  1648. '款式' => $record['款式'],
  1649. 'serial' => '第('.$serial.')包',
  1650. 'sys_rq' => $record['sys_rq']
  1651. ];
  1652. }
  1653. // 按 sys_rq (时间) 或 UniqId 排序(按实际需求选)
  1654. usort($reslist, function ($a, $b) {
  1655. return strtotime($b['sys_rq']) - strtotime($a['sys_rq']);
  1656. });
  1657. // 返回结构化的查询结果
  1658. $this->success('请求成功', [
  1659. 'result' => $res, // 原始查询结果
  1660. 'headers' => $sizes, // 尺码表头
  1661. 'records' => $reslist // 处理后的记录列表
  1662. ]);
  1663. }
  1664. break;
  1665. default:
  1666. $this->error('无效的 code 参数');
  1667. break;
  1668. }
  1669. }
  1670. /**
  1671. * 出库报工日志
  1672. * 确认出库 恢复出库状态
  1673. */
  1674. public function getTab() {
  1675. if (!$this->request->isPost()) {
  1676. $this->error('非法请求');
  1677. }
  1678. $params = $this->request->param();
  1679. // 校验子订单编号和出库日期
  1680. if (empty($params['order_id'])) {
  1681. $this->error('子订单编号不能为空');
  1682. }
  1683. // 处理出库逻辑
  1684. if ($params['code'] == '出库') {
  1685. if (strpos($params['order_id'], '-') !== false) {
  1686. // 查询传入的子订单编号的记录,获取其颜色备注
  1687. $cklist = \db('工单_印件资料')
  1688. ->field('子订单编号, ck_rq, 颜色备注')
  1689. ->where('子订单编号', $params['order_id'])
  1690. ->find();
  1691. if ($cklist) {
  1692. if (!empty($cklist['ck_rq'])) {
  1693. $this->error('该子订单已完成出库操作,请勿重复操作。');
  1694. } else {
  1695. // 获取相同颜色备注的所有子订单记录
  1696. $sameColorList = \db('工单_印件资料')
  1697. ->field('子订单编号, ck_rq')
  1698. ->where('颜色备注', $cklist['颜色备注'])
  1699. ->whereNull('ck_rq') // 只选择未出库的记录
  1700. ->select();
  1701. // 开始出库操作
  1702. $updateCount1 = 0;
  1703. foreach ($sameColorList as $item) {
  1704. // 更新 '工单_印件资料' 表中所有相同颜色备注的子订单的出库日期
  1705. $result1 = \db('工单_印件资料')
  1706. ->where('子订单编号', $item['子订单编号'])
  1707. ->update(['ck_rq' => date("Y-m-d H:i:s")]);
  1708. if ($result1 !== false) {
  1709. $updateCount1 += $result1;
  1710. }
  1711. }
  1712. // 检查 '工单_基本资料' 表的 '出库日期' 是否为空
  1713. $order = explode('-', $params['order_id'])[0];
  1714. $basicInfo = \db('工单_基本资料')
  1715. ->field('出库日期')
  1716. ->where('订单编号', $order)
  1717. ->find();
  1718. $updateCount2 = 0;
  1719. if (empty($basicInfo['出库日期'])) {
  1720. // 如果 '出库日期' 为空,则更新
  1721. $jbzl = \db('工单_基本资料')
  1722. ->where('订单编号', $order)
  1723. ->fetchSql(true)
  1724. ->update(['出库日期' => date("Y-m-d H:i:s")]);
  1725. $result2 = \db()->query($jbzl);
  1726. if ($result2 !== false) {
  1727. $updateCount2 = $result2;
  1728. }
  1729. }
  1730. if ($updateCount1 > 0) {
  1731. // 插入日志
  1732. foreach ($sameColorList as $item) {
  1733. $addlist = [
  1734. 'order_id' => $item['子订单编号'],
  1735. 'rq' => date("Y-m-d H:i:s"),
  1736. 'name' => $params['code'],
  1737. 'sys_id' => $params['sys_id']
  1738. ];
  1739. \db('设备_报工日志')->insertGetId($addlist);
  1740. }
  1741. $this->success("出库成功");
  1742. } else {
  1743. $this->error('出库失败');
  1744. }
  1745. }
  1746. } else {
  1747. $this->error('未找到对应的出库订单');
  1748. }
  1749. } else {
  1750. $this->error('请扫描子订单编号进行出库');
  1751. }
  1752. }
  1753. // 处理恢复逻辑
  1754. else if ($params['code'] == '恢复') {
  1755. $cklist = \db('工单_印件资料')
  1756. ->field('子订单编号, ck_rq')
  1757. ->where('子订单编号', $params['order_id'])
  1758. ->find();
  1759. if (!empty($cklist['ck_rq'])) {
  1760. $result1 = \db('工单_印件资料')
  1761. ->where('子订单编号', $params['order_id'])
  1762. ->fetchSql(true)
  1763. ->update(['ck_rq' => null]);
  1764. $updateCount1 = \db()->query($result1);
  1765. // 更新 '工单_基本资料' 表
  1766. $order = explode('-', $params['order_id'])[0];
  1767. $result2 = \db('工单_基本资料')
  1768. ->where('订单编号', $order)
  1769. ->fetchSql(true)
  1770. ->update(['出库日期' => null,'gd_statu'=>'1-计划中']);
  1771. $updateCount2 = \db()->query($result2);
  1772. if ($updateCount1 > 0 && $updateCount2 > 0) {
  1773. // 插入日志
  1774. $addlist = [
  1775. 'order_id' => $params['order_id'],
  1776. 'rq' => date("Y-m-d H:i:s"),
  1777. 'name' => $params['code'],
  1778. 'sys_id' => $params['sys_id']
  1779. ];
  1780. \db('设备_报工日志')->insertGetId($addlist);
  1781. $this->success($params['order_id'] . "恢复未出库");
  1782. } else {
  1783. $this->error($params['order_id'] . '该子订单是未出库状态,无需再次恢复');
  1784. }
  1785. } else {
  1786. $this->error('该子订单是未出库状态,无需再次恢复');
  1787. }
  1788. }
  1789. }
  1790. /**
  1791. * 裁剪完工报工修改工单_印件资料表一条数据
  1792. *
  1793. */
  1794. public function getInfo(){
  1795. if (!$this->request->isPost()) {
  1796. $this->error('非法请求');
  1797. }
  1798. $params = $this->request->param();
  1799. // 检查子订单编号和日期是否都存在
  1800. if (!isset($params['order_id']) || empty($params['order_id'])) {
  1801. $this->error('参数错误,子订单编号不能为空');
  1802. }
  1803. if (!isset($params['rq']) || empty($params['rq'])) {
  1804. $this->error('参数错误,日期不能为空');
  1805. }
  1806. function setIfNotEmpty($value) {
  1807. return (!empty($value) && $value != 0) ? $value : '';
  1808. }
  1809. // 先查询当前记录的原始数据
  1810. $existingData = \db('工单_印件资料')
  1811. ->where('子订单编号', $params['order_id'])
  1812. // ->where(function($query) {
  1813. // $query->whereNull('Mod_rq')->whereOr('Mod_rq', '');
  1814. // }) // 查询未删除数据
  1815. // ->order('子订单编号 desc')
  1816. ->find();
  1817. if ($existingData) {
  1818. // 计算新的累加值
  1819. $updateData = [
  1820. 'sc_rq' => date('Y-m-d H:i:s'),//生产报工日期
  1821. 'scsl1' => isset($params['scsl1']) && is_numeric($params['scsl1']) ? $existingData['scsl1'] + $params['scsl1'] : $existingData['scsl1'] + 0,
  1822. 'scsl2' => isset($params['scsl2']) && is_numeric($params['scsl2']) ? $existingData['scsl2'] + $params['scsl2'] : $existingData['scsl2'] + 0,
  1823. 'scsl3' => isset($params['scsl3']) && is_numeric($params['scsl3']) ? $existingData['scsl3'] + $params['scsl3'] : $existingData['scsl3'] + 0,
  1824. 'scsl4' => isset($params['scsl4']) && is_numeric($params['scsl4']) ? $existingData['scsl4'] + $params['scsl4'] : $existingData['scsl4'] + 0,
  1825. 'scsl5' => isset($params['scsl5']) && is_numeric($params['scsl5']) ? $existingData['scsl5'] + $params['scsl5'] : $existingData['scsl5'] + 0,
  1826. 'scsl6' => isset($params['scsl6']) && is_numeric($params['scsl6']) ? $existingData['scsl6'] + $params['scsl6'] : $existingData['scsl6'] + 0,
  1827. 'scsl7' => isset($params['scsl7']) && is_numeric($params['scsl7']) ? $existingData['scsl7'] + $params['scsl7'] : $existingData['scsl7'] + 0,
  1828. 'scsl8' => isset($params['scsl8']) && is_numeric($params['scsl8']) ? $existingData['scsl8'] + $params['scsl8'] : $existingData['scsl8'] + 0,
  1829. 'scsl9' => isset($params['scsl9']) && is_numeric($params['scsl9']) ? $existingData['scsl9'] + $params['scsl9'] : $existingData['scsl9'] + 0,
  1830. 'scsl10' => isset($params['scsl10']) && is_numeric($params['scsl10']) ? $existingData['scsl10'] + $params['scsl10'] : $existingData['scsl10'] + 0,
  1831. 'sctotal' => isset($params['sctotal']) && is_numeric($params['sctotal']) ? $existingData['sctotal'] + $params['sctotal'] : $existingData['sctotal'] + 0,
  1832. // 你可以在这里继续添加其他字段
  1833. ];
  1834. $updateCount = \db('工单_印件资料')
  1835. ->where('子订单编号', $params['order_id'])
  1836. ->fetchSql(true)
  1837. // ->where(function($query) {
  1838. // $query->whereNull('Mod_rq')->whereOr('Mod_rq', '');
  1839. // }) // 查询未删除数据
  1840. ->update($updateData);
  1841. $list_gd = \db()->query($updateCount);
  1842. } else {
  1843. echo "没有找到相关记录";
  1844. }
  1845. // 检查更新结果
  1846. if ($list_gd > 0) {
  1847. $this->success("更新成功");
  1848. } else {
  1849. // 查询数据库,是否能找到对应的子订单编号
  1850. $existingRecord = \db('工单_印件资料')
  1851. ->where('子订单编号', $params['order_id'])
  1852. // ->where('UniqId', $params['UniqId'])
  1853. ->find();
  1854. if ($existingRecord) {
  1855. $this->error('更新失败,可能是提供的更新数据与现有数据相同,未进行任何更改');
  1856. } else {
  1857. $this->error('更新失败,未找到对应的子订单编号');
  1858. }
  1859. }
  1860. }
  1861. /**
  1862. * 手工、大烫、总检、包装完工报工【通用的报工操作函数】
  1863. */
  1864. function reportWork($params) {
  1865. // 查询设备_产量计酬表的数据
  1866. $bglist = \db('设备_产量计酬')->field('订单编号,子订单编号,数量,ci_num,款号,sys_rq,serial,serial_num')
  1867. ->where('UniqId', $params['UniqId'])->fetchSql(true)->find();
  1868. $list_gd = \db()->query($bglist);
  1869. // 查询工单基本资料表的数据
  1870. $orderlist = \db('工单_基本资料')->where('订单编号', $list_gd[0]['订单编号'])->find();
  1871. // 检查code是否为 "包装"
  1872. if ($params['code'] == '包装') {
  1873. // 如果是 "包装",则增加工单基本资料表中的入仓数量
  1874. $addorder['工单入仓数量'] = $orderlist['工单入仓数量'] + $params['sl'];
  1875. $result = \db('工单_基本资料')->where('订单编号', $list_gd[0]['订单编号'])->fetchSql(true)->update($addorder);
  1876. $sql = \db()->query($result);
  1877. }
  1878. // 构建单条记录的函数,传递 $list_gd 作为参数
  1879. function buildRecords($item, $list_gd) {
  1880. return [
  1881. '订单编号' => $item['order'],
  1882. '子订单编号' => $item['order_id'],
  1883. '款号' => $list_gd[0]['款号'],
  1884. 'serial' => $list_gd[0]['serial'],
  1885. 'serial_num' => $list_gd[0]['serial_num'],
  1886. '工序编号' => $item['gx'],
  1887. '工序名称' => $item['gxmc'],
  1888. '尺码' => $item['cm'],
  1889. '数量' => $item['sl'],
  1890. 'sczl_jtbh' => $item['sczl_jtbh'],
  1891. 'sczl_bh' => $item['sczl_bh'],
  1892. '尾包' => $item['wb'],
  1893. 'sys_id' => $item['sys_id'],
  1894. 'sys_rq' => date('Y-m-d H:i:s'),
  1895. 'sczl_rq' => date('Y-m-d'),
  1896. 'ci_num' => $item['ci_num'] - $item['s_num'],
  1897. 's_num' => $item['s_num'],
  1898. ];
  1899. }
  1900. // 检查是否是索引数组(多条记录)
  1901. if (isset($params[0]) && is_array($params[0])) {
  1902. foreach ($params as $item) {
  1903. $list[] = buildRecords($item, $list_gd);
  1904. }
  1905. } else {
  1906. // 处理单条记录
  1907. $list[] = buildRecords($params, $list_gd);
  1908. }
  1909. // 插入数据到设备_产量计酬表
  1910. $result = \db('设备_产量计酬')->insertAll($list);
  1911. if ($result) {
  1912. $this->success('数据插入成功 : ' . date('H:i:s'));
  1913. } else {
  1914. $this->error('数据插入失败');
  1915. }
  1916. }
  1917. /**
  1918. * 裁剪新增、车缝新增、手工新增、大烫新增、总检新增、包装新增
  1919. * /work_order_spot_check/search
  1920. */
  1921. public function search(){
  1922. if (!$this->request->isPost()) {
  1923. $this->error('非法请求');
  1924. }
  1925. $params = $this->request->param();
  1926. // 根据不同的 code 值执行不同的逻辑
  1927. switch ($params['code']) {
  1928. case '裁剪':
  1929. $order_id = explode('-', $params['order_id']);
  1930. $order = $order_id[0];
  1931. $sql = \db('工单_基本资料')->where('订单编号', $order)->fetchSql(true)->find();
  1932. $order_list = \db()->query($sql);
  1933. if (empty($order_list)) {
  1934. $this->error('没有找到对应的订单信息');
  1935. return;
  1936. }
  1937. $insertData = [];
  1938. $dataList = isset($params[0]) && is_array($params[0]) ? $params : [$params];
  1939. foreach ($dataList as $item) {
  1940. $list = [
  1941. '订单编号' => $order_list[0]['订单编号'],
  1942. '款号' => $order_list[0]['生产款号'],
  1943. '子订单编号' => $item['order_id'],
  1944. '工序编号' => '2',
  1945. '工序名称' => '裁剪',
  1946. '尺码' => $item['cm'],
  1947. '数量' => $item['sl'],
  1948. 'sczl_jtbh' => $item['sczl_jtbh'],
  1949. 'sys_id' => $item['sys_id'],
  1950. 'sczl_rq' => date('Y-m-d 00:00:00'),
  1951. 'sys_rq' => date('Y-m-d H:i:s')
  1952. ];
  1953. $insertData[] = $list;
  1954. }
  1955. $result = \db('设备_产量计酬')->insertAll($insertData);
  1956. $result ? $this->success('数据批量插入成功 : ' . date('H:i:s')) : $this->error('数据插入失败');
  1957. break;
  1958. case '车缝':
  1959. $list = [];
  1960. $order_id = explode('-', $params['order_id']);
  1961. $order = $order_id[0];
  1962. $sql = \db('工单_基本资料')->where('订单编号', $order)->fetchSql(true)->find();
  1963. $list_gd = \db()->query($sql); // 查询的结果可能是多条记录,确保是数组
  1964. // 查询子订单编号对应的最大 serial 值
  1965. $list_order_id = \db('设备_产量计酬')
  1966. ->field('子订单编号, MAX(serial) as serial')
  1967. ->where('子订单编号', $params['order_id'])
  1968. ->where(function($query) {
  1969. $query->whereNull('mod_rq')->whereOr('mod_rq', '');
  1970. }) // 查询未删除数据
  1971. ->group('子订单编号')
  1972. ->find();
  1973. function buildRecord($item, $list_gd, $list_order_id) {
  1974. return [
  1975. '订单编号' => $list_gd[0]['订单编号'],
  1976. '款号' => $list_gd[0]['生产款号'],
  1977. '子订单编号' => $item['order_id'],
  1978. '工序编号' => 3,
  1979. '工序名称' => '车缝',
  1980. 'serial' => $item['serial'],
  1981. 'ci_num' => $item['sl'],
  1982. 'serial_num' => $item['serial_num'],
  1983. '尺码' => $item['cm'],
  1984. '数量' => $item['sl'],
  1985. 'sczl_jtbh' => $item['sczl_jtbh'],
  1986. 'sys_rq' => date('Y-m-d H:i:s'),
  1987. 'sczl_rq' => date('Y-m-d'),
  1988. '尾包' => $item['wb'],
  1989. 'sys_id' => $item['sys_id'],
  1990. 'sczl_bh' => $item['sczl_bh']
  1991. ];
  1992. }
  1993. if (isset($params[0]) && is_array($params[0])) {
  1994. foreach ($params as $item) {
  1995. $list[] = buildRecord($item, $list_gd, $list_order_id); // 所有记录使用相同的 serial
  1996. }
  1997. } else {
  1998. // 处理单条记录
  1999. $list[] = buildRecord($params, $list_gd, $list_order_id); // 单条记录
  2000. }
  2001. $result = \db('设备_产量计酬')->insertAll($list);
  2002. if ($result) {
  2003. $this->success('数据插入成功 : ' . date('H:i:s'));
  2004. } else {
  2005. $this->error('数据插入失败');
  2006. }
  2007. break;
  2008. case '手工':
  2009. case '大烫':
  2010. case '总检':
  2011. case '包装':
  2012. //调用reportWork方法名函数
  2013. $this->reportWork($params);
  2014. break;
  2015. default:
  2016. $this->error('无效的 code 参数');
  2017. break;
  2018. }
  2019. }
  2020. /**
  2021. * 物理地址获取机台编号
  2022. * /work_order_spot_check/getMachineMac
  2023. */
  2024. public function getMachineMac(){
  2025. if ($this->request->isGet() == false){$this->error('非法请求');}
  2026. $params = $this->request->get();
  2027. if (empty($params['sys_sbID'])){$this->error('物理地址参数不能为空');}
  2028. $data = \db('设备_基本资料')->where('sys_sbID', $params['sys_sbID'])
  2029. ->field('设备编号 as 机台号,生产工序,工序,rtrim(设备编组) as 组别,组长,sys_sbID')
  2030. // ->where('生产工序',$params['code'])
  2031. ->find();
  2032. $this->success('请求成功', $data);
  2033. }
  2034. /**
  2035. * 删除报工记录
  2036. * /work_order_spot_check/del
  2037. */
  2038. public function del() {
  2039. if (!$this->request->isGet()) {
  2040. return $this->error('请求错误');
  2041. }
  2042. $param = $this->request->param();
  2043. if (empty($param['UniqId'])) {
  2044. return $this->error('UniqId参数错误');
  2045. }
  2046. // 将 $param['UniqId'] 的值按逗号分割成数组
  2047. $printIdArray = explode(',', $param['UniqId']);
  2048. // 初始化失败计数器
  2049. $failureCount = 0;
  2050. foreach ($printIdArray as $uniqId) {
  2051. // 根据 UniqId 查询 设备_产量计酬 表中的订单编号、尺码和数量
  2052. $result = \db('设备_产量计酬')->where('UniqId', $uniqId)
  2053. ->field('订单编号,尺码,数量')
  2054. ->find();
  2055. // 检查是否找到结果
  2056. if (!$result) {
  2057. $failureCount++;
  2058. continue; // 跳过当前循环,处理下一个 UniqId
  2059. }
  2060. // 根据订单编号查询工单_印件资料表,并排除有修改日期的记录
  2061. $yresult = \db('工单_印件资料')->where('订单编号', $result['订单编号'])
  2062. ->whereNull('Mod_rq')
  2063. ->find();
  2064. // 检查是否找到结果
  2065. if (!$yresult) {
  2066. $failureCount++;
  2067. continue; // 跳过当前循环,处理下一个 UniqId
  2068. }
  2069. // 获取 result 中的尺码和数量
  2070. $size = $result['尺码'];
  2071. $quantity = $result['数量'];
  2072. // 遍历 yresult 中的 cm1 到 cm10 字段,查找匹配的尺码
  2073. for ($i = 1; $i <= 10; $i++) {
  2074. $cmField = 'cm' . $i;
  2075. $scslField = 'scsl' . $i;
  2076. // 检查 yresult 中是否存在对应的尺码字段,且值等于 result 中的尺码
  2077. if (isset($yresult[$cmField]) && $yresult[$cmField] === $size) {
  2078. // 找到对应 scsl 字段并进行数量减法
  2079. if (isset($yresult[$scslField])) {
  2080. $yresult[$scslField] -= $quantity;
  2081. // 防止数量变成负数
  2082. if ($yresult[$scslField] < 0) {
  2083. $yresult[$scslField] = 0;
  2084. }
  2085. // 更新数据库中的 scsl 字段
  2086. \db('工单_印件资料')
  2087. ->where('订单编号', $result['订单编号'])
  2088. ->update([$scslField => $yresult[$scslField]]);
  2089. }
  2090. break; // 找到并处理后,跳出循环
  2091. }
  2092. }
  2093. // 重新计算 scsl1 到 scsl10 的总和并更新 sctotal 字段
  2094. $newTotal = 0;
  2095. for ($i = 1; $i <= 10; $i++) {
  2096. $scslField = 'scsl' . $i;
  2097. if (isset($yresult[$scslField])) {
  2098. $newTotal += $yresult[$scslField];
  2099. }
  2100. }
  2101. // 更新 sctotal 的最新值到数据库
  2102. \db('工单_印件资料')
  2103. ->where('订单编号', $result['订单编号'])
  2104. ->update(['sctotal' => $newTotal]);
  2105. // 更新设备_产量计酬表的mod_rq和delsys_id字段
  2106. $updateData = [
  2107. 'mod_rq' => date('Y-m-d H:i:s'),
  2108. 'delsys_id' => $param['sys_id']
  2109. ];
  2110. $result = \db('设备_产量计酬')->where('UniqId', $uniqId)->fetchSql(true)->update($updateData);
  2111. $sql = \db()->query($result);
  2112. // 检查更新是否成功
  2113. if ($sql === false) {
  2114. $failureCount++;
  2115. }
  2116. }
  2117. // 如果没有失败记录,则表示删除成功
  2118. if ($failureCount === 0) {
  2119. return $this->success('删除成功');
  2120. } else {
  2121. return $this->error("操作完成,但有 {$failureCount} 条记录更新失败");
  2122. }
  2123. }
  2124. }