WorkOrder.php 100 KB

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