Procházet zdrojové kódy

增加委外工单

koul před 4 roky
rodič
revize
4b68c613f0
1 změnil soubory, kde provedl 135 přidání a 112 odebrání
  1. 135 112
      src/main/java/com/uas/eis/service/Impl/MESServiceImpl.java

+ 135 - 112
src/main/java/com/uas/eis/service/Impl/MESServiceImpl.java

@@ -222,9 +222,26 @@ public class MESServiceImpl implements MESService {
     public ApiResult<String> prodInOutMakeIn(HttpServletRequest request) {
         Map<String, JSONArray> data = getData(request);
         JSONArray jsonArray = data.get("main");
-        int id=0;
-        String code="";
+        String ma_tasktype="";
+        int piid=0;
+        String picode="";
+        String piclass="完工入库单";
+        String picaller="ProdInOut!Make!In";
+        int ma_id=0;
         List<String> sqls = new ArrayList<>();
+        Employee employee = baseDao.getJdbcTemplate().queryForObject("select * from employee where em_code=?",
+                new BeanPropertyRowMapper<Employee>(Employee.class), "mes");
+        JSONArray array = data.get("detail");
+        if(array!=null&&array.size()>0){
+            String macode = StringUtil.valueOf(JSON.parseObject(StringUtil.nvl(array.get(0), "{}")).get("FICMOBillNo"));
+            Object[] data1 = baseDao.getFieldsDataByCondition("Make", "ma_tasktype,ma_id", "ma_code in ('" + macode + "')");
+            if (data1!=null&&data1.length>0) {
+                ma_tasktype = StringUtil.nvl(data1[0], "");
+                ma_id = Integer.parseInt(StringUtil.nvl(data1[1], "0"));
+            }else {
+                return ApiResponse.failRsp("10077", request.getHeader("RequestId"), "工单在ERP不存在,请核对!");
+            }
+        }
         for (int i = 0; i <jsonArray.size() ; i++) {
             Object obj = jsonArray.get(i);
             JSONObject jsonObject2 = JSON.parseObject(StringUtil.nvl(obj, ""));
@@ -234,22 +251,41 @@ public class MESServiceImpl implements MESService {
             Map<Object, Object> map7 = JSONUtil.toMap(StringUtil.valueOf(jsonObject2.get("FSManagerID")));
             Map<Object, Object> map8 = JSONUtil.toMap(StringUtil.valueOf(jsonObject2.get("FBillerID")));
             String whcode = StringUtil.nvl(map4.get("FNumber"), "");
-            String whname = StringUtil.nvl(map4.get("FName"), "");
-            if ("".equals(whcode)){
-                return ApiResponse.failRsp("10073",request.getHeader("RequestId"),"MES仓库编号不能为空!");
+            if ("".equals(whcode)) {
+                return ApiResponse.failRsp("10073", request.getHeader("RequestId"), "MES仓库编号不能为空!");
             }
             int count = baseDao.getCount("select count(1) from customtabledetail left join customtable on cd_ctid=ct_id left join Warehouse on cd_varchar50_3=wh_code where ct_statuscode='AUDITED' and ct_caller='MESWarehouse' and cd_varchar50_1='" + whcode + "'");
-            if (count<=0){
-                return ApiResponse.failRsp("10074",request.getHeader("RequestId"),"MES仓库编号在ERP没有对应关系,请联系管理员新增!");
+            if (count <= 0) {
+                return ApiResponse.failRsp("10074", request.getHeader("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 + "'");
-            id = baseDao.getSeqId("PRODINOUT_SEQ");
-            code = baseDao.sGetMaxNumber("ProdInOut!Make!In", 2);
-            sqls.add("insert into prodinout (pi_id,pi_inoutno,pi_class,pi_date,pi_statuscode,pi_status,pi_whcode,pi_whname,pi_departmentcode,pi_departmentname,pi_emcode,pi_emname,pi_invostatuscode,pi_invostatus,pi_recordman,pi_recorddate,pi_printstatuscode,pi_printstatus) values ("+id+",'"+code+"','完工入库单',to_date('"+jsonObject2.get("Fdate")+"','yyyy/MM/dd HH24:mi:ss'),'UNPOST'," + "'未过账','"+warehouses[0]+"','"+warehouses[1]+"','"+map5.get("FNumber")+"','"+map5.get("FName")+ "','"+map6.get("FNumber")+"','"+map7.get("FName")+"','ENTERING','在录入','"+map8.get("FName")+"',to_date('"+jsonObject2.get("Fdate")+"','yyyy-MM-dd HH24:mi:ss'),'UNPRINT','未打印')");
+            if ("OS".equals(ma_tasktype)) {
+                picaller="ProdInOut!OutsideCheckIn";
+                piclass="委外验收单";
+                // 转入委外验收单主记录
+                piid = baseDao.getSeqId("PRODINOUT_SEQ");
+                picode = baseDao.sGetMaxNumber(picaller, 2);
+                sqls.add("insert into prodinout (pi_id,pi_inoutno,pi_class,pi_date,pi_statuscode,pi_status,pi_whcode," +
+                        "pi_whname,pi_departmentcode,pi_departmentname,pi_emcode,pi_emname,pi_invostatuscode," +
+                        "pi_invostatus,pi_recordman,pi_recorddate,pi_printstatuscode,pi_printstatus) values (" + piid + ",'" + picode + "','"+piclass+"',to_date('" + jsonObject2.get("Fdate") + "','yyyy-MM-dd HH24:mi:ss'),'UNPOST'," + "'未过账','" + warehouses[0] + "','" + warehouses[1] + "','" + map5.get("FNumber") + "','" + map5.get("FName") + "','" + map6.get("FNumber") + "','" + map7.get("FName") + "','ENTERING','在录入','" + map8.get("FName") + "',to_date('" + jsonObject2.get("Fdate") + "','yyyy-MM-dd HH24:mi:ss'),'UNPRINT','未打印')");
+                sqls.add("update prodinout set (pi_cardcode,pi_title,pi_currency,pi_rate,pi_receivecode," +
+                        "pi_receivename,pi_cardid,pi_paymentcode,pi_payment,pi_cop,pi_belongs," +
+                        "pi_sourcecode,pi_sellercode)=(select ma_vendcode,ma_vendname,ma_currency,ma_rate,ve_apvendcode," +
+                        "ve_apvendname,ve_id,ma_paymentscode,ma_payments,ma_cop,ve_buyername,ma_code,em_code from make left " +
+                        "join Vendor on ma_vendcode=ve_code left join employee on ve_buyerid=em_id where ma_id="+ma_id+")");
+                sqls.add("update ProdinOut set pi_sellername=pi_belongs where pi_id=" + piid);
+                sqls.add("update ProdinOut set pi_rate=(select CR_RATE from Currencys where pi_currency=cr_name) where pi_id="
+                            + piid + " and nvl(pi_rate,0)=0");
+            } else {
+                piid = baseDao.getSeqId("PRODINOUT_SEQ");
+                picode = baseDao.sGetMaxNumber(picaller, 2);
+                sqls.add("insert into prodinout (pi_id,pi_inoutno,pi_class,pi_date,pi_statuscode,pi_status,pi_whcode," +
+                        "pi_whname,pi_departmentcode,pi_departmentname,pi_emcode,pi_emname,pi_invostatuscode," +
+                        "pi_invostatus,pi_recordman,pi_recorddate,pi_printstatuscode,pi_printstatus) values (" + piid + ",'" + picode + "','"+piclass+"',to_date('" + jsonObject2.get("Fdate") + "','yyyy-MM-dd HH24:mi:ss'),'UNPOST'," + "'未过账','" + warehouses[0] + "','" + warehouses[1] + "','" + map5.get("FNumber") + "','" + map5.get("FName") + "','" + map6.get("FNumber") + "','" + map7.get("FName") + "','ENTERING','在录入','" + map8.get("FName") + "',to_date('" + jsonObject2.get("Fdate") + "','yyyy-MM-dd HH24:mi:ss'),'UNPRINT','未打印')");
+            }
         }
-        JSONArray array = data.get("detail");
         int detno=1;
-        for (int i = 0; i <array.size() ; i++) {
+        for (int i = 0; i <array.size(); i++) {
             Object obj = array.get(i);
             JSONObject jsonObject3 = JSON.parseObject(StringUtil.nvl(obj, ""));
             Map<Object, Object> map11 = JSONUtil.toMap(StringUtil.valueOf(jsonObject3.get("FItemID")));
@@ -281,41 +317,58 @@ public class MESServiceImpl implements MESService {
                 location = baseDao.getFieldDataByCondition("ProductLocation", "pl_name", "pl_id=" + fDCSPID);
             }
             String fSourceBillNo = StringUtil.nvl(jsonObject3.get("FSourceBillNo"), "");
-            int ma_id = Integer.parseInt(StringUtil.nvl(baseDao.getFieldDataByCondition("make", "ma_id",
-                    "ma_code='" + fSourceBillNo + "'"), "0"));
             String fICMOBillNo = StringUtil.nvl(jsonObject3.get("FICMOBillNo"), "");
             String fEntrySelfA0248 = StringUtil.nvl(jsonObject3.get("FEntrySelfA0248"), "");
             String fEntrySelfA0245 = StringUtil.nvl(jsonObject3.get("FEntrySelfA0245"), "");
             String fEntrySelfA0246 = StringUtil.nvl(jsonObject3.get("FEntrySelfA0246"), "");
-            String fEntrySelfA0247 = StringUtil.nvl(jsonObject3.get("FEntrySelfA0247"), "");
-            String fEntrySelfA0242 = StringUtil.nvl(jsonObject3.get("FEntrySelfA0242"), "");
-            sqls.add("insert into prodiodetail (pd_id,pd_piid,pd_inoutno,pd_piclass,pd_pdno,pd_ordercode,pd_prodcode," +
-                    "pd_batchcode,pd_inqty,pd_nxlh,pd_purcrate,pd_notinqty,pd_orderprice,pd_ordertotal,pd_price," +
-                    "pd_total,pd_whcode,pd_whname,pd_remark,pd_prodmadedate,pd_replydate,pd_location,pd_macode,pd_xb," +
-                    "pd_bz,pd_dbds,pd_zds) values (PRODIODETAIL_SEQ.nextval,"+id+",'"+code+"','完工入库单',"+detno+",'"+fSourceBillNo+
-                    "','"+map11.get("FNumber")+"','"+fBatchNo+"',"+fauxqty+",'"+fSecUnitID+"',"+fSecCoefficient+","+fSecQty+","+fAuxPlanPrice+","+fPlanAmount+","+fauxprice+"," +
-                    ""+famount+",'"+warehouses[0]+"','"+warehouses[1]+"','"+fnote+"',to_date('"+fEntrySelfA0248+ "'," +
-                    "'yyyy-MM-dd HH24:mi:ss'),to_date('"+fPeriodDate+"','yyyy-MM-dd HH24:mi:ss'),'"+location+"','"+fICMOBillNo+"','"+fEntrySelfA0246+"','"+fEntrySelfA0245+"',"+fEntrySelfA0247+","+fEntrySelfA0242+")");
-            detno++;
-            //更新已转完工数
-            sqls.add("update make set ma_tomadeqty=nvl((select sum(pd_inqty)-sum(case when pd_status=99 then pd_outqty else 0 end) from prodiodetail where pd_ordercode=ma_code and pd_piclass in('完工入库单','委外验收单','委外验退单')),0) where ma_id=" + ma_id);
-            sqls.add("update make set ma_tomadeqty=ma_madeqty where ma_id=" + ma_id + " and nvl(ma_madeqty,0)>nvl(ma_tomadeqty,0)");
-            //更新最大套料数
-            sqls.add("update make set ma_canmadeqty=nvl((select min(case when NVL(mm_havegetqty,0)-NVL(mm_scrapqty,0)>=mm_qty then ma_qty else floor((  nvl(mm_havegetqty, 0)-nvl(mm_scrapqty,0))*1.0/mm_oneuseqty )end) from makematerial where mm_maid=ma_id and nvl(mm_materialstatus,' ')=' ' and mm_oneuseqty>0 and mm_oneuseqty*ma_qty<=mm_qty+0.1 ),0)  where ma_id in ("+ ma_id + ") ");
-            sqls.add("update make set ma_canmadeqty=ma_qty where ma_id in (" + ma_id
-                    + ") and ma_id not in (select ma_id from make left join makematerial on ma_id=mm_maid where ma_id in (" + ma_id
-                    + ") and nvl(mm_materialstatus,' ')=' ' and mm_oneuseqty>0 and mm_oneuseqty*ma_qty<=mm_qty+0.1)");
-            sqls.add("update make set ma_canmadeqty=0 where ma_id in (" + ma_id + ") and NVL(ma_canmadeqty,0)<=0");
+            String fEntrySelfA0247 = StringUtil.nvl(jsonObject3.get("FEntrySelfA0247"), "0");
+            String fEntrySelfA0242 = StringUtil.nvl(jsonObject3.get("FEntrySelfA0242"), "0");
+            if ("OS".equals(ma_tasktype)) {
+                sqls.add("insert into prodiodetail (pd_id,pd_piid,pd_inoutno,pd_piclass,pd_pdno,pd_ordercode,pd_prodcode," +
+                        "pd_batchcode,pd_inqty,pd_nxlh,pd_purcrate,pd_notinqty,pd_orderprice,pd_ordertotal,pd_price," +
+                        "pd_total,pd_whcode,pd_whname,pd_remark,pd_prodmadedate,pd_replydate,pd_location,pd_macode,pd_xb," +
+                        "pd_bz,pd_dbds,pd_zds,pd_status) values (PRODIODETAIL_SEQ.nextval," + piid + ",'" + picode + "','"+piclass+
+                        "'," + detno + ",'" + fSourceBillNo +
+                        "','" + map11.get("FNumber") + "','" + fBatchNo + "'," + fauxqty + ",'" + fSecUnitID + "'," + fSecCoefficient + "," + fSecQty + "," + fAuxPlanPrice + "," + fPlanAmount + "," + fauxprice + "," +
+                        "" + famount + ",'" + warehouses[0] + "','" + warehouses[1] + "','" + fnote + "',to_date('" + fEntrySelfA0248 + "'," +
+                        "'yyyy-MM-dd HH24:mi:ss'),to_date('" + fPeriodDate + "','yyyy-MM-dd HH24:mi:ss'),'" + location + "','" + fICMOBillNo + "','" + fEntrySelfA0246 + "','" + fEntrySelfA0245 + "'," + fEntrySelfA0247 + "," + fEntrySelfA0242 + ",0)");
+                detno++;
+            }else {
+                sqls.add("insert into prodiodetail (pd_id,pd_piid,pd_inoutno,pd_piclass,pd_pdno,pd_ordercode,pd_prodcode," +
+                        "pd_batchcode,pd_inqty,pd_nxlh,pd_purcrate,pd_notinqty,pd_orderprice,pd_ordertotal,pd_price," +
+                        "pd_total,pd_whcode,pd_whname,pd_remark,pd_prodmadedate,pd_replydate,pd_location,pd_macode,pd_xb," +
+                        "pd_bz,pd_dbds,pd_zds,pd_status) values (PRODIODETAIL_SEQ.nextval," + piid + ",'" + picode + "','"+piclass+
+                        "'," + detno + ",'" + fSourceBillNo +
+                        "','" + map11.get("FNumber") + "','" + fBatchNo + "'," + fauxqty + ",'" + fSecUnitID + "'," + fSecCoefficient + "," + fSecQty + "," + fAuxPlanPrice + "," + fPlanAmount + "," + fauxprice + "," +
+                        "" + famount + ",'" + warehouses[0] + "','" + warehouses[1] + "','" + fnote + "',to_date('" + fEntrySelfA0248 + "'," +
+                        "'yyyy-MM-dd HH24:mi:ss'),to_date('" + fPeriodDate + "','yyyy-MM-dd HH24:mi:ss'),'" + location + "','" + fICMOBillNo + "','" + fEntrySelfA0246 + "','" + fEntrySelfA0245 + "'," + fEntrySelfA0247 + "," + fEntrySelfA0242 + ",0)");
+                detno++;
+            }
         }
+        if ("OS".equals(ma_tasktype)) {
+            sqls.add("update prodiodetail set (pd_location,pd_orderprice,pd_taxrate,pd_prodid,pd_orderid,pd_wccode)=" +
+                    "(select pr_location,ma_price,ma_taxrate,pr_id,ma_id,ma_wccode from make left join Product on " +
+                    "ma_prodcode=pr_code where ma_id="+ma_id+") where pd_piid="+piid);
+            sqls.add("update Prodiodetail set pd_whname=(select wh_description from warehouse where pd_whcode=wh_code) where pd_piid="
+                    + piid);
+            sqls.add("update Prodiodetail set pd_total=round(pd_inqty*pd_orderprice,2) where pd_piid=" + piid);
+        }
+        //更新已转完工数
+        sqls.add("update make set ma_tomadeqty=nvl((select sum(pd_inqty)-sum(case when pd_status=99 then pd_outqty else 0 end) from prodiodetail where pd_ordercode=ma_code and pd_piclass in('完工入库单','委外验收单','委外验退单')),0) where ma_id=" + ma_id);
+        sqls.add("update make set ma_tomadeqty=ma_madeqty where ma_id=" + ma_id + " and nvl(ma_madeqty,0)>nvl(ma_tomadeqty,0)");
+        //更新最大套料数
+        sqls.add("update make set ma_canmadeqty=nvl((select min(case when NVL(mm_havegetqty,0)-NVL(mm_scrapqty,0)>=mm_qty then ma_qty else floor((  nvl(mm_havegetqty, 0)-nvl(mm_scrapqty,0))*1.0/mm_oneuseqty )end) from makematerial where mm_maid=ma_id and nvl(mm_materialstatus,' ')=' ' and mm_oneuseqty>0 and mm_oneuseqty*ma_qty<=mm_qty+0.1 ),0)  where ma_id in (" + ma_id + ") ");
+        sqls.add("update make set ma_canmadeqty=ma_qty where ma_id in (" + ma_id
+                + ") and ma_id not in (select ma_id from make left join makematerial on ma_id=mm_maid where ma_id in (" + ma_id
+                + ") and nvl(mm_materialstatus,' ')=' ' and mm_oneuseqty>0 and mm_oneuseqty*ma_qty<=mm_qty+0.1)");
+        sqls.add("update make set ma_canmadeqty=0 where ma_id in (" + ma_id + ") and NVL(ma_canmadeqty,0)<=0");
         baseDao.execute(sqls);
-        Employee employee = baseDao.getJdbcTemplate().queryForObject("select * from employee where em_code=?",
-                new BeanPropertyRowMapper<Employee>(Employee.class), "mes");
-        String s = postProdInOut(id, "ProdInOut!Make!In", employee);
-        logger.info("完工入库单过账信息=="+s);
+        String s = postProdInOut(piid, picaller, employee);
+        logger.info("完工过账信息=="+s);
         if (s!=null){
-            return ApiResponse.successRsp("0",request.getHeader("RequestId"),"完工入库单::"+code+",过账失败,请在ERP重新过账!");
+            return ApiResponse.successRsp("0",request.getHeader("RequestId"),piclass+":"+picode+",过账失败,请在ERP重新过账!");
         }
-        return ApiResponse.successRsp("0",request.getHeader("RequestId"),"完工入库单:"+code+",请在ERP查看!");
+        return ApiResponse.successRsp("0",request.getHeader("RequestId"),piclass+":"+picode+",请在ERP查看!");
     }
 
     @Override
@@ -560,33 +613,26 @@ public class MESServiceImpl implements MESService {
             if ("".equals(whcode2)){
                 return ApiResponse.failRsp("10043",request.getHeader("RequestId"),"物料所在仓库不能为空!");
             }
-            int count2 = baseDao.getCount("select count(1) from customtabledetail left join customtable on " +
-                    "cd_ctid=ct_id left join Warehouse on cd_varchar50_3=wh_code where ct_statuscode='AUDITED' and " +
-                    "ct_caller='MESWarehouse' and cd_varchar50_1='" + whcode2 + "'");
+            int count2 = baseDao.getCount("select count(1) from customtabledetail left join customtable on cd_ctid=ct_id left join Warehouse on cd_varchar50_3=wh_code where ct_statuscode='AUDITED' and ct_caller='MESWarehouse' and cd_varchar50_1='" + whcode2 + "'");
             if (count2<=0){
                 return ApiResponse.failRsp("10044",request.getHeader("RequestId"),"MES仓库编号在ERP没有对应关系,请联系管理员新增!");
             }
-            warehouses2 = 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='" + whcode2 + "'");
+            warehouses2 = 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='" + whcode2 + "'");
 
             int lineId = Integer.parseInt(StringUtil.nvl(object.get("lineId"), "0"));
-            Object[] data1 = baseDao.getFieldsDataByCondition("MakeMaterial", "mm_detno,mm_maid",
-                    "mm_id in (" + lineId + ")");
+            int count4 = baseDao.getCount("select count(1) from MakeMaterial where mm_id in (" + lineId + ")");
+            if (count4<=0){
+                return ApiResponse.failRsp("10046",request.getHeader("RequestId"),"工单BOM在ERP没有不存在,明细ID:"+lineId+",请联系管理员!");
+            }
+            Object[] data1 = baseDao.getFieldsDataByCondition("MakeMaterial", "mm_detno,mm_maid","mm_id in (" + lineId + ")");
             maid = Integer.parseInt(StringUtil.nvl(data1[1], "0"));
             if(bccode!=null) {
-                sqls.add("insert into prodiodetail (pd_id,pd_piid,pd_inoutno,pd_piclass,pd_pdno,pd_prodcode," +
-                        "pd_outqty,pd_whcode,pd_whname,pd_inwhcode,pd_inwhname,pd_macode,pd_madetno) " +
-                        "values (PRODIODETAIL_SEQ.nextval," + bcpiid + ",'" + bccode + "','"+bcpiclass+"'," + detno + ",'" + itemNo +
-                        "'," + qty + ",'" + warehouses2[0] + "','" + warehouses2[1] + "','" + warehouses[0] + "','" + warehouses[1] + "','"+ficmoBillNo+"',"+data1[0]+")");
+                sqls.add("insert into prodiodetail (pd_id,pd_piid,pd_inoutno,pd_piclass,pd_pdno,pd_prodcode,pd_outqty,pd_whcode,pd_whname,pd_inwhcode,pd_inwhname,pd_macode,pd_madetno) values (PRODIODETAIL_SEQ.nextval," + bcpiid + ",'" + bccode + "','"+bcpiclass+"'," + detno + ",'" + itemNo + "'," + qty + ",'" + warehouses2[0] + "','" + warehouses2[1] + "','" + warehouses[0] + "','" + warehouses[1] + "','"+ficmoBillNo+"',"+data1[0]+")");
                 detno++;
             }
             int count1 = baseDao.getCount("select count(1) from MakeMaterial where mm_prodcode='" + itemNo + "' and mm_id in (" + lineId + ")");
             if (count1>0) {
-                SqlRowList sqlRowList = baseDao.queryForRowSet("select sum(round((mm_qty-nvl(mm_canuserepqty,0)-(nvl" +
-                        "(mm_havegetqty,0)-nvl(mm_haverepqty,0)+(nvl(mm_returnmqty,0)-nvl(mm_repreturnmqty,0))-(nvl" +
-                        "(mm_addqty,0)-NVL(mm_repaddqty,0)))-nvl(mm_totaluseqty,0)+NVL(mm_repqty,0))-NVL(mm_stepinqty,0)" +
-                        ",7)) mmqty from MakeMaterial where mm_prodcode='" + itemNo + "' and mm_id in (" + lineId + ")");
+                SqlRowList sqlRowList = baseDao.queryForRowSet("select sum(round((mm_qty-nvl(mm_canuserepqty,0)-(nvl(mm_havegetqty,0)-nvl(mm_haverepqty,0)+(nvl(mm_returnmqty,0)-nvl(mm_repreturnmqty,0))-(nvl(mm_addqty,0)-NVL(mm_repaddqty,0)))-nvl(mm_totaluseqty,0)+NVL(mm_repqty,0))-NVL(mm_stepinqty,0),7)) mmqty from MakeMaterial where mm_prodcode='" + itemNo + "' and mm_id in (" + lineId + ")");
                 if (sqlRowList.next()) {
                     Double mmqty = sqlRowList.getGeneralDouble("mmqty");
                     qty = qty.compareTo(mmqty) > 0 ? mmqty : qty;
@@ -594,9 +640,7 @@ public class MESServiceImpl implements MESService {
             }else {
                 int count3 = baseDao.getCount("select count(1) from MakeMaterialreplace where mp_prodcode='" + itemNo + "' and  mp_mmid in (" + lineId + ")");
                 if (count3>0){
-                    SqlRowList sqlRowList1 = baseDao.queryForRowSet("select sum(nvl(mp_canuseqty,0)-(nvl" +
-                            "(mp_haverepqty,0)-NVL(mp_addqty,0)+NVL(mp_returnmqty,0))-nvl(mp_repqty,0)) mpqty from " +
-                            "MakeMaterialreplace where mp_prodcode='" + itemNo + "' and  mp_mmid in (" + lineId + ")");
+                    SqlRowList sqlRowList1 = baseDao.queryForRowSet("select sum(nvl(mp_canuseqty,0)-(nvl(mp_haverepqty,0)-NVL(mp_addqty,0)+NVL(mp_returnmqty,0))-nvl(mp_repqty,0)) mpqty from MakeMaterialreplace where mp_prodcode='" + itemNo + "' and  mp_mmid in (" + lineId + ")");
                     if (sqlRowList1.next()){
                         Double mpqty = sqlRowList1.getGeneralDouble("mpqty");
                         qty = qty.compareTo(mpqty) > 0 ? mpqty : qty;
@@ -604,10 +648,7 @@ public class MESServiceImpl implements MESService {
                 }
             }
             if (qty>0) {
-                sqls.add("insert into prodiodetail (pd_id,pd_piid,pd_inoutno,pd_piclass,pd_pdno,pd_prodcode," +
-                        "pd_outqty,pd_whcode,pd_whname,pd_ordercode,pd_orderdetno,pd_macode,pd_madetno) " +
-                        "values (PRODIODETAIL_SEQ.nextval," + llpiid + ",'" + llcode + "','" + llpiclass + "'," + det + ",'" + itemNo +
-                        "'," + qty + ",'" + warehouses[0] + "','" + warehouses[1] + "','" + ficmoBillNo + "'," + data1[0] + ",'" + ficmoBillNo + "'," + data1[0] + ")");
+                sqls.add("insert into prodiodetail (pd_id,pd_piid,pd_inoutno,pd_piclass,pd_pdno,pd_prodcode,pd_outqty,pd_whcode,pd_whname,pd_ordercode,pd_orderdetno,pd_macode,pd_madetno,pd_status) values (PRODIODETAIL_SEQ.nextval," + llpiid + ",'" + llcode + "','" + llpiclass + "'," + det + ",'" + itemNo + "'," + qty + ",'" + warehouses[0] + "','" + warehouses[1] + "','" + ficmoBillNo + "'," + data1[0] + ",'" + ficmoBillNo + "'," + data1[0] + ",0)");
                 det++;
             }
         }
@@ -637,6 +678,8 @@ public class MESServiceImpl implements MESService {
             if (count>0) {
                 String s1 = postProdInOut(llpiid, llcaller, employee);
                 logger.info("S1==" + s1);
+                refreshTurnQty(maid,0);
+                setBackQty(String.valueOf(maid),0);
                 if (s1 != null) {
                     return ApiResponse.successRsp("0", request.getHeader("RequestId"), llpiclass+":" + llcode + ",过账失败,请在ERP重新过账!");
                 }
@@ -685,17 +728,11 @@ public class MESServiceImpl implements MESService {
             if (frob==0){
                 tlid = baseDao.getSeqId("PRODINOUT_SEQ");
                 tlcode = baseDao.sGetMaxNumber(tlcaller, 2);
-                sqls.add("INSERT INTO ProdInOut(pi_id, pi_inoutno, pi_date, pi_class,pi_invostatus,pi_invostatuscode," +
-                        "pi_recordman, pi_recorddate, pi_status, pi_statuscode,pi_updatedate,pi_updateman," +
-                        "pi_printstatuscode,pi_printstatus,pi_departmentcode,pi_departmentname,pi_emcode,pi_emname) VALUES ("+tlid+",'"+tlcode+"',to_date('"+jsonObject2.get("Fdate")+"','yyyy-MM-dd HH24:mi:ss'),'"+tlpiclass+"','在录入','ENTERING','"+map5.get("FName")+"',to_date('"+jsonObject2.get("Fdate")+"','yyyy-MM-dd HH24:mi:ss'),'UNPOST','未过账',sysdate,'"+map5.get("FName")+"','UNPRINT','未打印','"+map1.get("FNumber")+"','"+map1.get("FName")+"','"+map4.get("FNumber")+"','"+map4.get("FName")+"')");
+                sqls.add("INSERT INTO ProdInOut(pi_id, pi_inoutno, pi_date, pi_class,pi_invostatus,pi_invostatuscode,pi_recordman, pi_recorddate, pi_statuscode,pi_status,pi_updatedate,pi_updateman,pi_printstatuscode,pi_printstatus,pi_departmentcode,pi_departmentname,pi_emcode,pi_emname) VALUES ("+tlid+",'"+tlcode+"',to_date('"+jsonObject2.get("Fdate")+"','yyyy-MM-dd HH24:mi:ss'),'"+tlpiclass+"','在录入','ENTERING','"+map5.get("FName")+"',to_date('"+jsonObject2.get("Fdate")+"','yyyy-MM-dd HH24:mi:ss'),'UNPOST','未过账',sysdate,'"+map5.get("FName")+"','UNPRINT','未打印','"+map1.get("FNumber")+"','"+map1.get("FName")+"','"+map4.get("FNumber")+"','"+map4.get("FName")+"')");
             }
             id = baseDao.getSeqId("PRODINOUT_SEQ");
             code = baseDao.sGetMaxNumber(bccaller, 2);
-            sqls.add("insert into prodinout (pi_id,pi_inoutno,pi_class,pi_date,pi_statuscode,pi_status," +
-                    "pi_departmentcode,pi_departmentname,pi_emcode,pi_emname,pi_invostatuscode,pi_invostatus," +
-                    "pi_recordman,pi_recorddate,pi_printstatuscode,pi_printstatus,pi_inoutman,pi_type) " +
-                    "values ("+id+",'"+code+"','"+bcpiclass+"',to_date('"+jsonObject2.get("Fdate")+"','yyyy-MM-dd HH24:mi:ss')," +
-                    "'UNPOST','未过账','"+map1.get("FNumber")+"','"+map1.get("FName")+"','"+map4.get("FNumber")+"','"+map4.get("FName")+"','ENTERING','在录入','"+map5.get("FName")+"',to_date('"+jsonObject2.get("Fdate")+"','yyyy-MM-dd HH24:mi:ss'),'UNPRINT','未打印','"+map3.get("FName")+"','库存转移')");
+            sqls.add("insert into prodinout (pi_id,pi_inoutno,pi_class,pi_date,pi_statuscode,pi_status,pi_departmentcode,pi_departmentname,pi_emcode,pi_emname,pi_invostatuscode,pi_invostatus,pi_recordman,pi_recorddate,pi_printstatuscode,pi_printstatus,pi_inoutman,pi_type) values ("+id+",'"+code+"','"+bcpiclass+"',to_date('"+jsonObject2.get("Fdate")+"','yyyy-MM-dd HH24:mi:ss'),'UNPOST','未过账','"+map1.get("FNumber")+"','"+map1.get("FName")+"','"+map4.get("FNumber")+"','"+map4.get("FName")+"','ENTERING','在录入','"+map5.get("FName")+"',to_date('"+jsonObject2.get("Fdate")+"','yyyy-MM-dd HH24:mi:ss'),'UNPRINT','未打印','"+map3.get("FName")+"','库存转移')");
         }
         Object[] warehouses=null;
         Object[] warehouses2=null;
@@ -708,7 +745,7 @@ public class MESServiceImpl implements MESService {
             if ("".equals(itemNo)){
                 return ApiResponse.failRsp("10051",request.getHeader("RequestId"),"物料不能为空!");
             }
-            String qty = StringUtil.nvl(object.get("FAuxQty"), "");
+            Double qty =Double.parseDouble(StringUtil.nvl(object.get("FAuxQty"), ""));
             if ("".equals(qty)){
                 return ApiResponse.failRsp("10052",request.getHeader("RequestId"),"实发数量不能为空!");
             }
@@ -730,15 +767,11 @@ public class MESServiceImpl implements MESService {
             if ("".equals(whcode2)){
                 return ApiResponse.failRsp("10055",request.getHeader("RequestId"),"物料退回仓库不能为空!");
             }
-            int count2 = baseDao.getCount("select count(1) from customtabledetail left join customtable on " +
-                    "cd_ctid=ct_id left join Warehouse on cd_varchar50_3=wh_code where ct_statuscode='AUDITED' and " +
-                    "ct_caller='MESWarehouse' and cd_varchar50_1='" + whcode2 + "'");
+            int count2 = baseDao.getCount("select count(1) from customtabledetail left join customtable on cd_ctid=ct_id left join Warehouse on cd_varchar50_3=wh_code where ct_statuscode='AUDITED' and ct_caller='MESWarehouse' and cd_varchar50_1='" + whcode2 + "'");
             if (count2<=0){
                 return ApiResponse.failRsp("10056",request.getHeader("RequestId"),"MES仓库编号在ERP没有对应关系,请联系管理员新增!");
             }
-            warehouses2 = 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='" + whcode2 + "'");
+            warehouses2 = 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='" + whcode2 + "'");
 
             String ficmoBillNo = StringUtil.nvl(object.get("FICMOBillNo"), "");
             String fBatchNo = StringUtil.nvl(object.get("FBatchNo"), "");
@@ -747,23 +780,27 @@ public class MESServiceImpl implements MESService {
             if (count1<=0){
                 return ApiResponse.failRsp("10075",request.getHeader("RequestId"),"工单BOM不存在或ID不一致,请联系管理员处理!");
             }
-            Object[] objs = baseDao.getFieldsDataByCondition("Make left join MakeMaterial on ma_id=mm_maid", new String[] { "mm_id", "mm_code",
-                    "mm_detno", "mm_mdcode", "mm_prodcode", "mm_wccode","mm_havegetqty","ma_qty","ma_tomadeqty",
-                            "ma_madeqty","mm_oneuseqty","mm_qty"},
-                    "mm_id in (" + lineId+")");
+            Object[] objs = baseDao.getFieldsDataByCondition("Make left join MakeMaterial on ma_id=mm_maid", new String[] { "mm_id", "mm_code","mm_detno", "mm_mdcode", "mm_prodcode", "mm_wccode"}, "mm_id in (" + lineId+")");
+            sqls.add("insert into prodiodetail (pd_id,pd_piid,pd_inoutno,pd_piclass,pd_pdno,pd_prodcode,pd_outqty,pd_whcode,pd_whname,pd_inwhcode,pd_inwhname,pd_macode,pd_madetno) values (PRODIODETAIL_SEQ.nextval,"+id+",'"+code+"','"+bcpiclass+"',"+detno+",'"+itemNo+"',"+qty+",'"+warehouses[0]+"','"+warehouses[1]+"','"+warehouses2[0]+"','"+warehouses2[1]+"','"+ficmoBillNo+"',"+objs[2]+")");
             if (frob==0) {
                 Object prid = baseDao.getFieldDataByCondition("Product", "pr_id", "pr_code='" + itemNo + "'");
-                int seqId = baseDao.getSeqId("PRODIODETAIL_SEQ");
-                sqls.add("INSERT INTO ProdIODetail(pd_id, pd_piid, pd_inoutno, pd_piclass, pd_pdno, pd_status," +
-                        "pd_auditstatuscode,pd_auditstatus,pd_prodcode,pd_ordercode, pd_orderdetno,pd_macode,pd_madetno, " +
-                        "pd_plancode, pd_wccode, pd_orderid, pd_prodid," +
-                        "pd_whcode,pd_whname) VALUES ("+seqId+","+tlid+",'"+tlcode+"','"+tlpiclass+"',"+det+"," +
-                        "0,'ENTERING','在录入','"+itemNo+"','"+ficmoBillNo+"',"+objs[2]+",'"+ficmoBillNo+"',"+objs[2]+",'"+objs[3]+"','"+objs[5]+"',"+lineId+","+prid+",'"+warehouses[0]+"','"+warehouses[1]+"')");
+                SqlRowList rs = baseDao.queryForRowSet("select nvl(mm_totaluseqty,0)+nvl(mm_havegetqty,0)-nvl" +
+                        "(ma_tomadeqty,0)*nvl(mm_oneuseqty,0) mmqty,nvl(mm_havegetqty,0)-nvl(ma_tomadeqty,0)*nvl" +
+                        "(mm_oneuseqty,0) useqty,mm_totaluseqty from MakeMaterial left join Make on ma_id=mm_maid " +
+                        "where mm_id=" + objs[0]);
+                Double mmqty=0.0;
+                if (rs.next()){
+                    //最多可退数量
+                    mmqty = rs.getDouble("mmqty");
+                    //未使用数量
+                    Double useqty = rs.getDouble("useqty");
+                    //已转领料数
+                    Double mmTotaluseqty = rs.getDouble("mm_totaluseqty");
+                }
+                qty = qty.compareTo(mmqty) > 0 ? mmqty : qty;
+                sqls.add("INSERT INTO ProdIODetail(pd_id, pd_piid, pd_inoutno, pd_piclass, pd_pdno, pd_status,pd_auditstatuscode,pd_auditstatus,pd_prodcode,pd_ordercode, pd_orderdetno,pd_macode,pd_madetno,pd_plancode, pd_wccode, pd_orderid, pd_prodid,pd_whcode,pd_whname,pd_inqty,pd_description) VALUES (PRODIODETAIL_SEQ.nextval,"+tlid+",'"+tlcode+"','"+tlpiclass+"',"+det+ ",0,'ENTERING','在录入','"+itemNo+"','"+ficmoBillNo+"',"+objs[2]+",'"+ficmoBillNo+"',"+objs[2]+ ",'"+objs[3]+"','"+objs[5]+"',"+lineId+","+prid+",'"+warehouses[0]+"','"+warehouses[1]+"',"+qty+ ",'良品退仓')");
                 det++;
-                sqls.add("update prodiodetail set pd_inqty=(select nvl(mm_totaluseqty,0)-nvl(ma_tomadeqty,0)*nvl(mm_oneuseqty,0) from MakeMaterial left join Make on ma_id=mm_maid where mm_id="+objs[0]+") where pd_id="+seqId);
-
             }
-            sqls.add("insert into prodiodetail (pd_id,pd_piid,pd_inoutno,pd_piclass,pd_pdno,pd_prodcode,pd_outqty,pd_whcode,pd_whname,pd_inwhcode,pd_inwhname,pd_macode,pd_madetno) values (PRODIODETAIL_SEQ.nextval,"+id+",'"+code+"','"+bcpiclass+"',"+detno+",'"+itemNo+"',"+qty+",'"+warehouses[0]+"','"+warehouses[1]+"','"+warehouses2[0]+"','"+warehouses2[1]+"','"+ficmoBillNo+"',"+objs[2]+")");
             detno++;
         }
         if (frob==0){
@@ -773,7 +810,7 @@ public class MESServiceImpl implements MESService {
                     + tlid);
         }
         if ("OS".equals(ma_tasktype)) {
-            baseDao.execute("update prodinout set(pi_cardcode,pi_title,pi_receivecode,pi_receivename) = (select max(ma_vendcode),max(ma_vendname),max(nvl(ma_apvendcode,ma_vendcode)),max(nvl(ma_apvendname,ma_vendname)) from make where ma_id in (" + maid + ")) where pi_id=" + tlid);
+            sqls.add("update prodinout set(pi_cardcode,pi_title,pi_receivecode,pi_receivename) = (select max(ma_vendcode),max(ma_vendname),max(nvl(ma_apvendcode,ma_vendcode)),max(nvl(ma_apvendname,ma_vendname)) from make where ma_id in (" + maid + ")) where pi_id=" + tlid);
         }
         if (warehouses!=null){
             sqls.add("update prodinout set pi_whcode='"+warehouses[0]+"',pi_whname='"+warehouses[1]+"' where pi_id="+id);
@@ -787,8 +824,7 @@ public class MESServiceImpl implements MESService {
         if (count<=0){
             baseDao.deleteById("prodinout","pi_id",tlid);
         }
-        Employee employee = baseDao.getJdbcTemplate().queryForObject("select * from employee where em_code=?",
-                new BeanPropertyRowMapper<Employee>(Employee.class), "mes");
+        Employee employee = baseDao.getJdbcTemplate().queryForObject("select * from employee where em_code=?", new BeanPropertyRowMapper<Employee>(Employee.class), "mes");
         if (count>0) {
             String s1 = postProdInOut(tlid, tlcaller, employee);
             logger.info("S1==" + s1);
@@ -820,11 +856,7 @@ public class MESServiceImpl implements MESService {
             Map<Object, Object> map5 = JSONUtil.toMap(StringUtil.valueOf(jsonObject2.get("FBillerID")));
             id = baseDao.getSeqId("PRODINOUT_SEQ");
             code = baseDao.sGetMaxNumber("ProdInOut!AppropriationOut", 2);
-            sqls.add("insert into prodinout (pi_id,pi_inoutno,pi_class,pi_date,pi_statuscode,pi_status," +
-                    "pi_departmentcode,pi_departmentname,pi_emcode,pi_emname,pi_invostatuscode,pi_invostatus," +
-                    "pi_recordman,pi_recorddate,pi_printstatuscode,pi_printstatus,pi_type,pi_inoutman) " +
-                    "values ("+id+",'"+code+"','拨出单',to_date('"+jsonObject2.get("Fdate")+"','yyyy-MM-dd HH24:mi:ss')," +
-                    "'UNPOST','未过账','"+map1.get("FNumber")+"','"+map1.get("FName")+"','"+map4.get("FNumber")+"','"+map4.get("FName")+"','ENTERING','在录入','"+map5.get("FName")+"',to_date('"+jsonObject2.get("Fdate")+"','yyyy-MM-dd HH24:mi:ss'),'UNPRINT','未打印','"+map2.get("FName")+"','"+map3.get("FName")+"')");
+            sqls.add("insert into prodinout (pi_id,pi_inoutno,pi_class,pi_date,pi_statuscode,pi_status,pi_departmentcode,pi_departmentname,pi_emcode,pi_emname,pi_invostatuscode,pi_invostatus,pi_recordman,pi_recorddate,pi_printstatuscode,pi_printstatus,pi_type,pi_inoutman) values ("+id+",'"+code+"','拨出单',to_date('"+jsonObject2.get("Fdate")+"','yyyy-MM-dd HH24:mi:ss'),'UNPOST','未过账','"+map1.get("FNumber")+"','"+map1.get("FName")+"','"+map4.get("FNumber")+"','"+map4.get("FName")+"','ENTERING','在录入','"+map5.get("FName")+"',to_date('"+jsonObject2.get("Fdate")+"','yyyy-MM-dd HH24:mi:ss'),'UNPRINT','未打印','"+map2.get("FName")+"','"+map3.get("FName")+"')");
         }
         JSONArray array = data.get("detail");
         Object[] warehouses=null;
@@ -847,31 +879,22 @@ public class MESServiceImpl implements MESService {
             if ("".equals(whcode)){
                 return ApiResponse.failRsp("10063",request.getHeader("RequestId"),"发货仓库不能为空!");
             }
-            int count = baseDao.getCount("select count(1) from customtabledetail left join customtable on " +
-                    "cd_ctid=ct_id left join Warehouse on cd_varchar50_3=wh_code where ct_statuscode='AUDITED' and " +
-                    "ct_caller='MESWarehouse' and cd_varchar50_1='" + whcode + "'");
+            int count = baseDao.getCount("select count(1) from customtabledetail left join customtable on cd_ctid=ct_id left join Warehouse on cd_varchar50_3=wh_code where ct_statuscode='AUDITED' and ct_caller='MESWarehouse' and cd_varchar50_1='" + whcode + "'");
             if (count<=0){
                 return ApiResponse.failRsp("10064",request.getHeader("RequestId"),"MES仓库编号在ERP没有对应关系,请联系管理员新增!");
             }
-            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 + "'");Map<Object, Object> map2 = JSONUtil.toMap(StringUtil.valueOf(object.get("FDCStockID1")));
+            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 + "'");Map<Object, Object> map2 = JSONUtil.toMap(StringUtil.valueOf(object.get("FDCStockID1")));
             String whcode2 = StringUtil.nvl(map2.get("FNumber"), "");
             String whname2 = StringUtil.nvl(map2.get("FName"), "");
             if ("".equals(whcode2)){
                 return ApiResponse.failRsp("10065",request.getHeader("RequestId"),"收货仓库不能为空!");
             }
-            int count2 = baseDao.getCount("select count(1) from customtabledetail left join customtable on " +
-                    "cd_ctid=ct_id left join Warehouse on cd_varchar50_3=wh_code where ct_statuscode='AUDITED' and " +
-                    "ct_caller='MESWarehouse' and cd_varchar50_1='" + whcode2 + "'");
+            int count2 = baseDao.getCount("select count(1) from customtabledetail left join customtable on cd_ctid=ct_id left join Warehouse on cd_varchar50_3=wh_code where ct_statuscode='AUDITED' and ct_caller='MESWarehouse' and cd_varchar50_1='" + whcode2 + "'");
             if (count2<=0){
                 return ApiResponse.failRsp("10066",request.getHeader("RequestId"),"MES仓库编号在ERP没有对应关系,请联系管理员新增!");
             }
-            warehouses2 = 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='" + whcode2 + "'");
-            sqls.add("insert into prodiodetail (pd_id,pd_piid,pd_inoutno,pd_piclass,pd_pdno,pd_prodcode," +
-                    "pd_outqty,pd_whcode,pd_whname,pd_inwhcode,pd_inwhname) values (PRODIODETAIL_SEQ.nextval,"+id+",'"+code+"','拨出单',"+detno+",'"+itemNo+"',"+qty+",'"+warehouses[0]+"','"+warehouses[1]+"','"+warehouses2[0]+"','"+warehouses2[1]+"')");
+            warehouses2 = 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='" + whcode2 + "'");
+            sqls.add("insert into prodiodetail (pd_id,pd_piid,pd_inoutno,pd_piclass,pd_pdno,pd_prodcode,pd_outqty,pd_whcode,pd_whname,pd_inwhcode,pd_inwhname) values (PRODIODETAIL_SEQ.nextval,"+id+",'"+code+"','拨出单',"+detno+",'"+itemNo+"',"+qty+",'"+warehouses[0]+"','"+warehouses[1]+"','"+warehouses2[0]+"','"+warehouses2[1]+"')");
             detno++;
         }
         if (warehouses!=null){