WorkOrder.php 103 KB

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