WorkOrder.php 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203
  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. ];
  797. }
  798. Db::startTrans();
  799. try {
  800. //插入工单基本资料
  801. $sql= \db('工单_基本资料')->fetchSql(true)->insert($param);
  802. $res = \db()->query($sql);
  803. //插入订单物料信息
  804. $BomSql = \db('工单_bom资料')->fetchSql(true)->insertAll($data);
  805. $BomRes = \db()->query($BomSql);
  806. //提交
  807. Db::commit();
  808. }catch (\Exception $e){
  809. //回滚
  810. Db::rollback();
  811. }
  812. $this->success('成功');
  813. }
  814. /**
  815. *
  816. */
  817. // public function ClientList(){
  818. // }
  819. /**
  820. *
  821. */
  822. // public function ProductCodeList(){
  823. // }
  824. /**
  825. * 新增印件资料->印件资料添加
  826. * @return void
  827. * @throws \think\db\exception\BindParamException
  828. * @throws \think\exception\PDOException
  829. */
  830. public function PrintDetailAdd()
  831. {
  832. if (Request::instance()->isPost() === false){
  833. $this->error('请求错误');
  834. }
  835. $param = Request::instance()->post();
  836. if (empty($param)){
  837. $this->error('参数错误');
  838. }
  839. $param['Sys_rq'] = date('Y-m-d H:i:s',time());
  840. $process = [
  841. ['1','仓库出库'],['2','裁剪'],['3','车缝'],['4','后道收样'],['5','大烫'],['6','总检'],['7','包装']
  842. ];
  843. $processDetail = [];
  844. foreach ($process as $key=>$value){
  845. $total = null;
  846. if ($key !== 0){
  847. $total = $param['zdtotal'];
  848. }
  849. $processDetail[$key] = [
  850. '订单编号' => $param['订单编号'],
  851. '子订单编号' => $param['子订单编号'],
  852. '款号' => $param['款号'],
  853. '颜色' => $param['颜色'],
  854. '颜色备注' => $param['颜色备注'],
  855. '工序编号' => $value[0],
  856. '工序名称' => $value[1],
  857. '计划产量' => $total,
  858. 'Sys_id' => $param['Sys_id'],
  859. 'Sys_rq' => $param['Sys_rq']
  860. ];
  861. }
  862. //开启事务
  863. db()->startTrans();
  864. try{
  865. $priSql = \db('工单_印件资料')->fetchSql(true)->insert($param);
  866. $priRes = \db()->query($priSql);
  867. $proSql = \db('工单_工艺资料')->fetchSql(true)->insertAll($processDetail);
  868. $proRes = \db()->query($proSql);
  869. // 提交事务
  870. db()->commit();
  871. } catch (\Exception $e) {
  872. // 回滚事务
  873. db()->rollback();
  874. $this->error($e->getMessage());
  875. }
  876. if ($priRes !== false && $proRes !== false){
  877. $this->success('成功');
  878. }else{
  879. $this->error('失败');
  880. }
  881. }
  882. /**
  883. *
  884. */
  885. // public function ProcessDetailAdd(){
  886. // }
  887. /**
  888. *
  889. */
  890. // public function PrintDetailList(){
  891. // }
  892. /**
  893. * 子订单列表
  894. * @return null
  895. * @throws \think\db\exception\DataNotFoundException
  896. * @throws \think\db\exception\ModelNotFoundException
  897. * @throws \think\exception\DbException
  898. */
  899. public function PrintListData()
  900. {
  901. // 检查请求方式
  902. if ($this->request->isGet() === false) {
  903. $this->error('请求错误');
  904. }
  905. $param = $this->request->param();
  906. // 检查参数是否存在
  907. if (isset($param) === false) {
  908. $this->error('参数错误');
  909. }
  910. // 查询型号
  911. $where['Mod_rq'] = null;
  912. $xhdata = \db('工单_印件资料')
  913. ->where('订单编号', $param['order'])
  914. ->where($where)
  915. ->field('cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10')
  916. ->select();
  917. $arr = [];
  918. // 收集cm1到cm10的非空非null值
  919. foreach ($xhdata as $key => $value) {
  920. for ($i = 1; $i <= 10; $i++) {
  921. if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) {
  922. array_push($arr, $value['cm' . $i]);
  923. }
  924. }
  925. }
  926. $arr = array_unique($arr);
  927. sort($arr);
  928. // 查询详细列表
  929. $list = \db('工单_印件资料')
  930. ->where('订单编号', $param['order'])
  931. ->where($where)
  932. ->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')
  933. ->select();
  934. // 遍历列表并处理cm和cmsl字段
  935. foreach ($list as $key => $value) {
  936. for ($i = 1; $i <= 10; $i++) {
  937. if ($value['cm' . $i] !== '') {
  938. // 如果 cmsl 的值为 0,则设置为空字符串
  939. $list[$key][$value['cm' . $i]] = ($value['cmsl' . $i] === 0) ? '' : $value['cmsl' . $i];
  940. }
  941. // 删除原有的 cm 和 cmsl 字段
  942. // unset($list[$key]['cm' . $i], $list[$key]['cmsl' . $i]);
  943. }
  944. }
  945. // 自定义型号排序
  946. $customOrder = array('XXS','XS','S', 'M', 'L', 'XL','XXL', 'XXXL', 'XXXXL', '2XL', '3XL', '4XL');
  947. usort($arr, function ($a, $b) use ($customOrder) {
  948. $posA = array_search($a, $customOrder);
  949. $posB = array_search($b, $customOrder);
  950. return $posA - $posB;
  951. });
  952. // 返回结果
  953. $data['型号'] = $arr;
  954. $data['列表'] = $list;
  955. $this->success('成功', $data);
  956. }
  957. /**
  958. * 工单资料管理->印件资料删除
  959. * @return void
  960. * @throws \think\Exception
  961. * @throws \think\exception\PDOException
  962. */
  963. public function PrintDetailDel()
  964. {
  965. if ($this->request->isGet() === false){
  966. $this->error('请求错误');
  967. }
  968. $param = $this->request->param();
  969. if (isset($param['UniqId']) === false){
  970. $this->error('参数错误');
  971. }
  972. $printId = explode(',',$param['UniqId']);
  973. $i = 0;
  974. foreach ($printId as $value){
  975. $res = \db('工单_印件资料')
  976. ->where('Uniqid',$value)
  977. ->update(['Mod_rq'=>date('Y-m-d H:i:s',time())]);
  978. if ($res === false){
  979. $i++;
  980. }
  981. }
  982. if ($i === 0){
  983. $this->success('删除成功');
  984. }else{
  985. $this->error('删除失败');
  986. }
  987. }
  988. /**
  989. * 月度车间报工汇总->报工删除记录
  990. */
  991. public function ProcessDetailDel()
  992. {
  993. if ($this->request->isGet() === false){
  994. $this->error('请求错误');
  995. }
  996. $where['a.mod_rq'] = ['neq', ''];
  997. $list = \db('设备_产量计酬')->alias('a')
  998. ->join('工单_印件资料 b', 'b.订单编号 = a.订单编号 AND a.子订单编号 = a.子订单编号')
  999. ->join('工单_基本资料 j', 'b.订单编号 = j.订单编号', 'LEFT')
  1000. ->field('
  1001. b.订单编号, b.子订单编号, b.款号, b.颜色, b.船样, a.尺码, b.zdtotal as 制单数,b.颜色备注,
  1002. a.数量, a.sys_rq as 上报时间,a.UniqId,a.mod_rq,a.delsys_id,a.sczl_bh,
  1003. j.客户编号,j.生产款号,j.款式
  1004. ')
  1005. ->where($where)
  1006. ->order('a.mod_rq desc')
  1007. ->group('a.UniqId')
  1008. ->select();
  1009. // 提取所有的尺码,并去重
  1010. $sizeList = array_values(array_unique(array_column($list, '尺码')));
  1011. // 动态将尺码的数量添加到每个订单中,并替换已完成字段
  1012. foreach ($list as &$item) {
  1013. $size = $item['尺码'];
  1014. $item[$size] = $item['数量']; // 动态添加尺码字段,值为数量
  1015. // unset($item['数量']); // 移除原来的已完成字段
  1016. }
  1017. $data['total'] = count($list);
  1018. $data['table'] = $list;
  1019. $this->success('成功',$data);
  1020. }
  1021. /**
  1022. *
  1023. */
  1024. // public function WastageList(){
  1025. // }
  1026. /**
  1027. *
  1028. */
  1029. // public function getAnnexTable(){
  1030. // }
  1031. /**
  1032. *
  1033. */
  1034. // public function annexAdd(){
  1035. // }
  1036. /**
  1037. *
  1038. */
  1039. // public function annexDel(){
  1040. // }
  1041. /**
  1042. *
  1043. */
  1044. // public function getGdAnnexTable(){
  1045. // }
  1046. /**
  1047. * 产品附件新增
  1048. * 1.前端进行上传xlsx文件表格
  1049. * 2.接口接受数据文件进行保存xlsx文件以及pdf转换
  1050. * 3.前端进行预览pdf图片显示文件中的内容再页面上
  1051. */
  1052. public function gdAnnexAdd() {
  1053. ini_set('display_errors', 'On');
  1054. ini_set('error_reporting', E_ALL);
  1055. if (!$this->request->isPost()) {
  1056. $this->error('请求方式错误');
  1057. }
  1058. // 获取请求参数
  1059. $req = $this->request->param();
  1060. $relateId = $req['关联编号'];
  1061. $attachmentContent = $req['附件内容'];
  1062. $attachmentType = $req['附件类型'];
  1063. $prefixDir = ROOT_PATH . '/public/';
  1064. $uploadDir = ''.'uploads/' . date('Ymd') . '/' . $relateId;
  1065. DB::name('工单_基本资料')
  1066. ->where('订单编号', $relateId)
  1067. ->update(['gd_statu' => '2-生产中']);
  1068. // 检查并创建目录
  1069. if (!is_dir($prefixDir . $uploadDir)) {
  1070. mkdir($prefixDir . $uploadDir, 0777, true);
  1071. }
  1072. // 处理 Base64 附件内容
  1073. if (strpos($attachmentContent, 'base64,') !== false) {
  1074. $attachmentContent = explode('base64,', $attachmentContent)[1];
  1075. }
  1076. $fileContent = base64_decode($attachmentContent);
  1077. $filename = time();
  1078. // 初始化文件路径变量
  1079. $xlsxFilePath = '';
  1080. $pdfFilePath = '';
  1081. if ($attachmentType === 'pdf') {
  1082. // 保存 PDF 文件
  1083. $pdfFilePath = $uploadDir . '/' . $filename . '.pdf';
  1084. file_put_contents($prefixDir . $pdfFilePath, $fileContent);
  1085. } elseif ($attachmentType === 'xlsx') {
  1086. // 保存 Excel 文件
  1087. $xlsxFilePath = $uploadDir . '/' . $filename . '.xlsx';
  1088. file_put_contents($prefixDir . $xlsxFilePath, $fileContent);
  1089. // 转换为 PDF 文件
  1090. $pdfFilePath = $uploadDir . '/' . $filename . '.pdf';
  1091. $cmd = sprintf(
  1092. 'libreoffice --headless --convert-to pdf --outdir %s %s',
  1093. escapeshellarg($prefixDir . $uploadDir),
  1094. escapeshellarg($prefixDir . $xlsxFilePath)
  1095. );
  1096. exec($cmd, $out, $retval);
  1097. if ($retval !== 0) {
  1098. $this->error('Excel 转 PDF 失败');
  1099. }
  1100. } else {
  1101. $this->error('不支持的附件类型');
  1102. }
  1103. // 组织数据
  1104. $data = [
  1105. '关联编号' => $relateId,
  1106. 'sys_id' => $req['sys_id'],
  1107. '附件备注' => $req['附件备注'],
  1108. '附件类型' => $attachmentType,
  1109. 'sys_rq' => date('Y-m-d H:i:s'),
  1110. 'url' => $xlsxFilePath, // 保存 xlsx 文件路径(如果存在)
  1111. 'pdf' => $pdfFilePath // 保存 pdf 文件路径
  1112. ];
  1113. // 数据库事务处理
  1114. db()->startTrans();
  1115. try {
  1116. // 插入数据
  1117. $sql = db('工单_相关附件')->fetchSql(true)->insert($data);
  1118. $result = db()->query($sql);
  1119. db()->commit();
  1120. } catch (\Exception $e) {
  1121. // 回滚事务
  1122. db()->rollback();
  1123. $this->error($e->getMessage());
  1124. }
  1125. if ($result === false) {
  1126. $this->error('失败');
  1127. }
  1128. $this->success('成功');
  1129. }
  1130. /**
  1131. *
  1132. */
  1133. // public function gdAnnexDel()
  1134. // {
  1135. // }
  1136. /**
  1137. *
  1138. */
  1139. // public function WorkOrderDetailCope()
  1140. // {
  1141. // }
  1142. //
  1143. /**
  1144. *
  1145. */
  1146. // public function WorkOrderDetailListCope()
  1147. // {
  1148. // }
  1149. /**
  1150. *
  1151. */
  1152. // public function ProductCopeDetail()
  1153. // {
  1154. // }
  1155. /**
  1156. *
  1157. */
  1158. // public function PlannedProcessYield($workOrder,$yjno,$gxh,$feed)
  1159. // {
  1160. // }
  1161. /**
  1162. *
  1163. */
  1164. // public function WorkOrderDel()
  1165. // {
  1166. // }
  1167. /**
  1168. *
  1169. */
  1170. // public function ProductNameData()
  1171. // {
  1172. // }
  1173. /**
  1174. *
  1175. */
  1176. // public function getCouplet()
  1177. // }
  1178. /**
  1179. * 订单打印接口
  1180. * order:订单编号
  1181. * 通过订单编号获取订单表数据以及子订单数据
  1182. */
  1183. public function orderPrint(){
  1184. if ($this->request->isGet() === false){$this->error('请求错误');}
  1185. $param = $this->request->param();
  1186. if (empty($param['order'])){$this->error('参数错误');}
  1187. $where['Mod_rq'] = null;
  1188. //订单信息
  1189. $list = \db('工单_基本资料')
  1190. ->where('订单编号',$param['order'])
  1191. ->where($where)
  1192. ->field('订单编号,img,生产款号,客户编号,款式,落货日期,箱唛要求,面料,船样描述,船样合计,粘衬,订单数量,审核,审核日期,要求,water')
  1193. ->find();
  1194. //尺码表格表头
  1195. $xhdata = \db('工单_印件资料')->where('订单编号', $param['order'])->where($where)->select();
  1196. $arr = [];
  1197. foreach ($xhdata as $key => $value) {
  1198. for ($i = 1; $i <= 10; $i++) {
  1199. if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) {
  1200. array_push($arr, $value['cm' . $i]);
  1201. }
  1202. }
  1203. }
  1204. // 去重并重新索引
  1205. $arr = array_unique($arr);
  1206. $arr = array_values($arr);
  1207. // 自定义排序函数
  1208. usort($arr, function($a, $b) {
  1209. // 判断是否为数字
  1210. $isNumericA = is_numeric($a);
  1211. $isNumericB = is_numeric($b);
  1212. if ($isNumericA && $isNumericB) {
  1213. // 如果都是数字,按从小到大排序
  1214. return $a - $b;
  1215. } elseif (!$isNumericA && !$isNumericB) {
  1216. // 如果都是字母,按字母顺序排序(可以自定义顺序)
  1217. $sizeOrder = ['XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL','XXXXL'];
  1218. $posA = array_search($a, $sizeOrder);
  1219. $posB = array_search($b, $sizeOrder);
  1220. return $posA - $posB;
  1221. } else {
  1222. // 如果一个是数字一个是字母,数字排在前
  1223. return $isNumericA ? -1 : 1;
  1224. }
  1225. });
  1226. //打印table数据表格
  1227. $porlis = \db('工单_印件资料')
  1228. ->where('订单编号',$param['order'])
  1229. ->where('船样',0)
  1230. ->where($where)
  1231. ->field('子订单编号')
  1232. ->select();
  1233. //合并后的子订单条码数据
  1234. $subOrder = $porlis[0]['子订单编号'];
  1235. // 找到子订单编号中的 '-' 位置
  1236. $dashPos = strpos($subOrder, '-');
  1237. if ($dashPos !== false) {
  1238. // 提取 '-' 后面的部分
  1239. $afterDash = substr($subOrder, $dashPos + 1);
  1240. // 判断长度是否等于2或等于4
  1241. if (strlen($afterDash) == 2) {
  1242. // 查询船样为0的数据
  1243. $processlist = \db('工单_印件资料')
  1244. ->where('订单编号', $param['order'])
  1245. ->where($where)
  1246. ->where('船样', 0)
  1247. ->field('子订单编号,颜色,款号,zdtotal,颜色备注,color_id,
  1248. cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10,
  1249. cmsl1,cmsl2,cmsl3,cmsl4,cmsl5,cmsl6,cmsl7,cmsl8,cmsl9,cmsl10,Uniqid')
  1250. ->select();
  1251. // 初始化汇总数组
  1252. $scslTotals = [
  1253. "cmsl1" => 0,
  1254. "cmsl2" => 0,
  1255. "cmsl3" => 0,
  1256. "cmsl4" => 0,
  1257. "cmsl5" => 0,
  1258. "cmsl6" => 0,
  1259. "cmsl7" => 0,
  1260. "cmsl8" => 0,
  1261. "cmsl9" => 0,
  1262. "cmsl10" => 0,
  1263. "zdtotal" => 0, // 总计数量
  1264. ];
  1265. // 遍历数据集进行汇总
  1266. foreach ($processlist as $item) {
  1267. // 遍历每个尺码字段(cmsl1 到 cmsl10)
  1268. for ($i = 1; $i <= 10; $i++) {
  1269. // 获取当前字段的数量
  1270. $sizeQty = $item['cmsl' . $i];
  1271. // 判断数量是否有效(非空且大于0)
  1272. if (!empty($sizeQty)) {
  1273. // 累加当前字段的数量
  1274. $scslTotals['cmsl' . $i] += $sizeQty;
  1275. // 累加到总计字段
  1276. $scslTotals['zdtotal'] += $sizeQty;
  1277. }
  1278. }
  1279. }
  1280. $data['scslTotals'] = $scslTotals;
  1281. foreach ($processlist as $key => $value) {
  1282. // 将尺码和对应的数量从 cm1-cm10 和 cmsl1-cmsl10 转换为动态键值对
  1283. for ($i = 1; $i <= 10; $i++) {
  1284. if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) {
  1285. $processlist[$key][$value['cm' . $i]] = $value['cmsl' . $i];
  1286. }
  1287. // 移除原始的 cm 和 cmsl 字段
  1288. unset($processlist[$key]['cm' . $i], $processlist[$key]['cmsl' . $i]);
  1289. }
  1290. }
  1291. // 用于存储合并后的数据
  1292. $mergedData = [];
  1293. // 按颜色备注进行合并
  1294. foreach ($processlist as $item) {
  1295. $key = $item['颜色备注'];
  1296. if (!isset($mergedData[$key])) {
  1297. $mergedData[$key] = $item;
  1298. // 添加条码字段,值为子订单编号
  1299. $mergedData[$key]['条码'] = $item['子订单编号'];
  1300. } else {
  1301. // 合并尺码对应的数量
  1302. foreach ($item as $size => $quantity) {
  1303. if (is_numeric($size)) {
  1304. if (!isset($mergedData[$key][$size])) {
  1305. $mergedData[$key][$size] = 0;
  1306. }
  1307. $mergedData[$key][$size] += $quantity;
  1308. }
  1309. }
  1310. // 合并 zdtotal
  1311. $mergedData[$key]['zdtotal'] += $item['zdtotal'];
  1312. }
  1313. }
  1314. // 查询船样为1的数据
  1315. $chuanyang = \db('工单_印件资料')
  1316. ->where('订单编号', $param['order'])
  1317. ->where($where)
  1318. ->where('船样', 1)
  1319. ->field('子订单编号,颜色,款号,zdtotal,颜色备注,color_id,
  1320. cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10,
  1321. cmsl1,cmsl2,cmsl3,cmsl4,cmsl5,cmsl6,cmsl7,cmsl8,cmsl9,cmsl10,Uniqid')
  1322. ->select();
  1323. foreach ($chuanyang as $key => $value) {
  1324. // 将尺码和对应的数量从 cm1-cm10 和 cmsl1-cmsl10 转换为动态键值对
  1325. for ($i = 1; $i <= 10; $i++) {
  1326. if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) {
  1327. $chuanyang[$key][$value['cm' . $i]] = $value['cmsl' . $i];
  1328. }
  1329. // 移除原始的 cm 和 cmsl 字段
  1330. unset($chuanyang[$key]['cm' . $i], $chuanyang[$key]['cmsl' . $i]);
  1331. }
  1332. // 添加条码字段,值为子订单编号
  1333. $chuanyang[$key]['条码'] = $value['子订单编号'];
  1334. }
  1335. // 将合并后的数据插入到相应颜色备注的原始数据尾部
  1336. $finalList = [];
  1337. $groupedData = [];
  1338. // 将原始数据按颜色备注分组
  1339. foreach ($processlist as $item) {
  1340. $key = $item['颜色备注'];
  1341. if (!isset($groupedData[$key])) {
  1342. $groupedData[$key] = [];
  1343. }
  1344. $groupedData[$key][] = $item;
  1345. }
  1346. // 将合并后的数据插入到对应的颜色备注组的尾部
  1347. foreach ($groupedData as $key => $items) {
  1348. $finalList = array_merge($finalList, $items); // 先添加原始数据
  1349. if (isset($mergedData[$key])) {
  1350. $finalList[] = $mergedData[$key]; // 在组的尾部添加合并数据
  1351. }
  1352. }
  1353. // 将船样为1的数据添加到最终列表中
  1354. $finalList = array_merge($finalList, $chuanyang);
  1355. $data['process'] = $finalList;
  1356. $data['order'] = $list;
  1357. $data['xhdata'] = $arr;
  1358. $this->success('成功',$data);
  1359. } elseif (strlen($afterDash) == 4) {
  1360. //一条子订单编号一个条码,统计颜色
  1361. // $processlist = \db('工单_印件资料')
  1362. // ->where('订单编号', $param['order'])
  1363. // ->whereNull('Mod_rq') // 查询未删除数据
  1364. // ->select();
  1365. //
  1366. // $table = [];
  1367. // foreach ($processlist as $item) {
  1368. // // 当前子订单编号的数据
  1369. // $subOrder = [
  1370. // '颜色备注' => $item['颜色备注'],
  1371. // '色系名称' => $item['颜色'],
  1372. // '订单编号' => $item['订单编号'],
  1373. // '子订单编号' => $item['子订单编号'],
  1374. // '条码' => $item['子订单编号'],
  1375. // '款号' => $item['款号'],
  1376. // ];
  1377. // // 当前子订单编号的合计
  1378. // $subOrderTotal = 0;
  1379. // for ($i = 1; $i <= 10; $i++) {
  1380. // if (!empty($item['cm' . $i])) {
  1381. // $subOrder[$item['cm' . $i]] = $item['cmsl' . $i];
  1382. // // 累加每个尺码的数量
  1383. // $subOrderTotal += $item['cmsl' . $i];
  1384. // }
  1385. // unset($item['cm' . $i], $item['cmsl' . $i]);
  1386. // }
  1387. // $subOrder['合计'] = $subOrderTotal; // 添加合计
  1388. // $table[] = $subOrder; // 将当前子订单的数据添加到 $table 中
  1389. // }
  1390. $processlist = \db('工单_印件资料')
  1391. ->where('订单编号', $param['order'])
  1392. ->whereNull('Mod_rq') // 查询未删除数据
  1393. ->select();
  1394. $table = [];
  1395. foreach ($processlist as $item) {
  1396. // 当前子订单编号的数据
  1397. $subOrder = [
  1398. '颜色备注' => $item['颜色备注'],
  1399. '色系名称' => $item['颜色'],
  1400. '订单编号' => $item['订单编号'],
  1401. '子订单编号' => $item['子订单编号'],
  1402. '条码' => $item['子订单编号'],
  1403. '款号' => $item['款号'],
  1404. ];
  1405. // 当前子订单编号的合计
  1406. $subOrderTotal = 0;
  1407. for ($i = 1; $i <= 10; $i++) {
  1408. // 判断 cm 和 cmsl 是否有值,空值不显示,0 则显示
  1409. if (isset($item['cm' . $i]) && $item['cm' . $i] !== '') {
  1410. $subOrder[$item['cm' . $i]] = $item['cmsl' . $i] ?? 0; // 默认数量为 0
  1411. // 累加每个尺码的数量
  1412. $subOrderTotal += (int)$item['cmsl' . $i]; // 确保是数值类型
  1413. }
  1414. // 清理字段
  1415. unset($item['cm' . $i], $item['cmsl' . $i]);
  1416. }
  1417. $subOrder['合计'] = $subOrderTotal; // 添加合计
  1418. $table[] = $subOrder; // 将当前子订单的数据添加到 $table 中
  1419. }
  1420. // 初始化汇总数组
  1421. $scslTotals = [
  1422. "cmsl1" => 0,
  1423. "cmsl2" => 0,
  1424. "cmsl3" => 0,
  1425. "cmsl4" => 0,
  1426. "cmsl5" => 0,
  1427. "cmsl6" => 0,
  1428. "cmsl7" => 0,
  1429. "cmsl8" => 0,
  1430. "cmsl9" => 0,
  1431. "cmsl10" => 0,
  1432. "zdtotal" => 0, // 总计数量
  1433. ];
  1434. // 遍历数据集进行汇总
  1435. foreach ($processlist as $item) {
  1436. // 遍历每个尺码字段(cmsl1 到 cmsl10)
  1437. for ($i = 1; $i <= 10; $i++) {
  1438. // 获取当前字段的数量
  1439. $sizeQty = $item['cmsl' . $i];
  1440. // 判断数量是否有效(非空且大于0)
  1441. if (!empty($sizeQty)) {
  1442. // 累加当前字段的数量
  1443. $scslTotals['cmsl' . $i] += $sizeQty;
  1444. // 累加到总计字段
  1445. $scslTotals['zdtotal'] += $sizeQty;
  1446. }
  1447. }
  1448. }
  1449. $data['scslTotals'] = $scslTotals;
  1450. $data['process'] = $table;//汇总数据集
  1451. $data['order'] = $list;//表格数据
  1452. $data['xhdata'] = $arr;//尺码表头
  1453. $this->success('成功', $data);
  1454. // }
  1455. } else {
  1456. echo "子订单编号 - 后不是2位也不是4位:$afterDash";
  1457. }
  1458. } else {
  1459. echo "子订单编号中没有找到'-'";
  1460. }
  1461. }
  1462. /**
  1463. * 订单编号自动获取
  1464. * @return void
  1465. * @throws \think\db\exception\DataNotFoundException
  1466. * @throws \think\db\exception\ModelNotFoundException
  1467. * @throws \think\exception\DbException
  1468. */
  1469. public function getWorkOrder()
  1470. {
  1471. if ($this->request->isGet() === false){
  1472. $this->error('请求错误');
  1473. }
  1474. $time =substr( date('Ym',time()),2);
  1475. $lastOrder = \db('工单_基本资料')
  1476. ->where('订单编号','like','%'.$time.'%')
  1477. ->order('Uniqid desc')
  1478. ->find();
  1479. if (empty($lastOrder)){
  1480. $newNumber = 1;
  1481. }else{
  1482. $lastNumber = substr($lastOrder['订单编号'],6);
  1483. $newNumber = (int)$lastNumber + 1;
  1484. }
  1485. if ($newNumber<10){
  1486. $newOrder = 'DC'.$time.'00'.$newNumber;
  1487. }elseif ($newNumber>=10 && $newNumber<100){
  1488. $newOrder = 'DC'.$time.'0'.$newNumber;
  1489. }else{
  1490. $newOrder = 'DC'.$time.$newNumber;
  1491. }
  1492. $this->success('成功',$newOrder);
  1493. }
  1494. /**
  1495. * 获取子订单编号
  1496. * @return void
  1497. * @throws \think\db\exception\DataNotFoundException
  1498. * @throws \think\db\exception\ModelNotFoundException
  1499. * @throws \think\exception\DbException
  1500. */
  1501. public function getSuborder(){
  1502. // 确保是GET请求
  1503. if ($this->request->isGet() === false) {
  1504. $this->error('请求错误');
  1505. }
  1506. $param = $this->request->param();
  1507. if (empty($param) || !isset($param['cy']) || !isset($param['order'])) {
  1508. $this->error('参数错误');
  1509. }
  1510. // 判断是否“船样”获取对应的子订单编号
  1511. if ($param['cy'] == '否') {
  1512. //1.通过色系名称查询对应的编号
  1513. $colorlist = \db('工单_颜色编号')
  1514. ->field('colorcode, colorname')
  1515. ->where('colorname', $param['colorname'] ?? '')
  1516. ->find();
  1517. if (empty($colorlist)) {
  1518. $this->error('未找到对应的颜色编号');
  1519. }
  1520. $num = $param['order'] . '-' . $colorlist['colorcode'];
  1521. // 查询子订单编号
  1522. $data = \db('工单_印件资料')
  1523. ->field('子订单编号,cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10')
  1524. ->where('子订单编号', 'like', '%' . $num . '%')
  1525. ->where('船样', '=', 0)
  1526. ->order('子订单编号', 'desc')
  1527. ->find();
  1528. if (empty($data)) {
  1529. // 如果没有找到数据,生成默认的订单编号,后两位从00开始
  1530. $order = $param['order'] . '-' . $colorlist['colorcode'] . '00';
  1531. } else {
  1532. if (strlen($data['子订单编号']) == 12) {
  1533. $data = \db('工单_印件资料')
  1534. ->where('订单编号',$param['order'])
  1535. ->where('船样',0)
  1536. ->order('子订单编号 desc')
  1537. ->find();
  1538. if(empty($data)){
  1539. $order = $param['order'].'-01';
  1540. }else{
  1541. $num = (int)substr($data['子订单编号'],10) + 1;
  1542. if ($num<10){
  1543. $order = $param['order'].'-0'.$num;
  1544. }else{
  1545. $order = $param['order'].'-'.$num;
  1546. }
  1547. }
  1548. }else{
  1549. // 如果找到数据,提取子订单编号并递增
  1550. $order = $data['子订单编号'];
  1551. if (preg_match('/(.*-' . $colorlist['colorcode'] . ')(\d{2})$/', $order, $matches)) {
  1552. $prefix = $matches[1]; // 前缀部分(包括订单编号和颜色代码)
  1553. $number = $matches[2]; // 数字部分(后两位)
  1554. // 循环生成子订单编号并检查数据库中是否存在相同编号
  1555. do {
  1556. $incrementedNumber = (int)$number + 1;
  1557. // 将数字部分补充为两位数,例如 1 -> 01,2 -> 02
  1558. $order = $prefix . str_pad($incrementedNumber, 2, '0', STR_PAD_LEFT);
  1559. $exists = \db('工单_印件资料')->where('子订单编号', '=', $order)->find();
  1560. $number = $incrementedNumber; // 更新 number 用于下一次循环
  1561. } while ($exists); // 如果存在相同编号则继续循环递增
  1562. } else {
  1563. $this->error('订单编号格式错误');
  1564. }
  1565. }
  1566. }
  1567. //2.获取色系名称信息
  1568. $colorlist = \db('工单_颜色编号')->select();
  1569. //3.获取历史尺码数据
  1570. $cm_list = \db('工单_印件资料')
  1571. ->where('订单编号', $param['order'])
  1572. ->group('订单编号')
  1573. ->find();
  1574. $cm_data = [];
  1575. if ($cm_list) {
  1576. // 精准筛选字段名以 "cm" 开头并跟1到2位数字的字段
  1577. foreach ($cm_list as $key => $value) {
  1578. if (preg_match('/^cm\d{1,2}$/', $key)) {
  1579. $cm_data[$key] = $value ?? ''; // 如果值为 null,设为空字符串
  1580. }
  1581. }
  1582. } else {
  1583. // 如果查询结果为空,将 cm1 到 cm10 都设置为空
  1584. for ($i = 1; $i <= 10; $i++) {
  1585. $cm_data["cm$i"] = '';
  1586. }
  1587. }
  1588. $result = ['order' => $order,'colorlist' => $colorlist,'cm' => $cm_data];
  1589. $this->success('成功', $result);
  1590. } else if ($param['cy'] == '是') {
  1591. //1.获取船样子订单编号
  1592. $data = \db('工单_印件资料')
  1593. ->where('订单编号', $param['order'])
  1594. ->where('船样', '=', 1)
  1595. ->order('子订单编号 asc')
  1596. ->find();
  1597. if (empty($data)) {
  1598. // 如果没有数据,初始子订单编号为 '99'
  1599. $order = $param['order'] . '-99';
  1600. } else {
  1601. // 提取子订单编号中的数字部分
  1602. $subOrder = $data['子订单编号']; // 例如 "DC2410006-99"
  1603. if (preg_match('/-(\d+)$/', $subOrder, $matches)) {
  1604. $numberPart = (int)$matches[1]; // 提取到的数字部分
  1605. $newNumber = $numberPart - 1; // 减 1
  1606. // 将新的数字拼接回订单编号
  1607. $order = preg_replace('/-\d+$/', '-' . $newNumber, $subOrder);
  1608. } else {
  1609. $this->error('订单编号格式错误');
  1610. }
  1611. }
  1612. $this->success('成功', $order);
  1613. }
  1614. }
  1615. /**
  1616. * 获取PO号 【代码展示未用到】
  1617. */
  1618. public function getPonumber() {
  1619. if ($this->request->isGet() === false){
  1620. $this->error('请求错误');
  1621. }
  1622. $param = $this->request->param();
  1623. if (empty($param)){
  1624. $this->error('参数错误');
  1625. }
  1626. // $num = substr($param['child_order'], 0, 12); // 如果需要,可以用这个方式截取子订单编号
  1627. // $sql = "SELECT * FROM `工单_印件资料` WHERE `子订单编号` LIKE '{$num}%' ORDER BY `子订单编号` DESC LIMIT 1";
  1628. // $data = \db()->query($sql);
  1629. $colorlist = \db('工单_颜色编号')
  1630. ->field('colorcode,colorname')
  1631. ->where('colorname',$param['child_order'])
  1632. ->find();
  1633. $num = $param['order'] . '-' . $colorlist['colorcode']; // 生成 num,用于模糊查询
  1634. $data = \db('工单_印件资料')
  1635. ->where('子订单编号', 'like', '%' . $num . '%')
  1636. ->order('子订单编号', 'desc')
  1637. ->find();
  1638. if(count($data) === 1){
  1639. $order = $num.'01';
  1640. }else{
  1641. $number = (int)substr($data['子订单编号'],12,14) + 1;
  1642. if ($num<10){
  1643. $order = $num.'0'.$number;
  1644. }else{
  1645. $order = $num.$number;
  1646. }
  1647. }
  1648. $this->success('成功',$order);
  1649. }
  1650. /**
  1651. * 订单资料修改
  1652. * @return void
  1653. * @throws \think\Exception
  1654. * @throws \think\db\exception\BindParamException
  1655. * @throws \think\exception\PDOException
  1656. */
  1657. public function WorkOrderEdit()
  1658. {
  1659. if (Request::instance()->isPost() === false){
  1660. $this->error('请求错误');
  1661. }
  1662. $param = Request::instance()->post();
  1663. if (empty($param)){
  1664. $this->error('参数错误');
  1665. }
  1666. $id = $param['id'];
  1667. unset($param['id']);
  1668. $sql = \db('工单_基本资料')
  1669. ->where('Uniqid',$id)
  1670. ->fetchSql(true)
  1671. ->update($param);
  1672. $res = \db()->query($sql);
  1673. if ($res === false){
  1674. $this->error('失败');
  1675. }else{
  1676. $this->success('成功');
  1677. }
  1678. }
  1679. /**
  1680. * 颜色资料修改
  1681. * @return void
  1682. * @throws \think\Exception
  1683. * @throws \think\db\exception\BindParamException
  1684. * @throws \think\exception\PDOException
  1685. */
  1686. public function PrintDataEdit()
  1687. {
  1688. if(Request::instance()->post() === false){
  1689. $this->error('请求错误');
  1690. }
  1691. $param = Request::instance()->post();
  1692. if (empty($param)){
  1693. $this->error('参数错误');
  1694. }
  1695. // $id = $param['id'];
  1696. // unset($param['id']);
  1697. // $findlist = \db('工单_印件资料')
  1698. // ->where('Uniqid', $param['id'])
  1699. // ->find();
  1700. $updata = [
  1701. '订单编号' => $param['订单编号'],
  1702. '子订单编号' => $param['子订单编号'],
  1703. '款号' => $param['款号'],
  1704. '船样' => $param['船样'],
  1705. '颜色' => $param['颜色'],
  1706. 'color_id' => $param['color_id'],
  1707. '颜色备注' => $param['颜色备注'],
  1708. 'zdtotal' => $param['zdtotal']
  1709. ];
  1710. for ($i = 1; $i <= 10; $i++) {
  1711. $updata["cmsl{$i}"] = isset($param["cmsl{$i}"]) ? $param["cmsl{$i}"] : '';
  1712. }
  1713. $sql = \db('工单_印件资料')
  1714. ->where('Uniqid', $param['id'])
  1715. ->fetchSql(true)
  1716. ->update($updata);
  1717. $res = \db()->query($sql);
  1718. if ($res !== false) {
  1719. $this->success('修改成功');
  1720. } else {
  1721. $this->error('修改失败');
  1722. }
  1723. }
  1724. /**
  1725. * 图片上传
  1726. * @return void
  1727. */
  1728. public function ImgUpload(){
  1729. $file = request()->file('image');
  1730. if($file){
  1731. $info = $file->validate(['size'=>10485760,'ext'=>'jpg,png'])->move(ROOT_PATH . 'public' . DS . 'uploads');
  1732. if($info){
  1733. $fileName = $info->getSaveName();
  1734. // $ymd = date('Ymd');
  1735. // $imageUrl = '/uploads/' . $ymd.'/'.$fileName;
  1736. $imageUrl = '/uploads/' . str_replace('\\', '/', $fileName);
  1737. return json(['code' => 0, 'msg' => '成功', 'data' => ['url' => $imageUrl]]);
  1738. }else{
  1739. $res = $file->getError();
  1740. return json(['code' => 1, 'msg' => '失败', 'data' => $res]);
  1741. }
  1742. }
  1743. return json(['code' => 1, 'msg' => '没有文件上传', 'data' => null]);
  1744. }
  1745. /**
  1746. * 工单技术附件
  1747. * @return void
  1748. * @throws \think\db\exception\DataNotFoundException
  1749. * @throws \think\db\exception\ModelNotFoundException
  1750. * @throws \think\exception\DbException
  1751. */
  1752. public function OrderAttachments()
  1753. {
  1754. if ($this->request->isGet() === false){
  1755. $this->error('请求错误');
  1756. }
  1757. $param = $this->request->param();
  1758. if (empty($param)){
  1759. $this->error('参数错误');
  1760. }
  1761. $list = \db('工单_相关附件')
  1762. ->where('关联编号',$param['order'])
  1763. ->where('附件备注',$param['desc'])
  1764. ->whereNull('Mod_rq')
  1765. ->order('sys_rq desc')
  1766. ->limit(1)
  1767. ->select();
  1768. $this->success('成功',$list);
  1769. }
  1770. /**
  1771. * 订单BOM资料显示
  1772. * @return void
  1773. * @throws \think\db\exception\DataNotFoundException
  1774. * @throws \think\db\exception\ModelNotFoundException
  1775. * @throws \think\exception\DbException
  1776. */
  1777. public function OrderBomList()
  1778. {
  1779. if ($this->request->isGet() === false){
  1780. $this->error('请求错误');
  1781. }
  1782. $param = $this->request->param();
  1783. if (empty($param) || !isset($param['order'])){
  1784. $this->error('参数错误');
  1785. }
  1786. $where = ['a.BOM_工单编号'=>$param['order']];
  1787. $list = \db('工单_bom资料')
  1788. ->alias('a')
  1789. ->join('工单_基本资料 b','b.订单编号 = a.BOM_工单编号')
  1790. ->field('a.BOM_工单编号 as 订单编号,b.生产款号 as 生产款号,b.客户编号 as 客户编号,b.款式 as 款式,
  1791. a.BOM_物料名称 as 物料名称,a.BOM_投料单位 as 投料单位,a.BOM_计划用量 as 计划用料,a.BOM_标准用量 as 定额用料,
  1792. a.BOM_实际用量 as 裁床实际用料,a.BOM_领用数量 as 裁床领用面料,a.BOM_退还数量 as 裁床退回仓库面料,
  1793. a.BOM_desc as 备注,a.UNIQID,a.BOM_库存总量 as 入库总量,a.BOM_面料结余 as 面料结余,a.Sys_ID as ID,a.Sys_rq as 日期')
  1794. ->where($where)
  1795. ->select();
  1796. if (!empty($list)){
  1797. $this->success('成功',$list);
  1798. }
  1799. }
  1800. /**
  1801. * 订单面料修改接口
  1802. * @return void
  1803. * @throws \think\Exception
  1804. * @throws \think\db\exception\BindParamException
  1805. * @throws \think\exception\PDOException
  1806. */
  1807. public function FabricEdit()
  1808. {
  1809. if ($this->request->isPost() === false){
  1810. $this->error('请求错误');
  1811. }
  1812. $param = Request::instance()->post();
  1813. if (empty($param)){
  1814. $this->error('请求错误');
  1815. }
  1816. foreach ($param as $key=>$value){
  1817. $data = $value;
  1818. unset($data['UNIQID']);
  1819. if ($value['UNIQID'] !== '' || !empty($value['UNIQID'])){
  1820. $sql = \db('工单_bom资料')
  1821. ->where('UNIQID',$value['UNIQID'])
  1822. ->fetchSql(true)
  1823. ->update($data);
  1824. $res = \db()->query($sql);
  1825. }else{
  1826. $sql = \db('工单_bom资料')
  1827. ->fetchSql(true)
  1828. ->insert($value);
  1829. $res = \db()->query($sql);
  1830. }
  1831. if ($res === false){
  1832. $this->error('修改失败');
  1833. }
  1834. }
  1835. $this->success('修改成功');
  1836. }
  1837. /**
  1838. * 订单BOM出库、退还详情显示
  1839. * @return void
  1840. * @throws \think\db\exception\DataNotFoundException
  1841. * @throws \think\db\exception\ModelNotFoundException
  1842. * @throws \think\exception\DbException
  1843. */
  1844. public function FabricDetail()
  1845. {
  1846. if ($this->request->isGet() === false){
  1847. $this->error('请求错误');
  1848. }
  1849. $param = $this->request->param();
  1850. if (empty($param) || !isset($param['order'])){
  1851. $this->error('参数错误');
  1852. }
  1853. if (isset($param['searchh']) || !empty($param['search'])){
  1854. $list['入库记录'] = \db('设备_报工日志')
  1855. ->where('order_id|款号',$param['order'])
  1856. ->where('name','入库')
  1857. ->field('order_id as 订单编号,款号,number as 数量,rq as 入库时间,sys_id as 上报机台,recipient as 入仓人员')
  1858. ->order('出库时间 desc')
  1859. ->select();
  1860. }else{
  1861. //出库记录查询
  1862. $list['出库记录'] = \db('设备_报工日志')
  1863. ->where('order_id|款号',$param['order'])
  1864. ->where('name','出库')
  1865. ->field('order_id as 订单编号,款号,number as 数量,rq as 出库时间,sys_id as 上报机台,recipient as 出库人员')
  1866. ->order('出库时间 desc')
  1867. ->select();
  1868. //退还记录查询
  1869. $list['退还记录'] = \db('设备_报工日志')
  1870. ->where('order_id|款号',$param['order'])
  1871. ->where('name','退还')
  1872. ->field('order_id as 订单编号,款号,number as 数量,rq as 退还时间,sys_id as 上报机台,recipient as 退还人员')
  1873. ->order('出库时间 desc')
  1874. ->select();
  1875. }
  1876. $this->success('成功',$list);
  1877. }
  1878. //GPT
  1879. public function Gpt($massage)
  1880. {
  1881. // 设置 API 密钥
  1882. $apiKey = 'sk-e0JuPjMntkbgi1BoMjrqyyzMKzAxILkQzyGMSy3xiMupuoWY'; // 替换为您的 API 密钥
  1883. // 要发送给 GPT 的消息
  1884. $messages = [
  1885. [
  1886. 'role' => 'user',
  1887. 'content' => '你好,帮我按照:面料1、面料2、面料3……来整理归纳下面的面料信息,我只需要面料信息,不需要其他:'.$massage
  1888. ]
  1889. ];
  1890. // 创建请求数据
  1891. $data = [
  1892. 'model' => 'gpt-3.5-turbo', // 使用的模型
  1893. 'messages' => $messages,
  1894. 'max_tokens' => 100, // 设置最大 token 数
  1895. ];
  1896. // 初始化 cURL
  1897. $ch = curl_init('https://niubi.zeabur.app/v1/chat/completions');
  1898. // 设置 cURL 选项
  1899. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1900. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  1901. 'Content-Type: application/json',
  1902. 'Authorization: Bearer ' . $apiKey,
  1903. ]);
  1904. curl_setopt($ch, CURLOPT_POST, true);
  1905. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  1906. curl_setopt($ch, CURLOPT_CAINFO, ROOT_PATH . '/public/uploads/cacert.pem');
  1907. // 执行请求
  1908. $response = curl_exec($ch);
  1909. // 检查错误
  1910. if (curl_errno($ch)) {
  1911. echo 'Error:' . curl_error($ch);
  1912. }
  1913. // 关闭 cURL
  1914. curl_close($ch);
  1915. // 解析和输出响应
  1916. $responseData = json_decode($response, true);
  1917. // 获取 GPT 的回复
  1918. if (isset($responseData['choices'][0]['message']['content'])) {
  1919. //获取返回内容
  1920. $gptReply = $responseData['choices'][0]['message']['content'];
  1921. // halt($gptReply);
  1922. //返回面料信息
  1923. $gptArray = explode('面料',$gptReply);
  1924. array_shift($gptArray);
  1925. foreach ($gptArray as $key=>$value){
  1926. $gptArray[$key] = preg_replace('/\s+/', '', substr($value,4));
  1927. }
  1928. return $gptArray;
  1929. } else {
  1930. echo "未能获取 GPT 的回复。";
  1931. }
  1932. }
  1933. /**
  1934. * 面料库存
  1935. * @return void
  1936. * @throws \think\db\exception\DataNotFoundException
  1937. * @throws \think\db\exception\ModelNotFoundException
  1938. * @throws \think\exception\DbException
  1939. */
  1940. public function fabricList()
  1941. {
  1942. if ($this->request->isGet() === false){
  1943. $this->error('请求错误');
  1944. }
  1945. $param = $this->request->param();
  1946. if (empty($param) || !isset($param['order'])){
  1947. $this->error('参数错误');
  1948. }
  1949. $where = ['a.BOM_工单编号'=>$param['order']];
  1950. $list = \db('工单_bom资料')
  1951. ->alias('a')
  1952. ->join('工单_基本资料 b','b.订单编号 = a.BOM_工单编号')
  1953. ->field('a.BOM_工单编号 as 订单编号,b.生产款号 as 生产款号,b.客户编号 as 客户编号,b.款式 as 款式,
  1954. a.BOM_物料名称 as 物料名称,a.BOM_投料单位 as 投料单位,a.BOM_库存总量 as 库存总量,a.BOM_面料结余 as 面料结余,
  1955. a.UNIQID')
  1956. ->where($where)
  1957. ->select();
  1958. if (!empty($list)){
  1959. $this->success('成功',$list);
  1960. }
  1961. }
  1962. /**
  1963. * 单条面料详情
  1964. * @return void
  1965. * @throws \think\db\exception\DataNotFoundException
  1966. * @throws \think\db\exception\ModelNotFoundException
  1967. * @throws \think\exception\DbException
  1968. */
  1969. public function oneFabricDetail()
  1970. {
  1971. if ($this->request->isGet() === false){
  1972. $this->error('请求错误');
  1973. }
  1974. $param = $this->request->param();
  1975. if (empty($param)){
  1976. $this->error('参数错误');
  1977. }
  1978. //面料入库记录
  1979. $list['入库'] = \db('设备_报工日志')
  1980. ->where('order_id',$param['order'])
  1981. ->where('物料名称',$param['fabricName'])
  1982. ->where('name','入库')
  1983. ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,recipient as 入仓人员')
  1984. ->select();
  1985. //面料出库记录
  1986. $list['出库'] = \db('设备_报工日志')
  1987. ->where('order_id',$param['order'])
  1988. ->where('物料名称',$param['fabricName'])
  1989. ->where('name','出库')
  1990. ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,receipt_number as 出库单据编号,recipient as 领用人员')
  1991. ->select();
  1992. //面料退还记录
  1993. $list['退还'] = \db('设备_报工日志')
  1994. ->where('order_id',$param['order'])
  1995. ->where('物料名称',$param['fabricName'])
  1996. ->where('name','退还')
  1997. ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,recipient as 退还人员')
  1998. ->select();
  1999. $this->success('成功',$list);
  2000. }
  2001. }