WorkOrder.php 85 KB

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