瀏覽代碼

入库接口处理

koul 3 天之前
父節點
當前提交
4a62000e65
共有 1 個文件被更改,包括 170 次插入 和 83 次删除
  1. 170 83
      src/main/java/com/uas/eis/service/Impl/MESServiceImpl.java

+ 170 - 83
src/main/java/com/uas/eis/service/Impl/MESServiceImpl.java

@@ -70,103 +70,188 @@ public class MESServiceImpl implements MESService {
         String requestId = request.getHeader("RequestId");
         Map<String, JSONArray> data = getData(request);
         JSONArray jsonArray = data.get("main");
+        JSONArray array = data.get("detail");
         Object vaid=0;
         String caller="VerifyApply!ToOtherIn";
         String type="其它入库单";
         String djcaller="ProdInOut!OtherIn";
         String sellerName="";
         String whcode="";
+        String inType="";
+        String mesCode="";
+        String vendNo="";
+        String vendName="";
+        int piid = 0;
+        String piCode="";
+        List<String> sqls = new ArrayList<>();
         for (int i = 0; i <jsonArray.size() ; i++) {
             JSONObject jsonObject = JSON.parseObject(StringUtil.nvl(jsonArray.get(i), ""));
-            String mesCode = StringUtil.nvl(jsonObject.get("mesCode"),"");
+            mesCode = StringUtil.nvl(jsonObject.get("mesCode"),"");
             whcode= StringUtil.nvl(jsonObject.get("whNo"), "");
-            if ("".equals(mesCode)){
-                return ApiResponse.failRsp("10011",requestId,"ERP单据编号不能为空!");
-            }
-            int count = baseDao.getCountByCondition("verifyApply", "va_statuscode='AUDITED' and va_mescode='" + mesCode + "'");
-            if (count<1){
-                return ApiResponse.failRsp("10012",requestId,"单据未审核或不存在,请确认!");
-            }
-            vaid = baseDao.getFieldDataByCondition("verifyApply", "va_id", "va_statuscode='AUDITED' and va_mescode='" + mesCode + "'");
-            String vaClass = StringUtil.nvl(baseDao.getFieldDataByCondition("VerifyApply", "va_class", "va_id=" + vaid), "");
-            if ("委外收料单".equals(vaClass)) {
-                caller = "VerifyApply!ToOutsideCheckIn";
-                type = "委外验收单";
-                djcaller = "ProdInOut!OutsideCheckIn";
-            }else if("采购收料单".equals(vaClass)){
-                int count1 = baseDao.getCount("select count(1) from VerifyApplyDetail where nvl(vad_pucode,' ')<>' ' and vad_vaid=" + vaid);
-                if (count1 > 0) {
-                    caller = "VerifyApply!ToPurcIn";
-                    type = "采购验收单";
-                    djcaller = "ProdInOut!PurcCheckin";
+            inType = StringUtil.nvl(jsonObject.get("inType"), "");
+            vendNo = StringUtil.nvl(jsonObject.get("vendNo"), "");
+            vendName = StringUtil.nvl(jsonObject.get("vendName"), "");
+            piCode = StringUtil.nvl(jsonObject.get("piCode"), "");
+            if ("自购".equals(inType)) {
+                if ("".equals(mesCode)) {
+                    return ApiResponse.failRsp("10011", requestId, "ERP单据编号不能为空!");
                 }
-            }
-            //判断已转数
-            Object chekQty = baseDao.getJdbcTemplate().queryForObject("select wmsys.wm_concat('收料单:'||vad_code||'序号:'||vad_detno)  from VerifyApplyDetail where vad_qty < vad_yqty and nvl(vad_qty,0)>0 and nvl(vad_prodcode,' ')<>' ' and vad_vaid = "+vaid, String.class);
-            if(chekQty !=null){
-                return ApiResponse.failRsp("10013",requestId,"检测到" + chekQty + ",本次数量超出可转数量!");
-            }
-            if ("VerifyApply!ToPurcIn".equals(caller)) {
-                // 判断该收料单是否已经转入过采购验收单
-                Object code = baseDao.getFieldDataByCondition("VerifyApply", "va_code", "va_id=" + vaid);
-                code = baseDao.getFieldDataByCondition("ProdInOut", "pi_inoutno", "pi_sourcecode='" + code + "' and " + "PI_REFNO='采购收料单' and pi_class='采购验收单'");
-                if (code != null && !code.equals("")) {
-                    return ApiResponse.failRsp("10014", requestId, "该收料单已转入过采购验收单,验收单号[" + code + "]");
+                int count = baseDao.getCountByCondition("verifyApply", "va_statuscode='AUDITED' and va_mescode='" + mesCode + "'");
+                if (count < 1) {
+                    return ApiResponse.failRsp("10012", requestId, "单据未审核或不存在,请确认!");
                 }
-            }
-            if ("VerifyApply!ToOtherIn".equals(caller)) {
-                // 判断该收料单是否已经转入过其它入库单
-                Object code = baseDao.getFieldDataByCondition("VerifyApply", "va_code", "va_id=" + vaid);
-                code = baseDao.getFieldDataByCondition("ProdInOut", "pi_inoutno", "pi_sourcecode='" + code + "' and PI_REFNO='采购收料单' and pi_class='其它入库单'");
-                if (code != null && !code.equals("")) {
-                    return ApiResponse.failRsp("10014", requestId, "该收料单已转入过其它入库单,单号[" + code + "]");
+                vaid = baseDao.getFieldDataByCondition("verifyApply", "va_id", "va_statuscode='AUDITED' and va_mescode='" + mesCode + "'");
+                String vaClass = StringUtil.nvl(baseDao.getFieldDataByCondition("VerifyApply", "va_class", "va_id=" + vaid), "");
+                if ("委外收料单".equals(vaClass)) {
+                    caller = "VerifyApply!ToOutsideCheckIn";
+                    type = "委外验收单";
+                    djcaller = "ProdInOut!OutsideCheckIn";
+                } else if ("采购收料单".equals(vaClass)) {
+                    int count1 = baseDao.getCount("select count(1) from VerifyApplyDetail where nvl(vad_pucode,' ')<>' ' and vad_vaid=" + vaid);
+                    if (count1 > 0) {
+                        caller = "VerifyApply!ToPurcIn";
+                        type = "采购验收单";
+                        djcaller = "ProdInOut!PurcCheckin";
+                    }
                 }
-            }
-            if ("VerifyApply!ToOutsideCheckIn".equals(caller)) {
-                // 判断该收料单是否已经转入过委外验收单
-                Object code = baseDao.getFieldDataByCondition("VerifyApply", "va_code", "va_id=" + vaid);
-                code = baseDao.getFieldDataByCondition("ProdInOut", "pi_inoutno", "pi_sourcecode='" + code + "' and PI_REFNO='采购收料单' and pi_class='委外验收单'");
-                if (code != null && !code.equals("")) {
-                    return ApiResponse.failRsp("10014", requestId, "该收料单已转入过委外验收单,单号[" + code + "]");
+                //判断已转数
+                Object chekQty = baseDao.getJdbcTemplate().queryForObject("select wmsys.wm_concat('收料单:'||vad_code||'序号:'||vad_detno)  from VerifyApplyDetail where vad_qty < vad_yqty and nvl(vad_qty,0)>0 and nvl(vad_prodcode,' ')<>' ' and vad_vaid = " + vaid, String.class);
+                if (chekQty != null) {
+                    return ApiResponse.failRsp("10013", requestId, "检测到" + chekQty + ",本次数量超出可转数量!");
                 }
+                if ("VerifyApply!ToPurcIn".equals(caller)) {
+                    // 判断该收料单是否已经转入过采购验收单
+                    Object code = baseDao.getFieldDataByCondition("VerifyApply", "va_code", "va_id=" + vaid);
+                    code = baseDao.getFieldDataByCondition("ProdInOut", "pi_inoutno", "pi_sourcecode='" + code + "' and " + "PI_REFNO='采购收料单' and pi_class='采购验收单'");
+                    if (code != null && !code.equals("")) {
+                        return ApiResponse.failRsp("10014", requestId, "该收料单已转入过采购验收单,验收单号[" + code + "]");
+                    }
+                }
+                /*if ("VerifyApply!ToOtherIn".equals(caller)) {
+                    // 判断该收料单是否已经转入过其它入库单
+                    Object code = baseDao.getFieldDataByCondition("VerifyApply", "va_code", "va_id=" + vaid);
+                    code = baseDao.getFieldDataByCondition("ProdInOut", "pi_inoutno", "pi_sourcecode='" + code + "' and PI_REFNO='采购收料单' and pi_class='其它入库单'");
+                    if (code != null && !code.equals("")) {
+                        return ApiResponse.failRsp("10014", requestId, "该收料单已转入过其它入库单,单号[" + code + "]");
+                    }
+                }*/
+                if ("VerifyApply!ToOutsideCheckIn".equals(caller)) {
+                    // 判断该收料单是否已经转入过委外验收单
+                    Object code = baseDao.getFieldDataByCondition("VerifyApply", "va_code", "va_id=" + vaid);
+                    code = baseDao.getFieldDataByCondition("ProdInOut", "pi_inoutno", "pi_sourcecode='" + code + "' and PI_REFNO='采购收料单' and pi_class='委外验收单'");
+                    if (code != null && !code.equals("")) {
+                        return ApiResponse.failRsp("10014", requestId, "该收料单已转入过委外验收单,单号[" + code + "]");
+                    }
+                }
+                sellerName = StringUtil.nvl(jsonObject.get("sellerName"), "mes");
             }
-            sellerName = StringUtil.nvl(jsonObject.get("sellerName"), "mes");
         }
-        JSONArray array = data.get("detail");
-        for (int i = 0; i <array.size() ; i++) {
-            JSONObject json = JSON.parseObject(StringUtil.nvl(array.get(i), ""));
-            String itemNo = StringUtil.nvl(json.get("itemNo"), "");
-            Double qty = Double.parseDouble(StringUtil.nvl(json.get("qty"), "0"));
-            Double vadQty = baseDao.getSummaryByField("verifyapplydetail", "vad_qty", "vad_prodcode='" + itemNo + "' and vad_vaid=" + vaid);
-            int compare = qty.compareTo(vadQty);
-            if (compare>0){
-                return ApiResponse.failRsp("10017",requestId, "物料:"+itemNo+",收料数量:"+qty+"超出ERP收料单数量:"+vadQty+ ",不允许入库!");
-            }
-            if (compare<0){
-                return ApiResponse.failRsp("10018",requestId,"物料:"+itemNo+",收料数量:"+qty+"少于ERP收料单数量:"+vadQty+",不允许入库!");
-            }
+        // 主表未传仓库时,从明细行兜底取(MES 部分接口把仓库放在明细上)
+        if ("".equals(whcode) && array != null && array.size() > 0) {
+            JSONObject firstDetail = JSON.parseObject(StringUtil.nvl(array.get(0), "{}"));
+            whcode = StringUtil.nvl(firstDetail.get("whNo"), StringUtil.nvl(firstDetail.get("whCode"), ""));
         }
-        if ("".equals(whcode)){
-            return ApiResponse.failRsp("10015",requestId,"MES仓库编号不能为空!");
+        if ("".equals(whcode)) {
+            return ApiResponse.failRsp("10015", requestId, "MES仓库编号不能为空!");
         }
         int coun = baseDao.getCount("select count(1) from customtabledetail left join customtable on cd_ctid=ct_id where ct_statuscode='AUDITED' and ct_caller='MESWarehouse' and cd_varchar50_1='" + whcode + "'");
-        if (coun<=0){
-            return ApiResponse.failRsp("10016",requestId,"MES仓库编号在ERP没有对应关系,请联系管理员新增!");
+        if (coun <= 0) {
+            return ApiResponse.failRsp("10016", requestId, "MES仓库编号在ERP没有对应关系,请联系管理员新增!");
         }
         Object[] warehouses = baseDao.getFieldsDataByCondition("customtabledetail left join customtable on cd_ctid=ct_id left join Warehouse on cd_varchar50_3=wh_code", "cd_varchar50_3,wh_description", "ct_statuscode='AUDITED' and ct_caller='MESWarehouse' and cd_varchar50_1='" + whcode + "'");
-
-        int piid =0;
-        Employee employee = baseDao.getJdbcTemplate().queryForObject("select * from employee where em_name=?",
-                new BeanPropertyRowMapper<Employee>(Employee.class), sellerName);
-        // 转采购验收单
-        Key key = transferRepository.transfer(caller, vaid,employee);
-        piid = key.getId();
-        // 转入明细
-        transferRepository.transferDetail(caller, vaid, key,employee);
+        // 非自购单据不传 sellerName,默认取 mes 账号,避免查不到员工抛异常
+        if ("".equals(sellerName)) {
+            sellerName = "mes";
+        }
+        Employee employee;
+        try {
+            employee = baseDao.getJdbcTemplate().queryForObject("select * from employee where em_name=?",
+                    new BeanPropertyRowMapper<Employee>(Employee.class), sellerName);
+        } catch (Exception e) {
+            return ApiResponse.failRsp("10020", requestId, "ERP中不存在员工账号[" + sellerName + "],请联系管理员!");
+        }
+        if ("自购".equals(inType)) {
+            for (int i = 0; i < array.size(); i++) {
+                JSONObject json = JSON.parseObject(StringUtil.nvl(array.get(i), ""));
+                String itemNo = StringUtil.nvl(json.get("itemNo"), "");
+                Double qty = Double.parseDouble(StringUtil.nvl(json.get("qty"), "0"));
+                Double vadQty = baseDao.getSummaryByField("verifyapplydetail", "vad_qty", "vad_prodcode='" + itemNo + "' and vad_vaid=" + vaid);
+                int compare = qty.compareTo(vadQty);
+                if (compare > 0) {
+                    return ApiResponse.failRsp("10017", requestId, "物料:" + itemNo + ",收料数量:" + qty + "超出ERP收料单数量:" + vadQty + ",不允许入库!");
+                }
+                if (compare < 0) {
+                    return ApiResponse.failRsp("10018", requestId, "物料:" + itemNo + ",收料数量:" + qty + "少于ERP收料单数量:" + vadQty + ",不允许入库!");
+                }
+            }
+            // 转采购验收单
+            Key key = transferRepository.transfer(caller, vaid, employee);
+            piid = key.getId();
+            piCode=key.getCode();
+            // 转入明细
+            transferRepository.transferDetail(caller, vaid, key, employee);
+        }else{
+            if ("".equals(piCode)){
+                piCode=baseDao.sGetMaxNumber(djcaller,2);
+            }else {
+                if (baseDao.checkIf("prodinout" ,"pi_inoutno='" + piCode + "'")){
+                    return ApiResponse.failRsp("10019", requestId, "入库单号[" + piCode + "]已存在,请确认!");
+                }
+            }
+            piid= baseDao.getSeqId("ProdInOut_seq");
+            // 供应商名称以 ERP 档案为准,避免 MES 与 ERP 名称不一致
+            if (!"".equals(vendNo)) {
+                Object veName = baseDao.getFieldDataByCondition("vendor", "ve_name", "ve_code='" + vendNo + "'");
+                if (veName != null && !"".equals(veName.toString())) {
+                    vendName = veName.toString();
+                }
+            }
+            sqls.add("insert into prodinout(pi_id,pi_inoutno,pi_class,pi_date,pi_type,pi_cardcode,pi_title,"
+                    + "pi_status,pi_statuscode,pi_invostatus,pi_invostatuscode,pi_whcode,pi_whname,"
+                    + "pi_recordman,pi_recorddate,pi_updateman,pi_updatedate,pi_total,pi_mescode) "
+                    + "values(" + piid + ",'" + piCode + "','" + type + "',sysdate,'客供料入库','" + vendNo + "','" + vendName + "',"
+                    + "'未过账','UNPOST','已审核','AUDITED','" + warehouses[0] + "','" + warehouses[1] + "',"
+                    + "'" + employee.getEm_name() + "',sysdate,'" + employee.getEm_name() + "',sysdate,0,'" + piCode + "')");
+            // 补明细:原逻辑只 add 了主表 SQL,明细从未写入,导致后续 delete/update 落空、也无法过账
+            if (array == null || array.size() < 1) {
+                return ApiResponse.failRsp("10021", requestId, "入库明细不能为空!");
+            }
+            int detno = 1;
+            for (int i = 0; i < array.size(); i++) {
+                JSONObject json = JSON.parseObject(StringUtil.nvl(array.get(i), "{}"));
+                String itemNo = StringUtil.nvl(json.get("itemNo"), "");
+                if ("".equals(itemNo)) {
+                    return ApiResponse.failRsp("10022", requestId, "第" + detno + "行物料编号不能为空!");
+                }
+                String qtyStr = StringUtil.nvl(json.get("qty"), "");
+                double qty;
+                try {
+                    qty = Double.parseDouble(qtyStr);
+                } catch (Exception e) {
+                    return ApiResponse.failRsp("10023", requestId, "第" + detno + "行物料[" + itemNo + "]数量[" + qtyStr + "]格式不正确!");
+                }
+                if (qty <= 0) {
+                    return ApiResponse.failRsp("10023", requestId, "第" + detno + "行物料[" + itemNo + "]入库数量必须大于0!");
+                }
+                if (baseDao.getCount("select count(1) from product where pr_code='" + itemNo + "'") < 1) {
+                    return ApiResponse.failRsp("10024", requestId, "第" + detno + "行物料[" + itemNo + "]在ERP不存在,请核对!");
+                }
+                String remark = StringUtil.nvl(json.get("description"), "");
+                sqls.add("insert into prodiodetail(pd_id,pd_piid,pd_inoutno,pd_piclass,pd_pdno,pd_prodcode,pd_inqty,pd_outqty,pd_whcode,pd_whname,pd_remark) "
+                        + "values(PRODIODETAIL_SEQ.nextval," + piid + ",'" + piCode + "','" + type + "'," + detno + ",'" + itemNo + "'," + qty + ",0,'" + warehouses[0] + "','" + warehouses[1] + "','" + remark + "')");
+                detno++;
+            }
+            try {
+                baseDao.execute(sqls);
+            } catch (Exception e) {
+                logger.info("生成其它入库单失败:" + e.getMessage());
+                return ApiResponse.failRsp("10025", requestId, "生成其它入库单失败:" + e.getMessage());
+            }
+        }
         if (piid != 0) {
-            baseDao.execute("delete from prodiodetail where nvl(pd_inqty,0)<=0 and pd_piid="+piid);
-            baseDao.updateByCondition("prodiodetail", "pd_whcode='"+warehouses[0]+"',pd_whname='"+warehouses[1]+"'", "pd_piid=" + piid);
-            baseDao.updateByCondition("prodinout", "pi_whcode='"+warehouses[0]+"',pi_whname='"+warehouses[1]+"'", "pi_id=" + piid);
+            baseDao.execute("delete from prodiodetail where nvl(pd_inqty,0)<=0 and pd_piid=" + piid);
+            baseDao.updateByCondition("prodiodetail", "pd_whcode='" + warehouses[0] + "',pd_whname='" + warehouses[1] + "'", "pd_piid=" + piid);
+            baseDao.updateByCondition("prodinout", "pi_whcode='" + warehouses[0] + "',pi_whname='" + warehouses[1] + "'", "pi_id=" + piid);
             baseDao.execute("update prodiodetail set pd_prodid=(select pr_id from product where pd_prodcode=pr_code) where pd_piid=" + piid
                     + " and nvl(pd_prodcode,' ')<>' '");
             baseDao.execute("update prodiodetail set pd_whid=(select wh_id from warehouse where wh_code=pd_whcode) where pd_piid=" + piid
@@ -208,15 +293,17 @@ public class MESServiceImpl implements MESService {
                             "pu_turnstatuscode='" + statuscode + "',pu_turnstatus='" + status + "'", "pu_code='" + pCode + "'");
                 }
             }
-            // 修改收料状态
-            baseDao.updateByCondition("VerifyApplyDetail", "vad_yqty=vad_qty", "vad_vaid=" + vaid);
-            baseDao.updateByCondition("VerifyApply", "va_turnstatuscode='TURNIN',va_turnstatus='已入库'", "va_id=" + vaid);
+            if ("自购".equals(inType)) {
+                // 修改收料状态
+                baseDao.updateByCondition("VerifyApplyDetail", "vad_yqty=vad_qty", "vad_vaid=" + vaid);
+                baseDao.updateByCondition("VerifyApply", "va_turnstatuscode='TURNIN',va_turnstatus='已入库'", "va_id=" + vaid);
+            }
             String s = postProdInOut(piid, djcaller, employee);
-            logger.info("收料单入库=="+s);
-            if (s!=null){
-                return ApiResponse.successRsp("0",requestId,type+":"+key.getCode()+",过账失败,请在ERP重新过账!");
+            logger.info("收料单入库==" + s);
+            if (s != null) {
+                return ApiResponse.successRsp("0", requestId, type + ":" +piCode + ",过账失败,请在ERP重新过账!");
             }
-            return ApiResponse.successRsp("0",requestId,type+":"+key.getCode()+",请在ERP查看!");
+            return ApiResponse.successRsp("0", requestId, type + ":" + piCode + ",请在ERP查看!");
         }
         return ApiResponse.failRsp("10019",requestId,"ERP生成单据失败!");
     }