diff --git a/base/src/org/compiere/process/DistributionRun.java b/base/src/org/compiere/process/DistributionRun.java index 1d5217d725..99e653f1c3 100644 --- a/base/src/org/compiere/process/DistributionRun.java +++ b/base/src/org/compiere/process/DistributionRun.java @@ -32,6 +32,9 @@ import org.eevolution.model.MPPMRP; * Create Distribution * * @author Jorg Janke + * @author victor.perez@e-evolution.com + *
  • FR Let use the Distribution List and Distribution Run for DO + * @see http://sourceforge.net/tracker/index.php?func=detail&aid=2030865&group_id=176962&atid=879335 * @version $Id: DistributionRun.java,v 1.4 2006/07/30 00:51:02 jjanke Exp $ */ public class DistributionRun extends SvrProcess @@ -40,14 +43,20 @@ public class DistributionRun extends SvrProcess private int p_M_DistributionRun_ID = 0; /** Date Promised */ private Timestamp p_DatePromised = null; - /** Dicument Type */ + /** Date Promised To */ + private Timestamp p_DatePromised_To = null; + /** Document Type */ private int p_C_DocType_ID = 0; /** Test Mode */ private boolean p_IsTest = false; /** Warehouse to Distribution Order */ private int p_M_Warehouse_ID = 0; - /** Create Ordered **/ - private boolean p_CreateDO = true; + /** Consolidate Document **/ + private boolean p_ConsolidateDocument = false; + /** Distribution List **/ + private int p_M_DistributionList_ID = 0; + /** Distribute Based in DRP Demand **/ + private boolean p_BasedInDamnd = false; /** Distribution Run */ private MDistributionRun m_run = null; @@ -81,13 +90,20 @@ public class DistributionRun extends SvrProcess m_docType = new MDocType(getCtx(),p_C_DocType_ID, get_TrxName()); } else if (name.equals("DatePromised")) + { p_DatePromised = (Timestamp)para[i].getParameter(); + p_DatePromised_To = (Timestamp)para[i].getParameter_To(); + } else if (name.equals("IsTest")) p_IsTest = "Y".equals(para[i].getParameter()); else if (m_docType.getDocBaseType().equals(MDocType.DOCBASETYPE_DistributionOrder) & name.equals("M_Warehouse_ID")) p_M_Warehouse_ID=((BigDecimal)para[i].getParameter()).intValue(); - else if (m_docType.getDocBaseType().equals(MDocType.DOCBASETYPE_DistributionOrder) & name.equals("CreateDO")) - p_CreateDO="Y".equals((String)para[i].getParameter()); + else if (m_docType.getDocBaseType().equals(MDocType.DOCBASETYPE_DistributionOrder) & name.equals("ConsolidateDocument")) + p_ConsolidateDocument="Y".equals((String)para[i].getParameter()); + else if (m_docType.getDocBaseType().equals(MDocType.DOCBASETYPE_DistributionOrder) & name.equals("M_DistributionList_ID")) + p_M_DistributionList_ID=para[i].getParameterAsInt(); + else if (m_docType.getDocBaseType().equals(MDocType.DOCBASETYPE_DistributionOrder) & name.equals("IsRequiredDRP")) + p_BasedInDamnd = "Y".equals((String)para[i].getParameter()); else log.log(Level.SEVERE, "prepare - Unknown Parameter: " + name); } @@ -128,9 +144,17 @@ public class DistributionRun extends SvrProcess if(m_docType.getDocBaseType().equals(MDocType.DOCBASETYPE_DistributionOrder) & p_M_Warehouse_ID > 0) { - //Create Temp Lines - if (insertDetailsDistribution() == 0) - throw new Exception ("No Lines"); + if(p_BasedInDamnd) + { + if (insertDetailsDistributionDemand() == 0) + throw new Exception ("No Lines"); + + } + else + { + if (insertDetailsDistribution() == 0) + throw new Exception ("No Lines"); + } } else { @@ -536,6 +560,116 @@ public class DistributionRun extends SvrProcess } // createOrders + /** + * Insert Details + * @return number of rows inserted + */ + private int insertDetailsDistributionDemand() + { + // Handle NULL + String sql = "UPDATE M_DistributionRunLine SET MinQty = 0 WHERE MinQty IS NULL"; + int no = DB.executeUpdate(sql, get_TrxName()); + + sql = "UPDATE M_DistributionListLine SET MinQty = 0 WHERE MinQty IS NULL"; + no = DB.executeUpdate(sql, get_TrxName()); + + // Delete Old + sql = "DELETE FROM T_DistributionRunDetail WHERE M_DistributionRun_ID=" + + p_M_DistributionRun_ID; + no = DB.executeUpdate(sql, get_TrxName()); + log.fine("insertDetails - deleted #" + no); + + // Insert New + sql = "INSERT INTO T_DistributionRunDetail " + + "(M_DistributionRun_ID, M_DistributionRunLine_ID, M_DistributionList_ID, M_DistributionListLine_ID," + + "AD_Client_ID,AD_Org_ID, IsActive, Created,CreatedBy, Updated,UpdatedBy," + + "C_BPartner_ID, C_BPartner_Location_ID, M_Product_ID," + + "Ratio, MinQty, Qty) " + +"SELECT rl.M_DistributionRun_ID, rl.M_DistributionRunLine_ID,ll.M_DistributionList_ID, ll.M_DistributionListLine_ID, " + +"rl.AD_Client_ID,rl.AD_Org_ID, rl.IsActive, rl.Created,rl.CreatedBy, rl.Updated,rl.UpdatedBy, " + +"ll.C_BPartner_ID, ll.C_BPartner_Location_ID, rl.M_Product_ID," + // Ration for this process is equal QtyToDeliver + +" (ol.QtyOrdered-ol.QtyDelivered-TargetQty) , " + // Min Qty for this process is equal to TargetQty + +" 0 , 0 FROM M_DistributionRunLine rl " + +"INNER JOIN M_DistributionList l ON (rl.M_DistributionList_ID=l.M_DistributionList_ID) " + +"INNER JOIN M_DistributionListLine ll ON (rl.M_DistributionList_ID=ll.M_DistributionList_ID) " + +"INNER JOIN DD_Order o ON (o.C_BPartner_ID=ll.C_BPartner_ID) " + +"INNER JOIN DD_OrderLine ol ON (ol.DD_Order_ID=o.DD_Order_ID AND ol.M_Product_ID=rl.M_Product_ID) AND ol.DatePromised BETWEEN " + + DB.TO_DATE(p_DatePromised_To) +" AND "+ DB.TO_DATE(p_DatePromised) + +" INNER JOIN M_Locator loc ON (loc.M_Locator_ID=ol.M_Locator_ID AND loc.M_Warehouse_ID="+p_M_Warehouse_ID+") " + +" WHERE rl.M_DistributionRun_ID="+p_M_DistributionRun_ID+" AND rl.IsActive='Y' AND ll.IsActive='Y'"; + no = DB.executeUpdate(sql, get_TrxName()); + + Query query = MTable.get(getCtx(), MDistributionRunDetail.Table_ID). + createQuery(MDistributionRunDetail.COLUMNNAME_M_DistributionRun_ID + "=?", get_TrxName()); + query.setParameters(new Object[]{p_M_DistributionRun_ID}); + + List records = query.list(); + + for(MDistributionRunDetail record : records) + { + + MDistributionRunLine drl = (MDistributionRunLine) MTable.get(getCtx(), MDistributionRunLine.Table_ID).getPO(record.getM_DistributionRunLine_ID(), get_TrxName()); + MProduct product = MProduct.get(getCtx(), record.getM_Product_ID()); + + + BigDecimal ration = record.getRatio(); + BigDecimal totalration = getQtyDemand(record.getM_Product_ID()); + System.out.println("Value:" + product.getValue()); + System.out.println("Product:" + product.getName()); + System.out.println("Qty To Deliver:" + record.getRatio()); + System.out.println("Qty Target:" + record.getMinQty()); + System.out.println("Qty Total Available:" + drl.getTotalQty()); + System.out.println("Qty Total Demand:" + totalration); + + BigDecimal factor = ration.divide(totalration, 12 , BigDecimal.ROUND_HALF_UP); + record.setQty(drl.getTotalQty().multiply(factor)); + record.save(); + } + log.fine("inserted #" + no); + return no; + } // insertDetails + + private BigDecimal getQtyDemand(int M_Product_ID) + { + StringBuffer sql = new StringBuffer("SELECT SUM (QtyOrdered-QtyDelivered-TargetQty) FROM DD_OrderLine ol INNER JOIN M_Locator l ON (l.M_Locator_ID=ol.M_Locator_ID) INNER JOIN DD_Order o ON (o.DD_Order_ID=ol.DD_Order_ID) "); + sql.append(" WHERE o.DocStatus IN ('DR','IN') AND ol.DatePromised BETWEEN ? AND ? AND l.M_Warehouse_ID=? AND ol.M_Product_ID=? GROUP BY M_Product_ID, l.M_Warehouse_ID"); + + + PreparedStatement pstmt = null; + ResultSet rs = null; + try + { + pstmt = DB.prepareStatement (sql.toString(), get_TrxName()); + pstmt.setTimestamp(1, p_DatePromised); + pstmt.setTimestamp(2, p_DatePromised_To); + pstmt.setInt(3, p_M_Warehouse_ID); + pstmt.setInt(4, M_Product_ID); + + rs = pstmt.executeQuery(); + while (rs.next()) + { + return rs.getBigDecimal(1); + } + } + catch (Exception e) + { + log.log(Level.SEVERE,"doIt - " + sql, e); + return Env.ZERO; + } + finally + { + DB.close(rs, pstmt); + rs = null; + pstmt = null; + } + + return Env.ZERO; + } + + + /** * Insert Details * @return number of rows inserted @@ -563,13 +697,13 @@ public class DistributionRun extends SvrProcess + "Ratio, MinQty, Qty) " +"SELECT rl.M_DistributionRun_ID, rl.M_DistributionRunLine_ID,ll.M_DistributionList_ID, ll.M_DistributionListLine_ID, " +"rl.AD_Client_ID,rl.AD_Org_ID, rl.IsActive, rl.Created,rl.CreatedBy, rl.Updated,rl.UpdatedBy, " - +"ll.C_BPartner_ID, ll.C_BPartner_Location_ID, rl.M_Product_ID, ll.Ratio, " + +"ll.C_BPartner_ID, ll.C_BPartner_Location_ID, rl.M_Product_ID, 0 , " +"ol.TargetQty AS MinQty , 0 FROM M_DistributionRunLine rl " +"INNER JOIN M_DistributionList l ON (rl.M_DistributionList_ID=l.M_DistributionList_ID) " +"INNER JOIN M_DistributionListLine ll ON (rl.M_DistributionList_ID=ll.M_DistributionList_ID) " +"INNER JOIN DD_Order o ON (o.C_BPartner_ID=ll.C_BPartner_ID) " +"INNER JOIN DD_OrderLine ol ON (ol.DD_Order_ID=o.DD_Order_ID AND ol.M_Product_ID=rl.M_Product_ID) AND ol.DatePromised BETWEEN " - + DB.TO_DATE(new Timestamp (System.currentTimeMillis())) +" AND "+ DB.TO_DATE(p_DatePromised) + + DB.TO_DATE(p_DatePromised) +" AND "+ DB.TO_DATE(p_DatePromised_To) +" INNER JOIN M_Locator loc ON (loc.M_Locator_ID=ol.M_Locator_ID AND loc.M_Warehouse_ID="+p_M_Warehouse_ID+") " +" WHERE rl.M_DistributionRun_ID="+p_M_DistributionRun_ID+" AND l.RatioTotal<>0 AND rl.IsActive='Y' AND ll.IsActive='Y'"; no = DB.executeUpdate(sql, get_TrxName()); @@ -602,8 +736,21 @@ public class DistributionRun extends SvrProcess */ private boolean distributionOrders() { - if (!p_CreateDO) + //The Quantity Available is distribute with respect to Distribution Order Demand + if (p_BasedInDamnd) { + int M_Warehouse_ID = 0; + if (p_M_Warehouse_ID <= 0) + { + MOrgInfo oi_source = MOrgInfo.get(getCtx(), m_run.getAD_Org_ID()); + MWarehouse m_source = MWarehouse.get(getCtx(), oi_source.getM_Warehouse_ID()); + if(m_source == null) + throw new AdempiereException("Do not exist Defautl Warehouse Source"); + M_Warehouse_ID = m_source.getM_Warehouse_ID(); + } + else + M_Warehouse_ID = p_M_Warehouse_ID; + // For all lines for (int i = 0; i < m_details.length; i++) { @@ -613,27 +760,27 @@ public class DistributionRun extends SvrProcess sql.append(" WHERE o.DocStatus IN ('DR','IN') AND o.C_BPartner_ID = ? AND M_Product_ID=? AND l.M_Warehouse_ID=? AND ol.DatePromised BETWEEN ? AND ? "); PreparedStatement pstmt = null; - ResultSet rs = null; - Timestamp today = new Timestamp (System.currentTimeMillis()); + ResultSet rs = null; try { pstmt = DB.prepareStatement (sql.toString(),get_TrxName()); pstmt.setInt(1, detail.getC_BPartner_ID()); pstmt.setInt(2, detail.getM_Product_ID()); - pstmt.setInt(3, p_M_Warehouse_ID); - pstmt.setTimestamp(4, today); - pstmt.setTimestamp(5, p_DatePromised); - - + pstmt.setInt(3, M_Warehouse_ID); + pstmt.setTimestamp(4, p_DatePromised); + pstmt.setTimestamp(5, p_DatePromised_To); + rs = pstmt.executeQuery(); while (rs.next()) { // Create Order Line MDDOrderLine line = new MDDOrderLine(getCtx(), rs , get_TrxName()); line.setM_Product_ID(detail.getM_Product_ID()); - line.setConfirmedQty(detail.getActualAllocation()); - - //line.setPrice(); + line.setConfirmedQty(line.getTargetQty().add(detail.getActualAllocation())); + if(p_M_Warehouse_ID>0) + line.setDescription("Distribucion Pull"); + else + line.setDescription("Distribution Push"); if (!line.save()) { log.log(Level.SEVERE, "OrderLine not saved"); @@ -643,6 +790,7 @@ public class DistributionRun extends SvrProcess //addLog(0,null, detail.getActualAllocation(), order.getDocumentNo() // + ": " + bp.getName() + " - " + product.getName()); } + } catch (Exception e) { @@ -656,7 +804,6 @@ public class DistributionRun extends SvrProcess pstmt = null; } } - return true; } @@ -683,36 +830,7 @@ public class DistributionRun extends SvrProcess MBPartner bp = null; MDDOrder singleOrder = null; MProduct product = null; - // Consolidated Order - if (m_run.isCreateSingleOrder()) - { - bp = new MBPartner (getCtx(), m_run.getC_BPartner_ID(), get_TrxName()); - if (bp.get_ID() == 0) - throw new IllegalArgumentException("Business Partner not found - C_BPartner_ID=" + m_run.getC_BPartner_ID()); - // - if (!p_IsTest) - { - singleOrder = new MDDOrder (getCtx(), 0, get_TrxName()); - //singleOrder.setC_DocTypeTarget_ID(m_docType.getC_DocType_ID()); - singleOrder.setC_DocType_ID(m_docType.getC_DocType_ID()); - singleOrder.setIsSOTrx(m_docType.isSOTrx()); - singleOrder.setBPartner(bp); - if (m_run.getC_BPartner_Location_ID() != 0) - singleOrder.setC_BPartner_Location_ID(m_run.getC_BPartner_Location_ID()); - singleOrder.setDateOrdered(m_DateOrdered); - singleOrder.setDatePromised(p_DatePromised); - if (!singleOrder.save()) - { - log.log(Level.SEVERE, "Order not saved"); - return false; - } - m_counter++; - } - } - int lastC_BPartner_ID = 0; - int lastC_BPartner_Location_ID = 0; - MDDOrder order = null; MWarehouse m_source = null; MLocator m_locator= null ; MWarehouse m_target= null; @@ -732,6 +850,39 @@ public class DistributionRun extends SvrProcess if(ws==null) throw new AdempiereException("Warehouse Intransit do not found"); + + // Consolidated Single Order + if (m_run.isCreateSingleOrder()) + { + bp = new MBPartner (getCtx(), m_run.getC_BPartner_ID(), get_TrxName()); + if (bp.get_ID() == 0) + throw new IllegalArgumentException("Business Partner not found - C_BPartner_ID=" + m_run.getC_BPartner_ID()); + // + if (!p_IsTest) + { + singleOrder = new MDDOrder (getCtx(), 0, get_TrxName()); + singleOrder.setC_DocType_ID(m_docType.getC_DocType_ID()); + singleOrder.setIsSOTrx(m_docType.isSOTrx()); + singleOrder.setBPartner(bp); + if (m_run.getC_BPartner_Location_ID() != 0) + singleOrder.setC_BPartner_Location_ID(m_run.getC_BPartner_Location_ID()); + singleOrder.setDateOrdered(m_DateOrdered); + singleOrder.setDatePromised(p_DatePromised); + singleOrder.setM_Warehouse_ID(ws[0].getM_Warehouse_ID()); + if (!singleOrder.save()) + { + log.log(Level.SEVERE, "Order not saved"); + return false; + } + m_counter++; + } + } + + int lastC_BPartner_ID = 0; + int lastC_BPartner_Location_ID = 0; + MDDOrder order = null; + + // For all lines for (int i = 0; i < m_details.length; i++) { @@ -750,30 +901,42 @@ public class DistributionRun extends SvrProcess lastC_BPartner_ID = detail.getC_BPartner_ID(); lastC_BPartner_Location_ID = detail.getC_BPartner_Location_ID(); + bp = new MBPartner (getCtx(), detail.getC_BPartner_ID(), get_TrxName()); + MOrgInfo oi_target = MOrgInfo.get(getCtx(), bp.getAD_OrgBP_ID_Int()); + m_target = MWarehouse.get(getCtx(), oi_target.getM_Warehouse_ID()); + if(m_target==null) + throw new AdempiereException("Do not exist Default Warehouse Target"); + + m_locator_to = MLocator.getDefault(m_target); + + if (m_locator == null || m_locator_to == null) + { + throw new AdempiereException("Do not exist default Locator for Warehouses"); + } + + if(p_ConsolidateDocument) + { + MTable table = MTable.get(getCtx(), MDDOrder.Table_ID); + + Query query = table.createQuery("DocStatus IN ('DR','IN') AND AD_Org_ID=" + bp.getAD_OrgBP_ID_Int() + " AND " + + MDDOrder.COLUMNNAME_C_BPartner_ID +"=? AND " + + MDDOrder.COLUMNNAME_M_Warehouse_ID +"=? AND " + + MDDOrder.COLUMNNAME_DatePromised +" BETWEEN ? AND ? ", get_TrxName()); + query.setParameters(new Object[]{lastC_BPartner_ID, ws[0].getM_Warehouse_ID(), p_DatePromised,p_DatePromised_To}); + + order = query.first(); + } + // New Order if (order == null) { - bp = new MBPartner (getCtx(), detail.getC_BPartner_ID(), get_TrxName()); + //bp = new MBPartner (getCtx(), detail.getC_BPartner_ID(), get_TrxName()); if (!p_IsTest) { order = new MDDOrder (getCtx(), 0, get_TrxName()); - //order.setC_DocTypeTarget_ID(m_docType.getC_DocType_ID()); order.setAD_Org_ID(bp.getAD_OrgBP_ID_Int()); order.setC_DocType_ID(m_docType.getC_DocType_ID()); - order.setIsSOTrx(m_docType.isSOTrx()); - - MOrgInfo oi_target = MOrgInfo.get(getCtx(), bp.getAD_OrgBP_ID_Int()); - m_target = MWarehouse.get(getCtx(), oi_target.getM_Warehouse_ID()); - if(m_target==null) - throw new AdempiereException("Do not exist Default Warehouse Target"); - - m_locator_to = MLocator.getDefault(m_target); - - if (m_locator == null || m_locator_to == null) - { - throw new AdempiereException("Do not exist default Locator for Warehouses"); - } - + order.setIsSOTrx(m_docType.isSOTrx()); // Counter Doc if (counter && bp.getAD_OrgBP_ID_Int() > 0) @@ -794,6 +957,7 @@ public class DistributionRun extends SvrProcess if (detail.getC_BPartner_Location_ID() != 0) order.setC_BPartner_Location_ID(detail.getC_BPartner_Location_ID()); } + order.setM_Warehouse_ID(ws[0].getM_Warehouse_ID()); order.setDateOrdered(m_DateOrdered); order.setDatePromised(p_DatePromised); order.setIsInDispute(false); @@ -836,6 +1000,7 @@ public class DistributionRun extends SvrProcess line.setTargetQty(detail.getActualAllocation()); line.setQtyEntered(detail.getActualAllocation()); line.setConfirmedQty(detail.getActualAllocation()); + line.setDescription("Distribution Push"); //line.setQty(detail.getActualAllocation()); //line.setPrice(); if (!line.save()) diff --git a/base/src/org/eevolution/process/DistributionRunOrders.java b/base/src/org/eevolution/process/DistributionRunOrders.java index 9f7284f024..5f598f4d7a 100644 --- a/base/src/org/eevolution/process/DistributionRunOrders.java +++ b/base/src/org/eevolution/process/DistributionRunOrders.java @@ -39,9 +39,10 @@ import org.compiere.util.Msg; import org.compiere.util.Trx; /** - * MRPUpdate - * - * @author Victor Perez, e-Evolution, S.C. + * DistributionRun Orders + * @author victor.perez@e-evolution.com + *
  • FR Let use the Distribution List and Distribution Run for DO + * @see http://sourceforge.net/tracker/index.php?func=detail&aid=2030865&group_id=176962&atid=879335 */ public class DistributionRunOrders extends SvrProcess { @@ -52,17 +53,20 @@ public class DistributionRunOrders extends SvrProcess /** Date Promised */ private Timestamp p_DatePromised = null; + /** Date Promised */ + private Timestamp p_DatePromised_To = null; /** Organization */ private int p_AD_Org_ID = 0; /** Is Only Test */ private String p_IsTest = "N"; /** Warehouse */ private int p_M_Warehouse_ID = 0; - /** Create Ordered DO Yes Create No Update */ - private String p_CreateDO = "N"; + /** Create Distribution Order Consolidate */ + private String p_ConsolidateDocument = "N"; + /** Create Distribution Based in the DRP Demand */ + private String p_BasedInDamnd = "N"; private MDistributionRun m_run = null; - private int m_M_DistributionRun_ID = 0; /** @@ -78,23 +82,21 @@ public class DistributionRunOrders extends SvrProcess if (para[i].getParameter() == null) ; - else if (name.equals("AD_Org_ID")) - { - p_AD_Org_ID = ((BigDecimal)para[i].getParameter()).intValue(); - - } + else if (name.equals("AD_Org_ID")) + p_AD_Org_ID = ((BigDecimal)para[i].getParameter()).intValue(); else if (name.equals("M_Warehouse_ID")) - { p_M_Warehouse_ID = ((BigDecimal)para[i].getParameter()).intValue(); - } else if (name.equals("M_DistributionList_ID")) - { p_M_DistributionList_ID = ((BigDecimal)para[i].getParameter()).intValue(); - } else if (name.equals("DatePromised")) + { p_DatePromised = (Timestamp)para[i].getParameter(); - else if (name.equals("CreateDO")) - p_CreateDO = (String)para[i].getParameter(); + p_DatePromised_To = (Timestamp)para[i].getParameter_To(); + } + else if(name.equals("ConsolidateDocument")) + p_ConsolidateDocument = (String)para[i].getParameter(); + else if (name.equals("IsRequiredDRP")) + p_BasedInDamnd = (String)para[i].getParameter(); else if (name.equals("IsTest")) p_IsTest = (String)para[i].getParameter(); else @@ -109,27 +111,17 @@ public class DistributionRunOrders extends SvrProcess */ protected String doIt() throws Exception { - /*if(p_M_Warehouse_ID==0) - { - MWarehouse[] ws = MWarehouse.getForOrg(getCtx(), p_AD_Org_ID); - for(MWarehouse w : ws) - { - if(!deleteRecord(m_AD_Client_ID,p_AD_Org_ID,p_S_Resource_ID,w.getM_Warehouse_ID())) - throw new Exception(Msg.getMsg(getCtx(), "ProcessFailed"),CLogger.retrieveException()); - if(!createRecord(m_AD_Client_ID,p_AD_Org_ID,p_S_Resource_ID,w.getM_Warehouse_ID())) - throw new Exception(Msg.getMsg(getCtx(), "ProcessFailed"),CLogger.retrieveException()); - } - } - else - { - if(!deleteRecord(m_AD_Client_ID,p_AD_Org_ID,p_S_Resource_ID,p_M_Warehouse_ID)) - throw new Exception(Msg.getMsg(getCtx(), "ProcessFailed"),CLogger.retrieveException()); - if(!createRecord(m_AD_Client_ID,p_AD_Org_ID,p_S_Resource_ID,p_M_Warehouse_ID)) - throw new Exception(Msg.getMsg(getCtx(), "ProcessFailed"),CLogger.retrieveException()); - }*/ - - if(!generateDistribution()) + if(p_BasedInDamnd.equals("Y")) + { + if(!generateDistributionDemand()) throw new Exception(Msg.getMsg(getCtx(), "ProcessFailed"),CLogger.retrieveException()); + } + else + { + if(!generateDistribution()) + throw new Exception(Msg.getMsg(getCtx(), "ProcessFailed"),CLogger.retrieveException()); + } + if(!executeDistribution()) throw new Exception(Msg.getMsg(getCtx(), "ProcessFailed"),CLogger.retrieveException()); @@ -144,18 +136,16 @@ public class DistributionRunOrders extends SvrProcess //m_run.setDescription("Generate from DRP"); m_run.save(); - StringBuffer sql = new StringBuffer("SELECT M_Product_ID , SUM (QtyOrdered) AS TotalQty, l.M_Warehouse_ID FROM DD_OrderLine ol INNER JOIN M_Locator l ON (l.M_Locator_ID=ol.M_Locator_ID) INNER JOIN DD_Order o ON (o.DD_Order_ID=ol.DD_Order_ID) "); + StringBuffer sql = new StringBuffer("SELECT M_Product_ID , SUM (QtyOrdered-QtyDelivered) AS TotalQty, l.M_Warehouse_ID FROM DD_OrderLine ol INNER JOIN M_Locator l ON (l.M_Locator_ID=ol.M_Locator_ID) INNER JOIN DD_Order o ON (o.DD_Order_ID=ol.DD_Order_ID) "); sql.append(" WHERE o.DocStatus IN ('DR','IN') AND ol.DatePromised BETWEEN ? AND ? AND l.M_Warehouse_ID=? GROUP BY M_Product_ID, l.M_Warehouse_ID"); - PreparedStatement pstmt = null; ResultSet rs = null; - Timestamp today = new Timestamp (System.currentTimeMillis()); try { pstmt = DB.prepareStatement (sql.toString(),get_TrxName()); - pstmt.setTimestamp(1, today); - pstmt.setTimestamp(2, p_DatePromised); + pstmt.setTimestamp(1, p_DatePromised); + pstmt.setTimestamp(2, p_DatePromised_To); pstmt.setInt(3, p_M_Warehouse_ID); rs = pstmt.executeQuery(); @@ -182,6 +172,75 @@ public class DistributionRunOrders extends SvrProcess line += 10; } } + catch (Exception e) + { + log.log(Level.SEVERE,"doIt - " + sql, e); + return false; + } + finally + { + DB.close(rs, pstmt); + rs = null; + pstmt = null; + } + + return true; + } + + //Create Distribution Run Line + public boolean generateDistributionDemand() + { + m_run = new MDistributionRun(this.getCtx(), 0 , this.get_TrxName()); + m_run.setName("Generate from DRP " + p_DatePromised); + m_run.save(); + + StringBuffer sql = new StringBuffer("SELECT M_Product_ID , SUM (TargetQty) AS MinQty, SUM (QtyOrdered-QtyDelivered) AS TotalQty, l.M_Warehouse_ID FROM DD_OrderLine ol INNER JOIN M_Locator l ON (l.M_Locator_ID=ol.M_Locator_ID) INNER JOIN DD_Order o ON (o.DD_Order_ID=ol.DD_Order_ID) "); + sql.append(" WHERE o.DocStatus IN ('DR','IN') AND ol.DatePromised BETWEEN ? AND ? AND l.M_Warehouse_ID=? GROUP BY M_Product_ID, l.M_Warehouse_ID"); + + + PreparedStatement pstmt = null; + ResultSet rs = null; + try + { + pstmt = DB.prepareStatement (sql.toString(),get_TrxName()); + pstmt.setTimestamp(1, p_DatePromised); + pstmt.setTimestamp(2, p_DatePromised_To); + pstmt.setInt(3, p_M_Warehouse_ID); + + rs = pstmt.executeQuery(); + int line = 10; + while (rs.next()) + { + int M_Product_ID = rs.getInt("M_Product_ID"); + BigDecimal QtyAvailable = MStorage.getQtyAvailable(p_M_Warehouse_ID,0 , M_Product_ID , 0, get_TrxName()); + BigDecimal QtyToDistribute = rs.getBigDecimal("TotalQty"); + + MDistributionRunLine m_runLine = new MDistributionRunLine(getCtx(),0 ,get_TrxName()); + m_runLine.setM_DistributionRun_ID(m_run.getM_DistributionRun_ID()); + m_runLine.setAD_Org_ID(p_AD_Org_ID); + m_runLine.setM_DistributionList_ID(p_M_DistributionList_ID); + m_runLine.setLine(line); + m_runLine.setM_Product_ID(M_Product_ID); + m_runLine.setDescription(Msg.translate(getCtx(), "QtyAvailable") +" : " + QtyAvailable + " " +Msg.translate(getCtx(), "QtyOrdered") + " : " + QtyToDistribute); + //m_runLine.setMinQty(rs.getBigDecimal("MinQty")); + + BigDecimal QtyReserved = getQtyReserved(M_Product_ID); + + //if(QtyToDistribute.compareTo(QtyAvailable) > 0) + //{ + /*if(QtyReserved.compareTo(QtyAvailable) > 0) + { + QtyToDistribute = QtyAvailable; + } + else*/ + QtyToDistribute = QtyAvailable.subtract(QtyReserved); + //} + + m_runLine.setTotalQty(QtyToDistribute); + m_runLine.save(); + line += 10; + } + } catch (Exception e) { log.log(Level.SEVERE,"doIt - " + sql, e); @@ -196,14 +255,61 @@ public class DistributionRunOrders extends SvrProcess return true; } + /** + * Get Qty Reserved for a Warehouse + * @param M_Product_ID + * @return + */ + private BigDecimal getQtyReserved(int M_Product_ID) + { + StringBuffer sql = new StringBuffer("SELECT SUM (TargetQty) FROM DD_OrderLine ol INNER JOIN M_Locator l ON (l.M_Locator_ID=ol.M_Locator_ID) INNER JOIN DD_Order o ON (o.DD_Order_ID=ol.DD_Order_ID) "); + sql.append(" WHERE o.DocStatus IN ('DR','IN') AND ol.DatePromised BETWEEN ? AND ? AND l.M_Warehouse_ID=? AND ol.M_Product_ID=? GROUP BY M_Product_ID, l.M_Warehouse_ID"); + + + PreparedStatement pstmt = null; + ResultSet rs = null; + Timestamp today = new Timestamp (System.currentTimeMillis()); + try + { + pstmt = DB.prepareStatement (sql.toString(), get_TrxName()); + pstmt.setTimestamp(1, today); + pstmt.setTimestamp(2, p_DatePromised); + pstmt.setInt(3, p_M_Warehouse_ID); + pstmt.setInt(4, M_Product_ID); + rs = pstmt.executeQuery(); + while (rs.next()) + { + return rs.getBigDecimal(1); + } + } + catch (Exception e) + { + log.log(Level.SEVERE,"doIt - " + sql, e); + return Env.ZERO; + } + finally + { + DB.close(rs, pstmt); + rs = null; + pstmt = null; + } + + return Env.ZERO; + } + /** + * Execute Distribution Run + * @return + * @throws Exception + */ public boolean executeDistribution() throws Exception { int M_DocType_ID = 0; MDocType[] doc = MDocType.getOfDocBaseType(getCtx(), MDocType.DOCBASETYPE_DistributionOrder); - if (doc==null || doc.length == 0) { + if (doc==null || doc.length == 0) + { log.severe ("Not found default document type for docbasetype " + MDocType.DOCBASETYPE_DistributionOrder); throw new Exception(Msg.getMsg(getCtx(), "SequenceDocNotFound"),CLogger.retrieveException()); } @@ -215,8 +321,6 @@ public class DistributionRunOrders extends SvrProcess //Prepare Process int AD_Process_ID = 271; - - AD_Process_ID = MProcess.getProcess_ID("M_DistributionRun Create",get_TrxName()); MPInstance instance = new MPInstance(Env.getCtx(), AD_Process_ID, 0); @@ -226,7 +330,7 @@ public class DistributionRunOrders extends SvrProcess } //call process - ProcessInfo pi = new ProcessInfo ("CreateDistributionFromDRP", AD_Process_ID); + ProcessInfo pi = new ProcessInfo ("M_DistributionRun Orders", AD_Process_ID); pi.setAD_PInstance_ID (instance.getAD_PInstance_ID()); pi.setRecord_ID(m_run.getM_DistributionRun_ID()); // Add Parameter - Selection=Y @@ -236,18 +340,17 @@ public class DistributionRunOrders extends SvrProcess { String msg = "No Parameter added"; // not translated throw new Exception(msg,CLogger.retrieveException()); - } - + } // Add Parameter - DatePromised ip = new MPInstancePara(instance, 20); ip.setParameter("DatePromised", ""); ip.setP_Date(p_DatePromised); + ip.setP_Date_To(p_DatePromised_To); if (!ip.save()) { String msg = "No Parameter added"; // not translated throw new Exception(msg,CLogger.retrieveException()); - } - + } // Add Parameter - M_Warehouse_ID ip = new MPInstancePara(instance, 30); ip.setParameter("M_Warehouse_ID",p_M_Warehouse_ID); @@ -255,32 +358,43 @@ public class DistributionRunOrders extends SvrProcess { String msg = "No Parameter added"; // not translated throw new Exception(msg,CLogger.retrieveException()); - } - + } // Add Parameter - CreateDO ip = new MPInstancePara(instance, 40); - ip.setParameter("CreateDO",p_CreateDO); + ip.setParameter("ConsolidateDocument",p_ConsolidateDocument); if (!ip.save()) { String msg = "No Parameter added"; // not translated throw new Exception(msg,CLogger.retrieveException()); - } - + } // Add Parameter - IsTest=Y ip = new MPInstancePara(instance, 50); ip.setParameter("IsTest",p_IsTest); if (!ip.save()) + { + String msg = "No Parameter added"; // not translated + throw new Exception(msg,CLogger.retrieveException()); + } + //Distribution List + ip = new MPInstancePara(instance, 60); + ip.setParameter("M_DistributionList_ID",p_M_DistributionList_ID); + if (!ip.save()) + { + String msg = "No Parameter added"; // not translated + throw new Exception(msg,CLogger.retrieveException()); + } + //Based in DRP Demand + ip = new MPInstancePara(instance, 70); + ip.setParameter("IsRequiredDRP",p_BasedInDamnd); + if (!ip.save()) { String msg = "No Parameter added"; // not translated throw new Exception(msg,CLogger.retrieveException()); } - - // Execute Process MProcess worker = new MProcess(getCtx(),AD_Process_ID,get_TrxName()); worker.processIt(pi, Trx.get(get_TrxName(), true)); - //ProcessCtl worker = new ProcessCtl(this, 0 , pi, trx); - //worker.start(); // complete tasks in unlockUI / generateShipments_complete - return true; + m_run.delete(true); + return true; } }