WorkOrder.php 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726
  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. $massage = $param['面料'].',粘衬:'.$param['粘衬'];
  790. $mianliao = $this->Gpt($massage);
  791. $data = [];
  792. foreach ($mianliao as $key => $value){
  793. $data[$key] = [
  794. 'BOM_工单编号' => $param['订单编号'],
  795. 'BOM_物料名称' => $value,
  796. 'Sys_rq' => date('Y-m-d H:i:s',time()),
  797. 'Sys_id' => $param['Sys_id']
  798. ];
  799. }
  800. Db::startTrans();
  801. try {
  802. //插入工单基本资料
  803. $sql= \db('工单_基本资料')->fetchSql(true)->insert($param);
  804. $res = \db()->query($sql);
  805. //插入订单物料信息
  806. $BomSql = \db('工单_bom资料')->fetchSql(true)->insertAll($data);
  807. $BomRes = \db()->query($BomSql);
  808. //提交
  809. Db::commit();
  810. }catch (\Exception $e){
  811. //回滚
  812. Db::rollback();
  813. }
  814. $this->success('成功');
  815. }
  816. /**
  817. *
  818. */
  819. // public function ClientList(){
  820. // }
  821. /**
  822. *
  823. */
  824. // public function ProductCodeList(){
  825. // }
  826. /**
  827. * 新增印件资料->印件资料添加
  828. * @return void
  829. * @throws \think\db\exception\BindParamException
  830. * @throws \think\exception\PDOException
  831. */
  832. public function PrintDetailAdd()
  833. {
  834. if (Request::instance()->isPost() === false){
  835. $this->error('请求错误');
  836. }
  837. $param = Request::instance()->post();
  838. if (empty($param)){
  839. $this->error('参数错误');
  840. }
  841. $param['Sys_rq'] = date('Y-m-d H:i:s',time());
  842. $process = [
  843. ['1','仓库出库'],['2','裁剪'],['3','车缝'],['4','后道收样'],['5','大烫'],['6','总检'],['7','包装']
  844. ];
  845. $processDetail = [];
  846. foreach ($process as $key=>$value){
  847. $total = null;
  848. if ($key !== 0){
  849. $total = $param['zdtotal'];
  850. }
  851. $processDetail[$key] = [
  852. '订单编号' => $param['订单编号'],
  853. '子订单编号' => $param['子订单编号'],
  854. '款号' => $param['款号'],
  855. '颜色' => $param['颜色'],
  856. '颜色备注' => $param['颜色备注'],
  857. '工序编号' => $value[0],
  858. '工序名称' => $value[1],
  859. '计划产量' => $total,
  860. 'Sys_id' => $param['Sys_id'],
  861. 'Sys_rq' => $param['Sys_rq']
  862. ];
  863. }
  864. //开启事务
  865. db()->startTrans();
  866. try{
  867. $priSql = \db('工单_印件资料')->fetchSql(true)->insert($param);
  868. $priRes = \db()->query($priSql);
  869. $proSql = \db('工单_工艺资料')->fetchSql(true)->insertAll($processDetail);
  870. $proRes = \db()->query($proSql);
  871. // 提交事务
  872. db()->commit();
  873. } catch (\Exception $e) {
  874. // 回滚事务
  875. db()->rollback();
  876. $this->error($e->getMessage());
  877. }
  878. if ($priRes !== false && $proRes !== false){
  879. $this->success('成功');
  880. }else{
  881. $this->error('失败');
  882. }
  883. }
  884. /**
  885. *
  886. */
  887. // public function ProcessDetailAdd(){
  888. // }
  889. /**
  890. *
  891. */
  892. // public function PrintDetailList(){
  893. // }
  894. /**
  895. * 子订单列表
  896. * @return null
  897. * @throws \think\db\exception\DataNotFoundException
  898. * @throws \think\db\exception\ModelNotFoundException
  899. * @throws \think\exception\DbException
  900. */
  901. public function PrintListData()
  902. {
  903. // 检查请求方式
  904. if ($this->request->isGet() === false) {
  905. $this->error('请求错误');
  906. }
  907. $param = $this->request->param();
  908. // 检查参数是否存在
  909. if (isset($param) === false) {
  910. $this->error('参数错误');
  911. }
  912. // 查询型号
  913. $where['Mod_rq'] = null;
  914. $xhdata = \db('工单_印件资料')
  915. ->where('订单编号', $param['order'])
  916. ->where($where)
  917. ->field('cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10')
  918. ->select();
  919. $arr = [];
  920. // 收集cm1到cm10的非空非null值
  921. foreach ($xhdata as $key => $value) {
  922. for ($i = 1; $i <= 10; $i++) {
  923. if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) {
  924. array_push($arr, $value['cm' . $i]);
  925. }
  926. }
  927. }
  928. $arr = array_unique($arr);
  929. sort($arr);
  930. // 查询详细列表
  931. $list = \db('工单_印件资料')
  932. ->where('订单编号', $param['order'])
  933. ->where($where)
  934. ->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')
  935. ->select();
  936. // 遍历列表并处理cm和cmsl字段
  937. foreach ($list as $key => $value) {
  938. for ($i = 1; $i <= 10; $i++) {
  939. if ($value['cm' . $i] !== '') {
  940. // 如果 cmsl 的值为 0,则设置为空字符串
  941. $list[$key][$value['cm' . $i]] = ($value['cmsl' . $i] === 0) ? '' : $value['cmsl' . $i];
  942. }
  943. // 删除原有的 cm 和 cmsl 字段
  944. // unset($list[$key]['cm' . $i], $list[$key]['cmsl' . $i]);
  945. }
  946. }
  947. // 自定义型号排序
  948. $customOrder = array('XXS','XS','S', 'M', 'L', 'XL','XXL', 'XXXL', 'XXXXL', '2XL', '3XL', '4XL');
  949. usort($arr, function ($a, $b) use ($customOrder) {
  950. $posA = array_search($a, $customOrder);
  951. $posB = array_search($b, $customOrder);
  952. return $posA - $posB;
  953. });
  954. // 返回结果
  955. $data['型号'] = $arr;
  956. $data['列表'] = $list;
  957. $this->success('成功', $data);
  958. }
  959. /**
  960. * 工单资料管理->印件资料删除
  961. * @return void
  962. * @throws \think\Exception
  963. * @throws \think\exception\PDOException
  964. */
  965. public function PrintDetailDel()
  966. {
  967. if ($this->request->isGet() === false){
  968. $this->error('请求错误');
  969. }
  970. $param = $this->request->param();
  971. if (isset($param['UniqId']) === false){
  972. $this->error('参数错误');
  973. }
  974. $printId = explode(',',$param['UniqId']);
  975. $i = 0;
  976. foreach ($printId as $value){
  977. $res = \db('工单_印件资料')
  978. ->where('Uniqid',$value)
  979. ->update(['Mod_rq'=>date('Y-m-d H:i:s',time())]);
  980. if ($res === false){
  981. $i++;
  982. }
  983. }
  984. if ($i === 0){
  985. $this->success('删除成功');
  986. }else{
  987. $this->error('删除失败');
  988. }
  989. }
  990. /**
  991. * 月度车间报工汇总->报工删除记录
  992. */
  993. public function ProcessDetailDel()
  994. {
  995. if ($this->request->isGet() === false){
  996. $this->error('请求错误');
  997. }
  998. $param = $this->request->param();
  999. if (empty($param)) {
  1000. $this->error('参数错误');
  1001. }
  1002. $where['a.mod_rq'] = ['neq', ''];
  1003. $list = \db('设备_产量计酬')->alias('a')
  1004. ->join('工单_印件资料 b', 'b.订单编号 = a.订单编号 AND a.子订单编号 = a.子订单编号')
  1005. ->join('工单_基本资料 j', 'b.订单编号 = j.订单编号', 'LEFT')
  1006. ->field('
  1007. b.订单编号, b.子订单编号, b.款号, b.颜色, b.船样, a.尺码, b.zdtotal as 制单数,b.颜色备注,
  1008. a.数量, a.sys_rq as 上报时间,a.UniqId,a.mod_rq,a.delsys_id,a.sczl_bh,
  1009. j.客户编号,j.生产款号,j.款式
  1010. ')
  1011. ->where($where)
  1012. ->order('a.mod_rq desc')
  1013. ->limit($param['page'],$param['limit'])
  1014. ->group('a.UniqId')
  1015. ->select();
  1016. $count = \db('设备_产量计酬')->alias('a')
  1017. ->join('工单_印件资料 b', 'b.订单编号 = a.订单编号 AND a.子订单编号 = a.子订单编号')
  1018. ->join('工单_基本资料 j', 'b.订单编号 = j.订单编号', 'LEFT')
  1019. ->field('
  1020. b.订单编号, b.子订单编号, b.款号, b.颜色, b.船样, a.尺码, b.zdtotal as 制单数,b.颜色备注,
  1021. a.数量, a.sys_rq as 上报时间,a.UniqId,a.mod_rq,a.delsys_id,a.sczl_bh,
  1022. j.客户编号,j.生产款号,j.款式
  1023. ')
  1024. ->where($where)
  1025. ->order('a.mod_rq desc')
  1026. ->group('a.UniqId')
  1027. ->select();
  1028. // 提取所有的尺码,并去重
  1029. $sizeList = array_values(array_unique(array_column($list, '尺码')));
  1030. // 动态将尺码的数量添加到每个订单中,并替换已完成字段
  1031. foreach ($list as &$item) {
  1032. $size = $item['尺码'];
  1033. $item[$size] = $item['数量']; // 动态添加尺码字段,值为数量
  1034. // unset($item['数量']); // 移除原来的已完成字段
  1035. }
  1036. $data['total'] = count($count);
  1037. $data['table'] = $list;
  1038. $this->success('成功',$data);
  1039. }
  1040. /**
  1041. *
  1042. */
  1043. // public function WastageList(){
  1044. // }
  1045. /**
  1046. *
  1047. */
  1048. // public function getAnnexTable(){
  1049. // }
  1050. /**
  1051. *
  1052. */
  1053. // public function annexAdd(){
  1054. // }
  1055. /**
  1056. *
  1057. */
  1058. // public function annexDel(){
  1059. // }
  1060. /**
  1061. *
  1062. */
  1063. // public function getGdAnnexTable(){
  1064. // }
  1065. /**
  1066. * 产品附件新增
  1067. * 1.前端进行上传xlsx文件表格
  1068. * 2.接口接受数据文件进行保存xlsx文件以及pdf转换
  1069. * 3.前端进行预览pdf图片显示文件中的内容再页面上
  1070. */
  1071. public function gdAnnexAdd() {
  1072. ini_set('display_errors', 'On');
  1073. ini_set('error_reporting', E_ALL);
  1074. if (!$this->request->isPost()) {
  1075. $this->error('请求方式错误');
  1076. }
  1077. // 获取请求参数
  1078. $req = $this->request->param();
  1079. $relateId = $req['关联编号'];
  1080. $attachmentContent = $req['附件内容'];
  1081. $attachmentType = $req['附件类型'];
  1082. $prefixDir = ROOT_PATH . '/public/';
  1083. $uploadDir = ''.'uploads/' . date('Ymd') . '/' . $relateId;
  1084. DB::name('工单_基本资料')
  1085. ->where('订单编号', $relateId)
  1086. ->update(['gd_statu' => '2-生产中']);
  1087. // 检查并创建目录
  1088. if (!is_dir($prefixDir . $uploadDir)) {
  1089. mkdir($prefixDir . $uploadDir, 0777, true);
  1090. }
  1091. // 处理 Base64 附件内容
  1092. if (strpos($attachmentContent, 'base64,') !== false) {
  1093. $attachmentContent = explode('base64,', $attachmentContent)[1];
  1094. }
  1095. $fileContent = base64_decode($attachmentContent);
  1096. $filename = time();
  1097. // 初始化文件路径变量
  1098. $xlsxFilePath = '';
  1099. $pdfFilePath = '';
  1100. if ($attachmentType === 'pdf') {
  1101. // 保存 PDF 文件
  1102. $pdfFilePath = $uploadDir . '/' . $filename . '.pdf';
  1103. file_put_contents($prefixDir . $pdfFilePath, $fileContent);
  1104. } elseif ($attachmentType === 'xlsx') {
  1105. // 保存 Excel 文件
  1106. $xlsxFilePath = $uploadDir . '/' . $filename . '.xlsx';
  1107. file_put_contents($prefixDir . $xlsxFilePath, $fileContent);
  1108. // 转换为 PDF 文件
  1109. $pdfFilePath = $uploadDir . '/' . $filename . '.pdf';
  1110. $cmd = sprintf(
  1111. 'libreoffice --headless --convert-to pdf --outdir %s %s',
  1112. escapeshellarg($prefixDir . $uploadDir),
  1113. escapeshellarg($prefixDir . $xlsxFilePath)
  1114. );
  1115. exec($cmd, $out, $retval);
  1116. if ($retval !== 0) {
  1117. $this->error('Excel 转 PDF 失败');
  1118. }
  1119. } else {
  1120. $this->error('不支持的附件类型');
  1121. }
  1122. // 组织数据
  1123. $data = [
  1124. '关联编号' => $relateId,
  1125. 'sys_id' => $req['sys_id'],
  1126. '附件备注' => $req['附件备注'],
  1127. '附件类型' => $attachmentType,
  1128. 'sys_rq' => date('Y-m-d H:i:s'),
  1129. 'url' => $xlsxFilePath, // 保存 xlsx 文件路径(如果存在)
  1130. 'pdf' => $pdfFilePath // 保存 pdf 文件路径
  1131. ];
  1132. // 数据库事务处理
  1133. db()->startTrans();
  1134. try {
  1135. // 插入数据
  1136. $sql = db('工单_相关附件')->fetchSql(true)->insert($data);
  1137. $result = db()->query($sql);
  1138. db()->commit();
  1139. } catch (\Exception $e) {
  1140. // 回滚事务
  1141. db()->rollback();
  1142. $this->error($e->getMessage());
  1143. }
  1144. if ($result === false) {
  1145. $this->error('失败');
  1146. }
  1147. $this->success('成功');
  1148. }
  1149. /**
  1150. *
  1151. */
  1152. // public function gdAnnexDel()
  1153. // {
  1154. // }
  1155. /**
  1156. *
  1157. */
  1158. // public function WorkOrderDetailCope()
  1159. // {
  1160. // }
  1161. //
  1162. /**
  1163. *
  1164. */
  1165. // public function WorkOrderDetailListCope()
  1166. // {
  1167. // }
  1168. /**
  1169. *
  1170. */
  1171. // public function ProductCopeDetail()
  1172. // {
  1173. // }
  1174. /**
  1175. *
  1176. */
  1177. // public function PlannedProcessYield($workOrder,$yjno,$gxh,$feed)
  1178. // {
  1179. // }
  1180. /**
  1181. *
  1182. */
  1183. // public function WorkOrderDel()
  1184. // {
  1185. // }
  1186. /**
  1187. *
  1188. */
  1189. // public function ProductNameData()
  1190. // {
  1191. // }
  1192. /**
  1193. *
  1194. */
  1195. // public function getCouplet()
  1196. // }
  1197. /**
  1198. * 订单打印接口
  1199. * order:订单编号
  1200. * 通过订单编号获取订单表数据以及子订单数据
  1201. */
  1202. public function orderPrint(){
  1203. if ($this->request->isGet() === false){$this->error('请求错误');}
  1204. $param = $this->request->param();
  1205. if (empty($param['order'])){$this->error('参数错误');}
  1206. $where['Mod_rq'] = null;
  1207. //订单信息
  1208. $list = \db('工单_基本资料')
  1209. ->where('订单编号',$param['order'])
  1210. ->where($where)
  1211. ->field('订单编号,img,生产款号,客户编号,款式,落货日期,箱唛要求,面料,船样描述,船样合计,粘衬,订单数量,审核,审核日期,要求,water')
  1212. ->find();
  1213. //尺码表格表头
  1214. $xhdata = \db('工单_印件资料')->where('订单编号', $param['order'])->where($where)->select();
  1215. $arr = [];
  1216. foreach ($xhdata as $key => $value) {
  1217. for ($i = 1; $i <= 10; $i++) {
  1218. if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) {
  1219. array_push($arr, $value['cm' . $i]);
  1220. }
  1221. }
  1222. }
  1223. // 去重并重新索引
  1224. $arr = array_unique($arr);
  1225. $arr = array_values($arr);
  1226. // 自定义排序函数
  1227. usort($arr, function($a, $b) {
  1228. // 判断是否为数字
  1229. $isNumericA = is_numeric($a);
  1230. $isNumericB = is_numeric($b);
  1231. if ($isNumericA && $isNumericB) {
  1232. // 如果都是数字,按从小到大排序
  1233. return $a - $b;
  1234. } elseif (!$isNumericA && !$isNumericB) {
  1235. // 如果都是字母,按字母顺序排序(可以自定义顺序)
  1236. $sizeOrder = ['XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL','XXXXL'];
  1237. $posA = array_search($a, $sizeOrder);
  1238. $posB = array_search($b, $sizeOrder);
  1239. return $posA - $posB;
  1240. } else {
  1241. // 如果一个是数字一个是字母,数字排在前
  1242. return $isNumericA ? -1 : 1;
  1243. }
  1244. });
  1245. //打印table数据表格
  1246. $porlis = \db('工单_印件资料')
  1247. ->where('订单编号',$param['order'])
  1248. ->where('船样',0)
  1249. ->where($where)
  1250. ->field('子订单编号')
  1251. ->select();
  1252. //合并后的子订单条码数据
  1253. $subOrder = $porlis[0]['子订单编号'];
  1254. // 找到子订单编号中的 '-' 位置
  1255. $dashPos = strpos($subOrder, '-');
  1256. if ($dashPos !== false) {
  1257. // 提取 '-' 后面的部分
  1258. $afterDash = substr($subOrder, $dashPos + 1);
  1259. // 判断长度是否等于2或等于4
  1260. if (strlen($afterDash) == 2) {
  1261. // 查询船样为0的数据
  1262. $processlist = \db('工单_印件资料')
  1263. ->where('订单编号', $param['order'])
  1264. ->where($where)
  1265. ->where('船样', 0)
  1266. ->field('子订单编号,颜色,款号,zdtotal,颜色备注,color_id,
  1267. cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10,
  1268. cmsl1,cmsl2,cmsl3,cmsl4,cmsl5,cmsl6,cmsl7,cmsl8,cmsl9,cmsl10,Uniqid')
  1269. ->select();
  1270. // 初始化汇总数组
  1271. $scslTotals = [
  1272. "cmsl1" => 0,
  1273. "cmsl2" => 0,
  1274. "cmsl3" => 0,
  1275. "cmsl4" => 0,
  1276. "cmsl5" => 0,
  1277. "cmsl6" => 0,
  1278. "cmsl7" => 0,
  1279. "cmsl8" => 0,
  1280. "cmsl9" => 0,
  1281. "cmsl10" => 0,
  1282. "zdtotal" => 0, // 总计数量
  1283. ];
  1284. // 遍历数据集进行汇总
  1285. foreach ($processlist as $item) {
  1286. // 遍历每个尺码字段(cmsl1 到 cmsl10)
  1287. for ($i = 1; $i <= 10; $i++) {
  1288. // 获取当前字段的数量
  1289. $sizeQty = $item['cmsl' . $i];
  1290. // 判断数量是否有效(非空且大于0)
  1291. if (!empty($sizeQty)) {
  1292. // 累加当前字段的数量
  1293. $scslTotals['cmsl' . $i] += $sizeQty;
  1294. // 累加到总计字段
  1295. $scslTotals['zdtotal'] += $sizeQty;
  1296. }
  1297. }
  1298. }
  1299. $data['scslTotals'] = $scslTotals;
  1300. foreach ($processlist as $key => $value) {
  1301. // 将尺码和对应的数量从 cm1-cm10 和 cmsl1-cmsl10 转换为动态键值对
  1302. for ($i = 1; $i <= 10; $i++) {
  1303. if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) {
  1304. $processlist[$key][$value['cm' . $i]] = $value['cmsl' . $i];
  1305. }
  1306. // 移除原始的 cm 和 cmsl 字段
  1307. unset($processlist[$key]['cm' . $i], $processlist[$key]['cmsl' . $i]);
  1308. }
  1309. }
  1310. // 用于存储合并后的数据
  1311. $mergedData = [];
  1312. // 按颜色备注进行合并
  1313. foreach ($processlist as $item) {
  1314. $key = $item['颜色备注'];
  1315. if (!isset($mergedData[$key])) {
  1316. $mergedData[$key] = $item;
  1317. // 添加条码字段,值为子订单编号
  1318. $mergedData[$key]['条码'] = $item['子订单编号'];
  1319. } else {
  1320. // 合并尺码对应的数量
  1321. foreach ($item as $size => $quantity) {
  1322. if (is_numeric($size)) {
  1323. if (!isset($mergedData[$key][$size])) {
  1324. $mergedData[$key][$size] = 0;
  1325. }
  1326. $mergedData[$key][$size] += $quantity;
  1327. }
  1328. }
  1329. // 合并 zdtotal
  1330. $mergedData[$key]['zdtotal'] += $item['zdtotal'];
  1331. }
  1332. }
  1333. // 查询船样为1的数据
  1334. $chuanyang = \db('工单_印件资料')
  1335. ->where('订单编号', $param['order'])
  1336. ->where($where)
  1337. ->where('船样', 1)
  1338. ->field('子订单编号,颜色,款号,zdtotal,颜色备注,color_id,
  1339. cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10,
  1340. cmsl1,cmsl2,cmsl3,cmsl4,cmsl5,cmsl6,cmsl7,cmsl8,cmsl9,cmsl10,Uniqid')
  1341. ->select();
  1342. foreach ($chuanyang as $key => $value) {
  1343. // 将尺码和对应的数量从 cm1-cm10 和 cmsl1-cmsl10 转换为动态键值对
  1344. for ($i = 1; $i <= 10; $i++) {
  1345. if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) {
  1346. $chuanyang[$key][$value['cm' . $i]] = $value['cmsl' . $i];
  1347. }
  1348. // 移除原始的 cm 和 cmsl 字段
  1349. unset($chuanyang[$key]['cm' . $i], $chuanyang[$key]['cmsl' . $i]);
  1350. }
  1351. // 添加条码字段,值为子订单编号
  1352. $chuanyang[$key]['条码'] = $value['子订单编号'];
  1353. }
  1354. // 将合并后的数据插入到相应颜色备注的原始数据尾部
  1355. $finalList = [];
  1356. $groupedData = [];
  1357. // 将原始数据按颜色备注分组
  1358. foreach ($processlist as $item) {
  1359. $key = $item['颜色备注'];
  1360. if (!isset($groupedData[$key])) {
  1361. $groupedData[$key] = [];
  1362. }
  1363. $groupedData[$key][] = $item;
  1364. }
  1365. // 将合并后的数据插入到对应的颜色备注组的尾部
  1366. foreach ($groupedData as $key => $items) {
  1367. $finalList = array_merge($finalList, $items); // 先添加原始数据
  1368. if (isset($mergedData[$key])) {
  1369. $finalList[] = $mergedData[$key]; // 在组的尾部添加合并数据
  1370. }
  1371. }
  1372. // 将船样为1的数据添加到最终列表中
  1373. $finalList = array_merge($finalList, $chuanyang);
  1374. $data['process'] = $finalList;
  1375. $data['order'] = $list;
  1376. $data['xhdata'] = $arr;
  1377. $this->success('成功',$data);
  1378. } elseif (strlen($afterDash) == 4) {
  1379. //一条子订单编号一个条码,统计颜色
  1380. // $processlist = \db('工单_印件资料')
  1381. // ->where('订单编号', $param['order'])
  1382. // ->whereNull('Mod_rq') // 查询未删除数据
  1383. // ->select();
  1384. //
  1385. // $table = [];
  1386. // foreach ($processlist as $item) {
  1387. // // 当前子订单编号的数据
  1388. // $subOrder = [
  1389. // '颜色备注' => $item['颜色备注'],
  1390. // '色系名称' => $item['颜色'],
  1391. // '订单编号' => $item['订单编号'],
  1392. // '子订单编号' => $item['子订单编号'],
  1393. // '条码' => $item['子订单编号'],
  1394. // '款号' => $item['款号'],
  1395. // ];
  1396. // // 当前子订单编号的合计
  1397. // $subOrderTotal = 0;
  1398. // for ($i = 1; $i <= 10; $i++) {
  1399. // if (!empty($item['cm' . $i])) {
  1400. // $subOrder[$item['cm' . $i]] = $item['cmsl' . $i];
  1401. // // 累加每个尺码的数量
  1402. // $subOrderTotal += $item['cmsl' . $i];
  1403. // }
  1404. // unset($item['cm' . $i], $item['cmsl' . $i]);
  1405. // }
  1406. // $subOrder['合计'] = $subOrderTotal; // 添加合计
  1407. // $table[] = $subOrder; // 将当前子订单的数据添加到 $table 中
  1408. // }
  1409. $processlist = \db('工单_印件资料')
  1410. ->where('订单编号', $param['order'])
  1411. ->whereNull('Mod_rq') // 查询未删除数据
  1412. ->select();
  1413. $table = [];
  1414. foreach ($processlist as $item) {
  1415. // 当前子订单编号的数据
  1416. $subOrder = [
  1417. '颜色备注' => $item['颜色备注'],
  1418. '色系名称' => $item['颜色'],
  1419. '订单编号' => $item['订单编号'],
  1420. '子订单编号' => $item['子订单编号'],
  1421. '条码' => $item['子订单编号'],
  1422. '款号' => $item['款号'],
  1423. ];
  1424. // 当前子订单编号的合计
  1425. $subOrderTotal = 0;
  1426. for ($i = 1; $i <= 10; $i++) {
  1427. // 判断 cm 和 cmsl 是否有值,空值不显示,0 则显示
  1428. if (isset($item['cm' . $i]) && $item['cm' . $i] !== '') {
  1429. $subOrder[$item['cm' . $i]] = $item['cmsl' . $i] ?? 0; // 默认数量为 0
  1430. // 累加每个尺码的数量
  1431. $subOrderTotal += (int)$item['cmsl' . $i]; // 确保是数值类型
  1432. }
  1433. // 清理字段
  1434. unset($item['cm' . $i], $item['cmsl' . $i]);
  1435. }
  1436. $subOrder['合计'] = $subOrderTotal; // 添加合计
  1437. $table[] = $subOrder; // 将当前子订单的数据添加到 $table 中
  1438. }
  1439. // 初始化汇总数组
  1440. $scslTotals = [
  1441. "cmsl1" => 0,
  1442. "cmsl2" => 0,
  1443. "cmsl3" => 0,
  1444. "cmsl4" => 0,
  1445. "cmsl5" => 0,
  1446. "cmsl6" => 0,
  1447. "cmsl7" => 0,
  1448. "cmsl8" => 0,
  1449. "cmsl9" => 0,
  1450. "cmsl10" => 0,
  1451. "zdtotal" => 0, // 总计数量
  1452. ];
  1453. // 遍历数据集进行汇总
  1454. foreach ($processlist as $item) {
  1455. // 遍历每个尺码字段(cmsl1 到 cmsl10)
  1456. for ($i = 1; $i <= 10; $i++) {
  1457. // 获取当前字段的数量
  1458. $sizeQty = $item['cmsl' . $i];
  1459. // 判断数量是否有效(非空且大于0)
  1460. if (!empty($sizeQty)) {
  1461. // 累加当前字段的数量
  1462. $scslTotals['cmsl' . $i] += $sizeQty;
  1463. // 累加到总计字段
  1464. $scslTotals['zdtotal'] += $sizeQty;
  1465. }
  1466. }
  1467. }
  1468. $data['scslTotals'] = $scslTotals;
  1469. $data['process'] = $table;//汇总数据集
  1470. $data['order'] = $list;//表格数据
  1471. $data['xhdata'] = $arr;//尺码表头
  1472. $this->success('成功', $data);
  1473. // }
  1474. } else {
  1475. echo "子订单编号 - 后不是2位也不是4位:$afterDash";
  1476. }
  1477. } else {
  1478. echo "子订单编号中没有找到'-'";
  1479. }
  1480. }
  1481. /**
  1482. * 订单编号自动获取
  1483. * @return void
  1484. * @throws \think\db\exception\DataNotFoundException
  1485. * @throws \think\db\exception\ModelNotFoundException
  1486. * @throws \think\exception\DbException
  1487. */
  1488. public function getWorkOrder()
  1489. {
  1490. if ($this->request->isGet() === false){
  1491. $this->error('请求错误');
  1492. }
  1493. $time =substr( date('Ym',time()),2);
  1494. $lastOrder = \db('工单_基本资料')
  1495. ->where('订单编号','like','%'.$time.'%')
  1496. ->order('Uniqid desc')
  1497. ->find();
  1498. if (empty($lastOrder)){
  1499. $newNumber = 1;
  1500. }else{
  1501. $lastNumber = substr($lastOrder['订单编号'],6);
  1502. $newNumber = (int)$lastNumber + 1;
  1503. }
  1504. if ($newNumber<10){
  1505. $newOrder = 'DC'.$time.'00'.$newNumber;
  1506. }elseif ($newNumber>=10 && $newNumber<100){
  1507. $newOrder = 'DC'.$time.'0'.$newNumber;
  1508. }else{
  1509. $newOrder = 'DC'.$time.$newNumber;
  1510. }
  1511. $this->success('成功',$newOrder);
  1512. }
  1513. /**
  1514. * 获取子订单编号
  1515. * @return void
  1516. * @throws \think\db\exception\DataNotFoundException
  1517. * @throws \think\db\exception\ModelNotFoundException
  1518. * @throws \think\exception\DbException
  1519. */
  1520. public function getSuborder(){
  1521. // 确保是GET请求
  1522. if ($this->request->isGet() === false) {
  1523. $this->error('请求错误');
  1524. }
  1525. $param = $this->request->param();
  1526. if (empty($param) || !isset($param['cy']) || !isset($param['order'])) {
  1527. $this->error('参数错误');
  1528. }
  1529. // 判断是否“船样”获取对应的子订单编号
  1530. if ($param['cy'] == '否') {
  1531. //1.通过色系名称查询对应的编号
  1532. $colorlist = \db('工单_颜色编号')
  1533. ->field('colorcode, colorname')
  1534. ->where('colorname', $param['colorname'] ?? '')
  1535. ->find();
  1536. if (empty($colorlist)) {
  1537. $this->error('未找到对应的颜色编号');
  1538. }
  1539. $num = $param['order'] . '-' . $colorlist['colorcode'];
  1540. // 查询子订单编号
  1541. $data = \db('工单_印件资料')
  1542. ->field('子订单编号,cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10')
  1543. ->where('子订单编号', 'like', '%' . $num . '%')
  1544. ->where('船样', '=', 0)
  1545. ->order('子订单编号', 'desc')
  1546. ->find();
  1547. if (empty($data)) {
  1548. // 如果没有找到数据,生成默认的订单编号,后两位从00开始
  1549. $order = $param['order'] . '-' . $colorlist['colorcode'] . '00';
  1550. } else {
  1551. if (strlen($data['子订单编号']) == 12) {
  1552. $data = \db('工单_印件资料')
  1553. ->where('订单编号',$param['order'])
  1554. ->where('船样',0)
  1555. ->order('子订单编号 desc')
  1556. ->find();
  1557. if(empty($data)){
  1558. $order = $param['order'].'-01';
  1559. }else{
  1560. $num = (int)substr($data['子订单编号'],10) + 1;
  1561. if ($num<10){
  1562. $order = $param['order'].'-0'.$num;
  1563. }else{
  1564. $order = $param['order'].'-'.$num;
  1565. }
  1566. }
  1567. }else{
  1568. // 如果找到数据,提取子订单编号并递增
  1569. $order = $data['子订单编号'];
  1570. if (preg_match('/(.*-' . $colorlist['colorcode'] . ')(\d{2})$/', $order, $matches)) {
  1571. $prefix = $matches[1]; // 前缀部分(包括订单编号和颜色代码)
  1572. $number = $matches[2]; // 数字部分(后两位)
  1573. // 循环生成子订单编号并检查数据库中是否存在相同编号
  1574. do {
  1575. $incrementedNumber = (int)$number + 1;
  1576. // 将数字部分补充为两位数,例如 1 -> 01,2 -> 02
  1577. $order = $prefix . str_pad($incrementedNumber, 2, '0', STR_PAD_LEFT);
  1578. $exists = \db('工单_印件资料')->where('子订单编号', '=', $order)->find();
  1579. $number = $incrementedNumber; // 更新 number 用于下一次循环
  1580. } while ($exists); // 如果存在相同编号则继续循环递增
  1581. } else {
  1582. $this->error('订单编号格式错误');
  1583. }
  1584. }
  1585. }
  1586. //2.获取色系名称信息
  1587. $colorlist = \db('工单_颜色编号')->select();
  1588. //3.获取历史尺码数据
  1589. $cm_list = \db('工单_印件资料')
  1590. ->where('订单编号', $param['order'])
  1591. ->group('订单编号')
  1592. ->find();
  1593. $cm_data = [];
  1594. if ($cm_list) {
  1595. // 精准筛选字段名以 "cm" 开头并跟1到2位数字的字段
  1596. foreach ($cm_list as $key => $value) {
  1597. if (preg_match('/^cm\d{1,2}$/', $key)) {
  1598. $cm_data[$key] = $value ?? ''; // 如果值为 null,设为空字符串
  1599. }
  1600. }
  1601. } else {
  1602. // 如果查询结果为空,将 cm1 到 cm10 都设置为空
  1603. for ($i = 1; $i <= 10; $i++) {
  1604. $cm_data["cm$i"] = '';
  1605. }
  1606. }
  1607. $result = ['order' => $order,'colorlist' => $colorlist,'cm' => $cm_data];
  1608. $this->success('成功', $result);
  1609. } else if ($param['cy'] == '是') {
  1610. //1.获取船样子订单编号
  1611. $data = \db('工单_印件资料')
  1612. ->where('订单编号', $param['order'])
  1613. ->where('船样', '=', 1)
  1614. ->order('子订单编号 asc')
  1615. ->find();
  1616. if (empty($data)) {
  1617. // 如果没有数据,初始子订单编号为 '99'
  1618. $order = $param['order'] . '-99';
  1619. } else {
  1620. // 提取子订单编号中的数字部分
  1621. $subOrder = $data['子订单编号']; // 例如 "DC2410006-99"
  1622. if (preg_match('/-(\d+)$/', $subOrder, $matches)) {
  1623. $numberPart = (int)$matches[1]; // 提取到的数字部分
  1624. $newNumber = $numberPart - 1; // 减 1
  1625. // 将新的数字拼接回订单编号
  1626. $order = preg_replace('/-\d+$/', '-' . $newNumber, $subOrder);
  1627. } else {
  1628. $this->error('订单编号格式错误');
  1629. }
  1630. }
  1631. $this->success('成功', $order);
  1632. }
  1633. }
  1634. /**
  1635. * 获取PO号 【代码展示未用到】
  1636. */
  1637. public function getPonumber() {
  1638. if ($this->request->isGet() === false){
  1639. $this->error('请求错误');
  1640. }
  1641. $param = $this->request->param();
  1642. if (empty($param)){
  1643. $this->error('参数错误');
  1644. }
  1645. // $num = substr($param['child_order'], 0, 12); // 如果需要,可以用这个方式截取子订单编号
  1646. // $sql = "SELECT * FROM `工单_印件资料` WHERE `子订单编号` LIKE '{$num}%' ORDER BY `子订单编号` DESC LIMIT 1";
  1647. // $data = \db()->query($sql);
  1648. $colorlist = \db('工单_颜色编号')
  1649. ->field('colorcode,colorname')
  1650. ->where('colorname',$param['child_order'])
  1651. ->find();
  1652. $num = $param['order'] . '-' . $colorlist['colorcode']; // 生成 num,用于模糊查询
  1653. $data = \db('工单_印件资料')
  1654. ->where('子订单编号', 'like', '%' . $num . '%')
  1655. ->order('子订单编号', 'desc')
  1656. ->find();
  1657. if(count($data) === 1){
  1658. $order = $num.'01';
  1659. }else{
  1660. $number = (int)substr($data['子订单编号'],12,14) + 1;
  1661. if ($num<10){
  1662. $order = $num.'0'.$number;
  1663. }else{
  1664. $order = $num.$number;
  1665. }
  1666. }
  1667. $this->success('成功',$order);
  1668. }
  1669. /**
  1670. * 订单资料修改
  1671. * @return void
  1672. * @throws \think\Exception
  1673. * @throws \think\db\exception\BindParamException
  1674. * @throws \think\exception\PDOException
  1675. */
  1676. public function WorkOrderEdit()
  1677. {
  1678. if (Request::instance()->isPost() === false){
  1679. $this->error('请求错误');
  1680. }
  1681. $param = Request::instance()->post();
  1682. if (empty($param)){
  1683. $this->error('参数错误');
  1684. }
  1685. $id = $param['id'];
  1686. unset($param['id']);
  1687. $sql = \db('工单_基本资料')
  1688. ->where('Uniqid',$id)
  1689. ->fetchSql(true)
  1690. ->update($param);
  1691. $res = \db()->query($sql);
  1692. if ($res === false){
  1693. $this->error('失败');
  1694. }else{
  1695. $this->success('成功');
  1696. }
  1697. }
  1698. /**
  1699. * 颜色资料修改
  1700. * @return void
  1701. * @throws \think\Exception
  1702. * @throws \think\db\exception\BindParamException
  1703. * @throws \think\exception\PDOException
  1704. */
  1705. public function PrintDataEdit()
  1706. {
  1707. if(Request::instance()->post() === false){
  1708. $this->error('请求错误');
  1709. }
  1710. $param = Request::instance()->post();
  1711. if (empty($param)){
  1712. $this->error('参数错误');
  1713. }
  1714. // $id = $param['id'];
  1715. // unset($param['id']);
  1716. // $findlist = \db('工单_印件资料')
  1717. // ->where('Uniqid', $param['id'])
  1718. // ->find();
  1719. $updata = [
  1720. '订单编号' => $param['订单编号'],
  1721. '子订单编号' => $param['子订单编号'],
  1722. '款号' => $param['款号'],
  1723. '船样' => $param['船样'],
  1724. '颜色' => $param['颜色'],
  1725. 'color_id' => $param['color_id'],
  1726. '颜色备注' => $param['颜色备注'],
  1727. 'zdtotal' => $param['zdtotal']
  1728. ];
  1729. for ($i = 1; $i <= 10; $i++) {
  1730. $updata["cmsl{$i}"] = isset($param["cmsl{$i}"]) ? $param["cmsl{$i}"] : '';
  1731. }
  1732. $sql = \db('工单_印件资料')
  1733. ->where('Uniqid', $param['id'])
  1734. ->fetchSql(true)
  1735. ->update($updata);
  1736. $res = \db()->query($sql);
  1737. if ($res !== false) {
  1738. $this->success('修改成功');
  1739. } else {
  1740. $this->error('修改失败');
  1741. }
  1742. }
  1743. /**
  1744. * 图片上传
  1745. * @return void
  1746. */
  1747. public function ImgUpload(){
  1748. $file = request()->file('image');
  1749. if($file){
  1750. $info = $file->validate(['size'=>10485760,'ext'=>'jpg,png'])->move(ROOT_PATH . 'public' . DS . 'uploads');
  1751. if($info){
  1752. $fileName = $info->getSaveName();
  1753. // $ymd = date('Ymd');
  1754. // $imageUrl = '/uploads/' . $ymd.'/'.$fileName;
  1755. $imageUrl = '/uploads/' . str_replace('\\', '/', $fileName);
  1756. return json(['code' => 0, 'msg' => '成功', 'data' => ['url' => $imageUrl]]);
  1757. }else{
  1758. $res = $file->getError();
  1759. return json(['code' => 1, 'msg' => '失败', 'data' => $res]);
  1760. }
  1761. }
  1762. return json(['code' => 1, 'msg' => '没有文件上传', 'data' => null]);
  1763. }
  1764. /**
  1765. * 工单技术附件
  1766. * @return void
  1767. * @throws \think\db\exception\DataNotFoundException
  1768. * @throws \think\db\exception\ModelNotFoundException
  1769. * @throws \think\exception\DbException
  1770. */
  1771. public function OrderAttachments()
  1772. {
  1773. if ($this->request->isGet() === false){
  1774. $this->error('请求错误');
  1775. }
  1776. $param = $this->request->param();
  1777. if (empty($param)){
  1778. $this->error('参数错误');
  1779. }
  1780. $list = \db('工单_相关附件')
  1781. ->where('关联编号',$param['order'])
  1782. ->where('附件备注',$param['desc'])
  1783. ->whereNull('Mod_rq')
  1784. ->order('sys_rq desc')
  1785. ->limit(1)
  1786. ->select();
  1787. $this->success('成功',$list);
  1788. }
  1789. /**
  1790. * 订单BOM资料显示
  1791. * @return void
  1792. * @throws \think\db\exception\DataNotFoundException
  1793. * @throws \think\db\exception\ModelNotFoundException
  1794. * @throws \think\exception\DbException
  1795. */
  1796. public function OrderBomList()
  1797. {
  1798. if ($this->request->isGet() === false){
  1799. $this->error('请求错误');
  1800. }
  1801. $param = $this->request->param();
  1802. if (empty($param) || !isset($param['order'])){
  1803. $this->error('参数错误');
  1804. }
  1805. $where = ['a.BOM_工单编号'=>$param['order']];
  1806. $list = \db('工单_bom资料')
  1807. ->alias('a')
  1808. ->join('工单_基本资料 b','b.订单编号 = a.BOM_工单编号')
  1809. ->field('a.BOM_工单编号 as 订单编号,b.生产款号 as 生产款号,b.客户编号 as 客户编号,b.款式 as 款式,
  1810. a.BOM_物料名称 as 物料名称,a.BOM_投料单位 as 投料单位,a.BOM_计划用量 as 计划用料,a.BOM_标准用量 as 定额用料,
  1811. a.BOM_实际用量 as 裁床实际用料,a.BOM_领用数量 as 裁床领用面料,a.BOM_退还数量 as 裁床退回仓库面料,
  1812. a.BOM_desc as 备注,a.UNIQID,a.BOM_库存总量 as 入库总量,a.BOM_门封 as 门封,a.BOM_面料结余 as 面料结余,a.Sys_ID as ID,a.Sys_rq as 日期')
  1813. ->where($where)
  1814. ->whereNull('a.Mod_rq')
  1815. ->select();
  1816. if (!empty($list)) {
  1817. $this->success('成功', $list);
  1818. } else {
  1819. // 调用其他方法处理逻辑(例如记录日志、执行其他操作等)
  1820. $this->GdGtpAiOrder($param['order']);
  1821. // 返回空数据的统一处理
  1822. return $this->success('没有找到相关数据', []);
  1823. }
  1824. }
  1825. /**
  1826. * Bom资料删除
  1827. * */
  1828. public function Bomdel()
  1829. {
  1830. if ($this->request->isGet() === false) {
  1831. $this->error('请求错误');
  1832. }
  1833. // 获取请求参数
  1834. $param = $this->request->param();
  1835. // 检查参数是否为空或者缺少UNIQID
  1836. if (empty($param) || !isset($param['UNIQID'])) {
  1837. $this->error('参数错误');
  1838. }
  1839. // 判断UNIQID是否是逗号分隔的多个ID,如果是则拆分成数组,否则直接处理为单个ID
  1840. $uniqids = strpos($param['UNIQID'], ',') !== false ? explode(',', $param['UNIQID']) : [$param['UNIQID']];
  1841. $where = [];
  1842. $where['Mod_rq'] = date('Y-m-d H:i:s', time());
  1843. // 定义一个标志变量来追踪是否所有更新都成功
  1844. $allUpdated = true;
  1845. $failedUniqids = [];
  1846. // 遍历所有UNIQID并更新数据库
  1847. foreach ($uniqids as $uniqid) {
  1848. // 更新指定UNIQID的记录
  1849. $result = \db('工单_bom资料')
  1850. ->where('UNIQID', $uniqid)
  1851. ->update($where);
  1852. // 检查更新结果
  1853. if (!$result) {
  1854. // 如果某个UNIQID更新失败,记录失败的ID
  1855. $allUpdated = false;
  1856. $failedUniqids[] = $uniqid;
  1857. }
  1858. }
  1859. // 如果所有更新都成功,返回成功信息
  1860. if ($allUpdated) {
  1861. $list = \db('工单_bom资料')
  1862. ->whereIn('UNIQID', $uniqids) // 查询所有传入的UNIQID
  1863. ->select();
  1864. if (!empty($list)) {
  1865. $this->success('删除成功');
  1866. } else {
  1867. $this->GdGtpAiOrder($param['order']);
  1868. return $this->success('没有找到相关数据', []);
  1869. }
  1870. } else {
  1871. // 如果有更新失败的记录,返回失败的UNIQID
  1872. $this->error('部分更新失败,无法更新以下UNIQID: ' . implode(', ', $failedUniqids));
  1873. }
  1874. }
  1875. /**
  1876. * 前端选择订单时如果BOM资料数据为空则单独调用,重新生成最新
  1877. */
  1878. public function GdGtpAiOrder($order){
  1879. // 判断是否有指定的订单号
  1880. if (!empty($order)) {
  1881. // 查询单个订单的最大编号
  1882. $maxOrder = \db('工单_基本资料')
  1883. ->where('订单编号', 'like', "{$order}%")
  1884. ->order('订单编号', 'desc')
  1885. ->limit(1)
  1886. ->value('订单编号');
  1887. // 查询该订单的基本资料
  1888. $list = \db('工单_基本资料')
  1889. ->where('订单编号', 'like', "{$order}%")
  1890. ->order('订单编号', 'desc')
  1891. ->limit(1)
  1892. ->find();
  1893. // 如果面料数据为空,提示错误
  1894. if (empty($list['面料'])) {
  1895. $this->error('面料数据为空无法定义BOM');
  1896. }
  1897. // 处理订单编号
  1898. $numericPart = substr($maxOrder, 2);
  1899. $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT);
  1900. $param['订单编号'] = $order . $newNumericPart;
  1901. // 处理物料信息
  1902. $massage = empty($list['粘衬']) || $list['粘衬'] == '无' ? $list['面料'] : $list['面料'] . ',粘衬:' . $list['粘衬'];
  1903. $mianliao = $this->Gpt($massage);
  1904. // 插入物料数据
  1905. $data = [];
  1906. foreach ($mianliao as $key => $value) {
  1907. if (!empty($value) && $value !== '粘衬') { // 排除空值和粘衬
  1908. $data[] = [
  1909. 'BOM_工单编号' => $list['订单编号'],
  1910. 'BOM_物料名称' => $value,
  1911. 'Sys_rq' => date('Y-m-d H:i:s'),
  1912. 'Sys_ID' => '超级管理员'
  1913. ];
  1914. }
  1915. }
  1916. // 批量插入BOM资料
  1917. if (!empty($data)) {
  1918. \db('工单_bom资料')->insertAll($data);
  1919. }
  1920. $this->success('成功',$order);
  1921. } else {
  1922. // 如果没有指定订单号,批量查询订单号并处理
  1923. $has_bom = \db('工单_bom资料')->alias('a')->field('a.BOM_工单编号')->group('a.BOM_工单编号')->select();
  1924. $all_orders = \db('工单_基本资料')->alias('a')->field('a.订单编号')->where('a.面料', '<>', '')->group('a.订单编号')->select();
  1925. // 提取有BOM资料的订单号
  1926. $has_bom_orders = array_column($has_bom, 'BOM_工单编号');
  1927. // 筛选出没有对应BOM资料的订单号
  1928. $no_bom_orders = array_filter($all_orders, function ($order) use ($has_bom_orders) {
  1929. return !in_array($order['订单编号'], $has_bom_orders);
  1930. });
  1931. // 遍历没有BOM资料的订单
  1932. foreach ($no_bom_orders as $orderData) {
  1933. // 获取该订单号的最大订单编号
  1934. $maxOrder = \db('工单_基本资料')
  1935. ->where('订单编号', 'like', "{$orderData['订单编号']}%")
  1936. ->order('订单编号', 'desc')
  1937. ->limit(1)
  1938. ->value('订单编号');
  1939. // 获取该订单号的具体数据
  1940. $list = \db('工单_基本资料')
  1941. ->where('订单编号', 'like', "{$orderData['订单编号']}%")
  1942. ->order('订单编号', 'desc')
  1943. ->limit(1)
  1944. ->find();
  1945. if (empty($list['面料'])) {
  1946. $this->error("订单 {$orderData['订单编号']} 面料数据为空,无法定义BOM");
  1947. }
  1948. // 处理订单编号
  1949. $numericPart = substr($maxOrder, 2);
  1950. $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT);
  1951. $param['订单编号'] = $order . $newNumericPart;
  1952. // 处理物料信息
  1953. $massage = empty($list['粘衬']) || $list['粘衬'] == '无' ? $list['面料'] : $list['面料'] . ',粘衬:' . $list['粘衬'];
  1954. $mianliao = $this->Gpt($massage);
  1955. // 插入物料数据
  1956. $data = [];
  1957. foreach ($mianliao as $key => $value) {
  1958. if (!empty($value) && $value !== '粘衬') { // 排除空值和粘衬
  1959. $data[] = [
  1960. 'BOM_工单编号' => $list['订单编号'],
  1961. 'BOM_物料名称' => $value,
  1962. 'Sys_rq' => date('Y-m-d H:i:s'),
  1963. 'Sys_ID' => '超级管理员'
  1964. ];
  1965. }
  1966. }
  1967. // 批量插入BOM资料
  1968. if (!empty($data)) {
  1969. \db('工单_bom资料')->insertAll($data);
  1970. }
  1971. }
  1972. $this->success('成功');
  1973. }
  1974. }
  1975. /**
  1976. * 订单面料修改接口
  1977. * @return void
  1978. * @throws \think\Exception
  1979. * @throws \think\db\exception\BindParamException
  1980. * @throws \think\exception\PDOException
  1981. */
  1982. public function FabricEdit()
  1983. {
  1984. if ($this->request->isPost() === false){
  1985. $this->error('请求错误');
  1986. }
  1987. $param = Request::instance()->post();
  1988. if (empty($param)){
  1989. $this->error('请求错误');
  1990. }
  1991. foreach ($param as $key=>$value){
  1992. $data = $value;
  1993. unset($data['UNIQID']);
  1994. if ($value['UNIQID'] !== '' || !empty($value['UNIQID'])){
  1995. $sql = \db('工单_bom资料')
  1996. ->where('UNIQID',$value['UNIQID'])
  1997. ->fetchSql(true)
  1998. ->update($data);
  1999. $res = \db()->query($sql);
  2000. }else{
  2001. $sql = \db('工单_bom资料')
  2002. ->fetchSql(true)
  2003. ->insert($value);
  2004. $res = \db()->query($sql);
  2005. }
  2006. if ($res === false){
  2007. $this->error('修改失败');
  2008. }
  2009. }
  2010. $this->success('修改成功');
  2011. }
  2012. /**
  2013. * 订单BOM出库、退还详情显示
  2014. * @return void
  2015. * @throws \think\db\exception\DataNotFoundException
  2016. * @throws \think\db\exception\ModelNotFoundException
  2017. * @throws \think\exception\DbException
  2018. */
  2019. public function FabricDetail()
  2020. {
  2021. if ($this->request->isGet() === false){
  2022. $this->error('请求错误');
  2023. }
  2024. $param = $this->request->param();
  2025. if (empty($param) || !isset($param['order'])){
  2026. $this->error('参数错误');
  2027. }
  2028. if (isset($param['searchh']) || !empty($param['search'])){
  2029. $list['入库记录'] = \db('设备_报工日志')
  2030. ->where('order_id|款号',$param['order'])
  2031. ->where('name','入库')
  2032. ->field('id,order_id as 订单编号,款号,sum(number) as 数量,rq as 入库时间,sys_id as 入库人员,recipient as 入仓人员,receipt_number as 单号')
  2033. ->group('receipt_number')
  2034. ->order('入库时间 desc')
  2035. ->whereNull('Mod_rq')
  2036. ->select();
  2037. }else{
  2038. //出库记录查询
  2039. $list['出库记录'] = \db('设备_报工日志')
  2040. ->where('order_id|款号',$param['order'])
  2041. ->where('name','出库')
  2042. ->field('id,order_id as 订单编号,款号,sum(number) as 数量,rq as 出库时间,sys_id as 出库人员,receipt_number as 出库单据编号,recipient as 出库人员,receipt_number as 单号')
  2043. ->group('receipt_number')
  2044. ->order('出库时间 desc')
  2045. ->whereNull('Mod_rq')
  2046. ->select();
  2047. //退还记录查询
  2048. $list['退还记录'] = \db('设备_报工日志')
  2049. ->where('order_id|款号',$param['order'])
  2050. ->where('name','退还')
  2051. ->field('id,order_id as 订单编号,款号,sum(number) as 数量,rq as 退还时间,sys_id as 退还机台,recipient as 退还人员,receipt_number as 单号')
  2052. ->group('receipt_number')
  2053. ->order('退还时间 desc')
  2054. ->whereNull('Mod_rq')
  2055. ->select();
  2056. }
  2057. $this->success('成功',$list);
  2058. }
  2059. /**
  2060. * 入库、出库、退还详情数据
  2061. */
  2062. public function FabricDetaillist()
  2063. {
  2064. if ($this->request->isGet() === false){
  2065. $this->error('请求错误');
  2066. }
  2067. $param = $this->request->param();
  2068. if (empty($param) || !isset($param['order'])){
  2069. $this->error('参数错误');
  2070. }
  2071. if (isset($param['searchh']) || !empty($param['search'])){
  2072. $list['入库记录'] = \db('设备_报工日志')
  2073. ->where('receipt_number',$param['order'])
  2074. ->where('name','入库')
  2075. ->field('id,order_id as 订单编号,款号,number as 数量,rq as 入库时间,sys_id as 入库人员,recipient as 入仓人员,receipt_number as 单号,物料名称')
  2076. ->order('入库时间 desc')
  2077. ->whereNull('Mod_rq')
  2078. ->select();
  2079. }else{
  2080. //出库记录查询
  2081. $list['出库记录'] = \db('设备_报工日志')
  2082. ->where('receipt_number',$param['order'])
  2083. ->where('name','出库')
  2084. ->field('id,order_id as 订单编号,款号,number as 数量,rq as 出库时间,sys_id as 出库人员,recipient as 出库人员,receipt_number as 单号,物料名称')
  2085. ->order('出库时间 desc')
  2086. ->whereNull('Mod_rq')
  2087. ->select();
  2088. //退还记录查询
  2089. $list['退还记录'] = \db('设备_报工日志')
  2090. ->where('receipt_number',$param['order'])
  2091. ->where('name','退还')
  2092. ->field('id,order_id as 订单编号,款号,number as 数量,rq as 退还时间,sys_id as 退还机台,recipient as 退还人员,receipt_number as 单号,物料名称')
  2093. ->order('退还时间 desc')
  2094. ->whereNull('Mod_rq')
  2095. ->select();
  2096. }
  2097. $this->success('成功',$list);
  2098. }
  2099. /**
  2100. * 入库、出库、退还删除
  2101. */
  2102. public function FabricDetaildel()
  2103. {
  2104. if ($this->request->isPost() === false){
  2105. $this->error('请求错误');
  2106. }
  2107. $param = Request::instance()->post();
  2108. if (empty($param)){
  2109. $this->error('请求错误');
  2110. }
  2111. // 判断UNIQID是否是逗号分隔的多个ID,如果是则拆分成数组,否则直接处理为单个ID
  2112. $ids = strpos($param['id'], ',') !== false ? explode(',', $param['id']) : [$param['id']];
  2113. $where = [];
  2114. $where['Mod_id'] = $param['Mod_id'];
  2115. $where['Mod_rq'] = date('Y-m-d H:i:s', time());
  2116. // 定义一个标志变量来追踪是否所有更新都成功
  2117. $allUpdated = true;
  2118. $failedUniqids = [];
  2119. // 遍历所有UNIQID并更新数据库
  2120. foreach ($ids as $id) {
  2121. // 更新指定UNIQID的记录
  2122. $result = \db('设备_报工日志')
  2123. ->where('id', $id)
  2124. ->update($where);
  2125. // 检查更新结果
  2126. if (!$result) {
  2127. // 如果某个UNIQID更新失败,记录失败的ID
  2128. $allUpdated = false;
  2129. $failedUniqids[] = $id;
  2130. }
  2131. }
  2132. // 如果所有更新都成功,返回成功信息
  2133. if ($allUpdated) {
  2134. $list = \db('设备_报工日志')
  2135. ->whereIn('id', $id) // 查询所有传入的UNIQID
  2136. ->select();
  2137. if (!empty($list)) {
  2138. $this->success('删除成功');
  2139. } else {
  2140. $this->GdGtpAiOrder($param['order']);
  2141. return $this->success('没有找到相关数据', []);
  2142. }
  2143. } else {
  2144. // 如果有更新失败的记录,返回失败的UNIQID
  2145. $this->error('部分更新失败,无法更新以下UNIQID: ' . implode(', ', $failedUniqids));
  2146. }
  2147. }
  2148. //GPT
  2149. public function Gpt($massage)
  2150. {
  2151. // 设置 API 密钥
  2152. $apiKey = 'sk-e0JuPjMntkbgi1BoMjrqyyzMKzAxILkQzyGMSy3xiMupuoWY'; // 替换为您的 API 密钥
  2153. // 要发送给 GPT 的消息
  2154. $messages = [
  2155. [
  2156. 'role' => 'user',
  2157. 'content' => '你好,帮我按照:面料1、面料2、面料3……来整理归纳下面的面料信息,我只需要面料信息,不需要其他:'.$massage
  2158. ]
  2159. ];
  2160. // 创建请求数据
  2161. $data = [
  2162. 'model' => 'gpt-3.5-turbo', // 使用的模型
  2163. 'messages' => $messages,
  2164. 'max_tokens' => 100, // 设置最大 token 数
  2165. ];
  2166. // 初始化 cURL
  2167. $ch = curl_init('https://niubi.zeabur.app/v1/chat/completions');
  2168. // 设置 cURL 选项
  2169. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  2170. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  2171. 'Content-Type: application/json',
  2172. 'Authorization: Bearer ' . $apiKey,
  2173. ]);
  2174. curl_setopt($ch, CURLOPT_POST, true);
  2175. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  2176. curl_setopt($ch, CURLOPT_CAINFO, ROOT_PATH . '/public/uploads/cacert.pem');
  2177. // 执行请求
  2178. $response = curl_exec($ch);
  2179. // 检查错误
  2180. if (curl_errno($ch)) {
  2181. echo 'Error:' . curl_error($ch);
  2182. }
  2183. // 关闭 cURL
  2184. curl_close($ch);
  2185. // 解析和输出响应
  2186. $responseData = json_decode($response, true);
  2187. // 获取 GPT 的回复
  2188. if (isset($responseData['choices'][0]['message']['content'])) {
  2189. //获取返回内容
  2190. $gptReply = $responseData['choices'][0]['message']['content'];
  2191. // halt($gptReply);
  2192. //返回面料信息
  2193. $gptArray = explode('面料',$gptReply);
  2194. array_shift($gptArray);
  2195. foreach ($gptArray as $key=>$value){
  2196. $gptArray[$key] = preg_replace('/\s+/', '', substr($value,4));
  2197. }
  2198. return $gptArray;
  2199. } else {
  2200. echo "未能获取 GPT 的回复。";
  2201. }
  2202. }
  2203. /**
  2204. * 面料库存月份查询
  2205. */
  2206. public function fabricListmonth()
  2207. {
  2208. // 使用DATE_FORMAT函数提取年月部分,格式为 "YYYY-MM"
  2209. $list = \db('工单_bom资料')
  2210. ->alias('a')
  2211. ->join('工单_基本资料 b', 'b.订单编号 = a.BOM_工单编号')
  2212. ->field('DATE_FORMAT(a.Sys_rq, "%Y-%m") as date') // 提取年月,格式化为 "YYYY-MM"
  2213. ->group('date') // 对年月字段去重
  2214. ->select();
  2215. // 格式化返回的数据,生成期望的结构
  2216. $result = [];
  2217. foreach ($list as $item) {
  2218. $result[] = ['date' => $item['date']]; // 返回格式为 {"date": "YYYY-MM"}
  2219. }
  2220. if (!empty($result)) {
  2221. $this->success('成功', $result); // 返回查询到的结果
  2222. } else {
  2223. $this->success('没有找到相关数据', []); // 如果没有数据,返回空数组
  2224. }
  2225. }
  2226. /**
  2227. * 面料库存列表
  2228. * @return void
  2229. * @throws \think\db\exception\DataNotFoundException
  2230. * @throws \think\db\exception\ModelNotFoundException
  2231. * @throws \think\exception\DbException
  2232. */
  2233. public function fabricList()
  2234. {
  2235. if ($this->request->isGet() === false) {
  2236. $this->error('请求错误');
  2237. }
  2238. $param = $this->request->param();
  2239. $where = [];
  2240. // 根据传入的参数构造查询条件
  2241. if (isset($param['order'])) {
  2242. $where['a.BOM_工单编号'] = $param['order'];
  2243. }
  2244. if (isset($param['mouth'])) {
  2245. $where['a.Sys_rq'] = ['like', $param['mouth'] . '%'];
  2246. }
  2247. // 分页参数,防止未传递时出错
  2248. $page = isset($param['page']) ? (int)$param['page'] : 1;
  2249. $limit = isset($param['limit']) ? (int)$param['limit'] : 20; // 默认每页20条数据
  2250. // 获取数据
  2251. $data = \db('工单_bom资料')
  2252. ->alias('a')
  2253. ->join('工单_基本资料 b', 'b.订单编号 = a.BOM_工单编号')
  2254. ->field('a.BOM_工单编号 as 订单编号, b.生产款号 as 生产款号, b.客户编号 as 客户编号, b.款式 as 款式,
  2255. a.BOM_物料名称 as 物料名称, a.BOM_投料单位 as 投料单位, a.BOM_计划用量 as 计划用料, a.BOM_标准用量 as 定额用料,
  2256. a.BOM_实际用量 as 裁床实际用料, a.BOM_领用数量 as 裁床领用面料, a.BOM_退还数量 as 裁床退回仓库面料,
  2257. a.BOM_desc as 备注, a.UNIQID, a.BOM_库存总量 as 入库总量, a.BOM_门封 as 门封, a.BOM_面料结余 as 面料结余, a.Sys_ID as ID, a.Sys_rq as 日期')
  2258. ->where($where)
  2259. ->limit(($page - 1) * $limit, $limit)
  2260. ->select();
  2261. // 获取数据总数
  2262. $count = \db('工单_bom资料')
  2263. ->alias('a')
  2264. ->join('工单_基本资料 b', 'b.订单编号 = a.BOM_工单编号')
  2265. ->where($where)
  2266. ->count();
  2267. // 如果查询到数据
  2268. if (!empty($data)) {
  2269. // 构建返回数据
  2270. $list = [
  2271. 'total' => $count, // 总数
  2272. 'table' => $data // 数据内容
  2273. ];
  2274. // 返回成功响应
  2275. $this->success('成功', $list);
  2276. } else {
  2277. // 没有查询到数据
  2278. $this->success('没有找到相关数据', []);
  2279. }
  2280. }
  2281. /**
  2282. * 单条面料详情
  2283. * @return void
  2284. * @throws \think\db\exception\DataNotFoundException
  2285. * @throws \think\db\exception\ModelNotFoundException
  2286. * @throws \think\exception\DbException
  2287. */
  2288. public function oneFabricDetail()
  2289. {
  2290. if ($this->request->isGet() === false){
  2291. $this->error('请求错误');
  2292. }
  2293. $param = $this->request->param();
  2294. if (empty($param)){
  2295. $this->error('参数错误');
  2296. }
  2297. //面料入库记录
  2298. $list['入库'] = \db('设备_报工日志')
  2299. ->where('order_id',$param['order'])
  2300. ->where('物料名称',$param['fabricName'])
  2301. ->where('name','入库')
  2302. ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,recipient as 入仓人员')
  2303. ->select();
  2304. //面料出库记录
  2305. $list['出库'] = \db('设备_报工日志')
  2306. ->where('order_id',$param['order'])
  2307. ->where('物料名称',$param['fabricName'])
  2308. ->where('name','出库')
  2309. ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,receipt_number as 出库单据编号,recipient as 领用人员')
  2310. ->select();
  2311. //面料退还记录
  2312. $list['退还'] = \db('设备_报工日志')
  2313. ->where('order_id',$param['order'])
  2314. ->where('物料名称',$param['fabricName'])
  2315. ->where('name','退还')
  2316. ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,recipient as 退还人员')
  2317. ->select();
  2318. $this->success('成功',$list);
  2319. }
  2320. /**
  2321. * 单据号查询数据
  2322. * @return void
  2323. * @throws \think\db\exception\DataNotFoundException
  2324. * @throws \think\db\exception\ModelNotFoundException
  2325. * @throws \think\exception\DbException
  2326. */
  2327. public function ReceiptDetail()
  2328. {
  2329. if ($this->request->isGet() === false){
  2330. $this->error('请求错误');
  2331. }
  2332. $param = $this->request->param();
  2333. if (empty($param)){
  2334. $this->error('单据编号参数错误');
  2335. }
  2336. $list = \db('设备_报工日志')
  2337. // ->where('receipt_number','like','%',$param['receipt'].'%')
  2338. ->where('receipt_number',$param['receipt'])
  2339. ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,receipt_number as 出库单据编号,recipient as 领用人员')
  2340. ->whereNull('Mod_rq')
  2341. ->select();
  2342. if (empty($list)){
  2343. $this->error('未找到该出库单');
  2344. }else{
  2345. $this->success('成功',$list);
  2346. }
  2347. }
  2348. /**
  2349. * 出库单列表
  2350. * @return void
  2351. * @throws \think\db\exception\DataNotFoundException
  2352. * @throws \think\db\exception\ModelNotFoundException
  2353. * @throws \think\exception\DbException
  2354. */
  2355. public function ReceiptList()
  2356. {
  2357. if ($this->request->isGet() === false){
  2358. $this->error('请求错误');
  2359. }
  2360. $param = $this->request->param();
  2361. if (empty($param)){
  2362. $this->error('参数错误');
  2363. }
  2364. $list = \db('设备_报工日志')
  2365. ->where('rq','like','%'.$param['mouth'].'%')
  2366. ->field('receipt_number as 出库单,order_id as 订单编号,款号,物料名称,rq as 日期,sys_id as 操作机台,recipient as 领料人员,name as 单号类型')
  2367. ->group('出库单')
  2368. ->order('rq desc')
  2369. ->select();
  2370. if (empty($list)){
  2371. $this->success('未找到数据');
  2372. }else{
  2373. $this->success('成功',$list);
  2374. }
  2375. }
  2376. /**
  2377. * 出库单左侧菜单
  2378. * @return void
  2379. * @throws \think\db\exception\DataNotFoundException
  2380. * @throws \think\db\exception\ModelNotFoundException
  2381. * @throws \think\exception\DbException
  2382. */
  2383. public function getReceiptTab()
  2384. {
  2385. if ($this->request->isGet() === false){
  2386. $this->error('请求错误');
  2387. }
  2388. $list = \db('设备_报工日志')
  2389. ->field([
  2390. "DATE_FORMAT(rq, '%Y-%m') AS month",
  2391. ])
  2392. ->group('month')
  2393. ->order('month DESC')
  2394. ->select();
  2395. if (empty($list)){
  2396. $this->error('未找到数据');
  2397. }else{
  2398. $this->success('成功',$list);
  2399. }
  2400. }
  2401. /**
  2402. * 获取入库单号、出库单号
  2403. * @return void
  2404. */
  2405. public function gitReceiptNumber()
  2406. {
  2407. if ($this->request->isGet() === false){
  2408. $this->error('请求错误');
  2409. }
  2410. $param = $this->request->param();
  2411. if (empty($param)){
  2412. $this->error('参数错误');
  2413. }
  2414. $lastNumber = \db('设备_报工日志')
  2415. ->where('receipt_number','like',$param['number'].'%')
  2416. ->order('receipt_number desc')
  2417. ->limit(1)
  2418. ->column('receipt_number as 单号');
  2419. if (empty($lastNumber)){
  2420. $num = 1;
  2421. }else{
  2422. $num = (int)(substr($lastNumber[0],12,3))+1;
  2423. }
  2424. if ($num < 10){
  2425. $num = '00'.$num;
  2426. }elseif ($num>=10 && $num<100){
  2427. $num = '0'.$num;
  2428. }else{
  2429. $num;
  2430. }
  2431. $number = $param['number'].date('Ymd',time()).'-'.$num;
  2432. $lastNumber = \db('设备_报工日志')
  2433. ->where('name','出库')
  2434. ->order('recipient desc')
  2435. ->group('recipient')
  2436. ->limit(1)
  2437. ->value('recipient as 人员');
  2438. $data = [
  2439. 'number' => $number,
  2440. 'username' => $lastNumber
  2441. ];
  2442. $this->success('成功', $data);
  2443. }
  2444. /**
  2445. * 面料汇总左侧菜单
  2446. * @return void
  2447. * @throws \think\db\exception\DataNotFoundException
  2448. * @throws \think\db\exception\ModelNotFoundException
  2449. * @throws \think\exception\DbException
  2450. */
  2451. public function getFabricTab()
  2452. {
  2453. if ($this->request->isGet() === false){
  2454. $this->error('请求错误');
  2455. }
  2456. $list = \db('工单_bom资料')
  2457. ->field([
  2458. "DATE_FORMAT(Sys_rq, '%Y-%m') AS month",
  2459. ])
  2460. ->group('month')
  2461. ->order('month DESC')
  2462. ->select();
  2463. if (empty($list)){
  2464. $this->error('未找到数据');
  2465. }else{
  2466. $this->success('成功',$list);
  2467. }
  2468. }
  2469. }