WorkOrder.php 96 KB

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