WorkOrder.php 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf;
  6. use think\Config;
  7. use think\Db;
  8. use think\Request;
  9. use PhpOffice\PhpSpreadsheet\IOFactory;
  10. use function fast\e;
  11. /**
  12. * 工单资料管理
  13. */
  14. class WorkOrder extends Api
  15. {
  16. protected $noNeedLogin = ['*'];
  17. protected $noNeedRight = ['*'];
  18. public function _initialize()
  19. {
  20. if (isset($_SERVER['HTTP_ORIGIN'])) {
  21. header('Access-Control-Expose-Headers: __token__');//跨域让客户端获取到
  22. }
  23. //跨域检测
  24. check_cors_request();
  25. if (!isset($_COOKIE['PHPSESSID'])) {
  26. Config::set('session.id', $this->request->server("HTTP_SID"));
  27. }
  28. parent::_initialize();
  29. }
  30. /**
  31. * 工单资料菜单列表
  32. *
  33. * @ApiMethod (GET)
  34. * @return false|string
  35. * @throws \think\Exception
  36. */
  37. public function DataList()
  38. {
  39. $where['j.Mod_rq'] = null;
  40. $allCustomers = \db('erp_客户供应商')->alias('e')
  41. ->field('e.编号 as 客户编号')
  42. ->select();
  43. $customerData = [];
  44. foreach ($allCustomers as $customer) {
  45. $customerID = $customer['客户编号'];
  46. $customerData[$customerID] = ['计划中' => 0, '生产中' => 0];
  47. }
  48. $data = \db('erp_客户供应商')->alias('e')
  49. ->join('工单_基本资料 j', 'e.编号 = j.客户编号', 'LEFT')
  50. ->field('e.编号 as 客户编号, j.订单编号, j.gd_statu')
  51. ->where($where)
  52. ->select();
  53. foreach ($data as $row) {
  54. $customerID = $row['客户编号'];
  55. $status = $row['gd_statu'];
  56. if ($status == '1-计划中') {
  57. $customerData[$customerID]['计划中']++;
  58. } elseif ($status == '2-生产中') {
  59. $customerData[$customerID]['生产中']++;
  60. }
  61. }
  62. ksort($customerData);
  63. $output = [];
  64. foreach ($customerData as $customerID => $statusCounts) {
  65. $statusString = [];
  66. if ($statusCounts['计划中'] > 0) {
  67. $statusString[] = "计划中:{$statusCounts['计划中']}";
  68. }
  69. if ($statusCounts['生产中'] > 0) {
  70. $statusString[] = "生产中:{$statusCounts['生产中']}";
  71. }
  72. $output[] = "{$customerID}【" . implode(' ', $statusString) . "】";
  73. }
  74. $this->success('成功', $output);
  75. }
  76. /**
  77. * 工单基本资料列表
  78. * @ApiMethod (GET)
  79. * @param string $limit 查询长度
  80. * @param string $Gd_khdh 客户代号
  81. * @param string $startTime 接单日期开始时间
  82. * @param string $endTime 接单日期结束时间
  83. * @return \think\response\Json
  84. * @throws \think\exception\DbException
  85. */
  86. public function WorkOrderList()
  87. {
  88. if ($this->request->isGet() === false) {
  89. $this->error('请求错误');
  90. }
  91. $search = input('search');
  92. $page = input('page');
  93. $limit = input('limit');
  94. $param = $this->request->param();
  95. $where = [];
  96. if (!empty($search)) {
  97. $where['订单编号|生产款号|客户编号|款式|审核|Sys_id'] = ['like', '%' . $search . '%'];
  98. }
  99. $where['Mod_rq'] = null;
  100. $list = \db('工单_基本资料')
  101. ->where($where)
  102. ->order('订单编号 desc, Gd_statu desc, Sys_rq desc')
  103. ->limit(($page - 1) * $limit, $limit)
  104. ->select();
  105. $count = \db('工单_基本资料')
  106. ->where($where)
  107. ->order('订单编号 desc, Gd_statu desc, Sys_rq desc')
  108. ->select();
  109. // 提取所有订单编号
  110. $orderIds = array_column($list, '订单编号');
  111. // 查询所有在“工单_相关附件”表中存在的订单编号
  112. $relatedOrders = \db('工单_相关附件')
  113. ->whereIn('关联编号', $orderIds)
  114. ->whereIn('附件备注', '技术附件')
  115. ->column('关联编号');
  116. // 遍历数据,判断每个订单编号是否在相关附件表中
  117. foreach ($list as &$value) {
  118. if (in_array($value['订单编号'], $relatedOrders)) {
  119. $value['status'] = ''; // 有相关附件,status为空
  120. } else {
  121. $value['status'] = '*'; // 没有相关附件,标记为新订单
  122. }
  123. }
  124. $this->success('成功', ['data' => $list, 'total' => count($count)]);
  125. }
  126. /**
  127. * 月度客户订单汇总
  128. */
  129. public function ProductInformation()
  130. {
  131. if ($this->request->isGet() === false) {
  132. $this->error('请求错误');
  133. }
  134. // 查询生产中和未生产的数据,使用 CASE WHEN 进行分类统计
  135. $data = \db('工单_基本资料')->alias('j')
  136. ->field('
  137. j.客户编号,
  138. REPLACE(DATE_FORMAT(j.Sys_rq, "%Y-%m"), "-", "") as 年月,
  139. SUM(CASE WHEN j.gd_statu = "2-生产中" THEN 1 ELSE 0 END) as 生产中数量,
  140. SUM(CASE WHEN j.gd_statu = "1-计划中" THEN 1 ELSE 0 END) as 未生产数量
  141. ')
  142. ->where('j.Mod_rq', null) // 统一的查询条件
  143. ->group('j.客户编号, 年月')
  144. ->order('j.客户编号 asc')
  145. ->select();
  146. // 格式化数据
  147. $result = [];
  148. foreach ($data as $item) {
  149. $result[$item['年月']][] = $item['客户编号'] . '【生产中' . $item['生产中数量'] . ',计划中' . $item['未生产数量'] . '】';
  150. }
  151. $this->success('请求成功', ['data' => $result]);
  152. }
  153. /**
  154. * U8工单资料删除
  155. * @param string $workOrder 工单编号
  156. * @return void
  157. * @throws \think\Exception
  158. * @throws \think\exception\PDOException
  159. */
  160. public function orderDataDel()
  161. {
  162. if($this->request->isGet() === false){
  163. $this->error('请求错误');
  164. }
  165. $workOrder = input('Uniqid');
  166. if (empty($workOrder)){
  167. $this->error('参数错误');
  168. }
  169. $order = \db('工单_基本资料')->where('UniqId',$workOrder)->find();
  170. \db()->startTrans();
  171. try {
  172. \db('工单_印件资料')->where('订单编号',$order['订单编号'])->update(['Mod_rq'=>date('Y-m-d H:i:s')]);
  173. \db('工单_工艺资料')->where('订单编号',$order['订单编号'])->update(['Mod_rq'=>date('Y-m-d H:i:s')]);
  174. $res = \db('工单_基本资料')->where('UniqId',$workOrder)->update(['Mod_rq'=>date('Y-m-d H:i:s')]);
  175. \db()->commit();
  176. } catch (\Exception $e){
  177. \db()->rollback();
  178. }
  179. if ($res !== false){
  180. $this->success('成功');
  181. }else{
  182. $this->error('失败');
  183. }
  184. }
  185. /**
  186. * 印件资料修改
  187. * @ApiMethod (POST)
  188. * @param void
  189. * @return void
  190. * @throws \think\Exception
  191. * @throws \think\exception\PDOException
  192. */
  193. public function PrintedEdit()
  194. {
  195. if (Request::instance()->isPost() === false){
  196. $this->error('请求错误');
  197. }
  198. $param = Request::instance()->post();
  199. if (empty($param) || isset($param['Uniqid']) === false){
  200. $this->error('参数错误');
  201. }
  202. $param['Yj_Gdbh'] = \db('工单_印件资料')->where('Uniqid',$param['Uniqid'])->value('Yj_Gdbh');
  203. $data = [
  204. 'Yj_Gdbh' =>$param['Yj_Gdbh'],
  205. 'yj_Yjno' =>$param['yjno'],
  206. 'yj_Yjdh' =>$param['yjdh'],
  207. 'yj_yjmc' =>$param['yjmc'],
  208. 'yj_zzdh' =>$param['zzdh'],
  209. 'yj_zzdh1' =>$param['zzdh1'],
  210. 'yj_zzdh2' =>$param['zzdh2'],
  211. 'yj_zzdh3' =>$param['zzdh3'],
  212. 'yj_zzdh4' =>$param['zzdh4'],
  213. 'yj_zzmc' =>$param['zzmc'],
  214. 'yj_zzmc1' =>$param['zzmc1'],
  215. 'yj_zzmc2' =>$param['zzmc2'],
  216. 'yj_zzmc3' =>$param['zzmc3'],
  217. 'yj_zzmc4' =>$param['zzmc4'],
  218. 'yj_tlgg' =>$param['tlgg'],
  219. 'yj_klgg' =>$param['klgg'],
  220. 'Yj_核算规格' =>$param['hsgg'],
  221. 'yj_成品数量' =>$param['cpsl'],
  222. 'yj_平张投料' =>$param['pztl'],
  223. 'yj_ks' =>$param['ks'],
  224. 'yj_ls' =>$param['ls'],
  225. 'yj_desc' =>$param['desc'],
  226. ];
  227. $UniqId = $param['Uniqid'];
  228. $sql = \db('工单_印件资料')->where('Uniqid',$UniqId)->fetchSql(true)->update($data);
  229. $res = Db::query($sql);
  230. $process = \db('工单_工艺资料')
  231. ->where('Gy0_gdbh',$data['Yj_Gdbh'])
  232. ->where('Gy0_yjno',$data['yj_Yjno'])
  233. ->select();
  234. if ((int)$data['yj_平张投料'] > 0 && !empty($process)){
  235. //重新分配工序计划产量
  236. $result = $this->PlannedProcessYield($data['Yj_Gdbh'],$data['yj_Yjno'],0,$data['yj_平张投料']);
  237. if ($result === false){
  238. $this->success('修改工序产量失败');
  239. }
  240. }
  241. if ($res !== false){
  242. $this->success('成功');
  243. }else{
  244. $this->error('失败');
  245. }
  246. }
  247. /**
  248. * 工艺资料修改
  249. * @ApiMethod (POST)
  250. */
  251. public function ProcessDetailEdit()
  252. {
  253. if (Request::instance()->isPost() === false){
  254. $this->error('请求错误');
  255. }
  256. $param = Request::instance()->post();
  257. if (empty($param) || isset($param['UniqId']) === false){
  258. $this->error('参数错误');
  259. }
  260. if (empty($param['Gy0_shdh'])){
  261. $rate['rate0'] = 0;
  262. $rate['rate1'] = 0;
  263. }else{
  264. $rate = \db('dic_lzsh')->where('sys_bh',$param['Gy0_shdh'])->field('rtrim(sys_rate0) as rate0,rtrim(sys_rate1) as rate1')->find();
  265. }
  266. $param['Gy0_Rate0']= isset($rate['rate0'])?$rate['rate0']:0;
  267. $param['Gy0_Rate1'] = isset($rate['rate1'])?$rate['rate1']:0;
  268. $param['Mod_rq'] = date('Y-m-d H:i:s',time());
  269. $UniqId = $param['UniqId'];
  270. unset($param['UniqId']);
  271. //修改工艺资料
  272. $sql = \db('工单_工艺资料')->where('UniqId',$UniqId)->fetchSql(true)->update($param);
  273. $res = Db::query($sql);
  274. //获取工艺资料数据
  275. $list = \db('工单_工艺资料')->where('UniqId',$UniqId)->field('Gy0_yjno,Gy0_gxh,rtrim(Gy0_计划接货数) as 计划接货数')->find();
  276. // //修改工单状态
  277. // $status = \db('工单_基本资料')->where('Gd_gdbh',$param['Gy0_gdbh'])->field('rtrim(gd_statu) as status')->find();
  278. // if ($status['status'] !== '2-生产中'){
  279. // $statusSql = \db('工单_基本资料')->where('Gd_gdbh',$param['Gy0_gdbh'])->fetchSql(true)->update(['gd_statu'=>'2-生产中']);
  280. // Db::query($statusSql);
  281. // }
  282. //重新分配工序计划产量
  283. if ((int)$list['计划接货数'] > 0){
  284. $result = $this->PlannedProcessYield($param['Gy0_gdbh'],$list['Gy0_yjno'],$list['Gy0_gxh'],$list['计划接货数']);
  285. }
  286. if ($res !== false){
  287. $this->success('成功');
  288. }else{
  289. $this->error('失败');
  290. }
  291. }
  292. /**
  293. * 新增工单->添加工单
  294. * @ApiMethod (POST)
  295. * @param
  296. */
  297. public function WorkOrderAdd()
  298. {
  299. if (Request::instance()->isPost() === false){
  300. $this->error('请求错误');
  301. }
  302. $param = Request::instance()->post();
  303. if (empty($param)){
  304. $this->error('参数错误');
  305. }
  306. $param['Sys_rq'] = date('Y-m-d H:i:s');
  307. $param['gd_statu'] = '1-计划中';
  308. $prefix = substr($param['订单编号'], 0, 2); // e.g., "DC"
  309. $maxOrder = \db('工单_基本资料')
  310. ->where('订单编号', 'like', "{$prefix}%")
  311. ->order('订单编号', 'desc')
  312. ->limit(1)
  313. ->value('订单编号');
  314. if ($maxOrder) {
  315. $numericPart = substr($maxOrder, 2);
  316. $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT);
  317. $param['订单编号'] = $prefix . $newNumericPart;
  318. } else {
  319. $param['订单编号'] = $param['订单编号'];
  320. }
  321. if ($maxOrder) {
  322. $numericPart = substr($maxOrder, 2);
  323. $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT);
  324. $param['订单编号'] = $prefix . $newNumericPart;
  325. } else {
  326. $param['订单编号'] = $param['订单编号'];
  327. }
  328. $data = $param;
  329. unset($data['关联订单']);
  330. unset($data['关联面料ID']);
  331. //新增订单插入至工单基本资料
  332. $sql = \db('工单_基本资料')->fetchSql(true)->insert($data);
  333. \db()->query($sql);
  334. if (!empty($param['关联订单']) && !empty($param['关联面料ID'])){
  335. //关联订单编号
  336. $OrderId = $param['关联订单'];
  337. //关联面料ID
  338. $RelevanceId = $param['关联面料ID'];
  339. //查询订单BOM资料,面料资料
  340. $RelevanceList = explode(',',$RelevanceId);
  341. $FabricList = $BomList = $MaterielList = [];
  342. foreach ($RelevanceList as $item){
  343. //工单面料信息
  344. $Fabric = \db('工单_面料资料')
  345. ->where('UNIQID',$item)
  346. ->where('Mod_rq',null)
  347. ->field('BOM_颜色,BOM_物料编码,BOM_物料名称,BOM_投料单位,BOM_计划门幅,BOM_定额门幅')
  348. ->find();
  349. $Fabric['BOM_工单编号'] = $param['订单编号'];
  350. $Fabric['Sys_ID'] = $param['Sys_id'];
  351. $Fabric['Sys_rq'] = date('Y-m-d H:i:s',time());
  352. $Fabric['BOM_desc'] = $param['备注'];
  353. array_push($FabricList,$Fabric);
  354. //关联订单
  355. $AssociatedNumber = \db('工单关联表')
  356. ->where('订单编号',$OrderId)
  357. ->where('颜色',$Fabric['BOM_颜色'])
  358. ->where('物料编号',$Fabric['BOM_物料编号'])
  359. ->value('关联编号');
  360. $materiel = [
  361. '关联编号' => $AssociatedNumber['关联编号'],
  362. '订单编号' => $Fabric['BOM_工单编号'],
  363. '生产款号' => $param['生产款号'],
  364. '颜色' => $Fabric['BOM_颜色'],
  365. '物料编号' => $Fabric['BOM_物料编号'],
  366. '物料名称' => $Fabric['BOM_物料编号'],
  367. '备注' => $param['备注'],
  368. 'Sys_id' => $param['Sys_id'],
  369. 'Sys_rq' => date('Y-m-d H:i:s',time()),
  370. ];
  371. array_push($MaterielList,$materiel);
  372. }
  373. foreach ($materiel as $item){
  374. //工单BOM信息
  375. $Bom = \db('工单_bom资料')
  376. ->where('BOM_工单编号',$OrderId)
  377. ->where('BOM_物料名称',$item)
  378. ->where('Mod_rq',null)
  379. ->find();
  380. unset($Bom['UNQIID']);
  381. unset($Bom['Mod_rq']);
  382. $Bom['BOM_工单编号'] = $param['订单编号'];
  383. $Bom['Sys_ID'] = $param['Sys_id'];
  384. $Bom['Sys_rq'] = date('Y-m-d H:i:s',time());
  385. array_push($BomList,$Bom);
  386. }
  387. //插入数据
  388. Db::startTrans();
  389. try {
  390. //BOM表数据插入
  391. $BomSql = \db('工单_bom资料')->fetchSql(true)->insertAll($BomList);
  392. $BomRes = \db()->query($BomSql);
  393. //面料表数据插入
  394. $FabricSql = \db('工单_面料资料')->fetchSql(true)->insertAll($FabricList);
  395. $FabricRes = \db()->query($FabricSql);
  396. //工单关联表数据插入
  397. $materielSql = \db('工单关联表')->fetchSql(true)->insertAll($MaterielList);
  398. $materielRes = \db()->query($materielSql);
  399. //提交数据
  400. Db::commit();
  401. }catch (\Exception $e){
  402. //回滚事务
  403. Db::rollback();
  404. }
  405. }else{
  406. //判断新增时面料是否存在,如果有调用gtp自动生成BOM资料
  407. if (!empty($param['面料'])) {
  408. // 只有面料不为空时,才调用 GdGtpAiOrder 方法
  409. $this->GdGtpAiOrder($param['订单编号']);
  410. }
  411. }
  412. $this->success('成功');
  413. }
  414. /**
  415. * 新增印件资料->印件资料添加
  416. * @return void
  417. * @throws \think\db\exception\BindParamException
  418. * @throws \think\exception\PDOException
  419. */
  420. public function PrintDetailAdd()
  421. {
  422. if (Request::instance()->isPost() === false){
  423. $this->error('请求错误');
  424. }
  425. $param = Request::instance()->post();
  426. if (empty($param)){
  427. $this->error('参数错误');
  428. }
  429. $param['Sys_rq'] = date('Y-m-d H:i:s',time());
  430. $process = [
  431. ['1','仓库出库'],['2','裁剪'],['3','车缝'],['4','后道收样'],['5','大烫'],['6','总检'],['7','包装']
  432. ];
  433. $processDetail = [];
  434. foreach ($process as $key=>$value){
  435. $total = null;
  436. if ($key !== 0){
  437. $total = $param['zdtotal'];
  438. }
  439. $processDetail[$key] = [
  440. '订单编号' => $param['订单编号'],
  441. '子订单编号' => $param['子订单编号'],
  442. '款号' => $param['款号'],
  443. '颜色' => $param['颜色'],
  444. '颜色备注' => $param['颜色备注'],
  445. '工序编号' => $value[0],
  446. '工序名称' => $value[1],
  447. '计划产量' => $total,
  448. 'Sys_id' => $param['Sys_id'],
  449. 'Sys_rq' => $param['Sys_rq']
  450. ];
  451. }
  452. $color = \db('工单_面料资料')
  453. ->where('Bom_工单编号',$param['订单编号'])
  454. ->where('BOM_颜色',$param['颜色备注'])
  455. ->select();
  456. $colorList = [];
  457. if (empty($color)){
  458. $BomList = \db('工单_bom资料')
  459. ->where('BOM_工单编号',$param['订单编号'])
  460. ->where('Mod_rq',null)
  461. ->select();
  462. foreach ($BomList as $key=>$value){
  463. $colorList[$key] = [
  464. 'BOM_工单编号' => $param['订单编号'],
  465. 'BOM_颜色' => $param['颜色备注'],
  466. 'BOM_物料编码' => $param['款号'].'-'.$param['颜色备注'].($key+1),
  467. 'BOM_物料名称' => $value['BOM_物料名称'],
  468. 'BOM_投料单位' => '米',
  469. 'Sys_ID' => $param['Sys_id'],
  470. 'Sys_rq' => date('Y-m-d H:i:s',time()),
  471. 'BOM_desc' => $value['BOM_desc']
  472. ];
  473. }
  474. }
  475. //获取最新的关联编号
  476. $AssociatedNumber = \db('工单关联表')->order('关联编号 desc')->column('关联编号');
  477. if (empty($AssociatedNumber)){
  478. $number = 0;
  479. }else{
  480. $number = (int)substr($AssociatedNumber[0],3);
  481. }
  482. $MaterielList = [];
  483. //插入工单关联数据表
  484. foreach ($colorList as $key=>$value){
  485. $MaterielList[$key] = [
  486. '关联编号' => 'GDGL'.($number + $key + 1),
  487. '订单编号' => $value['BOM_工单编号'],
  488. '生产款号' => $param['款号'],
  489. '颜色' => $param['颜色备注'],
  490. '物料编号' => $value['BOM_物料编码'],
  491. '物料名称' => $value['BOM_物料名称'],
  492. '备注' => $value['BOM_desc'],
  493. 'Sys_id' => $param['Sys_id'],
  494. 'Sys_rq' => date('Y-m-d H:i:s',time())
  495. ];
  496. }
  497. //开启事务
  498. db()->startTrans();
  499. try{
  500. //工单颜色录入
  501. $priSql = \db('工单_印件资料')->fetchSql(true)->insert($param);
  502. $priRes = \db()->query($priSql);
  503. //工单工艺录入
  504. $proSql = \db('工单_工艺资料')->fetchSql(true)->insertAll($processDetail);
  505. $proRes = \db()->query($proSql);
  506. //工单面料录入
  507. $fabricSql = \db('工单_面料资料')->fetchSql(true)->insertAll($colorList);
  508. $fabricRes = \db()->query($fabricSql);
  509. //工单关联表录入
  510. $meterieSql = \db('工单关联表')->fetchSql(true)->insertAll($MaterielList);
  511. $meterieRes = \db()->query($meterieSql);
  512. // 提交事务
  513. db()->commit();
  514. } catch (\Exception $e) {
  515. // 回滚事务
  516. db()->rollback();
  517. $this->error($e->getMessage());
  518. }
  519. if ($priRes !== false && $proRes !== false){
  520. $this->success('成功');
  521. }else{
  522. $this->error('失败');
  523. }
  524. }
  525. /**
  526. * 子订单列表
  527. * @return null
  528. * @throws \think\db\exception\DataNotFoundException
  529. * @throws \think\db\exception\ModelNotFoundException
  530. * @throws \think\exception\DbException
  531. */
  532. public function PrintListData()
  533. {
  534. // 检查请求方式
  535. if ($this->request->isGet() === false) {
  536. $this->error('请求错误');
  537. }
  538. $param = $this->request->param();
  539. // 检查参数是否存在
  540. if (isset($param) === false) {
  541. $this->error('参数错误');
  542. }
  543. // 查询型号
  544. $where['Mod_rq'] = null;
  545. $xhdata = \db('工单_印件资料')
  546. ->where('订单编号', $param['order'])
  547. ->where($where)
  548. ->field('cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10')
  549. ->select();
  550. $arr = [];
  551. // 收集cm1到cm10的非空非null值
  552. foreach ($xhdata as $key => $value) {
  553. for ($i = 1; $i <= 10; $i++) {
  554. if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) {
  555. array_push($arr, $value['cm' . $i]);
  556. }
  557. }
  558. }
  559. $arr = array_unique($arr);
  560. sort($arr);
  561. // 查询详细列表
  562. $list = \db('工单_印件资料')
  563. ->where('订单编号', $param['order'])
  564. ->where($where)
  565. ->field('订单编号,子订单编号,款号,颜色,船样,zdtotal,Sys_id,Sys_rq,ck_rq,sc_rq,cm1,cm2,cm3,cm4,cm5,cm6,updatatime as 更新时间,颜色备注,color_id,cm7,cm8,cm9,cm10,cmsl1,cmsl2,cmsl3,cmsl4,cmsl5,cmsl6,cmsl7,cmsl8,cmsl9,cmsl10,Uniqid')
  566. ->select();
  567. // 遍历列表并处理cm和cmsl字段
  568. foreach ($list as $key => $value) {
  569. for ($i = 1; $i <= 10; $i++) {
  570. if ($value['cm' . $i] !== '') {
  571. // 如果 cmsl 的值为 0,则设置为空字符串
  572. $list[$key][$value['cm' . $i]] = ($value['cmsl' . $i] === 0) ? '' : $value['cmsl' . $i];
  573. }
  574. // 删除原有的 cm 和 cmsl 字段
  575. // unset($list[$key]['cm' . $i], $list[$key]['cmsl' . $i]);
  576. }
  577. }
  578. // 自定义型号排序
  579. $customOrder = array('XXS','XS','S', 'M', 'L', 'XL','XXL', 'XXXL', 'XXXXL', '2XL', '3XL', '4XL');
  580. usort($arr, function ($a, $b) use ($customOrder) {
  581. $posA = array_search($a, $customOrder);
  582. $posB = array_search($b, $customOrder);
  583. return $posA - $posB;
  584. });
  585. // 返回结果
  586. $data['型号'] = $arr;
  587. $data['列表'] = $list;
  588. $this->success('成功', $data);
  589. }
  590. /**
  591. * 工单资料管理->印件资料删除
  592. * @return void
  593. * @throws \think\Exception
  594. * @throws \think\exception\PDOException
  595. */
  596. public function PrintDetailDel()
  597. {
  598. if ($this->request->isGet() === false){
  599. $this->error('请求错误');
  600. }
  601. $param = $this->request->param();
  602. if (isset($param['UniqId']) === false){
  603. $this->error('参数错误');
  604. }
  605. $printId = explode(',',$param['UniqId']);
  606. $i = 0;
  607. foreach ($printId as $value){
  608. $res = \db('工单_印件资料')
  609. ->where('Uniqid',$value)
  610. ->update(['Mod_rq'=>date('Y-m-d H:i:s',time())]);
  611. if ($res === false){
  612. $i++;
  613. }
  614. }
  615. if ($i === 0){
  616. $this->success('删除成功');
  617. }else{
  618. $this->error('删除失败');
  619. }
  620. }
  621. /**
  622. * 月度车间报工汇总->报工删除记录
  623. */
  624. public function ProcessDetailDel()
  625. {
  626. if ($this->request->isGet() === false){
  627. $this->error('请求错误');
  628. }
  629. $param = $this->request->param();
  630. if (empty($param)) {
  631. $this->error('参数错误');
  632. }
  633. $where['a.mod_rq'] = ['neq', ''];
  634. $list = \db('设备_产量计酬')->alias('a')
  635. ->join('工单_印件资料 b', 'b.订单编号 = a.订单编号 AND a.子订单编号 = a.子订单编号')
  636. ->join('工单_基本资料 j', 'b.订单编号 = j.订单编号', 'LEFT')
  637. ->field('
  638. b.订单编号, b.子订单编号, b.款号, b.颜色, b.船样, a.尺码, b.zdtotal as 制单数,b.颜色备注,
  639. a.数量, a.sys_rq as 上报时间,a.UniqId,a.mod_rq,a.delsys_id,a.sczl_bh,
  640. j.客户编号,j.生产款号,j.款式
  641. ')
  642. ->where($where)
  643. ->order('a.mod_rq desc')
  644. ->limit($param['page'],$param['limit'])
  645. ->group('a.UniqId')
  646. ->select();
  647. $count = \db('设备_产量计酬')->alias('a')
  648. ->join('工单_印件资料 b', 'b.订单编号 = a.订单编号 AND a.子订单编号 = a.子订单编号')
  649. ->join('工单_基本资料 j', 'b.订单编号 = j.订单编号', 'LEFT')
  650. ->field('
  651. b.订单编号, b.子订单编号, b.款号, b.颜色, b.船样, a.尺码, b.zdtotal as 制单数,b.颜色备注,
  652. a.数量, a.sys_rq as 上报时间,a.UniqId,a.mod_rq,a.delsys_id,a.sczl_bh,
  653. j.客户编号,j.生产款号,j.款式
  654. ')
  655. ->where($where)
  656. ->order('a.mod_rq desc')
  657. ->group('a.UniqId')
  658. ->select();
  659. // 提取所有的尺码,并去重
  660. $sizeList = array_values(array_unique(array_column($list, '尺码')));
  661. // 动态将尺码的数量添加到每个订单中,并替换已完成字段
  662. foreach ($list as &$item) {
  663. $size = $item['尺码'];
  664. $item[$size] = $item['数量']; // 动态添加尺码字段,值为数量
  665. // unset($item['数量']); // 移除原来的已完成字段
  666. }
  667. $data['total'] = count($count);
  668. $data['table'] = $list;
  669. $this->success('成功',$data);
  670. }
  671. /**
  672. * 产品附件新增
  673. * 1.前端进行上传xlsx文件表格
  674. * 2.接口接受数据文件进行保存xlsx文件以及pdf转换
  675. * 3.前端进行预览pdf图片显示文件中的内容再页面上
  676. */
  677. public function gdAnnexAdd() {
  678. ini_set('display_errors', 'On');
  679. ini_set('error_reporting', E_ALL);
  680. if (!$this->request->isPost()) {
  681. $this->error('请求方式错误');
  682. }
  683. // 获取请求参数
  684. $req = $this->request->param();
  685. $relateId = $req['关联编号'];
  686. $attachmentContent = $req['附件内容'];
  687. $attachmentType = $req['附件类型'];
  688. $prefixDir = ROOT_PATH . '/public/';
  689. $uploadDir = ''.'uploads/' . date('Ymd') . '/' . $relateId;
  690. DB::name('工单_基本资料')
  691. ->where('订单编号', $relateId)
  692. ->update(['gd_statu' => '2-生产中']);
  693. // 检查并创建目录
  694. if (!is_dir($prefixDir . $uploadDir)) {
  695. mkdir($prefixDir . $uploadDir, 0777, true);
  696. }
  697. // 处理 Base64 附件内容
  698. if (strpos($attachmentContent, 'base64,') !== false) {
  699. $attachmentContent = explode('base64,', $attachmentContent)[1];
  700. }
  701. $fileContent = base64_decode($attachmentContent);
  702. $filename = time();
  703. // 初始化文件路径变量
  704. $xlsxFilePath = '';
  705. $pdfFilePath = '';
  706. if ($attachmentType === 'pdf') {
  707. // 保存 PDF 文件
  708. $pdfFilePath = $uploadDir . '/' . $filename . '.pdf';
  709. file_put_contents($prefixDir . $pdfFilePath, $fileContent);
  710. } elseif ($attachmentType === 'xlsx') {
  711. // 保存 Excel 文件
  712. $xlsxFilePath = $uploadDir . '/' . $filename . '.xlsx';
  713. file_put_contents($prefixDir . $xlsxFilePath, $fileContent);
  714. // 转换为 PDF 文件
  715. $pdfFilePath = $uploadDir . '/' . $filename . '.pdf';
  716. $cmd = sprintf(
  717. 'libreoffice --headless --convert-to pdf --outdir %s %s',
  718. escapeshellarg($prefixDir . $uploadDir),
  719. escapeshellarg($prefixDir . $xlsxFilePath)
  720. );
  721. exec($cmd, $out, $retval);
  722. if ($retval !== 0) {
  723. $this->error('Excel 转 PDF 失败');
  724. }
  725. } else {
  726. $this->error('不支持的附件类型');
  727. }
  728. // 组织数据
  729. $data = [
  730. '关联编号' => $relateId,
  731. 'sys_id' => $req['sys_id'],
  732. '附件备注' => $req['附件备注'],
  733. '附件类型' => $attachmentType,
  734. 'sys_rq' => date('Y-m-d H:i:s'),
  735. 'url' => $xlsxFilePath, // 保存 xlsx 文件路径(如果存在)
  736. 'pdf' => $pdfFilePath // 保存 pdf 文件路径
  737. ];
  738. // 数据库事务处理
  739. db()->startTrans();
  740. try {
  741. // 插入数据
  742. $sql = db('工单_相关附件')->fetchSql(true)->insert($data);
  743. $result = db()->query($sql);
  744. db()->commit();
  745. } catch (\Exception $e) {
  746. // 回滚事务
  747. db()->rollback();
  748. $this->error($e->getMessage());
  749. }
  750. if ($result === false) {
  751. $this->error('失败');
  752. }
  753. $this->success('成功');
  754. }
  755. /**
  756. * 订单打印接口
  757. * order:订单编号
  758. * 通过订单编号获取订单表数据以及子订单数据
  759. */
  760. public function orderPrint(){
  761. if ($this->request->isGet() === false){$this->error('请求错误');}
  762. $param = $this->request->param();
  763. if (empty($param['order'])){$this->error('参数错误');}
  764. $where['Mod_rq'] = null;
  765. //订单信息
  766. $list = \db('工单_基本资料')
  767. ->where('订单编号',$param['order'])
  768. ->where($where)
  769. ->field('订单编号,img,生产款号,客户编号,款式,落货日期,箱唛要求,面料,船样描述,船样合计,粘衬,订单数量,审核,审核日期,要求,water')
  770. ->find();
  771. //尺码表格表头
  772. $xhdata = \db('工单_印件资料')->where('订单编号', $param['order'])->where($where)->select();
  773. $arr = [];
  774. foreach ($xhdata as $key => $value) {
  775. for ($i = 1; $i <= 10; $i++) {
  776. if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) {
  777. array_push($arr, $value['cm' . $i]);
  778. }
  779. }
  780. }
  781. // 去重并重新索引
  782. $arr = array_unique($arr);
  783. $arr = array_values($arr);
  784. // 自定义排序函数
  785. usort($arr, function($a, $b) {
  786. // 判断是否为数字
  787. $isNumericA = is_numeric($a);
  788. $isNumericB = is_numeric($b);
  789. if ($isNumericA && $isNumericB) {
  790. // 如果都是数字,按从小到大排序
  791. return $a - $b;
  792. } elseif (!$isNumericA && !$isNumericB) {
  793. // 如果都是字母,按字母顺序排序(可以自定义顺序)
  794. $sizeOrder = ['XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL','XXXXL'];
  795. $posA = array_search($a, $sizeOrder);
  796. $posB = array_search($b, $sizeOrder);
  797. return $posA - $posB;
  798. } else {
  799. // 如果一个是数字一个是字母,数字排在前
  800. return $isNumericA ? -1 : 1;
  801. }
  802. });
  803. //打印table数据表格
  804. $porlis = \db('工单_印件资料')
  805. ->where('订单编号',$param['order'])
  806. ->where('船样',0)
  807. ->where($where)
  808. ->field('子订单编号')
  809. ->select();
  810. //合并后的子订单条码数据
  811. $subOrder = $porlis[0]['子订单编号'];
  812. // 找到子订单编号中的 '-' 位置
  813. $dashPos = strpos($subOrder, '-');
  814. if ($dashPos !== false) {
  815. // 提取 '-' 后面的部分
  816. $afterDash = substr($subOrder, $dashPos + 1);
  817. // 判断长度是否等于2或等于4
  818. if (strlen($afterDash) == 2) {
  819. // 查询船样为0的数据
  820. $processlist = \db('工单_印件资料')
  821. ->where('订单编号', $param['order'])
  822. ->where($where)
  823. ->where('船样', 0)
  824. ->field('子订单编号,颜色,款号,zdtotal,颜色备注,color_id,
  825. cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10,
  826. cmsl1,cmsl2,cmsl3,cmsl4,cmsl5,cmsl6,cmsl7,cmsl8,cmsl9,cmsl10,Uniqid')
  827. ->select();
  828. // 初始化汇总数组
  829. $scslTotals = [
  830. "cmsl1" => 0,
  831. "cmsl2" => 0,
  832. "cmsl3" => 0,
  833. "cmsl4" => 0,
  834. "cmsl5" => 0,
  835. "cmsl6" => 0,
  836. "cmsl7" => 0,
  837. "cmsl8" => 0,
  838. "cmsl9" => 0,
  839. "cmsl10" => 0,
  840. "zdtotal" => 0, // 总计数量
  841. ];
  842. // 遍历数据集进行汇总
  843. foreach ($processlist as $item) {
  844. // 遍历每个尺码字段(cmsl1 到 cmsl10)
  845. for ($i = 1; $i <= 10; $i++) {
  846. // 获取当前字段的数量
  847. $sizeQty = $item['cmsl' . $i];
  848. // 判断数量是否有效(非空且大于0)
  849. if (!empty($sizeQty)) {
  850. // 累加当前字段的数量
  851. $scslTotals['cmsl' . $i] += $sizeQty;
  852. // 累加到总计字段
  853. $scslTotals['zdtotal'] += $sizeQty;
  854. }
  855. }
  856. }
  857. $data['scslTotals'] = $scslTotals;
  858. foreach ($processlist as $key => $value) {
  859. // 将尺码和对应的数量从 cm1-cm10 和 cmsl1-cmsl10 转换为动态键值对
  860. for ($i = 1; $i <= 10; $i++) {
  861. if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) {
  862. $processlist[$key][$value['cm' . $i]] = $value['cmsl' . $i];
  863. }
  864. // 移除原始的 cm 和 cmsl 字段
  865. unset($processlist[$key]['cm' . $i], $processlist[$key]['cmsl' . $i]);
  866. }
  867. }
  868. // 用于存储合并后的数据
  869. $mergedData = [];
  870. // 按颜色备注进行合并
  871. foreach ($processlist as $item) {
  872. $key = $item['颜色备注'];
  873. if (!isset($mergedData[$key])) {
  874. $mergedData[$key] = $item;
  875. // 添加条码字段,值为子订单编号
  876. $mergedData[$key]['条码'] = $item['子订单编号'];
  877. } else {
  878. // 合并尺码对应的数量
  879. foreach ($item as $size => $quantity) {
  880. if (is_numeric($size)) {
  881. if (!isset($mergedData[$key][$size])) {
  882. $mergedData[$key][$size] = 0;
  883. }
  884. $mergedData[$key][$size] += $quantity;
  885. }
  886. }
  887. // 合并 zdtotal
  888. $mergedData[$key]['zdtotal'] += $item['zdtotal'];
  889. }
  890. }
  891. // 查询船样为1的数据
  892. $chuanyang = \db('工单_印件资料')
  893. ->where('订单编号', $param['order'])
  894. ->where($where)
  895. ->where('船样', 1)
  896. ->field('子订单编号,颜色,款号,zdtotal,颜色备注,color_id,
  897. cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10,
  898. cmsl1,cmsl2,cmsl3,cmsl4,cmsl5,cmsl6,cmsl7,cmsl8,cmsl9,cmsl10,Uniqid')
  899. ->select();
  900. foreach ($chuanyang as $key => $value) {
  901. // 将尺码和对应的数量从 cm1-cm10 和 cmsl1-cmsl10 转换为动态键值对
  902. for ($i = 1; $i <= 10; $i++) {
  903. if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) {
  904. $chuanyang[$key][$value['cm' . $i]] = $value['cmsl' . $i];
  905. }
  906. // 移除原始的 cm 和 cmsl 字段
  907. unset($chuanyang[$key]['cm' . $i], $chuanyang[$key]['cmsl' . $i]);
  908. }
  909. // 添加条码字段,值为子订单编号
  910. $chuanyang[$key]['条码'] = $value['子订单编号'];
  911. }
  912. // 将合并后的数据插入到相应颜色备注的原始数据尾部
  913. $finalList = [];
  914. $groupedData = [];
  915. // 将原始数据按颜色备注分组
  916. foreach ($processlist as $item) {
  917. $key = $item['颜色备注'];
  918. if (!isset($groupedData[$key])) {
  919. $groupedData[$key] = [];
  920. }
  921. $groupedData[$key][] = $item;
  922. }
  923. // 将合并后的数据插入到对应的颜色备注组的尾部
  924. foreach ($groupedData as $key => $items) {
  925. $finalList = array_merge($finalList, $items); // 先添加原始数据
  926. if (isset($mergedData[$key])) {
  927. $finalList[] = $mergedData[$key]; // 在组的尾部添加合并数据
  928. }
  929. }
  930. // 将船样为1的数据添加到最终列表中
  931. $finalList = array_merge($finalList, $chuanyang);
  932. $data['process'] = $finalList;
  933. $data['order'] = $list;
  934. $data['xhdata'] = $arr;
  935. $this->success('成功',$data);
  936. } elseif (strlen($afterDash) == 4) {
  937. //一条子订单编号一个条码,统计颜色
  938. // $processlist = \db('工单_印件资料')
  939. // ->where('订单编号', $param['order'])
  940. // ->whereNull('Mod_rq') // 查询未删除数据
  941. // ->select();
  942. //
  943. // $table = [];
  944. // foreach ($processlist as $item) {
  945. // // 当前子订单编号的数据
  946. // $subOrder = [
  947. // '颜色备注' => $item['颜色备注'],
  948. // '色系名称' => $item['颜色'],
  949. // '订单编号' => $item['订单编号'],
  950. // '子订单编号' => $item['子订单编号'],
  951. // '条码' => $item['子订单编号'],
  952. // '款号' => $item['款号'],
  953. // ];
  954. // // 当前子订单编号的合计
  955. // $subOrderTotal = 0;
  956. // for ($i = 1; $i <= 10; $i++) {
  957. // if (!empty($item['cm' . $i])) {
  958. // $subOrder[$item['cm' . $i]] = $item['cmsl' . $i];
  959. // // 累加每个尺码的数量
  960. // $subOrderTotal += $item['cmsl' . $i];
  961. // }
  962. // unset($item['cm' . $i], $item['cmsl' . $i]);
  963. // }
  964. // $subOrder['合计'] = $subOrderTotal; // 添加合计
  965. // $table[] = $subOrder; // 将当前子订单的数据添加到 $table 中
  966. // }
  967. $processlist = \db('工单_印件资料')
  968. ->where('订单编号', $param['order'])
  969. ->whereNull('Mod_rq') // 查询未删除数据
  970. ->select();
  971. $table = [];
  972. foreach ($processlist as $item) {
  973. // 当前子订单编号的数据
  974. $subOrder = [
  975. '颜色备注' => $item['颜色备注'],
  976. '色系名称' => $item['颜色'],
  977. '订单编号' => $item['订单编号'],
  978. '子订单编号' => $item['子订单编号'],
  979. '条码' => $item['子订单编号'],
  980. '款号' => $item['款号'],
  981. ];
  982. // 当前子订单编号的合计
  983. $subOrderTotal = 0;
  984. for ($i = 1; $i <= 10; $i++) {
  985. // 判断 cm 和 cmsl 是否有值,空值不显示,0 则显示
  986. if (isset($item['cm' . $i]) && $item['cm' . $i] !== '') {
  987. $subOrder[$item['cm' . $i]] = $item['cmsl' . $i] ?? 0; // 默认数量为 0
  988. // 累加每个尺码的数量
  989. $subOrderTotal += (int)$item['cmsl' . $i]; // 确保是数值类型
  990. }
  991. // 清理字段
  992. unset($item['cm' . $i], $item['cmsl' . $i]);
  993. }
  994. $subOrder['合计'] = $subOrderTotal; // 添加合计
  995. $table[] = $subOrder; // 将当前子订单的数据添加到 $table 中
  996. }
  997. // 初始化汇总数组
  998. $scslTotals = [
  999. "cmsl1" => 0,
  1000. "cmsl2" => 0,
  1001. "cmsl3" => 0,
  1002. "cmsl4" => 0,
  1003. "cmsl5" => 0,
  1004. "cmsl6" => 0,
  1005. "cmsl7" => 0,
  1006. "cmsl8" => 0,
  1007. "cmsl9" => 0,
  1008. "cmsl10" => 0,
  1009. "zdtotal" => 0, // 总计数量
  1010. ];
  1011. // 遍历数据集进行汇总
  1012. foreach ($processlist as $item) {
  1013. // 遍历每个尺码字段(cmsl1 到 cmsl10)
  1014. for ($i = 1; $i <= 10; $i++) {
  1015. // 获取当前字段的数量
  1016. $sizeQty = $item['cmsl' . $i];
  1017. // 判断数量是否有效(非空且大于0)
  1018. if (!empty($sizeQty)) {
  1019. // 累加当前字段的数量
  1020. $scslTotals['cmsl' . $i] += $sizeQty;
  1021. // 累加到总计字段
  1022. $scslTotals['zdtotal'] += $sizeQty;
  1023. }
  1024. }
  1025. }
  1026. $data['scslTotals'] = $scslTotals;
  1027. $data['process'] = $table;//汇总数据集
  1028. $data['order'] = $list;//表格数据
  1029. $data['xhdata'] = $arr;//尺码表头
  1030. $this->success('成功', $data);
  1031. // }
  1032. } else {
  1033. echo "子订单编号 - 后不是2位也不是4位:$afterDash";
  1034. }
  1035. } else {
  1036. echo "子订单编号中没有找到'-'";
  1037. }
  1038. }
  1039. /**
  1040. * 订单编号自动获取
  1041. * @return void
  1042. * @throws \think\db\exception\DataNotFoundException
  1043. * @throws \think\db\exception\ModelNotFoundException
  1044. * @throws \think\exception\DbException
  1045. */
  1046. public function getWorkOrder()
  1047. {
  1048. if ($this->request->isGet() === false){
  1049. $this->error('请求错误');
  1050. }
  1051. $time =substr( date('Ym',time()),2);
  1052. $lastOrder = \db('工单_基本资料')
  1053. ->where('订单编号','like','%'.$time.'%')
  1054. ->order('Uniqid desc')
  1055. ->find();
  1056. if (empty($lastOrder)){
  1057. $newNumber = 1;
  1058. }else{
  1059. $lastNumber = substr($lastOrder['订单编号'],6);
  1060. $newNumber = (int)$lastNumber + 1;
  1061. }
  1062. if ($newNumber<10){
  1063. $newOrder = 'DC'.$time.'00'.$newNumber;
  1064. }elseif ($newNumber>=10 && $newNumber<100){
  1065. $newOrder = 'DC'.$time.'0'.$newNumber;
  1066. }else{
  1067. $newOrder = 'DC'.$time.$newNumber;
  1068. }
  1069. $this->success('成功',$newOrder);
  1070. }
  1071. /**
  1072. * 获取子订单编号
  1073. * @return void
  1074. * @throws \think\db\exception\DataNotFoundException
  1075. * @throws \think\db\exception\ModelNotFoundException
  1076. * @throws \think\exception\DbException
  1077. */
  1078. public function getSuborder(){
  1079. // 确保是GET请求
  1080. if ($this->request->isGet() === false) {
  1081. $this->error('请求错误');
  1082. }
  1083. $param = $this->request->param();
  1084. if (empty($param) || !isset($param['cy']) || !isset($param['order'])) {
  1085. $this->error('参数错误');
  1086. }
  1087. // 判断是否“船样”获取对应的子订单编号
  1088. if ($param['cy'] == '否') {
  1089. //1.通过色系名称查询对应的编号
  1090. $colorlist = \db('工单_颜色编号')
  1091. ->field('colorcode, colorname')
  1092. ->where('colorname', $param['colorname'] ?? '')
  1093. ->find();
  1094. if (empty($colorlist)) {
  1095. $this->error('未找到对应的颜色编号');
  1096. }
  1097. $num = $param['order'] . '-' . $colorlist['colorcode'];
  1098. // 查询子订单编号
  1099. $data = \db('工单_印件资料')
  1100. ->field('子订单编号,cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10')
  1101. ->where('子订单编号', 'like', '%' . $num . '%')
  1102. ->where('船样', '=', 0)
  1103. ->order('子订单编号', 'desc')
  1104. ->find();
  1105. if (empty($data)) {
  1106. // 如果没有找到数据,生成默认的订单编号,后两位从00开始
  1107. $order = $param['order'] . '-' . $colorlist['colorcode'] . '00';
  1108. } else {
  1109. if (strlen($data['子订单编号']) == 12) {
  1110. $data = \db('工单_印件资料')
  1111. ->where('订单编号',$param['order'])
  1112. ->where('船样',0)
  1113. ->order('子订单编号 desc')
  1114. ->find();
  1115. if(empty($data)){
  1116. $order = $param['order'].'-01';
  1117. }else{
  1118. $num = (int)substr($data['子订单编号'],10) + 1;
  1119. if ($num<10){
  1120. $order = $param['order'].'-0'.$num;
  1121. }else{
  1122. $order = $param['order'].'-'.$num;
  1123. }
  1124. }
  1125. }else{
  1126. // 如果找到数据,提取子订单编号并递增
  1127. $order = $data['子订单编号'];
  1128. if (preg_match('/(.*-' . $colorlist['colorcode'] . ')(\d{2})$/', $order, $matches)) {
  1129. $prefix = $matches[1]; // 前缀部分(包括订单编号和颜色代码)
  1130. $number = $matches[2]; // 数字部分(后两位)
  1131. // 循环生成子订单编号并检查数据库中是否存在相同编号
  1132. do {
  1133. $incrementedNumber = (int)$number + 1;
  1134. // 将数字部分补充为两位数,例如 1 -> 01,2 -> 02
  1135. $order = $prefix . str_pad($incrementedNumber, 2, '0', STR_PAD_LEFT);
  1136. $exists = \db('工单_印件资料')->where('子订单编号', '=', $order)->find();
  1137. $number = $incrementedNumber; // 更新 number 用于下一次循环
  1138. } while ($exists); // 如果存在相同编号则继续循环递增
  1139. } else {
  1140. $this->error('订单编号格式错误');
  1141. }
  1142. }
  1143. }
  1144. //2.获取色系名称信息
  1145. $colorlist = \db('工单_颜色编号')->select();
  1146. //3.获取历史尺码数据
  1147. $cm_list = \db('工单_印件资料')
  1148. ->where('订单编号', $param['order'])
  1149. ->group('订单编号')
  1150. ->find();
  1151. $cm_data = [];
  1152. if ($cm_list) {
  1153. // 精准筛选字段名以 "cm" 开头并跟1到2位数字的字段
  1154. foreach ($cm_list as $key => $value) {
  1155. if (preg_match('/^cm\d{1,2}$/', $key)) {
  1156. $cm_data[$key] = $value ?? ''; // 如果值为 null,设为空字符串
  1157. }
  1158. }
  1159. } else {
  1160. // 如果查询结果为空,将 cm1 到 cm10 都设置为空
  1161. for ($i = 1; $i <= 10; $i++) {
  1162. $cm_data["cm$i"] = '';
  1163. }
  1164. }
  1165. $result = ['order' => $order,'colorlist' => $colorlist,'cm' => $cm_data];
  1166. $this->success('成功', $result);
  1167. } else if ($param['cy'] == '是') {
  1168. //1.获取船样子订单编号
  1169. $data = \db('工单_印件资料')
  1170. ->where('订单编号', $param['order'])
  1171. ->where('船样', '=', 1)
  1172. ->order('子订单编号 asc')
  1173. ->find();
  1174. if (empty($data)) {
  1175. // 如果没有数据,初始子订单编号为 '99'
  1176. $order = $param['order'] . '-99';
  1177. } else {
  1178. // 提取子订单编号中的数字部分
  1179. $subOrder = $data['子订单编号']; // 例如 "DC2410006-99"
  1180. if (preg_match('/-(\d+)$/', $subOrder, $matches)) {
  1181. $numberPart = (int)$matches[1]; // 提取到的数字部分
  1182. $newNumber = $numberPart - 1; // 减 1
  1183. // 将新的数字拼接回订单编号
  1184. $order = preg_replace('/-\d+$/', '-' . $newNumber, $subOrder);
  1185. } else {
  1186. $this->error('订单编号格式错误');
  1187. }
  1188. }
  1189. $this->success('成功', $order);
  1190. }
  1191. }
  1192. /**
  1193. * 获取PO号 【代码展示未用到】
  1194. */
  1195. public function getPonumber() {
  1196. if ($this->request->isGet() === false){
  1197. $this->error('请求错误');
  1198. }
  1199. $param = $this->request->param();
  1200. if (empty($param)){
  1201. $this->error('参数错误');
  1202. }
  1203. // $num = substr($param['child_order'], 0, 12); // 如果需要,可以用这个方式截取子订单编号
  1204. // $sql = "SELECT * FROM `工单_印件资料` WHERE `子订单编号` LIKE '{$num}%' ORDER BY `子订单编号` DESC LIMIT 1";
  1205. // $data = \db()->query($sql);
  1206. $colorlist = \db('工单_颜色编号')
  1207. ->field('colorcode,colorname')
  1208. ->where('colorname',$param['child_order'])
  1209. ->find();
  1210. $num = $param['order'] . '-' . $colorlist['colorcode']; // 生成 num,用于模糊查询
  1211. $data = \db('工单_印件资料')
  1212. ->where('子订单编号', 'like', '%' . $num . '%')
  1213. ->order('子订单编号', 'desc')
  1214. ->find();
  1215. if(count($data) === 1){
  1216. $order = $num.'01';
  1217. }else{
  1218. $number = (int)substr($data['子订单编号'],12,14) + 1;
  1219. if ($num<10){
  1220. $order = $num.'0'.$number;
  1221. }else{
  1222. $order = $num.$number;
  1223. }
  1224. }
  1225. $this->success('成功',$order);
  1226. }
  1227. /**
  1228. * 订单资料修改
  1229. * @return void
  1230. * @throws \think\Exception
  1231. * @throws \think\db\exception\BindParamException
  1232. * @throws \think\exception\PDOException
  1233. */
  1234. public function WorkOrderEdit()
  1235. {
  1236. if (Request::instance()->isPost() === false){
  1237. $this->error('请求错误');
  1238. }
  1239. $param = Request::instance()->post();
  1240. if (empty($param)){
  1241. $this->error('参数错误');
  1242. }
  1243. $id = $param['id'];
  1244. unset($param['id']);
  1245. $sql = \db('工单_基本资料')
  1246. ->where('Uniqid',$id)
  1247. ->fetchSql(true)
  1248. ->update($param);
  1249. $res = \db()->query($sql);
  1250. if ($res === false){
  1251. $this->error('失败');
  1252. }else{
  1253. $this->success('成功');
  1254. }
  1255. }
  1256. /**
  1257. * 颜色资料修改
  1258. * @return void
  1259. * @throws \think\Exception
  1260. * @throws \think\db\exception\BindParamException
  1261. * @throws \think\exception\PDOException
  1262. */
  1263. public function PrintDataEdit()
  1264. {
  1265. if(Request::instance()->post() === false){
  1266. $this->error('请求错误');
  1267. }
  1268. $param = Request::instance()->post();
  1269. if (empty($param)){
  1270. $this->error('参数错误');
  1271. }
  1272. // $id = $param['id'];
  1273. // unset($param['id']);
  1274. // $findlist = \db('工单_印件资料')
  1275. // ->where('Uniqid', $param['id'])
  1276. // ->find();
  1277. $updata = [
  1278. '订单编号' => $param['订单编号'],
  1279. '子订单编号' => $param['子订单编号'],
  1280. '款号' => $param['款号'],
  1281. '船样' => $param['船样'],
  1282. '颜色' => $param['颜色'],
  1283. 'color_id' => $param['color_id'],
  1284. '颜色备注' => $param['颜色备注'],
  1285. 'zdtotal' => $param['zdtotal']
  1286. ];
  1287. for ($i = 1; $i <= 10; $i++) {
  1288. $updata["cmsl{$i}"] = isset($param["cmsl{$i}"]) ? $param["cmsl{$i}"] : '';
  1289. }
  1290. //获取子订单颜色编号
  1291. // $color_id = \db('工单_印件资料')
  1292. // ->where('Uniqid',$param['id'])
  1293. // ->field('color_id')
  1294. // ->find();
  1295. // if ($color_id['color_id'] !== $param['color_id']){
  1296. //
  1297. // }
  1298. // $FabricData = ['BOM_颜色'=>$color_id['color_id']];
  1299. //修改面料颜色
  1300. // $FabricSql = \db('工单_面料资料')
  1301. // ->where('BOM_工单编号',$param['订单编号'])
  1302. // ->where('BOM_颜色',$color_id['color_id'])
  1303. // ->fetchSql(true)
  1304. // ->update($FabricData);
  1305. // \db()->query($FabricSql);
  1306. $sql = \db('工单_印件资料')
  1307. ->where('Uniqid', $param['id'])
  1308. ->fetchSql(true)
  1309. ->update($updata);
  1310. $res = \db()->query($sql);
  1311. if ($res !== false) {
  1312. $this->success('修改成功');
  1313. } else {
  1314. $this->error('修改失败');
  1315. }
  1316. }
  1317. /**
  1318. * 图片上传
  1319. * @return void
  1320. */
  1321. public function ImgUpload(){
  1322. $file = request()->file('image');
  1323. if($file){
  1324. $info = $file->validate(['size'=>10485760,'ext'=>'jpg,png'])->move(ROOT_PATH . 'public' . DS . 'uploads');
  1325. if($info){
  1326. $fileName = $info->getSaveName();
  1327. // $ymd = date('Ymd');
  1328. // $imageUrl = '/uploads/' . $ymd.'/'.$fileName;
  1329. $imageUrl = '/uploads/' . str_replace('\\', '/', $fileName);
  1330. return json(['code' => 0, 'msg' => '成功', 'data' => ['url' => $imageUrl]]);
  1331. }else{
  1332. $res = $file->getError();
  1333. return json(['code' => 1, 'msg' => '失败', 'data' => $res]);
  1334. }
  1335. }
  1336. return json(['code' => 1, 'msg' => '没有文件上传', 'data' => null]);
  1337. }
  1338. /**
  1339. * 工单技术附件
  1340. * @return void
  1341. * @throws \think\db\exception\DataNotFoundException
  1342. * @throws \think\db\exception\ModelNotFoundException
  1343. * @throws \think\exception\DbException
  1344. */
  1345. public function OrderAttachments()
  1346. {
  1347. if ($this->request->isGet() === false){
  1348. $this->error('请求错误');
  1349. }
  1350. $param = $this->request->param();
  1351. if (empty($param)){
  1352. $this->error('参数错误');
  1353. }
  1354. $list = \db('工单_相关附件')
  1355. ->where('关联编号',$param['order'])
  1356. ->where('附件备注',$param['desc'])
  1357. ->whereNull('Mod_rq')
  1358. ->order('sys_rq desc')
  1359. ->limit(1)
  1360. ->select();
  1361. $this->success('成功',$list);
  1362. }
  1363. /**
  1364. * 订单BOM资料显示
  1365. * @return void
  1366. * @throws \think\db\exception\DataNotFoundException
  1367. * @throws \think\db\exception\ModelNotFoundException
  1368. * @throws \think\exception\DbException
  1369. */
  1370. public function OrderBomList()
  1371. {
  1372. if ($this->request->isGet() === false){
  1373. $this->error('请求错误');
  1374. }
  1375. $param = $this->request->param();
  1376. if (empty($param) || !isset($param['order'])){
  1377. $this->error('参数错误');
  1378. }
  1379. $where = ['a.BOM_工单编号'=>$param['order']];
  1380. $list = \db('工单_bom资料')
  1381. ->alias('a')
  1382. ->join('工单_基本资料 b','b.订单编号 = a.BOM_工单编号')
  1383. ->field('a.BOM_工单编号 as 订单编号,b.生产款号 as 生产款号,b.客户编号 as 客户编号,b.款式 as 款式,
  1384. a.BOM_物料名称 as 物料名称,a.BOM_投料单位 as 投料单位,a.BOM_计划用量 as 计划用料,a.BOM_标准用量 as 定额用料,
  1385. a.BOM_实际用量 as 裁床实际用料,a.BOM_领用数量 as 裁床领用面料,a.BOM_退还数量 as 裁床退回仓库面料,
  1386. a.BOM_desc as 备注,a.UNIQID,a.BOM_库存总量 as 入库总量, a.BOM_计划门幅 as 计划门幅, a.BOM_定额门幅 as 定额门幅,a.BOM_面料结余 as 面料结余,a.Sys_ID as ID,a.Sys_rq as 日期')
  1387. ->where($where)
  1388. ->whereNull('a.Mod_rq')
  1389. ->select();
  1390. if (!empty($list)) {
  1391. $this->success('成功', $list);
  1392. } else {
  1393. // 调用其他方法处理逻辑(例如记录日志、执行其他操作等)
  1394. $this->GdGtpAiOrder($param['order']);
  1395. // 返回空数据的统一处理
  1396. return $this->success('没有找到相关数据', []);
  1397. }
  1398. }
  1399. /**
  1400. * Bom资料删除
  1401. * */
  1402. public function Bomdel()
  1403. {
  1404. if ($this->request->isGet() === false) {
  1405. $this->error('请求错误');
  1406. }
  1407. // 获取请求参数
  1408. $param = $this->request->param();
  1409. // 检查参数是否为空或者缺少UNIQID
  1410. if (empty($param) || !isset($param['UNIQID'])) {
  1411. $this->error('参数错误');
  1412. }
  1413. // 判断UNIQID是否是逗号分隔的多个ID,如果是则拆分成数组,否则直接处理为单个ID
  1414. $uniqids = strpos($param['UNIQID'], ',') !== false ? explode(',', $param['UNIQID']) : [$param['UNIQID']];
  1415. $where = [];
  1416. $where['Mod_rq'] = date('Y-m-d H:i:s', time());
  1417. // 定义一个标志变量来追踪是否所有更新都成功
  1418. $allUpdated = true;
  1419. $failedUniqids = [];
  1420. // 遍历所有UNIQID并更新数据库
  1421. foreach ($uniqids as $uniqid) {
  1422. // 更新指定UNIQID的记录
  1423. $result = \db('工单_bom资料')
  1424. ->where('UNIQID', $uniqid)
  1425. ->update($where);
  1426. // 检查更新结果
  1427. if (!$result) {
  1428. // 如果某个UNIQID更新失败,记录失败的ID
  1429. $allUpdated = false;
  1430. $failedUniqids[] = $uniqid;
  1431. }
  1432. }
  1433. // 如果所有更新都成功,返回成功信息
  1434. if ($allUpdated) {
  1435. $list = \db('工单_bom资料')
  1436. ->whereIn('UNIQID', $uniqids) // 查询所有传入的UNIQID
  1437. ->select();
  1438. if (!empty($list)) {
  1439. $this->success('删除成功');
  1440. } else {
  1441. $this->GdGtpAiOrder($param['order']);
  1442. return $this->success('没有找到相关数据', []);
  1443. }
  1444. } else {
  1445. // 如果有更新失败的记录,返回失败的UNIQID
  1446. $this->error('部分更新失败,无法更新以下UNIQID: ' . implode(', ', $failedUniqids));
  1447. }
  1448. }
  1449. /**
  1450. * 前端选择订单时如果BOM资料数据为空则单独调用,重新生成最新
  1451. */
  1452. public function GdGtpAiOrder($order){
  1453. // 判断是否有指定的订单号
  1454. if (!empty($order)) {
  1455. // 查询单个订单的最大编号
  1456. $maxOrder = \db('工单_基本资料')
  1457. ->where('订单编号', 'like', "{$order}%")
  1458. ->order('订单编号', 'desc')
  1459. ->limit(1)
  1460. ->value('订单编号');
  1461. // 查询该订单的基本资料
  1462. $list = \db('工单_基本资料')
  1463. ->where('订单编号', 'like', "{$order}%")
  1464. ->order('订单编号', 'desc')
  1465. ->limit(1)
  1466. ->find();
  1467. // 如果面料数据为空,提示错误
  1468. if (empty($list['面料'])) {
  1469. $this->error('面料数据为空无法定义BOM');
  1470. }
  1471. // 处理订单编号
  1472. $numericPart = substr($maxOrder, 2);
  1473. $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT);
  1474. $param['订单编号'] = $order . $newNumericPart;
  1475. // 处理物料信息
  1476. $massage = empty($list['粘衬']) || $list['粘衬'] == '无' ? $list['面料'] : $list['面料'] . ',粘衬:' . $list['粘衬'];
  1477. $mianliao = $this->Gpt($massage);
  1478. // 插入物料数据
  1479. $data = [];
  1480. foreach ($mianliao as $key => $value) {
  1481. if (!empty($value) && $value !== '粘衬') { // 排除空值和粘衬
  1482. $data[] = [
  1483. 'BOM_工单编号' => $list['订单编号'],
  1484. 'BOM_物料名称' => $value,
  1485. 'Sys_rq' => date('Y-m-d H:i:s'),
  1486. 'Sys_ID' => '超级管理员'
  1487. ];
  1488. }
  1489. }
  1490. // 批量插入BOM资料
  1491. if (!empty($data)) {
  1492. \db('工单_bom资料')->insertAll($data);
  1493. }
  1494. $this->success('成功',$order);
  1495. } else {
  1496. // 如果没有指定订单号,批量查询订单号并处理
  1497. $has_bom = \db('工单_bom资料')->alias('a')->field('a.BOM_工单编号')->group('a.BOM_工单编号')->select();
  1498. $all_orders = \db('工单_基本资料')->alias('a')->field('a.订单编号')->where('a.面料', '<>', '')->group('a.订单编号')->select();
  1499. // 提取有BOM资料的订单号
  1500. $has_bom_orders = array_column($has_bom, 'BOM_工单编号');
  1501. // 筛选出没有对应BOM资料的订单号
  1502. $no_bom_orders = array_filter($all_orders, function ($order) use ($has_bom_orders) {
  1503. return !in_array($order['订单编号'], $has_bom_orders);
  1504. });
  1505. // 遍历没有BOM资料的订单
  1506. foreach ($no_bom_orders as $orderData) {
  1507. // 获取该订单号的最大订单编号
  1508. $maxOrder = \db('工单_基本资料')
  1509. ->where('订单编号', 'like', "{$orderData['订单编号']}%")
  1510. ->order('订单编号', 'desc')
  1511. ->limit(1)
  1512. ->value('订单编号');
  1513. // 获取该订单号的具体数据
  1514. $list = \db('工单_基本资料')
  1515. ->where('订单编号', 'like', "{$orderData['订单编号']}%")
  1516. ->order('订单编号', 'desc')
  1517. ->limit(1)
  1518. ->find();
  1519. if (empty($list['面料'])) {
  1520. $this->error("订单 {$orderData['订单编号']} 面料数据为空,无法定义BOM");
  1521. }
  1522. // 处理订单编号
  1523. $numericPart = substr($maxOrder, 2);
  1524. $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT);
  1525. $param['订单编号'] = $order . $newNumericPart;
  1526. // 处理物料信息
  1527. $massage = empty($list['粘衬']) || $list['粘衬'] == '无' ? $list['面料'] : $list['面料'] . ',粘衬:' . $list['粘衬'];
  1528. $mianliao = $this->Gpt($massage);
  1529. // 插入物料数据
  1530. $data = [];
  1531. foreach ($mianliao as $key => $value) {
  1532. if (!empty($value) && $value !== '粘衬') { // 排除空值和粘衬
  1533. $data[] = [
  1534. 'BOM_工单编号' => $list['订单编号'],
  1535. 'BOM_物料名称' => $value,
  1536. 'Sys_rq' => date('Y-m-d H:i:s'),
  1537. 'Sys_ID' => '超级管理员'
  1538. ];
  1539. }
  1540. }
  1541. // 批量插入BOM资料
  1542. if (!empty($data)) {
  1543. \db('工单_bom资料')->insertAll($data);
  1544. }
  1545. }
  1546. $this->success('成功');
  1547. }
  1548. }
  1549. /**
  1550. * 订单面料修改接口
  1551. * @return void
  1552. * @throws \think\Exception
  1553. * @throws \think\db\exception\BindParamException
  1554. * @throws \think\exception\PDOException
  1555. */
  1556. public function FabricEdit()
  1557. {
  1558. if ($this->request->isPost() === false){
  1559. $this->error('请求错误');
  1560. }
  1561. $param = Request::instance()->post();
  1562. if (empty($param)){
  1563. $this->error('请求错误');
  1564. }
  1565. // 判断前端传来的参数是否为空,避免空订单编号造成问题
  1566. if(empty($param[0]['BOM_工单编号'])){
  1567. $this->error('请求错误,请重新打开此页面');
  1568. }
  1569. foreach ($param as $key=>$value){
  1570. $data = $value;
  1571. unset($data['UNIQID']);
  1572. if ($value['UNIQID'] !== '' || !empty($value['UNIQID'])){
  1573. $sql = \db('工单_bom资料')
  1574. ->where('UNIQID',$value['UNIQID'])
  1575. ->fetchSql(true)
  1576. ->update($data);
  1577. $res = \db()->query($sql);
  1578. }else{
  1579. $sql = \db('工单_bom资料')
  1580. ->fetchSql(true)
  1581. ->insert($value);
  1582. $res = \db()->query($sql);
  1583. }
  1584. if ($res === false){
  1585. $this->error('修改失败');
  1586. }
  1587. }
  1588. $this->success('修改成功');
  1589. }
  1590. /**
  1591. * 订单BOM出库、退还详情显示
  1592. * @return void
  1593. * @throws \think\db\exception\DataNotFoundException
  1594. * @throws \think\db\exception\ModelNotFoundException
  1595. * @throws \think\exception\DbException
  1596. */
  1597. public function FabricDetail()
  1598. {
  1599. if ($this->request->isGet() === false){
  1600. $this->error('请求错误');
  1601. }
  1602. $param = $this->request->param();
  1603. if (empty($param) || !isset($param['order'])){
  1604. $this->error('参数错误');
  1605. }
  1606. if (isset($param['searchh']) || !empty($param['search'])){
  1607. $list['入库记录'] = \db('设备_报工日志')
  1608. ->where('order_id|款号',$param['order'])
  1609. ->where('name','入库')
  1610. ->field('id,order_id as 订单编号,款号,sum(number) as 数量,rq as 入库时间,sys_id as 入库人员,recipient as 入仓人员,receipt_number as 单号')
  1611. ->group('receipt_number')
  1612. ->order('入库时间 desc')
  1613. ->whereNull('Mod_rq')
  1614. ->select();
  1615. }else{
  1616. //出库记录查询
  1617. $list['出库记录'] = \db('设备_报工日志')
  1618. ->where('order_id|款号',$param['order'])
  1619. ->where('name','出库')
  1620. ->field('id,order_id as 订单编号,款号,sum(number) as 数量,rq as 出库时间,sys_id as 出库人员,receipt_number as 出库单据编号,recipient as 出库人员,receipt_number as 单号')
  1621. ->group('receipt_number')
  1622. ->order('出库时间 desc')
  1623. ->whereNull('Mod_rq')
  1624. ->select();
  1625. //退还记录查询
  1626. $list['退还记录'] = \db('设备_报工日志')
  1627. ->where('order_id|款号',$param['order'])
  1628. ->where('name','退还')
  1629. ->field('id,order_id as 订单编号,款号,sum(number) as 数量,rq as 退还时间,sys_id as 退还机台,recipient as 退还人员,receipt_number as 单号')
  1630. ->group('receipt_number')
  1631. ->order('退还时间 desc')
  1632. ->whereNull('Mod_rq')
  1633. ->select();
  1634. }
  1635. $this->success('成功',$list);
  1636. }
  1637. /**
  1638. * 入库、出库、退还详情数据
  1639. */
  1640. public function FabricDetaillist()
  1641. {
  1642. if ($this->request->isGet() === false){
  1643. $this->error('请求错误');
  1644. }
  1645. $param = $this->request->param();
  1646. if (empty($param) || !isset($param['order'])){
  1647. $this->error('参数错误');
  1648. }
  1649. // 定义查询字段
  1650. $fields = '
  1651. a.id,
  1652. a.order_id as 订单编号,
  1653. a.客户编号,
  1654. a.款号 as 生产款号,
  1655. a.款式,
  1656. a.物料名称,
  1657. b.BOM_计划用量 as 计划用料,
  1658. b.BOM_标准用量 as 定额用料,
  1659. b.BOM_计划门幅 as 计划门幅,
  1660. b.BOM_定额门幅 as 定额门幅,
  1661. b.BOM_实际用量 as 裁床实际用料,
  1662. b.Bom_领用数量 as 裁床领用面料,
  1663. b.BOM_退还数量 as 裁床退还数量,
  1664. b.BOM_库存总量 as 库存总量,
  1665. b.BOM_面料结余 as 面料结余,
  1666. a.number as 入库数量,
  1667. a.number as 出库数量,
  1668. b.BOM_投料单位 as 投料单位,
  1669. a.rq as 入库时间,
  1670. a.rq as 出库时间,
  1671. a.sys_id as 入库人员,
  1672. a.sys_id as 出库人员,
  1673. a.recipient as 领用人员,
  1674. a.receipt_number as 单号,
  1675. b.BOM_desc as 备注
  1676. ';
  1677. if (isset($param['searchh']) || !empty($param['search'])){
  1678. $list['入库记录'] = \db('设备_报工日志')->alias('a')
  1679. ->join('工单_bom资料 b', 'b.BOM_工单编号 = a.order_id AND a.物料名称 = b.BOM_物料名称', 'left') // 多条件关联
  1680. ->where('a.receipt_number', $param['order'])
  1681. ->where('a.name', '入库')
  1682. ->field($fields)
  1683. ->order('a.rq desc')
  1684. ->whereNull('a.Mod_rq')
  1685. ->select();
  1686. }else{
  1687. //出库记录查询
  1688. $list['出库记录'] = \db('设备_报工日志')->alias('a')
  1689. ->join('工单_bom资料 b', 'b.BOM_工单编号 = a.order_id AND a.物料名称 = b.BOM_物料名称', 'left') // 多条件关联
  1690. ->where('a.receipt_number', $param['order'])
  1691. ->where('a.name', '出库')
  1692. ->field($fields)
  1693. ->order('a.rq desc')
  1694. ->whereNull('a.Mod_rq')
  1695. ->select();
  1696. //退还记录查询
  1697. $list['退还记录'] = \db('设备_报工日志')->alias('a')
  1698. ->join('工单_bom资料 b', 'b.BOM_工单编号 = a.order_id AND a.物料名称 = b.BOM_物料名称', 'left') // 多条件关联
  1699. ->where('a.receipt_number', $param['order'])
  1700. ->where('a.name', '退还')
  1701. ->field($fields)
  1702. ->order('a.rq desc')
  1703. ->whereNull('a.Mod_rq')
  1704. ->select();
  1705. }
  1706. $this->success('成功',$list);
  1707. }
  1708. /**
  1709. * 入库、出库、退还删除
  1710. */
  1711. public function FabricDetaildel()
  1712. {
  1713. if ($this->request->isPost() === false){
  1714. $this->error('请求错误');
  1715. }
  1716. $param = Request::instance()->post();
  1717. if (empty($param)){
  1718. $this->error('请求错误');
  1719. }
  1720. // 判断UNIQID是否是逗号分隔的多个ID,如果是则拆分成数组,否则直接处理为单个ID
  1721. $ids = strpos($param['id'], ',') !== false ? explode(',', $param['id']) : [$param['id']];
  1722. $where = [];
  1723. $where['Mod_id'] = $param['Mod_id'];
  1724. $where['Mod_rq'] = date('Y-m-d H:i:s', time());
  1725. // 定义一个标志变量来追踪是否所有更新都成功
  1726. $allUpdated = true;
  1727. $failedUniqids = [];
  1728. // 遍历所有UNIQID并更新数据库
  1729. foreach ($ids as $id) {
  1730. // 更新指定UNIQID的记录
  1731. $result = \db('设备_报工日志')
  1732. ->where('id', $id)
  1733. ->update($where);
  1734. // 检查更新结果
  1735. if (!$result) {
  1736. // 如果某个UNIQID更新失败,记录失败的ID
  1737. $allUpdated = false;
  1738. $failedUniqids[] = $id;
  1739. }
  1740. }
  1741. // 如果所有更新都成功,返回成功信息
  1742. if ($allUpdated) {
  1743. $list = \db('设备_报工日志')
  1744. ->whereIn('id', $id) // 查询所有传入的UNIQID
  1745. ->select();
  1746. if (!empty($list)) {
  1747. $this->success('删除成功');
  1748. } else {
  1749. $this->GdGtpAiOrder($param['order']);
  1750. return $this->success('没有找到相关数据', []);
  1751. }
  1752. } else {
  1753. // 如果有更新失败的记录,返回失败的UNIQID
  1754. $this->error('部分更新失败,无法更新以下UNIQID: ' . implode(', ', $failedUniqids));
  1755. }
  1756. }
  1757. //GPT
  1758. public function Gpt($massage)
  1759. {
  1760. // 设置 API 密钥
  1761. $apiKey = 'sk-e0JuPjMntkbgi1BoMjrqyyzMKzAxILkQzyGMSy3xiMupuoWY'; // 替换为您的 API 密钥
  1762. // 要发送给 GPT 的消息
  1763. $messages = [
  1764. [
  1765. 'role' => 'user',
  1766. 'content' => '你好,帮我按照:面料1、面料2、面料3……来整理归纳下面的面料信息,我只需要面料信息,不需要其他:'.$massage
  1767. ]
  1768. ];
  1769. // 创建请求数据
  1770. $data = [
  1771. 'model' => 'gpt-3.5-turbo', // 使用的模型
  1772. 'messages' => $messages,
  1773. 'max_tokens' => 100, // 设置最大 token 数
  1774. ];
  1775. // 初始化 cURL
  1776. $ch = curl_init('https://niubi.zeabur.app/v1/chat/completions');
  1777. // 设置 cURL 选项
  1778. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1779. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  1780. 'Content-Type: application/json',
  1781. 'Authorization: Bearer ' . $apiKey,
  1782. ]);
  1783. curl_setopt($ch, CURLOPT_POST, true);
  1784. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  1785. curl_setopt($ch, CURLOPT_CAINFO, ROOT_PATH . '/public/uploads/cacert.pem');
  1786. // 执行请求
  1787. $response = curl_exec($ch);
  1788. // 检查错误
  1789. if (curl_errno($ch)) {
  1790. echo 'Error:' . curl_error($ch);
  1791. }
  1792. // 关闭 cURL
  1793. curl_close($ch);
  1794. // 解析和输出响应
  1795. $responseData = json_decode($response, true);
  1796. // 获取 GPT 的回复
  1797. if (isset($responseData['choices'][0]['message']['content'])) {
  1798. //获取返回内容
  1799. $gptReply = $responseData['choices'][0]['message']['content'];
  1800. // halt($gptReply);
  1801. //返回面料信息
  1802. $gptArray = explode('面料',$gptReply);
  1803. array_shift($gptArray);
  1804. foreach ($gptArray as $key=>$value){
  1805. $gptArray[$key] = preg_replace('/\s+/', '', substr($value,4));
  1806. }
  1807. return $gptArray;
  1808. } else {
  1809. echo "未能获取 GPT 的回复。";
  1810. }
  1811. }
  1812. /**
  1813. * 面料库存月份查询
  1814. */
  1815. public function fabricListmonth()
  1816. {
  1817. // 使用DATE_FORMAT函数提取年月部分,格式为 "YYYY-MM"
  1818. $list = \db('工单_bom资料')
  1819. ->alias('a')
  1820. ->join('工单_基本资料 b', 'b.订单编号 = a.BOM_工单编号')
  1821. ->field('DATE_FORMAT(a.Sys_rq, "%Y-%m") as date') // 提取年月,格式化为 "YYYY-MM"
  1822. ->group('date')
  1823. ->order('date desc')
  1824. ->select();
  1825. // 格式化返回的数据,生成期望的结构
  1826. $result = [];
  1827. foreach ($list as $item) {
  1828. $result[] = ['date' => $item['date']]; // 返回格式为 {"date": "YYYY-MM"}
  1829. }
  1830. if (!empty($result)) {
  1831. $this->success('成功', $result); // 返回查询到的结果
  1832. } else {
  1833. $this->success('没有找到相关数据', []); // 如果没有数据,返回空数组
  1834. }
  1835. }
  1836. /**
  1837. * 面料库存列表
  1838. * @return void
  1839. * @throws \think\db\exception\DataNotFoundException
  1840. * @throws \think\db\exception\ModelNotFoundException
  1841. * @throws \think\exception\DbException
  1842. */
  1843. public function fabricList()
  1844. {
  1845. if ($this->request->isGet() === false) {
  1846. $this->error('请求错误');
  1847. }
  1848. $param = $this->request->param();
  1849. $where = [];
  1850. // 根据传入的参数构造查询条件
  1851. if (isset($param['order'])) {
  1852. $where['a.BOM_工单编号|b.生产款号|a.BOM_物料名称'] = ['like', $param['order'] . '%'];
  1853. }
  1854. if (isset($param['mouth'])) {
  1855. $where['a.Sys_rq'] = ['like', $param['mouth'] . '%'];
  1856. }
  1857. // 分页参数,防止未传递时出错
  1858. $page = isset($param['page']) ? (int)$param['page'] : 1;
  1859. $limit = isset($param['limit']) ? (int)$param['limit'] : 20; // 默认每页20条数据
  1860. // 获取数据
  1861. $data = \db('工单_面料资料')
  1862. ->alias('a')
  1863. ->join('工单_基本资料 b', 'b.订单编号 = a.BOM_工单编号')
  1864. ->field('a.BOM_工单编号 as 订单编号,a.BOM_颜色 as 颜色, b.生产款号 as 生产款号, b.客户编号 as 客户编号, b.款式 as 款式,
  1865. a.BOM_物料编码,a.BOM_物料名称 as 物料名称, a.BOM_投料单位 as 投料单位,
  1866. a.BOM_实际用量 as 裁床实际用料, a.BOM_领用数量 as 裁床领用面料, a.BOM_退还数量 as 裁床退回仓库面料,
  1867. a.BOM_desc as 备注, a.UNIQID, a.BOM_库存总量 as 入库总量, a.BOM_计划门幅 as 计划门幅, a.BOM_定额门幅 as 定额门幅,
  1868. a.BOM_面料结余 as 面料结余, a.Sys_ID as ID, a.Sys_rq as 日期,a.BOM_关联工单')
  1869. ->where($where)
  1870. ->order("a.BOM_工单编号 desc")
  1871. ->limit(($page - 1) * $limit, $limit)
  1872. ->select();
  1873. // 获取数据总数
  1874. $count = \db('工单_面料资料')
  1875. ->alias('a')
  1876. ->join('工单_基本资料 b', 'b.订单编号 = a.BOM_工单编号')
  1877. ->where($where)
  1878. ->count();
  1879. // 如果查询到数据
  1880. if (!empty($data)) {
  1881. // 构建返回数据
  1882. $list = [
  1883. 'total' => $count, // 总数
  1884. 'table' => $data // 数据内容
  1885. ];
  1886. // 返回成功响应
  1887. $this->success('成功', $list);
  1888. } else {
  1889. // 没有查询到数据
  1890. $this->success('没有找到相关数据', []);
  1891. }
  1892. }
  1893. /**
  1894. * 单条面料详情
  1895. * @return void
  1896. * @throws \think\db\exception\DataNotFoundException
  1897. * @throws \think\db\exception\ModelNotFoundException
  1898. * @throws \think\exception\DbException
  1899. */
  1900. public function oneFabricDetail()
  1901. {
  1902. if ($this->request->isGet() === false){
  1903. $this->error('请求错误');
  1904. }
  1905. $param = $this->request->param();
  1906. if (empty($param)){
  1907. $this->error('参数错误');
  1908. }
  1909. //面料入库记录
  1910. $list['入库'] = \db('设备_报工日志')
  1911. ->where('',$param['order'])
  1912. ->where('物料名称',$param['fabricName'])
  1913. ->where('name','入库')
  1914. ->where('Mod_rq',null)
  1915. ->field('order_id as 订单编号,款号,物料编码,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,recipient as 入仓人员')
  1916. ->select();
  1917. //面料出库记录
  1918. $list['出库'] = \db('设备_报工日志')
  1919. ->where('order_id',$param['order'])
  1920. ->where('物料名称',$param['fabricName'])
  1921. ->where('name','出库')
  1922. ->where('Mod_rq',null)
  1923. ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,receipt_number as 出库单据编号,recipient as 领用人员')
  1924. ->select();
  1925. //面料退还记录
  1926. $list['退还'] = \db('设备_报工日志')
  1927. ->where('order_id',$param['order'])
  1928. ->where('物料名称',$param['fabricName'])
  1929. ->where('name','退还')
  1930. ->where('Mod_rq',null)
  1931. ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,recipient as 退还人员')
  1932. ->select();
  1933. $this->success('成功',$list);
  1934. }
  1935. /**
  1936. * 单据号查询数据
  1937. * @return void
  1938. * @throws \think\db\exception\DataNotFoundException
  1939. * @throws \think\db\exception\ModelNotFoundException
  1940. * @throws \think\exception\DbException
  1941. */
  1942. public function ReceiptDetail()
  1943. {
  1944. if ($this->request->isGet() === false){
  1945. $this->error('请求错误');
  1946. }
  1947. $param = $this->request->param();
  1948. if (empty($param)){
  1949. $this->error('单据编号参数错误');
  1950. }
  1951. $list = \db('设备_报工日志')
  1952. // ->where('receipt_number','like','%',$param['receipt'].'%')
  1953. ->where('receipt_number',$param['receipt'])
  1954. ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,receipt_number as 出库单据编号,recipient as 领用人员')
  1955. ->whereNull('Mod_rq')
  1956. ->select();
  1957. if (empty($list)){
  1958. $this->error('未找到该出库单');
  1959. }else{
  1960. $this->success('成功',$list);
  1961. }
  1962. }
  1963. /**
  1964. * 出库单列表
  1965. * @return void
  1966. * @throws \think\db\exception\DataNotFoundException
  1967. * @throws \think\db\exception\ModelNotFoundException
  1968. * @throws \think\exception\DbException
  1969. */
  1970. public function ReceiptList()
  1971. {
  1972. if ($this->request->isGet() === false){
  1973. $this->error('请求错误');
  1974. }
  1975. $param = $this->request->param();
  1976. if (empty($param)){
  1977. $this->error('参数错误');
  1978. }
  1979. $where= [];
  1980. if (isset($param['search'])) {
  1981. $where['物料名称|款号|order_id|receipt_number'] = ['like', $param['search'] . '%'];
  1982. }
  1983. if (isset($param['mouth'])) {
  1984. $where['rq'] = ['like', $param['mouth'] . '%'];
  1985. }
  1986. $page = $param['page'];
  1987. $limit = $param['limit'];
  1988. $list = \db('设备_报工日志')
  1989. ->where($where)
  1990. ->field('receipt_number as 出库单,order_id as 订单编号,款号,物料名称,rq as 日期,sys_id as 操作机台,recipient as 领料人员,name as 单号类型')
  1991. ->group('出库单')
  1992. ->order('rq desc')
  1993. ->where('Mod_rq',null)
  1994. ->limit(($page-1)*$limit,$limit)
  1995. ->select();
  1996. $count = \db('设备_报工日志')
  1997. ->where($where)
  1998. ->field('receipt_number as 出库单')
  1999. ->group('出库单')
  2000. ->where('Mod_rq',null)
  2001. ->order('rq desc')
  2002. ->select();
  2003. if (empty($list)){
  2004. $this->success('未找到数据');
  2005. }else{
  2006. $data['total'] = count($count);
  2007. $data['table'] = $list;
  2008. $this->success('成功',$data);
  2009. }
  2010. }
  2011. /**
  2012. * 出库单左侧菜单
  2013. * @return void
  2014. * @throws \think\db\exception\DataNotFoundException
  2015. * @throws \think\db\exception\ModelNotFoundException
  2016. * @throws \think\exception\DbException
  2017. */
  2018. public function getReceiptTab()
  2019. {
  2020. if ($this->request->isGet() === false){
  2021. $this->error('请求错误');
  2022. }
  2023. $list = \db('设备_报工日志')
  2024. ->field([
  2025. "DATE_FORMAT(rq, '%Y-%m') AS month",
  2026. ])
  2027. ->group('month')
  2028. ->order('month DESC')
  2029. ->select();
  2030. if (empty($list)){
  2031. $this->error('未查询到入库、出库、退还数据');
  2032. }else{
  2033. $this->success('成功',$list);
  2034. }
  2035. }
  2036. /**
  2037. * 获取入库单号、出库单号
  2038. * @return void
  2039. */
  2040. public function gitReceiptNumber()
  2041. {
  2042. if ($this->request->isGet() === false){
  2043. $this->error('请求错误');
  2044. }
  2045. $param = $this->request->param();
  2046. if (empty($param)){
  2047. $this->error('参数错误');
  2048. }
  2049. $lastNumber = \db('设备_报工日志')
  2050. ->where('receipt_number','like',$param['number'].'%')
  2051. ->order('receipt_number desc')
  2052. ->limit(1)
  2053. ->column('receipt_number as 单号');
  2054. if (empty($lastNumber)){
  2055. $num = 1;
  2056. }else{
  2057. $num = (int)(substr($lastNumber[0],12,3))+1;
  2058. }
  2059. if ($num < 10){
  2060. $num = '00'.$num;
  2061. }elseif ($num>=10 && $num<100){
  2062. $num = '0'.$num;
  2063. }else{
  2064. $num;
  2065. }
  2066. $number = $param['number'].date('Ymd',time()).'-'.$num;
  2067. $lastNumber = \db('设备_报工日志')
  2068. ->where('name','出库')
  2069. ->order('recipient desc')
  2070. ->group('recipient')
  2071. ->limit(1)
  2072. ->value('recipient as 人员');
  2073. $data = [
  2074. 'number' => $number,
  2075. 'username' => $lastNumber
  2076. ];
  2077. $this->success('成功', $data);
  2078. }
  2079. }