WorkOrder.php 102 KB

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