| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\api\controller\OrderSuperLoss;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\DbException;
- use think\Db;
- /**
- * 成品入仓维护接口
- */
- class FinishedProductWarehousing extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- /**
- * 首页
- *成品入仓测试
- */
- public function index()
- {
- $this->success('请求成功');
- }
- /**
- * 侧边栏
- * @ApiMethod (GET)
- */
- public function getTab()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $rows = db('成品入仓')
- ->field('LEFT(Sys_rq, 10) as date, COUNT(*) as counts')
- ->group('date')
- ->order('UniqId desc')
- ->limit(50)
- ->select();
- $num = db('成品入仓')
- ->where('Sys_rq','>=',$rows[count($rows)-1]['date'])
- ->count();
- $arr = db('成品入仓')
- ->field('LEFT(Sys_rq, 10) as date, rtrim(sys_id) as sys_id, COUNT(*) as count')
- ->where('Sys_rq','>=',$rows[count($rows)-1]['date'])
- ->group('date, sys_id')
- ->limit($num)
- ->select();
- foreach($rows as $key=>$value){
- $rows[$key]['sys'] = [];
- foreach($arr as $k=>$v){
- if($value['date'] == $v['date']){
- unset($v['date']);
- array_push($rows[$key]['sys'],$v);
- unset($arr[$k]);
- }
- }
- $rows[$key]['date'] = str_replace('-', '.', $rows[$key]['date']);
- }
- $this->success('成功',$rows);
- }
- /**
- * 侧边栏
- * @ApiMethod (GET)
- */
- public function getTabByGdbh()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $num = db('成品入仓')->count();
- $rows = db('成品入仓')
- ->field('jjcp_gdbh, rtrim(jjcp_cpdh) as jjcp_cpdh, LEFT(Sys_rq, 10) as Sys_rq')
- ->order('Sys_rq desc')
- ->limit($num)
- ->select();
- $num = db('工单_基本资料')->count();
- $arr = db('工单_基本资料')
- ->limit($num)
- ->column('CONCAT(Gd_gdbh, "-", rtrim(Gd_cpdh)) AS gdcp, CONCAT(rtrim(Gd_khdh), " 【", rtrim(Gd_khmc)) AS kh');
- $data = [];
- foreach ($rows as $value) {
- $key = $value['jjcp_gdbh'].'-'.$value['jjcp_cpdh'];
- if (isset($arr[$key]) && !isset($data[$arr[$key]])) {
- $data[$arr[$key]] = $value['Sys_rq'];
- }
- }
- // 对键进行升序排序
- uksort($data, function($a, $b) use ($data) {
- if ($data[$a] == $data[$b]) {
- return ($a > $b) ? 1 : -1; // 在值相同的情况下,根据键进行升序排列
- }
- return ($data[$a] < $data[$b]) ? 1 : -1;
- });
- $this->success('成功',$data);
- }
- /**
- * 列表
- * @ApiMethod (GET)
- * @param string $date 时间
- * @param string $sys_id 用户
- */
- public function getList()
- {
- // 验证请求方式
- if (!$this->request->isGet()) {
- $this->error('请求方式错误');
- }
-
- $req = $this->request->param();
- if (!isset($req['date'])) {
- $this->error('参数缺失');
- }
-
- // 设置分页参数,默认值
- $page = isset($req['page']) && !empty($req['page']) ? intval($req['page']) : 1;
- $limit = isset($req['limit']) && !empty($req['limit']) ? intval($req['limit']) : 20;
-
- // 构建查询条件
- if (strpos($req['date'], '-')) {
- $where = ['c.Sys_rq' => ['like', $req['date'] . '%']];
- $option = ['Sys_rq' => ['like', $req['date'] . '%']];
- } else {
- $where = [];
- $option = [];
- }
-
- if (isset($req['sys_id']) && !empty($req['sys_id'])) {
- $where['c.Sys_id'] = ['LIKE', $req['sys_id'] . '%'];
- $option['Sys_id'] = ['LIKE', $req['sys_id'] . '%'];
- }
-
- // 获取总数
- $total = db('成品入仓')->where($option)->count();
-
- // 查询数据(带分页)
- $rows = db('成品入仓')->alias('c')
- ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
- rtrim(c.jjcp_num) as jjcp_num, c.jjcp_gdbh,
- c.jjcp_yjno, c.jjcp_sl, g.订单数量, rtrim(c.jjcp_dw) as jjcp_dw, LEFT(c.jjcp_sj, 10) as jjcp_sj,
- rtrim(c.jjcp_smb) as jjcp_smb, rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
- rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称,
- rtrim(c.客户料号) as 客户料号, rtrim(c.jjcp_desc) as jjcp_desc, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(g.Gd_客户名称) as Gd_客户名称,
- rtrim(c.机型备注) as 机型备注, rtrim(c.Sys_id) as Sys_id, c.Sys_rq, c.Mod_rq, c.UniqId')
- ->where($where)
- ->join('工单_基本资料 g', 'g.Gd_gdbh=c.jjcp_gdbh AND g.行号 = c.jjcp_yjno AND g.Gd_cpdh=c.jjcp_cpdh')
- ->order('c.UniqId desc')
- ->limit(($page - 1) * $limit, $limit)
- ->select();
-
- // 处理数据
- foreach ($rows as $key => $value) {
- $row = db('成品入仓')
- ->field('LEFT(Sys_rq, 10) as Sys_rq,jjcp_sl')
- ->where(['jjcp_gdbh' => $value['jjcp_gdbh'], 'jjcp_cpdh' => $value['jjcp_cpdh']])
- ->order('Sys_rq desc')
- ->select();
-
- $rows[$key]['jjcp_sls'] = 0;
- foreach ($row as $k => $v) {
- $rows[$key]['jjcp_sls'] += $v['jjcp_sl'];
- }
-
- if ($value['订单数量'] != 0) {
- $rows[$key]['完成率'] = number_format($rows[$key]['jjcp_sls'] / $value['订单数量'] / 100, 2);
- } else {
- $rows[$key]['完成率'] = '';
- }
-
- $rows[$key]['订单数量'] = floatval($value['订单数量'] * 10000);
- $rows[$key]['最近入仓日期'] = $row[0]['Sys_rq'];
- $rows[$key]['Mod_rq'] = $value['Mod_rq'] == '1900-01-01 00:00:00' ? '' : $value['Mod_rq'];
- }
-
- $data = [
- 'total' => $total,
- 'rows' => $rows,
- ];
-
- $this->success('成功', $data);
- }
- /**
- * 定位
- * @ApiMethod (GET)
- * @param string $gdbh 工单编号
- * @param string $cpmc 产品名称
- * @param string $page 页码
- * @param string $limit 数量
- */
- public function locate()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- $page = 1;
- $limit = 15;
- if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
- if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
- if (isset($req['gdbh']) && !empty($req['gdbh'])){
- $where = ['c.jjcp_gdbh'=>[ 'like', '%' . $req['gdbh'] . '%']];
- $option = ['jjcp_gdbh'=>[ 'like', '%' . $req['gdbh'] . '%']];
- //客户编号
- $rows = db('成品入仓')->alias('c')
- ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
- rtrim(c.jjcp_num) as jjcp_num, c.jjcp_gdbh,
- c.jjcp_yjno, c.jjcp_sl, g.订单数量, rtrim(c.jjcp_dw) as jjcp_dw, LEFT(c.jjcp_sj, 10) as jjcp_sj,
- rtrim(c.jjcp_smb) as jjcp_smb, rtrim(y.yj_Yjdh) as yj_Yjdh, rtrim(y.yj_yjmc) as yj_yjmc,
- rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
- rtrim(c.客户料号) as 客户料号, rtrim(c.jjcp_desc) as jjcp_desc, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(g.Gd_客户名称) as Gd_客户名称,
- rtrim(c.机型备注) as 机型备注, rtrim(c.Sys_id) as Sys_id, c.Sys_rq, c.Mod_rq, c.UniqId')
- ->where($where)
- ->where('y.yj_Yjno=c.jjcp_yjno')
- ->where('g.Gd_cpdh=c.jjcp_cpdh')
- ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
- ->join('工单_印件资料 y','y.Yj_Gdbh=c.jjcp_gdbh')
- ->order('c.UniqId desc')
- ->page($page,$limit)
- ->select();
- $total = db('成品入仓')->where($option)->count();
- }else{
- if (isset($req['cpmc']) && !empty($req['cpmc'])){
- //查询工单表
- $gd = db('工单_基本资料')
- ->where('Gd_cpmc', 'LIKE', '%'.$req['cpmc'].'%')
- ->column('Gd_gdbh');
- $where = ['c.jjcp_gdbh'=>['in', $gd]];
- $option = ['jjcp_gdbh'=>['in', $gd]];
- //客户编号
- $rows = db('成品入仓')->alias('c')
- ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
- rtrim(c.jjcp_num) as jjcp_num, c.jjcp_gdbh,
- c.jjcp_yjno, c.jjcp_sl, g.订单数量, rtrim(c.jjcp_dw) as jjcp_dw, LEFT(c.jjcp_sj, 10) as jjcp_sj,
- rtrim(c.jjcp_smb) as jjcp_smb, rtrim(y.yj_Yjdh) as yj_Yjdh, rtrim(y.yj_yjmc) as yj_yjmc,
- rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
- rtrim(c.客户料号) as 客户料号, rtrim(c.jjcp_desc) as jjcp_desc, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(g.Gd_客户名称) as Gd_客户名称,
- rtrim(c.机型备注) as 机型备注, rtrim(c.Sys_id) as Sys_id, c.Sys_rq, c.Mod_rq, c.UniqId')
- ->where($where)
- ->where('y.yj_Yjno=c.jjcp_yjno')
- ->where('g.Gd_cpdh=c.jjcp_cpdh')
- ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
- ->join('工单_印件资料 y','y.Yj_Gdbh=c.jjcp_gdbh')
- ->order('c.UniqId desc')
- ->page($page,$limit)
- ->select();
- $total = db('成品入仓')->where($option)->count();
- }else{
- $this->error('参数错误');
- }
- }
- foreach ($rows as $key=>$value) {
- $row = db('成品入仓')
- ->field('LEFT(Sys_rq, 10) as Sys_rq,jjcp_sl')
- ->where(['jjcp_gdbh'=>$value['jjcp_gdbh'], 'jjcp_cpdh'=>$value['jjcp_cpdh']])
- ->order('Sys_rq desc')
- ->select();
- $rows[$key]['jjcp_sls'] = 0;
- foreach ($row as $k=>$v) {
- $rows[$key]['jjcp_sls'] += $v['jjcp_sl'];
- }
- if ($value['订单数量']!=0){
- $rows[$key]['完成率'] = number_format($rows[$key]['jjcp_sls']/$value['订单数量']/100,2);
- }else{
- $rows[$key]['完成率'] = '';
- }
- $rows[$key]['订单数量'] = floatval($value['订单数量']*10000);
- $rows[$key]['最近入仓日期'] = $row[0]['Sys_rq'];
- $rows[$key]['Mod_rq'] = $value['Mod_rq']=='1900-01-01 00:00:00' ? '' :$value['Mod_rq'];
- }
- $data = [
- 'total' => $total,
- 'rows' => $rows,
- ];
- $this->success('成功',$data);
- }
- /**
- * 获取信息
- * @ApiMethod (GET)
- * @param string $UniqId UniqId
- */
- public function getInfo()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- if (isset($req['UniqId']) && !empty($req['UniqId'])){
- $UniqId = $req['UniqId'];
- }else{
- $this->error('参数错误');
- }
- //客户编号
- $rows = db('成品入仓')->alias('c')
- ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
- rtrim(c.jjcp_num) as jjcp_num,
- c.jjcp_gdbh, c.jjcp_yjno, rtrim(c.订单编号) as 订单编号,
- rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
- rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称,
- rtrim(c.客户料号) as 客户料号,
- rtrim(c.jjcp_smb) as jjcp_smb, LEFT(c.jjcp_sj, 10) as jjcp_sj,
- c.jjcp_sl, rtrim(c.jjcp_dw) as jjcp_dw,
- g.订单数量*10000 as 订单数量, rtrim(c.机型备注) as 机型备注, rtrim(c.jjcp_desc) as jjcp_desc,
- rtrim(c.Sys_id) as Sys_id, c.Sys_rq, c.Mod_rq, c.UniqId')
- ->where('c.UniqId',$UniqId)
- ->where('g.Gd_cpdh=c.jjcp_cpdh')
- ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
- ->find();
- $rows['jjcp_sls'] = db('成品入仓')
- ->where(['jjcp_gdbh'=>$rows['jjcp_gdbh'], 'jjcp_cpdh'=>$rows['jjcp_cpdh']])
- ->sum('jjcp_sl');
- $this->success('成功',$rows);
- }
- /**
- * 获取仓库信息
- * @ApiMethod (GET)
- * @param string keyword 关键词
- */
- public function getCk()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- $key_word = '';
- if (isset($req['key_word']) && !empty($req['key_word'])){
- $key_word = $req['key_word'];
- }
- $rs = db('物料_仓库信息')
- ->field('rtrim(编号) as 编号, rtrim(名称) as 名称')
- ->where('编号|名称','like','%'.$key_word.'%')
- ->where('名称','in',['面材仓库','主要产成品'])
- ->select();
- $this->success('成功',$rs);
- }
- /**
- * 查询印件工序及产品名称
- * @ApiMethod (GET)
- * @param string $gdbh 工单编号
- */
- public function getGd()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- if (isset($req['gdbh']) && !empty($req['gdbh'])){
- $gdbh = $req['gdbh'];
- }else{
- $this->error('参数错误');
- }
- $rows = db('工单_基本资料')->alias('g')
- ->field('g.Gd_gdbh, j.yj_yjno, rtrim(g.销售订单号) as 销售订单号, rtrim(j.yj_Yjdh) as yj_Yjdh, rtrim(j.yj_yjmc) as yj_yjmc, rtrim(g.成品代号) as 成品代号, rtrim(g.成品名称) as 成品名称, rtrim(g.客户料号) as 客户料号')
- ->where(['g.Gd_gdbh'=>$gdbh])
- ->join(['工单_印件资料'=>'j'],'g.Gd_cpdh=j.yj_yjdh and g.Gd_gdbh=j.Yj_Gdbh')
- ->group('j.yj_yjno')
- ->order('j.yj_yjno asc')
- ->select();
- foreach ($rows as $key=>$value){
- $rows[$key]['gdyj'] = $value['Gd_gdbh'].' -'.sprintf("%02d", $value['yj_yjno']).'-'.$value['yj_yjmc'];
- }
- $this->success('成功',$rows);
- }
- /**
- * 查询工单数量
- * @ApiMethod (GET)
- * @param string $gdbh 工单编号
- * @param string $cpdh 产品代号
- * @param string $rcsl 入仓数量
- */
- public function getGdsl()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- if (isset($req['gdbh']) && !empty($req['gdbh'])){
- $gdbh = $req['gdbh'];
- }else{
- $this->error('参数错误');
- }
- if (isset($req['cpdh']) && !empty($req['cpdh'])){
- $cpdh = $req['cpdh'];
- }else{
- $this->error('参数错误');
- }
- $rows = db('工单_基本资料')->alias('g')
- ->field('订单数量*10000 as 订单数量, gd_statu')
- ->where(['Gd_gdbh'=>$gdbh, 'Gd_cpdh'=>$cpdh])
- ->find();
- if (!$rows) $this->error('未查询到记录');
- $rows['jjcp_sls'] = db('成品入仓')
- ->where(['jjcp_gdbh'=>$gdbh, 'jjcp_cpdh'=>$cpdh])
- ->sum('jjcp_sl');
- $this->success('成功',$rows);
- }
- /**
- * 新增
- * @ApiMethod (POST)
- */
- public function add()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- $data = $this->buildAddData($req);
- $this->validateProductName($data['jjcp_gdbh'], $data['jjcp_yjno'], $data['jjcp_cpmc']);
- $existingEndBoard = $this->findExistingEndBoard($data['jjcp_gdbh'], $data['jjcp_yjno']);
- if ($existingEndBoard) {
- $this->error(
- $existingEndBoard['jjcp_gdbh'] . '工单 -- 印件' . $existingEndBoard['jjcp_yjno'] . '已经进入末版,无法继续操作'
- );
- }
- $this->validateMaterialReceived($data['jjcp_gdbh'], $data['jjcp_yjno']);
-
- $data['UniqId'] = $this->generateFinishStockUniqId();
-
- $mongodb = Db::connect(config('database.mongodb'));
- $this->saveFinishStock($data, $mongodb);
- // 接单日期在 2026-08-01 及以后,且中间库 U8_06工单资料存在该工单时才同步
- if ($this->shouldSyncFinishStockMiddle($data['jjcp_gdbh'])) {
- try {
- $synchronization = new \app\api\controller\Synchronization();
- $synchronization->syncFinishStockMiddle($data['UniqId'], '新增');
- } catch (\Exception $e) {
- $this->error('成品入仓已保存,但' . $e->getMessage(), ['UniqId' => $data['UniqId']]);
- }
- }
-
- if ($this->isEndBoard($data['jjcp_smb'])) {
- $this->handleEndBoardSuperLoss($data['jjcp_gdbh'], $data['jjcp_yjno']);
- }
- $this->success('成功', ['UniqId' => $data['UniqId']]);
- }
- /**
- * 查询成品入仓同步操作日志
- * @ApiMethod (GET)
- * @param int|string $UniqId 成品入仓 UniqId
- */
- public function getSyncLog()
- {
- if (!$this->request->isGet()) {
- $this->error('请求方式错误');
- }
- $uniqId = $this->request->param('UniqId');
- if ($uniqId === null || $uniqId === '') {
- $this->error('参数错误', 'UniqId');
- }
- $list = db('工单_成品入仓同步操作日志')
- ->where('UniqId', (int)$uniqId)
- ->field('id,Gdbh,wlbm,yjmc,Yjdh,Yjno,sys_id,sys_rq,type,status,status_msg,UniqId')
- ->order('id desc')
- ->select();
- $this->success('成功', $list ?: []);
- }
- /**
- * 修改
- * 前端传:UniqId、数量(jjcp_sl)、smb(jjcp_smb)、sys_id(Sys_id)、时间(jjcp_sj)
- * @ApiMethod (POST)
- */
- public function edit()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- $uniqId = $req['UniqId'] ?? ($req['UniaId'] ?? null);
- if ($uniqId === null || trim((string)$uniqId) === '') {
- $this->error('参数错误', 'UniqId', 100);
- }
- $uniqId = (int)$uniqId;
- $old = db('成品入仓')->where('UniqId', $uniqId)->find();
- if (empty($old)) {
- $this->error('未找到成品入仓记录');
- }
- $hasQty = array_key_exists('jjcp_sl', $req) || array_key_exists('数量', $req);
- $hasSmb = array_key_exists('jjcp_smb', $req) || array_key_exists('smb', $req);
- $hasSj = array_key_exists('jjcp_sj', $req);
- $sysId = $req['Sys_id'] ?? ($req['sys_id'] ?? null);
- if (!$hasQty && !$hasSmb && !$hasSj && ($sysId === null || $sysId === '')) {
- $this->error('参数错误', '', 111);
- }
- $data = ['Mod_rq' => date('Y-m-d H:i:s')];
- if ($sysId !== null && $sysId !== '') {
- $data['Sys_id'] = $sysId;
- }
- if ($hasSj) {
- $data['jjcp_sj'] = $req['jjcp_sj'];
- }
- $oldQty = (float)$old['jjcp_sl'];
- $newQty = $oldQty;
- $qtyChanged = false;
- if ($hasQty) {
- $newQty = (float)($req['jjcp_sl'] ?? $req['数量']);
- $data['jjcp_sl'] = $newQty;
- $qtyChanged = abs($newQty - $oldQty) > 0.00001;
- }
- $oldSmb = (string)$old['jjcp_smb'];
- $newSmb = $oldSmb;
- $smbChanged = false;
- if ($hasSmb) {
- $newSmb = (string)($req['jjcp_smb'] ?? $req['smb']);
- $data['jjcp_smb'] = $newSmb;
- $smbChanged = trim($newSmb) !== trim($oldSmb);
- }
- $sjChanged = false;
- if ($hasSj) {
- $sjChanged = trim((string)$req['jjcp_sj']) !== trim((string)$old['jjcp_sj']);
- }
- // 改为末板时,校验同工单同印件是否已有其他末板记录
- if ($smbChanged && $this->isEndBoard($newSmb)) {
- $existingEndBoard = $this->findExistingEndBoard($old['jjcp_gdbh'], $old['jjcp_yjno']);
- if ($existingEndBoard && (int)$existingEndBoard['UniqId'] !== $uniqId) {
- $this->error(
- $existingEndBoard['jjcp_gdbh'] . '工单 -- 印件' . $existingEndBoard['jjcp_yjno'] . '已经进入末版,无法继续操作'
- );
- }
- }
- $mongodb = Db::connect(config('database.mongodb'));
- db()->startTrans();
- try {
- $sql = db('成品入仓')->where('UniqId', $uniqId)->fetchSql(true)->update($data);
- $bool = db()->query($sql);
- $mongodb->name('finished_products')
- ->where('UniqId', $uniqId)
- ->update($data);
- db()->commit();
- } catch (\Exception $e) {
- db()->rollback();
- $this->error($e->getMessage());
- }
- if ($bool === false) {
- $this->error('失败');
- }
- // 同步中间表:
- // - 数量变更:插入差值为新同步数据(增加为正、减少为负)
- // - 仅修改 smb / 时间:正常同步完整数量
- // - 数量与其他同时改:只同步差值
- if ($this->shouldSyncFinishStockMiddle($old['jjcp_gdbh']) && ($qtyChanged || $smbChanged || $sjChanged)) {
- try {
- $synchronization = new \app\api\controller\Synchronization();
- if ($qtyChanged) {
- $qtyDiff = $newQty - $oldQty; // 增加为正,减少为负
- $synchronization->syncFinishStockMiddle($uniqId, '修改', $qtyDiff, $sysId);
- } else {
- $synchronization->syncFinishStockMiddle($uniqId, '修改', null, $sysId);
- }
- } catch (\Exception $e) {
- $this->error('成品入仓已保存,但' . $e->getMessage(), ['UniqId' => $uniqId]);
- }
- }
- if ($smbChanged && $this->isEndBoard($newSmb)) {
- $this->handleEndBoardSuperLoss($old['jjcp_gdbh'], $old['jjcp_yjno']);
- }
- $this->success('成功');
- }
- /**
- * 删除
- * 前端传:UniqId、sys_id(Sys_id)
- * @ApiMethod (POST)
- */
- public function del()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- if (!(isset($req['UniqId']) && trim($req['UniqId']) != '')) {
- $this->error('参数错误', 'UniqId', 100);
- }
- $sysId = $req['Sys_id'] ?? ($req['sys_id'] ?? null);
- if ($sysId === null || trim((string)$sysId) === '') {
- $this->error('参数错误', 'sys_id', 101);
- }
- $uniqId = (int)$req['UniqId'];
- $old = db('成品入仓')->where('UniqId', $uniqId)->find();
- if (empty($old)) {
- $this->error('未找到成品入仓记录');
- }
- // 满足同步条件才回冲:MES插入类型=新增,数量取负;不满足则正常删除
- if ($this->shouldSyncFinishStockMiddle($old['jjcp_gdbh'])) {
- try {
- $synchronization = new \app\api\controller\Synchronization();
- $synchronization->syncFinishStockMiddle(
- $uniqId,
- '新增',
- -1 * (float)$old['jjcp_sl'],
- $sysId
- );
- } catch (\Exception $e) {
- $this->error('同步中间表失败,未删除:' . $e->getMessage(), ['UniqId' => $uniqId]);
- }
- }
- $mongodb = Db::connect(config('database.mongodb'));
- db()->startTrans();
- try {
- $sql = db('成品入仓')->where('UniqId', $uniqId)->fetchSql(true)->delete();
- $bool = db()->query($sql);
- $mongodb->name('finished_products')
- ->where('UniqId', $uniqId)
- ->delete();
- db()->commit();
- } catch (\Exception $e) {
- db()->rollback();
- $this->error($e->getMessage());
- }
- if ($bool === false) {
- $this->error('失败');
- }
- $this->success('成功');
- }
- /**
- * excel导出
- * @ApiMethod (POST)
- * @param string date
- * @param string sys_id
- * @param array fields
- */
- public function export(){
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- if (!isset($req['date'])) $this->error('参数缺失');
- if (!(isset($req['fields']) && count($req['fields'])!=0)){
- $this->error('参数错误','fields',100);
- }
- if(strpos($req['date'],'-')){
- $where = ['c.Sys_rq'=>['like',$req['date'].'%']];
- }else if(empty($req['date'])){
- $where = [];
- }else{
- $where = ['c.jjcp_gdbh'=>$req['date']];
- }
- if (isset($req['sys_id']) && !empty($req['sys_id'])){
- $where['c.Sys_id'] = ['LIKE',$req['sys_id'].'%'];
- }
- $arr = [
- '入仓类型' =>['入仓类型'],
- '仓库编号' =>['仓库编号'],
- '仓库名称' =>['仓库名称'],
- 'jjcp_num' =>['入仓单号'],
- 'jjcp_gdbh' =>['工单编号'],
- 'jjcp_yjno' =>['印件号'],
- 'jjcp_sl' =>['入仓数量'],
- 'jjcp_sls' =>['累计入仓数量'],
- '订单数量' =>['订单数量'],
- '完成率' =>['完成率'],
- 'jjcp_dw' =>['单位'],
- 'jjcp_sj' =>['入仓时间'],
- 'jjcp_smb' =>['首末板'],
- 'jjcp_cpdh' =>['印件代号'],
- 'jjcp_cpmc' =>['印件名称'],
- '成品编码' =>['产品编号'],
- '成品名称' =>['产品名称'],
- '客户料号' =>['客户料号'],
- 'jjcp_desc' =>['备注'],
- '客户编号' =>['客户编号'],
- 'Gd_客户名称' =>['客户名称'],
- '最近入仓日期' =>['最近入仓日期'],
- '机型备注' =>['机型备注'],
- ];
- $data[0] = [];
- foreach ($req['fields'] as $k=>$v){
- if(array_key_exists($v,$arr)){
- array_push($data[0],$arr[$v][0]);
- }
- }
- $num = db('成品入仓')->alias('c')
- ->where($where)
- ->where('g.Gd_cpdh=c.jjcp_cpdh')
- ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
- ->count();
- $rows = [];
- for($i=1;$i<=ceil($num/10000);$i++){
- $p = $i == ceil($num/10000) ? $num%10000 : 10000;
- $r = db('成品入仓')->alias('c')
- ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
- rtrim(c.jjcp_num) as jjcp_num, c.jjcp_gdbh,
- c.jjcp_yjno, c.jjcp_sl, g.订单数量, rtrim(c.jjcp_dw) as jjcp_dw, LEFT(c.jjcp_sj, 10) as jjcp_sj,
- rtrim(c.jjcp_smb) as jjcp_smb, rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
- rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称,
- rtrim(c.客户料号) as 客户料号, rtrim(c.jjcp_desc) as jjcp_desc, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(g.Gd_客户名称) as Gd_客户名称,
- rtrim(c.机型备注) as 机型备注')
- ->where($where)
- ->where('g.Gd_cpdh=c.jjcp_cpdh')
- ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
- ->order('c.UniqId desc')
- ->limit($i,$p)
- ->select();
- $rows = array_merge($rows,$r);
- }
- foreach ($rows as $key=>$value) {
- $row = db('成品入仓')
- ->field('LEFT(Sys_rq, 10) as Sys_rq,jjcp_sl')
- ->where(['jjcp_gdbh'=>$value['jjcp_gdbh'], 'jjcp_cpdh'=>$value['jjcp_cpdh']])
- ->order('Sys_rq desc')
- ->select();
- $subArray = [];
- foreach ($arr as $k=>$v){
- if($k=='jjcp_sls'){
- $subArray[$k] = 0;
- foreach ($row as $n) {
- $subArray[$k] += $n['jjcp_sl'];
- }
- }else if($k=='完成率'){
- if ($value['订单数量']!=0){
- $subArray[$k] = 0;
- foreach ($row as $n) {
- $subArray[$k] += $n['jjcp_sl'];
- }
- $subArray[$k] = number_format($subArray[$k]/$value['订单数量']/100,2);
- }else{
- $subArray[$k] = '';
- }
- }else if($k=='最近入仓日期'){
- $subArray[$k] = $row[0]['Sys_rq'];
- }else{
- $subArray[$k] = $rows[$key][$k];
- }
- }
- array_push($data,$subArray);
- }
- $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
- }
- /**
- * 月度A
- * @ApiMethod (POST)
- * @param string start_time
- * @param string end_time
- * @param string file_name
- * @param array fields
- */
- public function monthExportA(){
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- $arr = [
- 'start_time', 'end_time', 'file_name'
- ];
- foreach ($arr as $key => $value){
- if (!isset($req[$value])){
- $this->error('参数错误',$value,$key+1);
- }
- }
- if (!(isset($req['fields']) && count($req['fields'])!=0)){
- $this->error('参数错误','fields',100);
- }
- $arr = [
- '入仓类型' =>['入仓类型'],
- '仓库编号' =>['仓库编号'],
- '仓库名称' =>['仓库名称'],
- 'jjcp_gdbh' =>['工单编号'],
- 'jjcp_cpdh' =>['入仓存货编号'],
- 'jjcp_cpmc' =>['入仓存货名称'],
- 'jjcp_sl' =>['入仓数量'],
- 'jjcp_dw' =>['单位'],
- 'jjcp_sj' =>['入仓时间'],
- 'jjcp_smb' =>['首末板'],
- 'jjcp_num' =>['单据编号'],
- '订单编号' =>['订单编号'],
- ];
- $data[0] = [];
- foreach ($req['fields'] as $k=>$v){
- if(array_key_exists($v,$arr)){
- array_push($data[0],$arr[$v][0]);
- }
- }
- //客户编号
- $rows = db('成品入仓')->alias('c')
- ->field('rtrim(c.入仓类型) as 入仓类型, rtrim(c.仓库编号) as 仓库编号, rtrim(c.仓库名称) as 仓库名称,
- c.jjcp_gdbh, rtrim(c.jjcp_cpdh) as jjcp_cpdh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
- c.jjcp_sl, rtrim(c.jjcp_dw) as jjcp_dw, c.jjcp_sj,
- rtrim(c.jjcp_smb) as jjcp_smb,
- rtrim(c.jjcp_num) as jjcp_num, rtrim(c.订单编号) as 订单编号
- ')
- ->where('c.jjcp_sj','between time',[$req['start_time'], $req['end_time']])
- ->order('c.jjcp_sj asc')
- ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
- ->where('g.Gd_cpdh=c.jjcp_cpdh')
- ->select();
- foreach ($rows as $key=>$value) {
- $subArray = [];
- foreach ($arr as $k=>$v){
- $subArray[$k] = $rows[$key][$k];
- }
- array_push($data,$subArray);
- }
- $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
- }
- /**
- * 月度B
- * @ApiMethod (POST)
- * @param string start_time
- * @param string end_time
- * @param string file_name
- * @param array fields
- */
- public function monthExportB(){
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- $arr = [
- 'start_time', 'end_time', 'file_name'
- ];
- foreach ($arr as $key => $value){
- if (!isset($req[$value])){
- $this->error('参数错误',$value,$key+1);
- }
- }
- if (!(isset($req['fields']) && count($req['fields'])!=0)){
- $this->error('参数错误','fields',100);
- }
- $arr = [
- '入仓类型' =>['入仓类型'],
- '客户编号' =>['客户编号'],
- '客户名称' =>['客户名称'],
- '产品类别' =>['产品类别'],
- 'jjcp_gdbh' =>['工单编号'],
- '成品编码' =>['成品编码'],
- '成品名称' =>['成品名称'],
- 'jjcp_sls' =>['入仓数量'],
- '大箱产量' =>['大箱产量'],
- '产品单价' =>['万张单价'],
- '产值万元' =>['产值万元'],
- ];
- $data[0] = [];
- foreach ($req['fields'] as $k=>$v){
- if(array_key_exists($v,$arr)){
- array_push($data[0],$arr[$v][0]);
- }
- }
- $rows1 = db('成品入仓')->alias('c')
- ->field('rtrim(c.入仓类型) as 入仓类型, LEFT(c.成品编码,'.config('product_code_digit').') as 客户编号, rtrim(b.客户名称) as 客户名称,
- rtrim(b.产品类别) as 产品类别, c.jjcp_gdbh,
- rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称
- ')
- ->where('c.jjcp_sj','between time',[$req['start_time'], $req['end_time']])
- ->order('客户编号 asc,b.产品类别 desc,c.成品编码 asc,c.jjcp_gdbh asc')
- ->join('产品_基本资料 b','b.产品编号=c.jjcp_cpdh')
- ->group('c.jjcp_gdbh')
- ->select();
- $rows2 = db('成品入仓')
- ->field('rtrim(入仓类型) as 入仓类型, rtrim(成品编码) as 客户编号,
- rtrim(成品编码) as 客户名称, rtrim(成品编码) as 产品类别,
- jjcp_gdbh,rtrim(成品编码) as 成品编码, rtrim(成品名称) as 成品名称
- ')
- ->where('jjcp_sj','between time',[$req['start_time'], $req['end_time']])
- ->where('成品编码','')
- ->order('jjcp_gdbh asc')
- ->group('jjcp_gdbh')
- ->select();
- $rows = array_merge($rows2,$rows1);
- $num = db('工单_基本资料')->count();
- $arr1 = db('工单_基本资料')->limit($num)
- ->column('CONCAT(Gd_gdbh, "-", rtrim(Gd_cpdh)) AS gdcp, 产品单价 as 万张单价');
- $brr = array_column($rows,'jjcp_gdbh');
- $brr = db('成品入仓')
- ->where('jjcp_gdbh', 'in', $brr)
- ->group('jjcp_gdbh')
- ->column('jjcp_gdbh, sum(jjcp_sl)');
- foreach ($rows as $key=>$value) {
- $subArray = [];
- foreach ($arr as $k=>$v){
- if($k=='jjcp_sls'){
- $subArray[$k] = $brr[$value['jjcp_gdbh']];
- }else if ($k=='大箱产量'){
- if($value['产品类别']=='条盒'){
- $subArray[$k] = $brr[$value['jjcp_gdbh']]/250;
- }else if($value['产品类别']=='小盒'){
- $subArray[$k] = $brr[$value['jjcp_gdbh']]/2500;
- }else{
- $subArray[$k] = 0;
- }
- }else if ($k=='产品单价'){
- if($value['产品类别']!=''){
- if (array_key_exists($value['jjcp_gdbh'].'-'.$value['成品编码'],$arr1)){
- $subArray[$k] = floatval(number_format($arr1[$value['jjcp_gdbh'].'-'.$value['成品编码']],4));
- }
- }else{
- $subArray[$k] = 0;
- }
- }else if ($k=='产值万元'){
- if($value['产品类别']!=''){
- if (array_key_exists($value['jjcp_gdbh'].'-'.$value['成品编码'],$arr1)){
- $subArray[$k] = floatval(number_format($arr1[$value['jjcp_gdbh'].'-'.$value['成品编码']]*$brr[$value['jjcp_gdbh']]/100000000,2));
- }
- }else{
- $subArray[$k] = 0;
- }
- }else{
- $subArray[$k] = $rows[$key][$k];
- }
- }
- array_push($data,$subArray);
- }
- $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
- }
- /**
- * 各日统计列表
- * @ApiMethod (GET)
- * @param string $date 时间
- * @param string $sys_id 用户
- */
- public function getDayList()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- $page = 1;
- $limit = 15;
- if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
- if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
- $where = [
- 'c.jjcp_smb'=>['<>',''],
- ];
- if (isset($req['key_word']) && !empty($req['key_word'])){
- $where['c.jjcp_sj | c.jjcp_gdbh | c.jjcp_cpmc | c.jjcp_cpdh | g.实际投料'] = ['like','%'.$req['key_word'].'%'];
- }
- //客户编号
- $rows = db('成品入仓')->alias('c')
- ->field('LEFT(c.jjcp_sj, 10) as jjcp_sj, c.jjcp_gdbh, rtrim(c.jjcp_cpmc) as jjcp_cpmc,
- rtrim(c.jjcp_cpdh) as jjcp_cpdh, g.实际投料')
- ->where($where)
- ->where('g.Gd_cpdh=c.jjcp_cpdh')
- ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
- ->order('c.jjcp_sj desc')
- ->group('c.jjcp_gdbh, c.jjcp_cpdh')
- ->page($page,$limit)
- ->select();
- $total = db('成品入仓')->alias('c')
- ->where($where)
- ->where('g.Gd_cpdh=c.jjcp_cpdh')
- ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
- ->group('c.jjcp_gdbh, c.jjcp_cpdh')
- ->count();
- $brr = db('成品入仓')
- ->group('jjcp_gdbh,jjcp_cpdh')
- ->column('CONCAT(jjcp_gdbh, "-", rtrim(jjcp_cpdh)), sum(jjcp_sl)');
- foreach ($rows as $key=>$value) {
- $rows[$key]['实际投料'] = (string)floatval($value['实际投料']*10000);
- if(array_key_exists($value['jjcp_gdbh'].'-'.$value['jjcp_cpdh'],$brr)){
- $rows[$key]['入仓数量'] = $brr[$value['jjcp_gdbh'].'-'.$value['jjcp_cpdh']];
- if($rows[$key]['实际投料']!=0){
- $rows[$key]['投入产出率'] = number_format($rows[$key]['入仓数量']/$rows[$key]['实际投料']*100,2);
- }else{
- $rows[$key]['投入产出率'] = '';
- }
- }else{
- $rows[$key]['入仓数量'] = '';
- $rows[$key]['投入产出率'] = '';
- }
- }
- $data = [
- 'total' => $total,
- 'rows' => $rows,
- ];
- $this->success('成功',$data);
- }
- /**
- * 各日统计列表excel导出
- * @ApiMethod (POST)
- * @param string file_name
- * @param array fields
- */
- public function dayExport()
- {
- //get请求
- if(!$this->request->isPost()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- if (!isset($req['file_name'])){
- $this->error('参数错误','file_name');
- }
- if (!(isset($req['fields']) && count($req['fields'])!=0)){
- $this->error('参数错误','fields',100);
- }
- if (isset($req['key_word']) && !empty($req['key_word'])){
- $where['c.jjcp_sj | c.jjcp_gdbh | c.jjcp_cpmc | c.jjcp_cpdh | g.实际投料'] = ['like','%'.$req['key_word'].'%'];
- }
- $arr = [
- 'jjcp_gdbh' =>['jjcp_gdbh'],
- 'jjcp_yjno' =>['jjcp_yjno'],
- 'jjcp_cpmc' =>['印件名称'],
- '成品编码' =>['成品编码'],
- '成品名称' =>['成品名称'],
- '实际投料' =>['实际投料'],
- 'jjcp_sls' =>['入仓数量'],
- 'jjcp_sj' =>['完工日期'],
- ];
- $data[0] = [];
- foreach ($req['fields'] as $k=>$v){
- if(array_key_exists($v,$arr)){
- array_push($data[0],$arr[$v][0]);
- }
- }
- $where = [
- 'c.jjcp_smb'=>['<>','']
- ];
- //客户编号
- $rows = db('成品入仓')->alias('c')
- ->field('c.jjcp_gdbh, c.jjcp_yjno, rtrim(c.jjcp_cpmc) as jjcp_cpmc, rtrim(c.jjcp_cpdh) as jjcp_cpdh,
- rtrim(c.成品编码) as 成品编码, rtrim(c.成品名称) as 成品名称, sum(jjcp_sl) as jjcp_sls,
- g.实际投料, LEFT(c.jjcp_sj, 10) as jjcp_sj
- ')
- ->where($where)
- ->where('g.Gd_cpdh=c.jjcp_cpdh')
- ->join('工单_基本资料 g','g.Gd_gdbh=c.jjcp_gdbh')
- ->order('c.jjcp_sj desc')
- ->group('c.jjcp_gdbh, c.jjcp_cpdh')
- ->select();
- foreach ($rows as $key=>$value) {
- $subArray = [];
- foreach ($arr as $k=>$v){
- if($k=='实际投料'){
- $subArray[$k] = (string)floatval($value['实际投料']*10000);
- }else{
- $subArray[$k] = $rows[$key][$k];
- }
- }
- array_push($data,$subArray);
- }
- $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
- }
- /**
- * 入仓末板数据统计列表
- * @ApiMethod (GET)
- * @param string $date 时间
- * @param string $sys_id 用户
- */
- public function mbList()
- {
- //get请求
- if(!$this->request->isGet()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- // $page = 1;
- // $limit = 15;
- // if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
- // if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
- $date = date('Y-m-d',strtotime('-3 month'));
- $arr1 = db('db_包装产量预报')
- ->field('sczl_gdbh1, sczl_gdbh2, sczl_gdbh3, sczl_gdbh4, sczl_gdbh5, sczl_gdbh6,
- cast(substr(sczl_yjGx1,1,2) as signed) as sczl_yjGx1, cast(substr(sczl_yjGx2,1,2) as signed) as sczl_yjGx2,
- cast(substr(sczl_yjGx3,1,2) as signed) as sczl_yjGx3, cast(substr(sczl_yjGx4,1,2) as signed) as sczl_yjGx4,
- cast(substr(sczl_yjGx5,1,2) as signed) as sczl_yjGx5, cast(substr(sczl_yjGx6,1,2) as signed) as sczl_yjGx6,
- (sczl_cl1*sczl_PgCl1+sczl_clAdd1) as sczl_cls1,
- (sczl_cl2*sczl_PgCl2+sczl_clAdd2) as sczl_cls2,
- (sczl_cl3*sczl_PgCl3+sczl_clAdd3) as sczl_cls3,
- (sczl_cl4*sczl_PgCl4+sczl_clAdd4) as sczl_cls4,
- (sczl_cl5*sczl_PgCl5+sczl_clAdd5) as sczl_cls5,
- (sczl_cl6*sczl_PgCl6+sczl_clAdd6) as sczl_cls6
- ')
- ->where('sczl_rq','>=',$date)
- ->select();
- $data1 = [];
- foreach ($arr1 as $k=>$v){
- for ($i=1;$i<=6;$i++){
- if (!empty($v['sczl_gdbh'.$i])){
- $v['sczl_gdbh'.$i] = strtoupper($v['sczl_gdbh'.$i]);
- if(array_key_exists($v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i],$data1)){
- $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] += $v['sczl_cls'.$i];
- }else{
- $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] = $v['sczl_cls'.$i];
- }
- }
- }
- }
- krsort($data1);
- //成品入仓中jjcp_smb不为空的工单
- $rows2 = db('成品入仓')
- ->where('jjcp_sj','>=',$date)
- ->group('jjcp_gdbh, jjcp_cpdh')
- ->column("CONCAT( jjcp_gdbh, '-', jjcp_yjno) AS gdyj, max(jjcp_smb) as jjcp_smb,
- sum(jjcp_sl) as jjcp_sls, left(max(jjcp_sj),10) as jjcp_sj,UniqId");
-
- $data = [];
- foreach($data1 as $key=>$value){
- $subArray = [];
- $gdbh = strtoupper(explode('-',$key)[0]);
- $yjno = explode('-',$key)[1];
- $res = db('工单_印件资料')
- ->where('Yj_Gdbh',$gdbh)
- ->where('yj_Yjno',$yjno)
- ->field('Yj_Gdbh, yj_Yjno, yj_yjmc, yj_成品数量, (yj_平张投料 * yj_ks * yj_ls) as 投料')
- ->find();
- if(!empty($res)){
- $subArray['Yj_Gdbh'] = $res['Yj_Gdbh'];
- $subArray['yj_Yjno'] = $res['yj_Yjno'];
- $subArray['yj_yjmc'] = trim($res['yj_yjmc']);
- $subArray['yj_成品数量'] = (string)floatval($res['yj_成品数量']*10000);
- $subArray['yj_实际投料'] = (string)floatval($res['投料']);
- }else{
- $subArray['Yj_Gdbh'] = '';
- $subArray['yj_Yjno'] = '';
- $subArray['yj_yjmc'] = '';
- $subArray['yj_成品数量'] = '';
- $subArray['yj_实际投料'] = '';
- }
- if(array_key_exists($key,$rows2)){
- $subArray['jjcp_smb'] = $rows2[$key]['jjcp_smb'];
- $subArray['jjcp_sls'] = $rows2[$key]['jjcp_sls'];
- $subArray['jjcp_sj'] = $rows2[$key]['jjcp_sj'];
- }else{
- $subArray['jjcp_smb'] = '';
- $subArray['jjcp_sls'] = '';
- $subArray['jjcp_sj'] = '';
- }
- $subArray['sczl_cls'] = (string)floatval($value);
- array_push($data,$subArray);
- }
- $data = [
- 'total' => count($data),
- 'rows' => $data,
- ];
- $this->success('成功',$data);
- }
- /**
- * 入仓末板数据统计列表excel导出
- * @ApiMethod (POST)
- * @param string file_name
- * @param array fields
- */
- public function mbExport()
- {
- //get请求
- if(!$this->request->isPost()){
- $this->error('请求方式错误');
- }
- $req = $this->request->param();
- if (!isset($req['file_name'])){
- $this->error('参数错误','file_name');
- }
- if (!(isset($req['fields']) && count($req['fields'])!=0)){
- $this->error('参数错误','fields',100);
- }
- $arr = [
- 'Yj_Gdbh' =>['工单编号'],
- 'yj_Yjno' =>['印件号'],
- 'yj_yjmc' =>['印件名称'],
- 'yj_成品数量' =>['计划交货数量'],
- 'yj_实际投料' =>['印件投料'],
- 'sczl_cls' =>['包装产量'],
- 'jjcp_sj' =>['最近入仓时间'],
- 'jjcp_sls' =>['入仓产量'],
- 'status' =>['完工'],
- '缺数' =>['缺数'],
- ];
- $data[0] = [];
- foreach ($req['fields'] as $k=>$v){
- if(array_key_exists($v,$arr)){
- array_push($data[0],$arr[$v][0]);
- }
- }
- $date = date('Y-m-d',strtotime('-3 month'));
- $arr1 = db('db_包装产量预报')
- ->field('sczl_gdbh1, sczl_gdbh2, sczl_gdbh3, sczl_gdbh4, sczl_gdbh5, sczl_gdbh6,
- cast(substr(sczl_yjGx1,1,2) as signed) as sczl_yjGx1, cast(substr(sczl_yjGx2,1,2) as signed) as sczl_yjGx2,
- cast(substr(sczl_yjGx3,1,2) as signed) as sczl_yjGx3, cast(substr(sczl_yjGx4,1,2) as signed) as sczl_yjGx4,
- cast(substr(sczl_yjGx5,1,2) as signed) as sczl_yjGx5, cast(substr(sczl_yjGx6,1,2) as signed) as sczl_yjGx6,
- (sczl_cl1*sczl_PgCl1+sczl_clAdd1) as sczl_cls1,
- (sczl_cl2*sczl_PgCl2+sczl_clAdd2) as sczl_cls2,
- (sczl_cl3*sczl_PgCl3+sczl_clAdd3) as sczl_cls3,
- (sczl_cl4*sczl_PgCl4+sczl_clAdd4) as sczl_cls4,
- (sczl_cl5*sczl_PgCl5+sczl_clAdd5) as sczl_cls5,
- (sczl_cl6*sczl_PgCl6+sczl_clAdd6) as sczl_cls6
- ')
- ->where('sczl_rq','>=',$date)
- ->select();
- $data1 = [];
- foreach ($arr1 as $k=>$v){
- for ($i=1;$i<=6;$i++){
- if(array_key_exists($v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i],$data1)){
- $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] += $v['sczl_cls'.$i];
- }else{
- $data1[$v['sczl_gdbh'.$i].'-'.$v['sczl_yjGx'.$i]] = $v['sczl_cls'.$i];
- }
- }
- }
- krsort($data1);
- //成品入仓中jjcp_smb不为空的工单
- $rows1 = db('成品入仓')
- ->where('jjcp_smb','<>','')
- ->order('jjcp_gdbh desc')
- ->column("CONCAT(jjcp_gdbh, '-', jjcp_yjno) AS gdyj, UniqId");
- $rows2 = db('成品入仓')
- ->where('jjcp_sj','>=',$date)
- ->group('jjcp_gdbh, jjcp_cpdh')
- ->column("CONCAT( jjcp_gdbh, '-', jjcp_yjno) AS gdyj, jjcp_smb,
- sum(jjcp_sl) as jjcp_sls, left(max(jjcp_sj),10) as jjcp_sj");
- $rows3 = db('工单_印件资料')->cache(true,86400)
- ->group('Yj_Gdbh, yj_Yjno')
- ->column("CONCAT( Yj_Gdbh, '-', yj_Yjno) AS gdyj,
- Yj_Gdbh, yj_Yjno, yj_yjmc, yj_成品数量, yj_实际投料");
- foreach($data1 as $key=>$value){
- if(!array_key_exists($key,$rows1)){
- $subArray = [];
- foreach ($arr as $k=>$v){
- if($k=='yj_成品数量' || $k=='yj_实际投料'){
- if(array_key_exists($key,$rows3)){
- $subArray[$k] = (string)floatval($rows3[$key][$k]*10000);
- }else{
- $subArray[$k] = '';
- }
- }else if($k=='Yj_Gdbh' || $k=='yj_Yjno'){
- if(array_key_exists($key,$rows3)){
- $subArray[$k] = $rows3[$key][$k];
- }else{
- $subArray[$k] = '';
- }
- }else if($k=='yj_yjmc'){
- if(array_key_exists($key,$rows3)){
- $subArray[$k] = trim($rows3[$key][$k]);
- }else{
- $subArray[$k] = '';
- }
- }else if($k=='sczl_cls'){
- $subArray[$k] = (string)floatval($value);
- }else if($k=='status'){
- if(array_key_exists($key,$rows2) && $rows2[$key]['jjcp_smb'] !=''){
- $subArray[$k] = '完工';
- }else{
- $subArray[$k] = '';
- }
- }else if($k=='缺数'){
- if(array_key_exists($key,$rows2) && $rows2[$key]['jjcp_smb'] !=''){
- $subArray[$k]['缺数'] = (int)$rows3[$key]['yj_成品数量']-(int)$value;
- }else{
- $subArray[$k] = '';
- }
- }else{
- if(array_key_exists($key,$rows2)){
- $subArray[$k] = $rows2[$key][$k];
- $subArray[$k] = $rows2[$key][$k];
- $subArray[$k] = $rows2[$key][$k];
- }else{
- $subArray[$k] = '';
- $subArray[$k] = '';
- $subArray[$k] = '';
- }
- }
- }
- array_push($data,$subArray);
- }
- }
- $this->success('成功',['file_name'=>$req['file_name'],'data'=>$data]);
- }
- /**
- * 构建新增入仓数据
- * @param array $req
- * @return array
- */
- protected function buildAddData(array $req)
- {
- $fields = [
- 'Sys_id',
- '入仓类型', '仓库编号', '仓库名称',
- 'jjcp_num',
- 'jjcp_gdbh', 'jjcp_yjno', '订单编号',
- 'jjcp_cpdh', 'jjcp_cpmc',
- '成品编码', '成品名称',
- '客户料号',
- 'jjcp_smb', 'jjcp_sj',
- 'jjcp_sl', 'jjcp_dw',
- '机型备注',
- 'jjcp_desc',
- ];
- $data = [];
- foreach ($fields as $index => $field) {
- if (!isset($req[$field])) {
- $this->error('参数错误', $field, $index + 1);
- }
- $data[$field] = $req[$field];
- }
- $data['Sys_rq'] = date('Y-m-d H:i:s');
- return $data;
- }
- /**
- * 校验印件产品名称
- * @param string $gdbh
- * @param string $yjno
- * @param string $cpmc
- */
- protected function validateProductName($gdbh, $yjno, $cpmc)
- {
- $name = db('工单_印件资料')
- ->where('Yj_Gdbh', $gdbh)
- ->where('yj_Yjno', $yjno)
- ->value('yj_yjmc');
- if ($name !== $cpmc) {
- $this->error('产品名称错误');
- }
- }
- /**
- * 查询工单印件是否已存在末版记录
- * @param string $gdbh
- * @param string $yjno
- * @return array|null
- */
- protected function findExistingEndBoard($gdbh, $yjno)
- {
- return db('成品入仓')
- ->where('jjcp_gdbh', $gdbh)
- ->where('jjcp_yjno', $yjno)
- ->where('jjcp_smb', '<>', '')
- ->find();
- }
- /**
- * 校验工单印件是否已完全领料
- * @param string $gdbh
- * @param string $yjno
- */
- protected function validateMaterialReceived($gdbh, $yjno)
- {
- $materialRecord = db('工单_印件资料')
- ->alias('a')
- ->join('物料_收发记录 b', 'a.Yj_Gdbh=b.st_gdbh and a.yj_cpdh=b.cpdh')
- ->where('a.Yj_Gdbh', $gdbh)
- ->where('a.yj_Yjno', $yjno)
- ->group('b.st_wlbh')
- ->order('b.st_wlbh asc')
- ->column('b.st_wlbh');
- $bomRecord = db('工单_印件资料')
- ->alias('a')
- ->join('工单_bom资料 b', 'a.Yj_Gdbh=b.BOM_工单编号 and a.yj_Yjno=b.BOM_工单行号 and a.yj_cpdh=b.BOM_产品编号')
- ->where('a.Yj_Gdbh', $gdbh)
- ->where('a.yj_Yjno', $yjno)
- ->where('b.BOM_实际用量', '<>', 0)
- ->group('b.BOM_物料编码')
- ->order('b.BOM_物料编码 asc')
- ->column('b.BOM_物料编码');
- foreach ($bomRecord as $materialCode) {
- if (!in_array($materialCode, $materialRecord)) {
- $this->error('工单' . $gdbh . '印件' . $yjno . '物料' . $materialCode . '未领料');
- }
- }
- }
- /**
- * 生成成品入仓 UniqId
- * @return int
- */
- protected function generateFinishStockUniqId()
- {
- $uniqId = db('成品入仓')->max('UniqId');
- return $uniqId < 10000000 ? 10000000 : $uniqId + 1;
- }
- /**
- * 保存成品入仓数据到 MySQL 和 MongoDB
- * @param array $data
- * @param mixed $mongodb
- */
- protected function saveFinishStock(array $data, $mongodb)
- {
- db()->startTrans();
- try {
- $sql = db('成品入仓')->fetchSql(true)->insert($data);
- $inserted = db()->query($sql);
- if ($inserted === false) {
- throw new \Exception('MySQL 保存失败');
- }
- $mongodb->name('finished_products')->insert($data);
- db()->commit();
- } catch (\Exception $e) {
- db()->rollback();
- $this->error($e->getMessage());
- }
- }
- /**
- * 判断是否为末版(仅判断首字符是否为「末」)
- * @param string $smb
- * @return bool
- */
- protected function isEndBoard($smb)
- {
- $smb = preg_replace('/^[\s\x{3000}]+|[\s\x{3000}]+$/u', '', (string)$smb);
- if ($smb === '') {
- return false;
- }
- return mb_substr($smb, 0, 1, 'UTF-8') === '末';
- }
- /**
- * 是否需要同步成品入仓中间表
- * 规则:
- * 1. 工单编号以 Y 开头,且 >= Y2608001
- * 2. 工单_基本资料.接单日期 >= 2026-08-01
- * 3. 中间库 U8_06工单资料 中存在该工单编号
- * @param string $gdbh
- * @return bool
- */
- protected function shouldSyncFinishStockMiddle($gdbh)
- {
- $gdbh = strtoupper(trim((string)$gdbh));
- if ($gdbh === '' || $gdbh[0] !== 'Y') {
- return false;
- }
- if ((int)substr($gdbh, 1) < 2608001) {
- return false;
- }
- $orderDate = db('工单_基本资料')->where('Gd_gdbh', $gdbh)->value('接单日期');
- if (empty($orderDate)) {
- return false;
- }
- if (strtotime(trim((string)$orderDate)) < strtotime('2026-08-01')) {
- return false;
- }
- // db3:正式中间库,仅查询 U8_06工单资料是否存在该工单
- // 实际写入走 syncFinishStockMiddle 中的 database.db8(本地 MySQL 测试库)
- $middleDb = Db::connect(config('database.db3'));
- $exists = $middleDb->name('U8_06工单资料')->where('工单编号', $gdbh)->count();
- return $exists > 0;
- }
- /**
- * 末版入仓后进入超节损
- * @param string $gdbh
- * @param string $yjno
- */
- protected function handleEndBoardSuperLoss($gdbh, $yjno)
- {
- $orderLoss = new OrderSuperLoss();
- $result = [];
- try {
- $result = $orderLoss->OneOrderSuperLoss($gdbh, $yjno);
- } catch (DataNotFoundException $e) {
- } catch (ModelNotFoundException $e) {
- } catch (DbException $e) {
- }
- if (empty($result)) {
- return;
- }
- db('工单_质量考核汇总')
- ->where('Gy0_gdbh', $gdbh)
- ->where('印件及工序', $yjno)
- ->delete();
- $sql = db('工单_质量考核汇总')->fetchSql(true)->insertAll($result);
- $inserted = db()->query($sql);
- if ($inserted === false) {
- $this->error('超节损数据保存失败');
- }
- }
- }
|