WorkOrder.php 74 KB

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