WorkOrderSpotCheck.php 122 KB

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