| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658 |
- <?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);
- $warnings = [];
- // 1. 末板超节损(与同步独立,失败不阻断后续)
- if ($this->isEndBoard($data['jjcp_smb'])) {
- try {
- $this->handleEndBoardSuperLoss($data['jjcp_gdbh'], $data['jjcp_yjno']);
- } catch (\Exception $e) {
- $warnings[] = '超节损计算失败:' . $e->getMessage();
- }
- }
- // 2. 中间表同步(与超节损独立;失败/未同步均写入日志 status_msg)
- $syncMsg = $this->syncFinishStockOrLogSkip($data, $data['UniqId'], '新增');
- if ($syncMsg !== null) {
- $warnings[] = $syncMsg;
- }
- $result = ['UniqId' => $data['UniqId']];
- if (!empty($warnings)) {
- $result['warnings'] = $warnings;
- }
- $this->success(empty($warnings) ? '成功' : '成品入仓已保存,但部分后续处理失败', $result);
- }
- /**
- * 查询成品入仓同步操作日志
- * @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('失败');
- }
- $warnings = [];
- // 1. 改为末板时先做超节损(与同步独立)
- if ($smbChanged && $this->isEndBoard($newSmb)) {
- try {
- $this->handleEndBoardSuperLoss($old['jjcp_gdbh'], $old['jjcp_yjno']);
- } catch (\Exception $e) {
- $warnings[] = '超节损计算失败:' . $e->getMessage();
- }
- }
- // 2. 同步中间表(与超节损独立;失败/未同步均写入日志)
- // - 数量变更:插入差值为新同步数据(增加为正、减少为负)
- // - 仅修改 smb / 时间:正常同步完整数量
- // - 数量与其他同时改:只同步差值
- if ($qtyChanged || $smbChanged || $sjChanged) {
- $recordForSync = array_merge($old, $data);
- $iQtyOverride = $qtyChanged ? ($newQty - $oldQty) : null;
- $syncMsg = $this->syncFinishStockOrLogSkip(
- $recordForSync,
- $uniqId,
- '修改',
- $iQtyOverride,
- $sysId
- );
- if ($syncMsg !== null) {
- $warnings[] = $syncMsg;
- }
- }
- if (!empty($warnings)) {
- $this->success('成品入仓已保存,但部分后续处理失败', [
- 'UniqId' => $uniqId,
- 'warnings' => $warnings,
- ]);
- }
- $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('未找到成品入仓记录');
- }
- // 满足同步条件才回冲;不满足或失败都写日志。同步失败不再阻断删除
- $syncMsg = $this->syncFinishStockOrLogSkip(
- $old,
- $uniqId,
- '新增',
- -1 * (float)$old['jjcp_sl'],
- $sysId
- );
- $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('失败');
- }
- if ($syncMsg !== null) {
- $this->success('删除成功,但同步处理异常', [
- 'UniqId' => $uniqId,
- 'warnings' => [$syncMsg],
- ]);
- }
- $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') === '末';
- }
- /**
- * 是否需要同步成品入仓中间表
- * @param string $gdbh
- * @return bool
- */
- protected function shouldSyncFinishStockMiddle($gdbh)
- {
- return $this->explainFinishStockSyncGate($gdbh)['ok'];
- }
- /**
- * 同步门槛判定及跳过原因(便于写入同步日志 status_msg)
- * @param string $gdbh
- * @return array{ok:bool,reason:string}
- */
- protected function explainFinishStockSyncGate($gdbh)
- {
- $gdbh = strtoupper(trim((string)$gdbh));
- if ($gdbh === '' || !isset($gdbh[0]) || $gdbh[0] !== 'Y') {
- return ['ok' => false, 'reason' => '不满足同步条件:工单编号不以Y开头或为空[' . $gdbh . ']'];
- }
- if ((int)substr($gdbh, 1) < 2608001) {
- return ['ok' => false, 'reason' => '不满足同步条件:工单编号小于Y2608001[' . $gdbh . ']'];
- }
- try {
- $orderDate = db('工单_基本资料')->where('Gd_gdbh', $gdbh)->value('接单日期');
- } catch (\Exception $e) {
- return ['ok' => false, 'reason' => '查询工单_基本资料接单日期失败:' . $e->getMessage()];
- }
- if (empty($orderDate)) {
- return ['ok' => false, 'reason' => '不满足同步条件:工单_基本资料无接单日期[' . $gdbh . ']'];
- }
- if (strtotime(trim((string)$orderDate)) < strtotime('2026-08-01')) {
- return ['ok' => false, 'reason' => '不满足同步条件:接单日期[' . trim((string)$orderDate) . ']早于2026-08-01'];
- }
- try {
- // db3:正式中间库,仅查询 U8_06工单资料是否存在该工单
- $middleDb = Db::connect(config('database.db3'));
- $exists = $middleDb->name('U8_06工单资料')->where('工单编号', $gdbh)->count();
- } catch (\Exception $e) {
- return ['ok' => false, 'reason' => '查询正式库U8_06工单资料失败:' . $e->getMessage()];
- }
- if ($exists <= 0) {
- return ['ok' => false, 'reason' => '不满足同步条件:正式库U8_06工单资料不存在工单[' . $gdbh . ']'];
- }
- return ['ok' => true, 'reason' => ''];
- }
- /**
- * 按判定结果执行同步,或写入「未同步」原因到日志表
- * @param array $record 成品入仓记录(至少含业务字段)
- * @param int|string $uniqId
- * @param string $insertType
- * @param float|int|null $iQtyOverride
- * @param string|null $usernameOverride
- * @return string|null 失败或未同步时的提示文案;成功返回 null
- */
- protected function syncFinishStockOrLogSkip(array $record, $uniqId, $insertType, $iQtyOverride = null, $usernameOverride = null)
- {
- $gdbh = isset($record['jjcp_gdbh']) ? $record['jjcp_gdbh'] : '';
- $gate = $this->explainFinishStockSyncGate($gdbh);
- $synchronization = new \app\api\controller\Synchronization();
- if (!$gate['ok']) {
- $synchronization->logFinishStockSync($record, $uniqId, $insertType, '未同步', $gate['reason']);
- // 条件不满足仅记日志,不作为接口告警
- return null;
- }
- try {
- $synchronization->syncFinishStockMiddle($uniqId, $insertType, $iQtyOverride, $usernameOverride);
- return null;
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- }
- /**
- * 末版入仓后进入超节损
- * @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) {
- throw new \Exception('超节损数据保存失败');
- }
- }
- }
|