diff --git a/base/src/org/compiere/wf/MWFNode.java b/base/src/org/compiere/wf/MWFNode.java index 6b651605c6..4c8dca6bd7 100644 --- a/base/src/org/compiere/wf/MWFNode.java +++ b/base/src/org/compiere/wf/MWFNode.java @@ -628,4 +628,20 @@ public class MWFNode extends X_AD_WF_Node return success; } // afterDelete + /** + * Check if the workflow node is valid for given date + * @param date + * @return true if valid + */ + public boolean isValidFromTo(Timestamp date) + { + Timestamp validFrom = getValidFrom(); + Timestamp validTo = getValidTo(); + + if (validFrom != null && date.before(validFrom)) + return false; + if (validTo != null && date.after(validTo)) + return false; + return true; + } } // M_WFNext diff --git a/base/src/org/compiere/wf/MWorkflow.java b/base/src/org/compiere/wf/MWorkflow.java index 68a5e45028..00a581415f 100644 --- a/base/src/org/compiere/wf/MWorkflow.java +++ b/base/src/org/compiere/wf/MWorkflow.java @@ -19,6 +19,7 @@ package org.compiere.wf; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Timestamp; import java.util.ArrayList; import java.util.Calendar; import java.util.Properties; @@ -934,4 +935,20 @@ public class MWorkflow extends X_AD_Workflow return DB.getSQLValue(null, sql, product.getValue(), AD_Client_ID); } + /** + * Check if the workflow is valid for given date + * @param date + * @return true if valid + */ + public boolean isValidFromTo(Timestamp date) + { + Timestamp validFrom = getValidFrom(); + Timestamp validTo = getValidTo(); + + if (validFrom != null && date.before(validFrom)) + return false; + if (validTo != null && date.after(validTo)) + return false; + return true; + } } // MWorkflow_ID diff --git a/base/src/org/eevolution/model/MPPMRP.java b/base/src/org/eevolution/model/MPPMRP.java index f7f5c051e0..69cc7b2263 100644 --- a/base/src/org/eevolution/model/MPPMRP.java +++ b/base/src/org/eevolution/model/MPPMRP.java @@ -28,9 +28,9 @@ import org.compiere.model.MRequisition; import org.compiere.model.MRequisitionLine; import org.compiere.model.MResource; import org.compiere.model.MResourceType; -import org.compiere.model.MSequence; import org.compiere.model.MTable; import org.compiere.model.MWarehouse; +import org.compiere.model.Query; import org.compiere.model.X_C_DocType; import org.compiere.model.X_M_Forecast; import org.compiere.model.X_M_ForecastLine; @@ -45,9 +45,13 @@ import org.compiere.wf.MWorkflow; * * @author Victor Perez www.e-evolution.com * @version $Id: MPPMRP.java,v 1.4 2004/05/13 06:05:22 vpj-cd Exp $ + * + * @author Teo Sarca, www.arhipac.ro */ public class MPPMRP extends X_PP_MRP { + private static final long serialVersionUID = 1L; + /************************************************************************** * Default Constructor * @param ctx context @@ -86,7 +90,7 @@ public class MPPMRP extends X_PP_MRP * @param X_M_ForecastLine Forecast Line * @param delete Indicate if this record is delete */ - public static void M_ForecastLine(X_M_ForecastLine fl,boolean delete) + public static void M_ForecastLine(X_M_ForecastLine fl, boolean delete) { String sql = null; String trxName = fl.get_TrxName(); @@ -94,13 +98,12 @@ public class MPPMRP extends X_PP_MRP if (delete) { sql = "DELETE FROM PP_MRP WHERE M_ForecastLine_ID = "+ fl.getM_ForecastLine_ID() +" AND AD_Client_ID = " + fl.getAD_Client_ID(); - - DB.executeUpdate(sql, trxName); + DB.executeUpdateEx(sql, trxName); return; } - MWarehouse[] w = MWarehouse.getForOrg(m_ctx,fl.getAD_Org_ID()); - X_M_Forecast f = new X_M_Forecast(m_ctx,fl.getM_Forecast_ID(), trxName); + MWarehouse[] w = MWarehouse.getForOrg(m_ctx, fl.getAD_Org_ID()); + X_M_Forecast f = new X_M_Forecast(m_ctx, fl.getM_Forecast_ID(), trxName); String WhereClause = "M_ForecastLine_ID=?"; MPPMRP mrp = (MPPMRP)MTable.get(m_ctx, MPPMRP.Table_ID).getPO(WhereClause, new Object[]{fl.getM_ForecastLine_ID()}, trxName); if(mrp!=null) @@ -169,58 +172,31 @@ public class MPPMRP extends X_PP_MRP */ public static void C_OrderLine(MOrderLine ol, boolean delete) { - Properties m_ctx = ol.getCtx(); - String sql = null; - String trxName = ol.getParent().get_TrxName(); + Properties ctx = ol.getCtx(); + String trxName = ol.get_TrxName(); + final String whereClause = "AD_Client_ID = ? AND C_OrderLine_ID = ?"; + Object[] params = new Object[]{ol.getAD_Client_ID(),ol.getC_OrderLine_ID()}; if (delete) { - sql = "DELETE FROM PP_MRP WHERE C_OrderLine_ID = "+ ol.getC_OrderLine_ID() +" AND AD_Client_ID = " + ol.getAD_Client_ID(); - DB.executeUpdate(sql ,trxName); - int PP_Order_ID = DB.getSQLValue(trxName,"SELECT PP_Order_ID FROM PP_Order o WHERE o.AD_Client_ID = ? AND o.C_OrderLine_ID = ? ", ol.getAD_Client_ID(),ol.getC_OrderLine_ID()); - if (PP_Order_ID != -1 ) + DB.executeUpdateEx("DELETE FROM PP_MRP WHERE "+whereClause, params, trxName); + MPPOrder order = new Query(ctx, MPPOrder.Table_Name, whereClause, trxName) + .setParameters(params) + .first(); + if (order != null && !order.isProcessed()) { - MPPOrder order = new MPPOrder(m_ctx, PP_Order_ID,trxName); - if (!MPPOrder.DOCSTATUS_Completed.equals(order.getDocStatus()) - && !MPPOrder.DOCSTATUS_Closed.equals(order.getDocStatus()) - ) - { - order.deleteEx(true, trxName); - } + order.deleteEx(true, trxName); } return; } - String WhereClause = "AD_Client_ID = ? AND C_OrderLine_ID = ?"; - MPPMRP mrp = (MPPMRP)MTable.get(m_ctx, MPPMRP.Table_ID) - .getPO(WhereClause, new Object[]{ol.getAD_Client_ID(),ol.getC_OrderLine_ID()}, trxName); - if(mrp != null) + + MPPMRP mrp = new Query(ctx, MPPMRP.Table_Name, whereClause, trxName) + .setParameters(params) + .first(); + if(mrp == null) { - mrp.setDescription(ol.getDescription()); - mrp.setName("MRP"); - mrp.setQty(ol.getQtyOrdered().subtract(ol.getQtyDelivered())); - mrp.setDatePromised(ol.getDatePromised()); - mrp.setDateStartSchedule(ol.getDatePromised()); - mrp.setDateFinishSchedule(ol.getDatePromised()); - mrp.setDateOrdered(ol.getDateOrdered()); - mrp.setM_Product_ID(ol.getM_Product_ID()); - mrp.setM_Warehouse_ID(ol.getM_Warehouse_ID()); - mrp.setDocStatus(ol.getParent().getDocStatus()); - mrp.saveEx(); - } - else - { - mrp = new MPPMRP(m_ctx, 0,trxName); - mrp.setC_OrderLine_ID(ol.getC_OrderLine_ID()); - mrp.setName("MRP"); - mrp.setDescription(ol.getDescription()); + mrp = new MPPMRP(ctx, 0,trxName); mrp.setC_Order_ID(ol.getC_Order_ID()); - mrp.setQty(ol.getQtyOrdered().subtract(ol.getQtyDelivered())); - mrp.setDatePromised(ol.getDatePromised()); - mrp.setDateStartSchedule(ol.getDatePromised()); - mrp.setDateFinishSchedule(ol.getDatePromised()); - mrp.setDateOrdered(ol.getDateOrdered()); - mrp.setM_Product_ID(ol.getM_Product_ID()); - mrp.setM_Warehouse_ID(ol.getM_Warehouse_ID()); - mrp.setDocStatus(ol.getParent().getDocStatus()); + mrp.setC_OrderLine_ID(ol.getC_OrderLine_ID()); if (ol.getParent().isSOTrx()) { mrp.setOrderType(MPPMRP.ORDERTYPE_SalesOrder); @@ -231,36 +207,46 @@ public class MPPMRP extends X_PP_MRP mrp.setOrderType(MPPMRP.ORDERTYPE_PurchaseOrder); mrp.setTypeMRP(MPPMRP.TYPEMRP_Supply); } - mrp.saveEx(); } + mrp.setDescription(ol.getDescription()); + mrp.setName("MRP"); + mrp.setQty(ol.getQtyOrdered().subtract(ol.getQtyDelivered())); + mrp.setDatePromised(ol.getDatePromised()); + mrp.setDateStartSchedule(ol.getDatePromised()); + mrp.setDateFinishSchedule(ol.getDatePromised()); + mrp.setDateOrdered(ol.getDateOrdered()); + mrp.setM_Product_ID(ol.getM_Product_ID()); + mrp.setM_Warehouse_ID(ol.getM_Warehouse_ID()); + mrp.setDocStatus(ol.getParent().getDocStatus()); + mrp.saveEx(); - MPPOrder order = (MPPOrder) MTable.get(m_ctx, MPPOrder.Table_ID).getPO("AD_Client_ID = ? AND C_OrderLine_ID = ? ", new Object[]{ol.getAD_Client_ID(),ol.getC_OrderLine_ID()},trxName); - if (order == null ) + MPPOrder order = new Query(ctx, MPPOrder.Table_Name, whereClause, trxName) + .setParameters(params) + .first(); + if (order == null) { - MProduct product = MProduct.get(m_ctx,ol.getM_Product_ID()); - WhereClause = "AD_Client_ID = ? AND Value = ?"; - MPPProductBOM bom = (MPPProductBOM) MTable.get(m_ctx, MPPProductBOM.Table_ID).getPO(WhereClause,new Object[]{ ol.getAD_Client_ID(),product.getValue()}, trxName); + MProduct product = MProduct.get(ctx,ol.getM_Product_ID()); + MPPProductBOM bom = MPPProductBOM.getDefault(product, trxName); if (bom != null) { if (bom.getBOMType().equals(MPPProductBOM.BOMTYPE_Make_To_Order)) { - WhereClause = "ManufacturingResourceType = 'PT' AND IsManufacturingResource = 'Y' AND AD_Client_ID = ? AND M_Warehouse_ID = ?"; - MResource m_resource = (MResource)MTable.get(m_ctx,MResource.Table_ID).getPO(WhereClause, new Object[]{ ol.getAD_Client_ID(),ol.getM_Warehouse_ID()}, trxName); - WhereClause = "AD_Client_ID = ? AND Value = ?"; - MWorkflow m_workflow = (MWorkflow)MTable.get(m_ctx,MWorkflow.Table_ID).getPO(WhereClause, new Object[]{ ol.getAD_Client_ID(),product.getValue()}, trxName); + String WhereClause = "ManufacturingResourceType = 'PT' AND IsManufacturingResource = 'Y' AND AD_Client_ID = ? AND M_Warehouse_ID = ?"; + MResource m_resource = (MResource)MTable.get(ctx,MResource.Table_ID).getPO(WhereClause, new Object[]{ ol.getAD_Client_ID(),ol.getM_Warehouse_ID()}, trxName); + MWorkflow m_workflow = MWorkflow.get(ctx, MWorkflow.getWorkflowSearchKey(ctx, product)); if (m_resource != null && m_workflow != null) { - MDocType[] doc = MDocType.getOfDocBaseType(m_ctx,X_C_DocType.DOCBASETYPE_ManufacturingOrder); - int C_DocType_ID = doc[0].getC_DocType_ID(); - order = new MPPOrder(m_ctx, 0 , trxName); + MDocType[] doc = MDocType.getOfDocBaseType(ctx, X_C_DocType.DOCBASETYPE_ManufacturingOrder); + int C_DocType_ID = doc[0].getC_DocType_ID(); + + order = new MPPOrder(ctx, 0 , trxName); order.setC_OrderLine_ID(ol.getC_OrderLine_ID()); - order.setDocumentNo(MSequence.getDocumentNo(C_DocType_ID,trxName,true)); - order.setS_Resource_ID(m_resource.getS_Resource_ID()); + order.setS_Resource_ID(m_resource.get_ID()); order.setM_Warehouse_ID(ol.getM_Warehouse_ID()); order.setM_Product_ID(ol.getM_Product_ID()); order.setM_AttributeSetInstance_ID(ol.getM_AttributeSetInstance_ID()); - order.setPP_Product_BOM_ID(bom.getPP_Product_BOM_ID()); - order.setAD_Workflow_ID(m_workflow.getAD_Workflow_ID()); + order.setPP_Product_BOM_ID(bom.get_ID()); + order.setAD_Workflow_ID(m_workflow.get_ID()); //order.setPlanner_ID(SupplyPlanner_ID); order.setLine(10); order.setQtyDelivered(Env.ZERO); @@ -268,7 +254,7 @@ public class MPPMRP extends X_PP_MRP order.setQtyScrap(Env.ZERO); order.setDateOrdered(ol.getDateOrdered()); order.setDatePromised(ol.getDatePromised()); - order.setDateStartSchedule(TimeUtil.addDays(ol.getDatePromised(), (MPPMRP.getDays(m_ctx,m_resource.getS_Resource_ID(),m_workflow.getAD_Workflow_ID(), ol.getQtyOrdered(),ol.get_TrxName())).negate().intValue())); + order.setDateStartSchedule(TimeUtil.addDays(ol.getDatePromised(), (MPPMRP.getDays(ctx,m_resource.getS_Resource_ID(),m_workflow.getAD_Workflow_ID(), ol.getQtyOrdered(),ol.get_TrxName())).negate().intValue())); order.setDateFinishSchedule(ol.getDatePromised()); order.setQtyEntered(ol.getQtyEntered()); order.setQtyOrdered(ol.getQtyOrdered()); @@ -278,18 +264,17 @@ public class MPPMRP extends X_PP_MRP order.setC_DocTypeTarget_ID(C_DocType_ID); order.setC_DocType_ID(C_DocType_ID); order.setPriorityRule(MPPOrder.PRIORITYRULE_High); - order.save(trxName); + order.saveEx(); order.prepareIt(); order.setDocAction(MPPOrder.DOCSTATUS_Completed); order.saveEx(); } - } + } } } else { - - if (MPPOrder.DOCSTATUS_Completed != order.getDocStatus() || MPPOrder.DOCSTATUS_Closed != order.getDocStatus()) + if (!order.isProcessed()) { order.setQtyEntered(ol.getQtyEntered()); order.setDatePromised(ol.getDatePromised()); @@ -314,47 +299,32 @@ public class MPPMRP extends X_PP_MRP if (delete) { sql = "DELETE FROM PP_MRP WHERE PP_Order_ID = "+ o.getPP_Order_ID() +" AND AD_Client_ID = " + o.getAD_Client_ID(); - DB.executeUpdate(sql ,trxName); + DB.executeUpdateEx(sql ,trxName); return; } - String WhereClause = "TypeMRP = 'S' AND OrderType='MOP' AND AD_Client_ID=? AND PP_Order_ID = ?"; - MPPMRP mrp = (MPPMRP)MTable.get(m_ctx,MPPMRP.Table_ID).getPO(WhereClause, new Object[]{o.getAD_Client_ID() , o.getPP_Order_ID()}, trxName); - if(mrp != null) + String whereClause = "TypeMRP=? AND OrderType=? AND AD_Client_ID=? AND PP_Order_ID=?"; + MPPMRP mrp = new Query(m_ctx, MPPMRP.Table_Name, whereClause, trxName) + .setParameters(new Object[]{MPPMRP.TYPEMRP_Supply, MPPMRP.ORDERTYPE_ManufacturingOrder, o.getAD_Client_ID() , o.getPP_Order_ID()}) + .first(); + if(mrp == null) { - mrp.setDescription(o.getDescription()); - mrp.setName(o.getDocumentNo()); - mrp.setQty(o.getQtyOrdered().subtract(o.getQtyDelivered())); - mrp.setDatePromised(o.getDatePromised()); - mrp.setDateOrdered(o.getDateOrdered()); - mrp.setDateStartSchedule(o.getDateStartSchedule()); - mrp.setDateFinishSchedule(o.getDateFinishSchedule()); - mrp.setM_Product_ID(o.getM_Product_ID()); - mrp.setM_Warehouse_ID(o.getM_Warehouse_ID()); - mrp.setS_Resource_ID(o.getS_Resource_ID()); - mrp.setDocStatus(o.getDocStatus()); - mrp.saveEx(); - } - else - { mrp = new MPPMRP(m_ctx, 0, trxName); mrp.setPP_Order_ID(o.getPP_Order_ID()); - mrp.setDescription(o.getDescription()); - mrp.setName(o.getDocumentNo()); - mrp.setQty(o.getQtyOrdered().subtract(o.getQtyDelivered())); - mrp.setDatePromised(o.getDatePromised()); - mrp.setDateOrdered(o.getDateOrdered()); - mrp.setDateStartSchedule(o.getDateStartSchedule()); - mrp.setDateFinishSchedule(o.getDateStartSchedule()); - mrp.setM_Product_ID(o.getM_Product_ID()); - mrp.setM_Warehouse_ID(o.getM_Warehouse_ID()); - mrp.setS_Resource_ID(o.getS_Resource_ID()); - mrp.setOrderType(MPPMRP.ORDERTYPE_ManufacturingOrder); mrp.setTypeMRP(MPPMRP.TYPEMRP_Supply); - mrp.setDocStatus(o.getDocStatus()); - mrp.saveEx(); - } - - return ; + mrp.setOrderType(MPPMRP.ORDERTYPE_ManufacturingOrder); + } + mrp.setDescription(o.getDescription()); + mrp.setName(o.getDocumentNo()); + mrp.setQty(o.getQtyOrdered().subtract(o.getQtyDelivered())); + mrp.setDatePromised(o.getDatePromised()); + mrp.setDateOrdered(o.getDateOrdered()); + mrp.setDateStartSchedule(o.getDateStartSchedule()); + mrp.setDateFinishSchedule(o.getDateStartSchedule()); + mrp.setM_Product_ID(o.getM_Product_ID()); + mrp.setM_Warehouse_ID(o.getM_Warehouse_ID()); + mrp.setS_Resource_ID(o.getS_Resource_ID()); + mrp.setDocStatus(o.getDocStatus()); + mrp.saveEx(); } /** @@ -364,56 +334,40 @@ public class MPPMRP extends X_PP_MRP */ public static void PP_Order_BOMLine(MPPOrderBOMLine obl,boolean delete) { - String sql = null; - String trxName = obl.getParent().get_TrxName(); - Properties m_ctx = obl.getCtx(); + String trxName = obl.get_TrxName(); + Properties ctx = obl.getCtx(); if (delete) { - sql = "DELETE FROM PP_MRP WHERE PP_Order_BOMLine_ID = "+ obl.getPP_Order_BOMLine_ID() +" AND AD_Client_ID = " + obl.getAD_Client_ID(); - DB.executeUpdate(sql ,trxName); + final String sql = "DELETE FROM PP_MRP WHERE PP_Order_BOMLine_ID=? AND AD_Client_ID=?"; + DB.executeUpdateEx(sql, new Object[]{obl.get_ID(), obl.getAD_Client_ID()}, trxName); return; } - String WhereClause = "TypeMRP = 'D' AND OrderType='MOP' AND AD_Client_ID=? AND PP_Order_BOMLine_ID = ? "; - MPPMRP mrp = (MPPMRP)MTable.get(m_ctx, MPPMRP.Table_ID).getPO(WhereClause, new Object[]{obl.getAD_Client_ID(),obl.getPP_Order_BOMLine_ID()}, trxName); + final String whereClause = "TypeMRP=? AND OrderType=? AND PP_Order_BOMLine_ID=? AND AD_Client_ID=?"; + MPPMRP mrp = new Query(ctx, MPPMRP.Table_Name, whereClause, trxName) + .setParameters(new Object[]{TYPEMRP_Demand, ORDERTYPE_ManufacturingOrder, obl.get_ID(), obl.getAD_Client_ID()}) + .first(); MPPOrder o = obl.getParent(); - if(mrp!=null) + if(mrp == null) { - mrp.setName(o.getDocumentNo()); - mrp.setDescription(o.getDescription()); - mrp.setQty(obl.getQtyRequiered().subtract(obl.getQtyDelivered())); - mrp.setDatePromised(o.getDatePromised()); - mrp.setDateOrdered(o.getDateOrdered()); - mrp.setDateStartSchedule(o.getDateStartSchedule()); - mrp.setDateFinishSchedule(o.getDateFinishSchedule()); - mrp.setM_Product_ID(obl.getM_Product_ID()); - mrp.setM_Warehouse_ID(obl.getM_Warehouse_ID()); - mrp.setS_Resource_ID(o.getS_Resource_ID()); - mrp.setDocStatus(o.getDocStatus()); - mrp.saveEx(); - } - else - { - mrp = new MPPMRP(m_ctx , 0,trxName); + mrp = new MPPMRP(ctx, 0, trxName); mrp.setPP_Order_BOMLine_ID(obl.getPP_Order_BOMLine_ID()); - mrp.setName(o.getDocumentNo()); - mrp.setDescription(o.getDescription()); mrp.setPP_Order_ID(o.getPP_Order_ID()); - mrp.setQty(obl.getQtyRequiered().subtract(obl.getQtyDelivered())); - mrp.setDatePromised(o.getDatePromised()); - mrp.setDateOrdered(o.getDateOrdered()); - mrp.setDateStartSchedule(o.getDateStartSchedule()); - mrp.setDateFinishSchedule(o.getDateFinishSchedule()); - mrp.setM_Product_ID(obl.getM_Product_ID()); - mrp.setM_Warehouse_ID(obl.getM_Warehouse_ID()); - mrp.setS_Resource_ID(o.getS_Resource_ID()); - mrp.setDocStatus(o.getDocStatus()); mrp.setOrderType(MPPMRP.ORDERTYPE_ManufacturingOrder); mrp.setTypeMRP(MPPMRP.TYPEMRP_Demand); - mrp.saveEx(); - } - return; + mrp.setName(o.getDocumentNo()); + mrp.setDescription(o.getDescription()); + mrp.setQty(obl.getQtyRequiered().subtract(obl.getQtyDelivered())); + mrp.setDatePromised(o.getDatePromised()); + mrp.setDateOrdered(o.getDateOrdered()); + mrp.setDateStartSchedule(o.getDateStartSchedule()); + mrp.setDateFinishSchedule(o.getDateFinishSchedule()); + mrp.setM_Product_ID(obl.getM_Product_ID()); + mrp.setM_Warehouse_ID(obl.getM_Warehouse_ID()); + mrp.setS_Resource_ID(o.getS_Resource_ID()); + mrp.setDocStatus(o.getDocStatus()); + mrp.saveEx(); } /** @@ -428,7 +382,7 @@ public class MPPMRP extends X_PP_MRP if (delete) { sql = "DELETE FROM PP_MRP WHERE DD_Order_ID = "+ o.getDD_Order_ID() +" AND AD_Client_ID = " + o.getAD_Client_ID(); - DB.executeUpdate(sql ,trxName); + DB.executeUpdateEx(sql, trxName); return; } } @@ -446,7 +400,7 @@ public class MPPMRP extends X_PP_MRP if (delete) { sql = "DELETE FROM PP_MRP WHERE DD_OrderLine_ID = "+ ol.getDD_OrderLine_ID() +" AND AD_Client_ID = " + ol.getAD_Client_ID(); - DB.executeUpdate(sql ,trxName); + DB.executeUpdateEx(sql ,trxName); return; } String whereClause = "TypeMRP = 'D' AND OrderType='DOO' AND AD_Client_ID=? AND DD_OrderLine_ID = ?"; @@ -529,54 +483,41 @@ public class MPPMRP extends X_PP_MRP */ public static void M_RequisitionLine( MRequisitionLine rl , boolean delete) { - String sql = null; String trxName = rl.get_TrxName(); - Properties m_ctx = rl.getCtx(); + Properties ctx = rl.getCtx(); + final String whereClause = "M_RequisitionLine_ID=? AND AD_Client_ID=?"; + Object[] params = new Object[]{rl.getM_RequisitionLine_ID(), rl.getAD_Client_ID()}; + if (delete) { - sql = "DELETE FROM PP_MRP WHERE M_RequisitionLine_ID = "+ rl.getM_RequisitionLine_ID() +" AND AD_Client_ID = " + rl.getAD_Client_ID(); - DB.executeUpdate(sql,trxName); //reorder by hamed + DB.executeUpdateEx("DELETE FROM PP_MRP WHERE "+whereClause, params, trxName); return; } - String whereClause = new String("AD_Client_ID=? AND M_RequisitionLine_ID = ? "); - MPPMRP mrp = (MPPMRP)MTable.get(m_ctx, MPPMRP.Table_ID).getPO(whereClause, new Object[]{rl.getAD_Client_ID(),rl.getM_RequisitionLine_ID()}, trxName); - MRequisition r = new MRequisition(m_ctx, rl.getM_Requisition_ID(),trxName); - if(mrp!=null) - { - mrp.setName("MRP"); - mrp.setDescription(rl.getDescription()); - mrp.setQty(rl.getQty()); - mrp.setDatePromised(r.getDateRequired()); - mrp.setDateStartSchedule(r.getDateRequired()); - mrp.setDateFinishSchedule(r.getDateRequired()); - mrp.setDateOrdered(r.getDateRequired()); - mrp.setM_Product_ID(rl.getM_Product_ID()); - mrp.setM_Warehouse_ID(r.getM_Warehouse_ID()); - mrp.setDocStatus(r.getDocStatus()); - mrp.saveEx(); - } - else - { - mrp = new MPPMRP(m_ctx, 0,trxName); + MPPMRP mrp = new Query(ctx, MPPMRP.Table_Name, whereClause, trxName) + .setParameters(params) + .first(); + MRequisition r = rl.getParent(); + if (mrp == null) + { + mrp = new MPPMRP(ctx, 0, trxName); mrp.setM_Requisition_ID(rl.getM_Requisition_ID()); mrp.setM_RequisitionLine_ID(rl.getM_RequisitionLine_ID()); - mrp.setName("MRP"); - mrp.setDescription(rl.getDescription()); - mrp.setQty(rl.getQty()); - mrp.setDatePromised(r.getDateRequired()); - mrp.setDateOrdered(r.getDateRequired()); - mrp.setDateStartSchedule(r.getDateRequired()); - mrp.setDateFinishSchedule(r.getDateRequired()); - mrp.setM_Product_ID(rl.getM_Product_ID()); - mrp.setM_Warehouse_ID(r.getM_Warehouse_ID()); - mrp.setDocStatus(r.getDocStatus()); mrp.setOrderType(MPPMRP.ORDERTYPE_MaterialRequisition); mrp.setTypeMRP(MPPMRP.TYPEMRP_Supply); mrp.setIsAvailable(true); - mrp.saveEx(); } - return; + mrp.setName("MRP"); + mrp.setDescription(rl.getDescription()); + mrp.setQty(rl.getQty()); + mrp.setDatePromised(r.getDateRequired()); + mrp.setDateStartSchedule(r.getDateRequired()); + mrp.setDateFinishSchedule(r.getDateRequired()); + mrp.setDateOrdered(r.getDateRequired()); + mrp.setM_Product_ID(rl.getM_Product_ID()); + mrp.setM_Warehouse_ID(r.getM_Warehouse_ID()); + mrp.setDocStatus(r.getDocStatus()); + mrp.saveEx(); } /** @@ -586,17 +527,15 @@ public class MPPMRP extends X_PP_MRP */ public static void M_Requisition(MRequisition r) { - - String trxName = r.get_TrxName(); - String WhereClause = "AD_Client_ID=? AND M_Requisition_ID=?"; - MPPMRP mrp = (MPPMRP)MTable.get(r.getCtx(),MRequisition.Table_ID).getPO(WhereClause, new Object[]{r.getAD_Client_ID(), r.getM_Requisition_ID()},trxName); - if(mrp!=null) + final String whereClause = "M_Requisition_ID=? AND AD_Client_ID=?"; + MPPMRP mrp = new Query(r.getCtx(), MPPMRP.Table_Name, whereClause, r.get_TrxName()) + .setParameters(new Object[]{r.getM_Requisition_ID(), r.getAD_Client_ID()}) + .first(); + if(mrp != null) { - mrp.setDocStatus(r.getDocStatus()); - mrp.saveEx(); + mrp.setDocStatus(r.getDocStatus()); + mrp.saveEx(); } - return; - } @@ -609,8 +548,9 @@ public class MPPMRP extends X_PP_MRP */ public static BigDecimal getQtyOnHand(int AD_Client_ID, int M_Warehouse_ID ,int M_Product_ID,String trxName) { - String sql = "SELECT SUM(bomQtyOnHand (M_Product_ID ,"+M_Warehouse_ID+",0)) AS OnHand FROM M_Product WHERE AD_Client_ID=? AND M_Product_ID=?"; - BigDecimal QtyOnHand = DB.getSQLValueBD(trxName, sql, new Object[]{AD_Client_ID,M_Product_ID}); + final String sql = "SELECT SUM(bomQtyOnHand (M_Product_ID,?,0)) AS OnHand FROM M_Product" + +" WHERE AD_Client_ID=? AND M_Product_ID=?"; + BigDecimal QtyOnHand = DB.getSQLValueBD(trxName, sql, new Object[]{M_Warehouse_ID,AD_Client_ID,M_Product_ID}); if (QtyOnHand == null) QtyOnHand = Env.ZERO; return QtyOnHand; @@ -624,12 +564,11 @@ public class MPPMRP extends X_PP_MRP */ public static int getMaxLowLevel(Properties ctx, String trxName) { - int LowLevel = 0; int AD_Client_ID = Env.getAD_Client_ID(ctx); // - String sql = "SELECT MAX("+MProduct.COLUMNNAME_LowLevel+") FROM M_Product" - +" WHERE AD_Client_ID=? AND "+MProduct.COLUMNNAME_LowLevel+" IS NOT NULL"; - LowLevel = DB.getSQLValue(trxName, sql, AD_Client_ID); + final String sql = "SELECT MAX("+MProduct.COLUMNNAME_LowLevel+") FROM M_Product" + +" WHERE AD_Client_ID=? AND "+MProduct.COLUMNNAME_LowLevel+" IS NOT NULL"; + int LowLevel = DB.getSQLValue(trxName, sql, AD_Client_ID); return LowLevel + 1; } diff --git a/base/src/org/eevolution/model/MPPOrder.java b/base/src/org/eevolution/model/MPPOrder.java index b9a6544ff2..f8540972d4 100644 --- a/base/src/org/eevolution/model/MPPOrder.java +++ b/base/src/org/eevolution/model/MPPOrder.java @@ -17,14 +17,14 @@ package org.eevolution.model; import java.io.File; import java.math.BigDecimal; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Timestamp; import java.util.List; import java.util.Properties; -import java.util.logging.Level; +import org.adempiere.exceptions.AdempiereException; import org.compiere.model.MAcctSchema; +import org.compiere.model.MClient; import org.compiere.model.MCost; import org.compiere.model.MDocType; import org.compiere.model.MProduct; @@ -36,6 +36,7 @@ import org.compiere.model.MWarehouse; import org.compiere.model.ModelValidationEngine; import org.compiere.model.ModelValidator; import org.compiere.model.PO; +import org.compiere.model.POResultSet; import org.compiere.model.Query; import org.compiere.model.X_C_DocType; import org.compiere.print.ReportEngine; @@ -57,10 +58,12 @@ import org.compiere.wf.MWorkflow; * @author Victor Perez www.e-evolution.com * @version $Id: MOrder.java,v 1.57 2004/05/21 02:27:38 vpj-cd Exp $ * - * @author Teo Sarca, SC ARHIPAC SERVICE SRL - *
  • BF [ 2041819 ] Can't delete PP Order + * @author Teo Sarca, www.arhipac.ro */ -public class MPPOrder extends X_PP_Order implements DocAction { +public class MPPOrder extends X_PP_Order implements DocAction +{ + private static final long serialVersionUID = 1L; + /** * Create new Order by copying * @param ctx context @@ -69,7 +72,9 @@ public class MPPOrder extends X_PP_Order implements DocAction { * @param counter create counter links * @return Order */ - public static MPPOrder copyFrom(MPPOrder from, Timestamp dateDoc, int C_DocTypeTarget_ID, boolean isSOTrx, boolean counter) { + public static MPPOrder copyFrom(MPPOrder from, Timestamp dateDoc, int C_DocTypeTarget_ID, + boolean isSOTrx, boolean counter) + { MPPOrder to = new MPPOrder(from.getCtx(), 0, "PP_Order"); PO.copyValues(from, to, from.getAD_Client_ID(), from.getAD_Org_ID()); to.setPP_Order_ID(0); @@ -109,7 +114,7 @@ public class MPPOrder extends X_PP_Order implements DocAction { to.setRef_Order_ID(0); // */ - if (!to.save()) throw new IllegalStateException("Could not create Order"); + to.saveEx(); /*if (counter) from.setRef_Order_ID(to.getC_Order_ID()); @@ -124,7 +129,8 @@ public class MPPOrder extends X_PP_Order implements DocAction { * @param ctx context * @param C_Order_ID order to load, (0 create new order) */ - public MPPOrder(Properties ctx, int PP_Order_ID, String trxName) { + public MPPOrder(Properties ctx, int PP_Order_ID, String trxName) + { super(ctx, PP_Order_ID, trxName); // New if (PP_Order_ID == 0) { @@ -136,12 +142,14 @@ public class MPPOrder extends X_PP_Order implements DocAction { setProcessing(false); setPosted(false); MDocType[] doc = MDocType.getOfDocBaseType(getCtx(), MDocType.DOCBASETYPE_ManufacturingOrder); - if(doc == null) - throw new IllegalArgumentException ("C_DocType_ID is mandatory."); + if(doc == null) + { + throw new IllegalArgumentException ("C_DocType_ID is mandatory."); + } else { - setC_DocType_ID(doc[0].getC_DocType_ID()); - setC_DocTypeTarget_ID(doc[0].getC_DocType_ID()); + setC_DocType_ID(doc[0].getC_DocType_ID()); + setC_DocTypeTarget_ID(doc[0].getC_DocType_ID()); } //set_ValueNoCheck("DocumentNo", null); @@ -155,7 +163,8 @@ public class MPPOrder extends X_PP_Order implements DocAction { * @param project Project to create Order from * @param DocSubTypeSO if SO DocType Target (default DocSubTypeSO_OnCredit) */ - public MPPOrder(MProject project, int PP_Product_BOM_ID,int AD_Workflow_ID) { + public MPPOrder(MProject project, int PP_Product_BOM_ID,int AD_Workflow_ID) + { this(project.getCtx(), 0, project.get_TrxName()); setAD_Client_ID(project.getAD_Client_ID()); setAD_Org_ID(project.getAD_Org_ID()); @@ -202,53 +211,11 @@ public class MPPOrder extends X_PP_Order implements DocAction { * @param ctx context * @param rs result set record */ - public MPPOrder(Properties ctx, ResultSet rs, String trxName) { + public MPPOrder(Properties ctx, ResultSet rs, String trxName) + { super(ctx, rs, trxName); } // MOrder - /************************************************************************** - * Default Constructor - * @param ctx context - * @param C_Order_ID order to load, (0 create new order) - */ - /* - public MPPOrder(Properties ctx, org.compiere.model.MOrderLine line ,int S_Resource_ID, int PP_Product_BOM_ID, int AD_Workflow_ID , String trxName) - - { - super(ctx, 0, trxName); - setLine(line.getLine()); - - //MProduct product = MProduct.get(getCtx(),line.getM_Product_ID()); - //int S_Resource_ID = DB.getSQLValue(trxName,"SELECT S_Resource_ID FROM S_Resource r WHERE r.ManufacturingResourceType = 'PL' AND s.IsManufacturingResource = 'Y' AND r.AD_Client_ID = ? AND r.M_Warehouse_ID = ? LIMIT 1", getAD_Client_ID(),line.getM_Warehouse_ID()); - //int PP_Product_BOM_ID = DB.getSQLValue(trxName,"SELECT PP_Product_BOM_ID FROM PP_Product_BOM bom WHERE bom.AD_Client_ID = ? AND bom.Value = ? ", getAD_Client_ID(),product.getValue()); - //int AD_Workflow_ID = DB.getSQLValue(trxName,"SELECT AD_Workflow_ID FROM AD_Workflow wf WHERE wf.AD_Client_ID = ? AND bom.Value = ? ", getAD_Client_ID(),product.getValue()); - int SupplyPlanner_ID = 0; - setS_Resource_ID(S_Resource_ID); - setM_Warehouse_ID(line.getM_Warehouse_ID()); - setM_Product_ID(line.getM_Product_ID()); - setM_AttributeSetInstance_ID(line.getM_AttributeSetInstance_ID()); - setPP_Product_BOM_ID(PP_Product_BOM_ID); - setAD_Workflow_ID(AD_Workflow_ID); - setPlanner_ID(SupplyPlanner_ID); - setQtyDelivered(Env.ZERO); - setQtyReject(Env.ZERO); - setQtyScrap(Env.ZERO); - setDateOrdered(line.getDateOrdered()); - setDatePromised(line.getDatePromised()); - setDateStartSchedule(TimeUtil.addDays(line.getDatePromised(), (MPPMRP.getDays(S_Resource_ID,AD_Workflow_ID, line.getQtyOrdered())).negate().intValue())); - setDateFinishSchedule(line.getDatePromised()); - setQtyEntered(line.getQtyEntered()); - setQtyOrdered(line.getQtyOrdered()); - setC_UOM_ID(line.getC_UOM_ID()); - setPosted(false); - setProcessed(false); - //setC_DocTypeTarget_ID(C_DocType_ID); - //setC_DocType_ID(C_DocType_ID); - setPriorityRule(this.PRIORITYRULE_High); - setDocStatus(DOCSTATUS_Drafted); - setDocAction(DOCSTATUS_Completed); - }*/ - /** * Overwrite Client/Org if required * @param AD_Client_ID client @@ -258,134 +225,17 @@ public class MPPOrder extends X_PP_Order implements DocAction { super.setClientOrg(AD_Client_ID, AD_Org_ID); } // setClientOrg - /** - * Set C_Resource - * @param C_Resource Plant - */ - /* - public void setC_Resource_ID (int C_Resource_ID) - { - super.setC_Resource_ID (C_Resource_ID); - } // C_Resource Plant - */ - - /** - * Set Warehouse - * @param M_Warehouse_ID warehouse - */ - public void setM_Warehouse_ID(int M_Warehouse_ID) { - super.setM_Warehouse_ID(M_Warehouse_ID); - } // setM_Warehouse_ID - - /*************************************************************************/ - /* - public static final String DocSubTypeSO_Standard = "SO"; - public static final String DocSubTypeSO_Quotation = "OB"; - public static final String DocSubTypeSO_Proposal = "ON"; - public static final String DocSubTypeSO_Prepay = "PR"; - public static final String DocSubTypeSO_POS = "WR"; - public static final String DocSubTypeSO_Warehouse = "WP"; - public static final String DocSubTypeSO_OnCredit = "WI"; - public static final String DocSubTypeSO_RMA = "RM"; - */ - /** - * Set Target Sales Document Type - * @param DocSubTypeSO_x SO sub type - see DocSubTypeSO_* - */ - /* - public void setC_DocTypeTarget_ID(String DocSubTypeSO_x) { - String sql = "SELECT C_DocType_ID FROM C_DocType WHERE AD_Client_ID=? AND DocSubTypeSO=? ORDER BY IsDefault DESC"; - int C_DocType_ID = DB.getSQLValue(sql, getAD_Client_ID(), DocSubTypeSO_x); - if (C_DocType_ID <= 0) - log.log(Level.SEVERE ,("setC_DocTypeTarget_ID - Not found for AD_Client_ID=" + getAD_Client_ID() + ", SubType=" + DocSubTypeSO_x); - else { - log.fine("setC_DocTypeTarget_ID - " + DocSubTypeSO_x); - setC_DocTypeTarget_ID(C_DocType_ID); - setIsSOTrx(true); - } - } // setC_DocTypeTarget_ID - */ - - /** - * Set Target Document Type. - * Standard Order or PO - */ - /* - public void setC_DocTypeTarget_ID() { - if (isSOTrx()) // SO = Std Order - { - //setC_DocTypeTarget_ID(DocSubTypeSO_Standard); - return; - } - // PO - String sql = "SELECT C_DocType_ID FROM C_DocType WHERE AD_Client_ID=? AND DocBaseType='POO' ORDER BY IsDefault DESC"; - int C_DocType_ID = DB.getSQLValue(sql, getAD_Client_ID()); - if (C_DocType_ID <= 0) - log.log(Level.SEVERE ,("setC_DocTypeTarget_ID - No POO found for AD_Client_ID=" + getAD_Client_ID()); - else { - log.fine("setC_DocTypeTarget_ID (PO) - " + C_DocType_ID); - setC_DocTypeTarget_ID(C_DocType_ID); - } - } // setC_DocTypeTarget_ID - */ - - /** - * Copy Lines From other Order - * @param order order - * @param counter set counter info - * @return number of lines copied - */ - /* - public int copyPP_Order_BOMLinesFrom(PP_Order PP_Order, boolean counter) { - if (isProcessed() || isPosted() || PP_Order == null) - return 0; - PP_Order_BOMLine[] fromLines = PP_Order.getLines(false); - int count = 0; - for (int i = 0; i < fromLines.length; i++) { - PP_Order_BOMLine line = new PP_Order_BOMLine(this); - PO.copyValues(fromLines[i], line, getAD_Client_ID(), getAD_Org_ID()); - line.setPP_Order_ID(getPP_Order_ID()); - //line.setOrder(order); - line.setLine(0); - line.setM_AttributeSetInstance_ID(0); - line.setS_ResourceAssignment_ID(0); - // - /*line.setQtyDelivered(Env.ZERO); - line.setQtyInvoiced(Env.ZERO); - line.setQtyReserved(Env.ZERO); - line.setDateDelivered(null); - line.setDateInvoiced(null); - // Tax - if (getC_BPartner_ID() != order.getC_BPartner_ID()) - line.setTax(); // recalculate - // - if (counter) - line.setRef_OrderLine_ID(fromLines[i].getC_OrderLine_ID()); - else - line.setRef_OrderLine_ID(0); - - line.setProcessed(false); - if (line.save()) - count++; - // Cross Link - if (counter) { - fromLines[i].setRef_OrderLine_ID(line.getC_OrderLine_ID()); - fromLines[i].save(); - } - } - if (fromLines.length != count) - log.log(Level.SEVERE ,("copyLinesFrom - Line difference - From=" + fromLines.length + " <> Saved=" + count); - return count; - } // copyLinesFrom - */ - /************************************************************************** * String Representation * @return info */ - public String toString() { - StringBuffer sb = new StringBuffer("MPPOrder[").append(get_ID()).append("-").append(getDocumentNo()).append(",IsSOTrx=").append(isSOTrx()).append( - ",C_DocType_ID=").append(getC_DocType_ID()).append("]"); + public String toString() + { + StringBuffer sb = new StringBuffer("MPPOrder[").append(get_ID()) + .append("-").append(getDocumentNo()) + .append(",IsSOTrx=").append(isSOTrx()) + .append(",C_DocType_ID=").append(getC_DocType_ID()) + .append("]"); return sb.toString(); } // toString @@ -418,33 +268,20 @@ public class MPPOrder extends X_PP_Order implements DocAction { * Propergate to Lines/Taxes * @param processed processed */ - public void setProcessed(boolean processed) { + public void setProcessed(boolean processed) + { super.setProcessed(processed); - if (get_ID() == 0) return; - String set = "SET Processed='" + (processed ? "Y" : "N") + "' WHERE PP_Order_ID=" + getPP_Order_ID(); - int noLine = DB.executeUpdate("UPDATE PP_Order " + set, get_TrxName()); - //m_lines = null; - //log.fine("setProcessed - " + processed + " - Lines=" + noLine + ", Tax=" + noTax); + + // Update DB: + if (get_ID() == 0) + return; + String sql = "UPDATE PP_Order SET Processed=? WHERE PP_Order_ID=?"; + DB.executeUpdateEx(sql, new Object[]{(processed ? "Y" : "N"), get_ID()}, get_TrxName()); } // setProcessed - protected boolean beforeSave(boolean newRecord) { - - if (newRecord) { - // make sure DocType set to 0 - // Bugfix by Gunther Hoppe, 10.10.2005 - // Begin - // What!? - /* - if (getC_DocType_ID() == 0) - setC_DocType_ID(0); - */ - - // Set this values at constructor! - //setDocStatus(DocumentEngine.STATUS_NotApproved); - //setDocAction(DocumentEngine.ACTION_Void); - // End - } - + @Override + protected boolean beforeSave(boolean newRecord) + { if (getAD_Client_ID() == 0) { m_processMsg = "AD_Client_ID = 0"; return false; @@ -466,405 +303,107 @@ public class MPPOrder extends X_PP_Order implements DocAction { return true; } - /************************************************************************** - * Before Save - * @param newRecord new - * @return save - */ - protected boolean afterSave(boolean newRecord, boolean success) { - + @Override + protected boolean afterSave(boolean newRecord, boolean success) + { + if (!success) { + return false; + } if (!newRecord) { return success; } - log.fine("afterSave - MPPOrder Query ok"); - - //setC_DocType_ID(0); // Create BOM Head - MPPProductBOM PP_Product_BOM = new MPPProductBOM(getCtx(), getPP_Product_BOM_ID(), get_TrxName()); + MPPProductBOM PP_Product_BOM = MPPProductBOM.get(getCtx(), getPP_Product_BOM_ID()); + if (PP_Product_BOM.isValidFromTo(getDateStartSchedule())) + { + MPPOrderBOM PP_Order_BOM = new MPPOrderBOM(PP_Product_BOM, getPP_Order_ID(), get_TrxName()); + PP_Order_BOM.saveEx(); - boolean ValidFromBOM = true; - boolean ValidToBOM = true; - if (PP_Product_BOM.getValidFrom() != null) - ValidFromBOM = getDateStartSchedule().compareTo(PP_Product_BOM.getValidFrom()) >= 0 ? true : false; - - if (PP_Product_BOM.getValidTo() != null) ValidToBOM = getDateStartSchedule().compareTo(PP_Product_BOM.getValidTo()) <= 0 ? true : false; - - if (ValidFromBOM && ValidToBOM) { - MPPOrderBOM PP_Order_BOM = new MPPOrderBOM(getCtx(), 0, get_TrxName()); - PP_Order_BOM.setPP_Order_ID(getPP_Order_ID()); - PP_Order_BOM.setBOMType(PP_Product_BOM.getBOMType()); - PP_Order_BOM.setBOMUse(PP_Product_BOM.getBOMUse()); - PP_Order_BOM.setM_ChangeNotice_ID(PP_Product_BOM.getM_ChangeNotice_ID()); - PP_Order_BOM.setHelp(PP_Product_BOM.getHelp()); - PP_Order_BOM.setCopyFrom(PP_Product_BOM.getCopyFrom()); - PP_Order_BOM.setProcessing(PP_Product_BOM.isProcessing()); - PP_Order_BOM.setHelp(PP_Product_BOM.getHelp()); - PP_Order_BOM.setDescription(PP_Product_BOM.getDescription()); - PP_Order_BOM.setM_AttributeSetInstance_ID(PP_Product_BOM.getM_AttributeSetInstance_ID()); - PP_Order_BOM.setM_Product_ID(PP_Product_BOM.getM_Product_ID()); - PP_Order_BOM.setName(PP_Product_BOM.getName()); - PP_Order_BOM.setRevision(PP_Product_BOM.getRevision()); - PP_Order_BOM.setValidFrom(PP_Product_BOM.getValidFrom()); - PP_Order_BOM.setValidTo(PP_Product_BOM.getValidTo()); - PP_Order_BOM.setValue(PP_Product_BOM.getValue()); - PP_Order_BOM.setDocumentNo(PP_Product_BOM.getDocumentNo()); - PP_Order_BOM.setC_UOM_ID(PP_Product_BOM.getC_UOM_ID()); - PP_Order_BOM.save(); - - MPPProductBOMLine[] PP_Product_BOMline = PP_Product_BOM.getLines(); - - for (int i = 0; i < PP_Product_BOMline.length; i++) { - boolean ValidFromBOMLine = true; - boolean ValidToBOMLine = true; - - if (PP_Product_BOMline[i].getValidFrom() != null) - ValidFromBOMLine = getDateStartSchedule().compareTo(PP_Product_BOMline[i].getValidFrom()) >= 0 ? true : false; - - if (PP_Product_BOMline[i].getValidTo() != null) - ValidToBOMLine = getDateStartSchedule().compareTo(PP_Product_BOMline[i].getValidTo()) <= 0 ? true : false; - - if (ValidFromBOMLine && ValidToBOMLine) { - MPPOrderBOMLine PP_Order_BOMLine = new MPPOrderBOMLine(getCtx(), 0, get_TrxName()); - PP_Order_BOMLine.setM_ChangeNotice_ID(PP_Product_BOMline[i].getM_ChangeNotice_ID()); - PP_Order_BOMLine.setHelp(PP_Product_BOMline[i].getHelp()); - PP_Order_BOMLine.setAssay(PP_Product_BOMline[i].getAssay()); - PP_Order_BOMLine.setQtyBatch(PP_Product_BOMline[i].getQtyBatch()); - PP_Order_BOMLine.setQtyBOM(PP_Product_BOMline[i].getQtyBOM()); - PP_Order_BOMLine.setIsQtyPercentage(PP_Product_BOMline[i].isQtyPercentage()); - PP_Order_BOMLine.setComponentType(PP_Product_BOMline[i].getComponentType()); - PP_Order_BOMLine.setC_UOM_ID(PP_Product_BOMline[i].getC_UOM_ID()); - PP_Order_BOMLine.setForecast(PP_Product_BOMline[i].getForecast()); - PP_Order_BOMLine.setIsCritical(PP_Product_BOMline[i].isCritical()); - PP_Order_BOMLine.setIssueMethod(PP_Product_BOMline[i].getIssueMethod()); - PP_Order_BOMLine.setLeadTimeOffset(PP_Product_BOMline[i].getLeadTimeOffset()); - PP_Order_BOMLine.setM_AttributeSetInstance_ID(PP_Product_BOMline[i].getM_AttributeSetInstance_ID()); - PP_Order_BOMLine.setPP_Order_BOM_ID(PP_Order_BOM.getPP_Order_BOM_ID()); - PP_Order_BOMLine.setPP_Order_ID(getPP_Order_ID()); - PP_Order_BOMLine.setM_Product_ID(PP_Product_BOMline[i].getM_Product_ID()); - PP_Order_BOMLine.setScrap(PP_Product_BOMline[i].getScrap()); - PP_Order_BOMLine.setValidFrom(PP_Product_BOMline[i].getValidFrom()); - PP_Order_BOMLine.setValidTo(PP_Product_BOMline[i].getValidTo()); - PP_Order_BOMLine.setM_Warehouse_ID(getM_Warehouse_ID()); - PP_Order_BOMLine.setBackflushGroup(PP_Product_BOMline[i].getBackflushGroup()); - - BigDecimal QtyOrdered = getQtyOrdered(); - log.log(Level.INFO, "PP_Order_BOMLine.getQtyBOM()" + PP_Order_BOMLine.getQtyBOM() + "PP_Product_BOMline[i].getQtyBOM()" - + PP_Product_BOMline[i].getQtyBOM()); - - if (PP_Order_BOMLine.isQtyPercentage()) { - BigDecimal qty = PP_Order_BOMLine.getQtyBatch().multiply(QtyOrdered); - if (PP_Order_BOMLine.getComponentType().equals(PP_Order_BOMLine.COMPONENTTYPE_Packing)) - PP_Order_BOMLine.setQtyRequiered(qty.divide(new BigDecimal(100), 8, qty.ROUND_UP)); - if (PP_Order_BOMLine.getComponentType().equals(PP_Order_BOMLine.COMPONENTTYPE_Component) - || PP_Order_BOMLine.getComponentType().equals(PP_Order_BOMLine.COMPONENTTYPE_Phantom)) - PP_Order_BOMLine.setQtyRequiered(qty.divide(new BigDecimal(100), 8, qty.ROUND_UP)); - else if (PP_Order_BOMLine.getComponentType().equals(PP_Order_BOMLine.COMPONENTTYPE_Tools)) - PP_Order_BOMLine.setQtyRequiered(PP_Order_BOMLine.getQtyBOM()); - - } - else { - if (PP_Order_BOMLine.getComponentType().equals(PP_Order_BOMLine.COMPONENTTYPE_Component) - || PP_Order_BOMLine.getComponentType().equals(PP_Order_BOMLine.COMPONENTTYPE_Phantom)) - PP_Order_BOMLine.setQtyRequiered(PP_Order_BOMLine.getQtyBOM().multiply(QtyOrdered)); - else if (PP_Order_BOMLine.getComponentType().equals(PP_Order_BOMLine.COMPONENTTYPE_Packing)) - PP_Order_BOMLine.setQtyRequiered(PP_Order_BOMLine.getQtyBOM().multiply(QtyOrdered)); - else if (PP_Order_BOMLine.getComponentType().equals(PP_Order_BOMLine.COMPONENTTYPE_Tools)) - PP_Order_BOMLine.setQtyRequiered(PP_Order_BOMLine.getQtyBOM()); - } - // Set Scrap of Component - BigDecimal Scrap = PP_Order_BOMLine.getScrap(); - if (!Scrap.equals(Env.ZERO)) { - Scrap = Scrap.divide(new BigDecimal(100), 8, BigDecimal.ROUND_UP); - PP_Order_BOMLine.setQtyRequiered(PP_Order_BOMLine.getQtyRequiered().divide(Env.ONE.subtract(Scrap), 8, - BigDecimal.ROUND_HALF_UP)); - } - - PP_Order_BOMLine.save(); - - } // end if From / To component - - MPPOrderBOMLine[] lines = getLines(); - for (int l = 0; l < lines.length; l++) { - if (lines[l].getComponentType().equals(MPPProductBOMLine.COMPONENTTYPE_Phantom)) { - lines[l].setQtyRequiered(Env.ZERO); - lines[l].save(get_TrxName()); - } - } + for (MPPProductBOMLine PP_Product_BOMline : PP_Product_BOM.getLines()) + { + if (PP_Product_BOMline.isValidFromTo(getDateStartSchedule())) + { + MPPOrderBOMLine PP_Order_BOMLine = new MPPOrderBOMLine(PP_Product_BOMline, + getPP_Order_ID(), PP_Order_BOM.get_ID(), + getM_Warehouse_ID(), + get_TrxName()); + PP_Order_BOMLine.setQtyOrdered(getQtyOrdered()); + PP_Order_BOMLine.saveEx(); + } // end if valid From / To } // end Create Order BOM } // end if From / To parent // Create Workflow (Routing & Process - - MWorkflow AD_Workflow = new MWorkflow(getCtx(), getAD_Workflow_ID(), get_TrxName()); - - boolean ValidFromWF = true; - boolean ValidToWF = true; - if (AD_Workflow.getValidFrom() != null) ValidFromWF = getDateStartSchedule().compareTo(AD_Workflow.getValidFrom()) >= 0 ? true : false; - - if (AD_Workflow.getValidTo() != null && getDateStartSchedule() != null) - ValidToWF = getDateStartSchedule().compareTo(AD_Workflow.getValidTo()) <= 0 ? true : false; - - if (ValidFromWF && ValidToWF) { - MPPOrderWorkflow PP_Order_Workflow = new MPPOrderWorkflow(getCtx(), 0, get_TrxName()); - PP_Order_Workflow.setValue(AD_Workflow.getValue()); - PP_Order_Workflow.setWorkflowType(AD_Workflow.getWorkflowType()); - PP_Order_Workflow.setQtyBatchSize(AD_Workflow.getQtyBatchSize()); - PP_Order_Workflow.setName(AD_Workflow.getName()); - PP_Order_Workflow.setAccessLevel(AD_Workflow.getAccessLevel()); - PP_Order_Workflow.setAuthor(AD_Workflow.getAuthor()); - PP_Order_Workflow.setDurationUnit(AD_Workflow.getDurationUnit()); - PP_Order_Workflow.setDuration(AD_Workflow.getDuration()); - PP_Order_Workflow.setEntityType(AD_Workflow.getEntityType()); - PP_Order_Workflow.setIsDefault(AD_Workflow.isDefault()); - PP_Order_Workflow.setPublishStatus(AD_Workflow.getPublishStatus()); - PP_Order_Workflow.setVersion(AD_Workflow.getVersion()); - PP_Order_Workflow.setCost(AD_Workflow.getCost()); - PP_Order_Workflow.setWaitingTime(AD_Workflow.getWaitingTime()); - PP_Order_Workflow.setWorkingTime(AD_Workflow.getWorkingTime()); - PP_Order_Workflow.setAD_WF_Responsible_ID(AD_Workflow.getAD_WF_Responsible_ID()); - PP_Order_Workflow.setAD_Workflow_ID(AD_Workflow.getAD_Workflow_ID()); - PP_Order_Workflow.setLimit(AD_Workflow.getLimit()); - PP_Order_Workflow.setPP_Order_ID(getPP_Order_ID()); - PP_Order_Workflow.setPriority(AD_Workflow.getPriority()); - PP_Order_Workflow.setValidateWorkflow(AD_Workflow.getValidateWorkflow()); - PP_Order_Workflow.setS_Resource_ID(AD_Workflow.getS_Resource_ID()); - PP_Order_Workflow.setQueuingTime(AD_Workflow.getQueuingTime()); - PP_Order_Workflow.setSetupTime(AD_Workflow.getSetupTime()); - PP_Order_Workflow.setMovingTime(AD_Workflow.getMovingTime()); - PP_Order_Workflow.setProcessType(AD_Workflow.getProcessType()); - PP_Order_Workflow.setAD_Table_ID(AD_Workflow.getAD_Table_ID()); - PP_Order_Workflow.setAD_WF_Node_ID(AD_Workflow.getAD_WF_Node_ID()); - PP_Order_Workflow.setAD_WorkflowProcessor_ID(AD_Workflow.getAD_WorkflowProcessor_ID()); - PP_Order_Workflow.setDescription(AD_Workflow.getDescription()); - PP_Order_Workflow.setValidFrom(AD_Workflow.getValidFrom()); - PP_Order_Workflow.setValidTo(AD_Workflow.getValidTo()); - PP_Order_Workflow.save(); - - MWFNode[] AD_WF_Node = AD_Workflow.getNodes(false, getAD_Client_ID()); - - if (AD_WF_Node != null && AD_WF_Node.length != 0) { - for (int g = 0; g < AD_WF_Node.length; g++) { - - boolean ValidFromNode = true; - boolean ValidToNode = true; - - if (AD_WF_Node[g].getValidFrom() != null) - ValidFromNode = getDateStartSchedule().compareTo(AD_WF_Node[g].getValidFrom()) >= 0 ? true : false; - - if (AD_WF_Node[g].getValidTo() != null) - ValidToNode = getDateStartSchedule().compareTo(AD_WF_Node[g].getValidTo()) <= 0 ? true : false; - - if (ValidFromNode && ValidToNode) { - MPPOrderNode PP_Order_Node = new MPPOrderNode(getCtx(), 0, get_TrxName()); - PP_Order_Node.setAction(AD_WF_Node[g].getAction()); - PP_Order_Node.setAD_WF_Node_ID(AD_WF_Node[g].getAD_WF_Node_ID()); - PP_Order_Node.setAD_WF_Responsible_ID(AD_WF_Node[g].getAD_WF_Responsible_ID()); - PP_Order_Node.setAD_Workflow_ID(AD_WF_Node[g].getAD_Workflow_ID()); - PP_Order_Node.setCost(AD_WF_Node[g].getCost()); - PP_Order_Node.setDuration(AD_WF_Node[g].getDuration()); - PP_Order_Node.setEntityType(AD_WF_Node[g].getEntityType()); - PP_Order_Node.setIsCentrallyMaintained(AD_WF_Node[g].isCentrallyMaintained()); - PP_Order_Node.setJoinElement(AD_WF_Node[g].getJoinElement()); // X - PP_Order_Node.setLimit(AD_WF_Node[g].getLimit()); - PP_Order_Node.setPP_Order_ID(getPP_Order_ID()); - PP_Order_Node.setPP_Order_Workflow_ID(PP_Order_Workflow.getPP_Order_Workflow_ID()); - PP_Order_Node.setName(AD_WF_Node[g].getName()); - PP_Order_Node.setPriority(AD_WF_Node[g].getPriority()); - PP_Order_Node.setSplitElement(AD_WF_Node[g].getSplitElement()); // X - PP_Order_Node.setSubflowExecution(AD_WF_Node[g].getSubflowExecution()); - PP_Order_Node.setValue(AD_WF_Node[g].getValue()); - PP_Order_Node.setS_Resource_ID(AD_WF_Node[g].getS_Resource_ID()); - PP_Order_Node.setSetupTime(AD_WF_Node[g].getSetupTime()); - PP_Order_Node.setSetupTimeRequiered(AD_WF_Node[g].getSetupTime()); - BigDecimal time = new BigDecimal(AD_WF_Node[g].getDuration()).multiply(getQtyOrdered()); - PP_Order_Node.setDurationRequiered(time.intValue()); - PP_Order_Node.setMovingTime(AD_WF_Node[g].getMovingTime()); - PP_Order_Node.setWaitingTime(AD_WF_Node[g].getWaitingTime()); - PP_Order_Node.setWorkingTime(AD_WF_Node[g].getWorkingTime()); - PP_Order_Node.setQueuingTime(AD_WF_Node[g].getQueuingTime()); - PP_Order_Node.setXPosition(AD_WF_Node[g].getXPosition()); - PP_Order_Node.setYPosition(AD_WF_Node[g].getYPosition()); - PP_Order_Node.setDocAction(AD_WF_Node[g].getDocAction()); - PP_Order_Node.setAD_Column_ID(AD_WF_Node[g].getAD_Column_ID()); - PP_Order_Node.setAD_Form_ID(AD_WF_Node[g].getAD_Form_ID()); - PP_Order_Node.setAD_Image_ID(AD_WF_Node[g].getAD_Image_ID()); - PP_Order_Node.setAD_Window_ID(AD_WF_Node[g].getAD_Window_ID()); - PP_Order_Node.setAD_Process_ID(AD_WF_Node[g].getAD_Process_ID()); - PP_Order_Node.setAttributeName(AD_WF_Node[g].getAttributeName()); - PP_Order_Node.setAttributeValue(AD_WF_Node[g].getAttributeValue()); - PP_Order_Node.setC_BPartner_ID(AD_WF_Node[g].getC_BPartner_ID()); - PP_Order_Node.setStartMode(AD_WF_Node[g].getStartMode()); - PP_Order_Node.setFinishMode(AD_WF_Node[g].getFinishMode()); - PP_Order_Node.setValidFrom(AD_WF_Node[g].getValidFrom()); - PP_Order_Node.setValidTo(AD_WF_Node[g].getValidTo()); - PP_Order_Node.save(); - - MWFNodeNext[] AD_WF_NodeNext = AD_WF_Node[g].getTransitions(getAD_Client_ID()); - log.log(Level.INFO, "AD_WF_NodeNext" + AD_WF_NodeNext.length); - if (AD_WF_NodeNext != null && AD_WF_NodeNext.length != 0) { - for (int n = 0; n < AD_WF_NodeNext.length; n++) { - MPPOrderNodeNext PP_Order_NodeNext = new MPPOrderNodeNext(getCtx(), 0, get_TrxName()); - PP_Order_NodeNext.setAD_WF_Node_ID(AD_WF_NodeNext[n].getAD_WF_Node_ID()); - PP_Order_NodeNext.setAD_WF_Next_ID(AD_WF_NodeNext[n].getAD_WF_Next_ID()); - PP_Order_NodeNext.setPP_Order_Node_ID(PP_Order_Node.getPP_Order_Node_ID()); - PP_Order_NodeNext.setPP_Order_Next_ID(0); - PP_Order_NodeNext.setDescription(AD_WF_NodeNext[n].getDescription()); - PP_Order_NodeNext.setEntityType(AD_WF_NodeNext[n].getEntityType()); - PP_Order_NodeNext.setIsStdUserWorkflow(AD_WF_NodeNext[n].isStdUserWorkflow()); - PP_Order_NodeNext.setPP_Order_ID(getPP_Order_ID()); - PP_Order_NodeNext.setSeqNo(AD_WF_NodeNext[n].getSeqNo()); - PP_Order_NodeNext.setTransitionCode(AD_WF_NodeNext[n].getTransitionCode()); - PP_Order_NodeNext.save(); - }// for NodeNext - } - - }// node valid from/to - + MWorkflow AD_Workflow = MWorkflow.get(getCtx(), getAD_Workflow_ID()); + if (AD_Workflow.isValidFromTo(getDateStartSchedule())) + { + MPPOrderWorkflow PP_Order_Workflow = new MPPOrderWorkflow(AD_Workflow, get_ID(), get_TrxName()); + PP_Order_Workflow.saveEx(); + for (MWFNode AD_WF_Node : AD_Workflow.getNodes(false, getAD_Client_ID())) + { + if (AD_WF_Node.isValidFromTo(getDateStartSchedule())) + { + MPPOrderNode PP_Order_Node = new MPPOrderNode(AD_WF_Node, PP_Order_Workflow, + getQtyOrdered(), + get_TrxName()); + PP_Order_Node.saveEx(); + + for (MWFNodeNext AD_WF_NodeNext : AD_WF_Node.getTransitions(getAD_Client_ID())) + { + new MPPOrderNodeNext(AD_WF_NodeNext, PP_Order_Node, get_TrxName()).saveEx(); + }// for NodeNext }// for node } - - // set transitions for order - MPPOrderWorkflow OrderWorkflow = new MPPOrderWorkflow(getCtx(), PP_Order_Workflow.getPP_Order_Workflow_ID(), get_TrxName()); - MPPOrderNode[] OrderNodes = OrderWorkflow.getNodes(false, getAD_Client_ID()); - - if (OrderNodes != null && OrderNodes.length != 0) { - for (int h = 0; h < OrderNodes.length; h++) { - // set workflow start node - if (OrderWorkflow.getAD_WF_Node_ID() == OrderNodes[h].getAD_WF_Node_ID()) { - OrderWorkflow.setPP_Order_Node_ID(OrderNodes[h].getPP_Order_Node_ID()); - } - MPPOrderNodeNext[] nexts = OrderNodes[h].getTransitions(getAD_Client_ID()); - if (nexts != null && nexts.length != 0) { - log.log(Level.INFO, "Node Transition" + nexts.length); - for (int x = 0; x < nexts.length; x++) { - - String sql = "SELECT PP_Order_Node_ID FROM PP_Order_Node WHERE PP_Order_ID = ? AND AD_WF_Node_ID = ? AND AD_Client_ID=?"; - PreparedStatement pstmt = null; - ResultSet rs = null; - try { - - pstmt = DB.prepareStatement(sql, get_TrxName()); - pstmt.setInt(1, nexts[x].getPP_Order_ID()); - pstmt.setInt(2, nexts[x].getAD_WF_Next_ID()); - pstmt.setInt(3, nexts[x].getAD_Client_ID()); - rs = pstmt.executeQuery(); - while (rs.next()) { - nexts[x].setPP_Order_Next_ID(rs.getInt(1)); - nexts[x].save(); - } - } - catch (Exception e) { - log.log(Level.SEVERE, "doIt - " + sql, e); - } - finally { - DB.close(rs, pstmt); - rs = null; - pstmt = null; - } - - }// end for NodeNext - } + // Update transitions nexts and set first node + PP_Order_Workflow.loadNodes(); + for (MPPOrderNode orderNode : PP_Order_Workflow.getNodes(false, getAD_Client_ID())) + { + // set workflow start node + if (PP_Order_Workflow.getAD_WF_Node_ID() == orderNode.getAD_WF_Node_ID()) { + PP_Order_Workflow.setPP_Order_Node_ID(orderNode.getPP_Order_Node_ID()); + } + // set node next + for (MPPOrderNodeNext next : orderNode.getTransitions(getAD_Client_ID())) + { + next.setPP_Order_Next_ID(); + next.saveEx(); } } - - OrderWorkflow.save(); - + PP_Order_Workflow.saveEx(); } // workflow valid from/to return true; } // beforeSave - protected boolean beforeDelete() { + protected boolean beforeDelete() + { // OrderBOMLine - if (getDocStatus().equals(DOCSTATUS_Drafted) || getDocStatus().equals(DOCSTATUS_InProgress)) { - int[] ids = null; - PO po = null; - // - ids = PO.getAllIDs("PP_Order_Cost", "PP_Order_ID=" + get_ID() + " AND AD_Client_ID=" + getAD_Client_ID(), get_TrxName()); - for (int i = 0; i < ids.length; i++) { - - po = new MPPOrderCost(getCtx(), ids[i], get_TrxName()); - po.deleteEx(true); - } - // - ids = PO.getAllIDs("PP_Order_Node_Asset", "PP_Order_ID=" + get_ID() + " AND AD_Client_ID=" + getAD_Client_ID(), get_TrxName()); - for (int i = 0; i < ids.length; i++) { - - po = new X_PP_Order_Node_Asset(getCtx(), ids[i], get_TrxName()); - po.deleteEx(true); - } - // Reset workflow start node - DB.executeUpdate("UPDATE PP_Order_Workflow SET PP_Order_Node_ID=NULL WHERE PP_Order_ID=" + get_ID() + " AND AD_Client_ID=" + getAD_Client_ID(), get_TrxName()); - // - ids = PO.getAllIDs("PP_Order_NodeNext", "PP_Order_ID=" + get_ID() + " AND AD_Client_ID=" + getAD_Client_ID(), get_TrxName()); - for (int i = 0; i < ids.length; i++) { - po = new MPPOrderNodeNext(getCtx(), ids[i], get_TrxName()); - po.deleteEx(true); - } + if (getDocStatus().equals(DOCSTATUS_Drafted) || getDocStatus().equals(DOCSTATUS_InProgress)) + { + String whereClause = "PP_Order_ID=? AND AD_Client_ID=?"; + Object[] params = new Object[]{get_ID(), getAD_Client_ID()}; - // - ids = PO.getAllIDs("PP_Order_Node", "PP_Order_ID=" + get_ID() + " AND AD_Client_ID=" + getAD_Client_ID(), get_TrxName()); - for (int i = 0; i < ids.length; i++) { - po = new MPPOrderNode(getCtx(), ids[i], get_TrxName()); - po.deleteEx(true); - } - - // - ids = PO.getAllIDs("PP_Order_Node_Product", "PP_Order_ID=" + get_ID() + " AND AD_Client_ID=" + getAD_Client_ID(), get_TrxName()); - for (int i = 0; i < ids.length; i++) { - po = new X_PP_Order_Node_Product(getCtx(), ids[i], get_TrxName()); - po.deleteEx(true); - } - // - ids = PO.getAllIDs("PP_Order_Workflow", "PP_Order_ID=" + get_ID() + " AND AD_Client_ID=" + getAD_Client_ID(), get_TrxName()); - for (int i = 0; i < ids.length; i++) { - po = new MPPOrderWorkflow(getCtx(), ids[i], get_TrxName()); - po.deleteEx(true); - } - // - ids = PO.getAllIDs("PP_Order_BOMLine", "PP_Order_ID=" + get_ID() + " AND AD_Client_ID=" + getAD_Client_ID(), get_TrxName()); - for (int i = 0; i < ids.length; i++) { - - po = new MPPOrderBOMLine(getCtx(), ids[i], get_TrxName()); - po.deleteEx(true); - } - // - ids = PO.getAllIDs("PP_Order_BOM", "PP_Order_ID=" + get_ID() + " AND AD_Client_ID=" + getAD_Client_ID(), get_TrxName()); - for (int i = 0; i < ids.length; i++) { - po = new MPPOrderBOM(getCtx(), ids[i], get_TrxName()); - po.deleteEx(true); - } - } //return true; + // Delete Cost: + deletePO(MPPOrderCost.Table_Name, whereClause, params); + // Delete workflow: + DB.executeUpdateEx("UPDATE PP_Order_Workflow SET PP_Order_Node_ID=NULL WHERE "+whereClause, params, get_TrxName()); // Reset workflow start node + deletePO(X_PP_Order_Node_Asset.Table_Name, whereClause, params); + deletePO(X_PP_Order_Node_Product.Table_Name, whereClause, params); + deletePO(MPPOrderNodeNext.Table_Name, whereClause, params); + deletePO(MPPOrderNode.Table_Name, whereClause, params); + deletePO(MPPOrderWorkflow.Table_Name, whereClause, params); + // Delete BOM: + deletePO(MPPOrderBOMLine.Table_Name, whereClause, params); + deletePO(MPPOrderBOM.Table_Name, whereClause, params); + } return true; } // beforeDelete - /************************************************************************** - * Process Order - Start Process - * @return true if ok - */ - /* - public boolean processOrder(String docAction) { - setDocAction(docAction); - save(); - log.fine("processOrder - " + getDocAction()); - int AD_Process_ID = 104; // C_Order_Post - MProcess pp = new MProcess(getCtx(), AD_Process_ID); - boolean ok = pp.processIt(getPP_Order_ID()).isOK(); - load(); // reload - //log.fine("processOrder - ok=" + ok + " - GrandTotal=" + getGrandTotal()); - return ok; - } // process - */ - - /************************************************************************** - * Process document - * @param processAction document action - * @return true if performed - */ - public boolean processIt(String processAction) { + public boolean processIt(String processAction) + { m_processMsg = null; DocumentEngine engine = new DocumentEngine(this, getDocStatus()); return engine.processIt(processAction, getDocAction()); @@ -902,9 +441,10 @@ public class MPPOrder extends X_PP_Order implements DocAction { public String prepareIt() { log.info("prepareIt - " + toString()); log.info(toString()); + m_processMsg = ModelValidationEngine.get().fireDocValidate(this, ModelValidator.TIMING_BEFORE_PREPARE); - if (m_processMsg != null) return DocAction.STATUS_Invalid; - org.compiere.model.MDocType dt = org.compiere.model.MDocType.get(getCtx(), getC_DocTypeTarget_ID()); + if (m_processMsg != null) + return DocAction.STATUS_Invalid; // Std Period open? /*if (!MPeriod.isOpen(getCtx(), getDateAcct(), dt.getDocBaseType())) @@ -942,23 +482,17 @@ public class MPPOrder extends X_PP_Order implements DocAction { setC_DocType_ID(getC_DocTypeTarget_ID()); } - MDocType doc = new MDocType(getCtx(), getC_DocType_ID(), get_TrxName()); - if (doc.getDocBaseType().equals(X_C_DocType.DOCBASETYPE_QualityOrder)) return DocAction.STATUS_InProgress; + MDocType doc = MDocType.get(getCtx(), getC_DocType_ID()); + if (doc.getDocBaseType().equals(X_C_DocType.DOCBASETYPE_QualityOrder)) + return DocAction.STATUS_InProgress; if (lines.length == 0) { m_processMsg = "@NoLines@"; return DocAction.STATUS_Invalid; } - if (!reserveStock(lines)) { - m_processMsg = "Cannot reserve Stock"; - return DocAction.STATUS_Invalid; - } - - if (!orderStock()) { - m_processMsg = "Cannot Order Stock"; - return DocAction.STATUS_Invalid; - } + reserveStock(lines); + orderStock(); m_justPrepared = true; // if (!DOCACTION_Complete.equals(getDocAction())) don't set for just prepare @@ -966,15 +500,17 @@ public class MPPOrder extends X_PP_Order implements DocAction { return DocAction.STATUS_InProgress; } // prepareIt - private boolean orderStock() { - MProduct product = new MProduct(getCtx(), getM_Product_ID(), get_TrxName()); - if (product != null && product.isStocked()) { - + private void orderStock() + { + MProduct product = MProduct.get(getCtx(), getM_Product_ID()); + if (product != null && product.isStocked()) + { BigDecimal target = getQtyOrdered(); BigDecimal difference = target.subtract(getQtyReserved()).subtract(getQtyDelivered()); - - if (difference.signum() == 0) return true; + if (difference.signum() == 0) + return ; BigDecimal ordered = difference; + int M_Locator_ID = 0; // Get Locator to reserve if (getM_AttributeSetInstance_ID() != 0) // Get existing Location @@ -986,56 +522,37 @@ public class MPPOrder extends X_PP_Order implements DocAction { M_Locator_ID = wh.getDefaultLocator().getM_Locator_ID(); } //4Layers - Necessary to clear order quantities when called from closeIt - //4Layers - Necessary to clear order quantities when called from closeIt - if (DOCACTION_Close.equals(getDocAction())) { + if (DOCACTION_Close.equals(getDocAction())) + { if (!MStorage.add(getCtx(), getM_Warehouse_ID(), M_Locator_ID, getM_Product_ID(), getM_AttributeSetInstance_ID(), - getM_AttributeSetInstance_ID(), Env.ZERO, Env.ZERO, ordered, get_TrxName())) { - return false; + getM_AttributeSetInstance_ID(), Env.ZERO, Env.ZERO, ordered, get_TrxName())) + { + throw new AdempiereException(); } } - else { + else + { // Update Storage if (!MStorage.add(getCtx(), getM_Warehouse_ID(), M_Locator_ID, getM_Product_ID(), getM_AttributeSetInstance_ID(), - getM_AttributeSetInstance_ID(), Env.ZERO, Env.ZERO, ordered, get_TrxName())) { - return false; + getM_AttributeSetInstance_ID(), Env.ZERO, Env.ZERO, ordered, get_TrxName())) + { + throw new AdempiereException(); } } setQtyReserved(getQtyReserved().add(difference)); - // update line - if (!save(get_TrxName())) return false; + saveEx(get_TrxName()); } - - return true; } /** * Reserve Inventory. - * Counterpart: MInOut.completeIt() - * @param dt document type or null * @param lines order lines (ordered by M_Product_ID for deadlock prevention) * @return true if (un) reserved */ - private boolean reserveStock(MPPOrderBOMLine[] lines) { - //vpj if (dt == null) - //vpj dt = MDocType.get(getCtx(), getC_DocType_ID()); - - // Binding - //vpj boolean binding = !dt.isProposal(); - // Not binding - i.e. Target=0 - //vpj if (DOCACTION_Void.equals(getDocAction()) - // // Closing Binding Quotation - //vpj || (MDocType.DOCSUBTYPESO_Quotation.equals(dt.getDocSubTypeSO()) - //vpj && DOCACTION_Close.equals(getDocAction())) - //vpj || isDropShip() ) - //vpj binding = false; - boolean isSOTrx = isSOTrx(); - //vpj log.fine("Binding=" + binding + " - IsSOTrx=" + isSOTrx); - // Force same WH for all but SO/PO + private void reserveStock(MPPOrderBOMLine[] lines) + { int header_M_Warehouse_ID = getM_Warehouse_ID(); - //vpj if (MDocType.DOCSUBTYPESO_StandardOrder.equals(dt.getDocSubTypeSO()) - //vpj || MDocType.DOCBASETYPE_PurchaseOrder.equals(dt.getDocBaseType())) - //vpj header_M_Warehouse_ID = 0; // don't enforce // Always check and (un) Reserve Inventory for (int i = 0; i < lines.length; i++) { @@ -1043,14 +560,17 @@ public class MPPOrder extends X_PP_Order implements DocAction { // Check/set WH/Org if (header_M_Warehouse_ID != 0) // enforce WH { - if (header_M_Warehouse_ID != line.getM_Warehouse_ID()) line.setM_Warehouse_ID(header_M_Warehouse_ID); - if (getAD_Org_ID() != line.getAD_Org_ID()) line.setAD_Org_ID(getAD_Org_ID()); + if (header_M_Warehouse_ID != line.getM_Warehouse_ID()) + line.setM_Warehouse_ID(header_M_Warehouse_ID); + if (getAD_Org_ID() != line.getAD_Org_ID()) + line.setAD_Org_ID(getAD_Org_ID()); } // Binding //vpj BigDecimal target = binding ? line.getQtyOrdered() : Env.ZERO; BigDecimal target = line.getQtyRequiered(); BigDecimal difference = target.subtract(line.getQtyReserved()).subtract(line.getQtyDelivered()); - if (difference.signum() == 0) continue; + if (difference.signum() == 0) + continue; log.fine("Line=" + line.getLine() + " - Target=" + target + ",Difference=" + difference + " - Requiered=" + line.getQtyRequiered() + ",Reserved=" + line.getQtyReserved() + ",Delivered=" + line.getQtyDelivered()); @@ -1075,32 +595,38 @@ public class MPPOrder extends X_PP_Order implements DocAction { } // Update Storage if (!MStorage.add(getCtx(), line.getM_Warehouse_ID(), M_Locator_ID, line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), - line.getM_AttributeSetInstance_ID(), Env.ZERO, reserved, ordered, get_TrxName())) return false; + line.getM_AttributeSetInstance_ID(), Env.ZERO, reserved, ordered, get_TrxName())) + { + throw new AdempiereException(); + } } // stockec // update line line.setQtyReserved(line.getQtyReserved().add(difference)); - if (!line.save(get_TrxName())) return false; + line.saveEx(get_TrxName()); } } // reverse inventory - return true; } // reserveStock /** * Approve Document * @return true if success */ - public boolean approveIt() { + public boolean approveIt() + { log.info("approveIt - " + toString()); - MDocType doc = new MDocType(getCtx(), getC_DocType_ID(), get_TrxName()); - if (doc.getDocBaseType().equals(X_C_DocType.DOCBASETYPE_QualityOrder)) { - int QM_Specification_ID = DB.getSQLValue(get_TrxName(), - "SELECT QM_Specification_ID FROM QM_Specification WHERE PP_Product_BOM_ID=? AND AD_Workflow_ID =?", getPP_Product_BOM_ID(), - getAD_Workflow_ID()); - MQMSpecification qms = new MQMSpecification(getCtx(), QM_Specification_ID, get_TrxName()); - return qms.isValid(getM_AttributeSetInstance_ID()); + MDocType doc = MDocType.get(getCtx(), getC_DocType_ID()); + if (doc.getDocBaseType().equals(X_C_DocType.DOCBASETYPE_QualityOrder)) + { + String whereClause = "PP_Product_BOM_ID=? AND AD_Workflow_ID =?"; + MQMSpecification qms = new Query(getCtx(), MQMSpecification.Table_Name, whereClause, get_TrxName()) + .setParameters(new Object[]{getPP_Product_BOM_ID(), getAD_Workflow_ID()}) + .first(); + return qms != null ? qms.isValid(getM_AttributeSetInstance_ID()) : true; } else + { setIsApproved(true); + } return true; } // approveIt @@ -1109,7 +635,8 @@ public class MPPOrder extends X_PP_Order implements DocAction { * Reject Approval * @return true if success */ - public boolean rejectIt() { + public boolean rejectIt() + { log.info("rejectIt - " + toString()); setIsApproved(false); return true; @@ -1119,8 +646,8 @@ public class MPPOrder extends X_PP_Order implements DocAction { * Complete Document * @return new status (Complete, In Progress, Invalid, Waiting ..) */ - public String completeIt() { - + public String completeIt() + { // Just prepare if (DOCACTION_Prepare.equals(getDocAction())) { setProcessed(false); @@ -1130,57 +657,40 @@ public class MPPOrder extends X_PP_Order implements DocAction { // Re-Check if (!m_justPrepared) { String status = prepareIt(); - if (!DocAction.STATUS_InProgress.equals(status)) return status; + if (!DocAction.STATUS_InProgress.equals(status)) + return status; } + m_processMsg = ModelValidationEngine.get().fireDocValidate(this, ModelValidator.TIMING_BEFORE_COMPLETE); + if (m_processMsg != null) + { + return DocAction.STATUS_Invalid; + } + // Implicit Approval - if (!isApproved()) approveIt(); - log.info("completeIt - " + toString()); + if (!isApproved()) + approveIt(); + StringBuffer info = new StringBuffer(); - int C_AcctSchema_ID = Env.getContextAsInt(getCtx(), "$C_AcctSchema_ID"); - log.info("AcctSchema_ID" + C_AcctSchema_ID); - MAcctSchema C_AcctSchema = new MAcctSchema(getCtx(), C_AcctSchema_ID, get_TrxName()); - log.info("Cost_Group_ID" + C_AcctSchema.getM_CostType_ID()); + MAcctSchema acctSchema = MClient.get(getCtx(), getAD_Client_ID()).getAcctSchema(); + log.info("Cost_Group_ID" + acctSchema.getM_CostType_ID()); - MCost[] costs = MCost.getCosts(getCtx(), getAD_Client_ID(), getAD_Org_ID(), getM_Product_ID(), C_AcctSchema.getM_CostType_ID(), - C_AcctSchema_ID, get_TrxName()); - if (costs != null) { - for (MCost cost : costs) { - MPPOrderCost PP_Order_Cost = new MPPOrderCost(getCtx(), 0, get_TrxName()); - PP_Order_Cost.setPP_Order_ID(getPP_Order_ID()); - PP_Order_Cost.setC_AcctSchema_ID(cost.getC_AcctSchema_ID()); - PP_Order_Cost.setCumulatedAmt(cost.getCumulatedAmt()); - PP_Order_Cost.setCumulatedQty(cost.getCumulatedQty()); - PP_Order_Cost.setCurrentCostPriceLL(cost.getCurrentCostPriceLL()); - PP_Order_Cost.setCurrentCostPrice(cost.getCurrentCostPrice()); - PP_Order_Cost.setM_Product_ID(getM_Product_ID()); - PP_Order_Cost.setM_AttributeSetInstance_ID(cost.getM_AttributeSetInstance_ID()); - PP_Order_Cost.setM_CostElement_ID(cost.getM_CostElement_ID()); - PP_Order_Cost.save(); - } + MCost[] costs = MCost.getCosts(getCtx(), getAD_Client_ID(), getAD_Org_ID(), getM_Product_ID(), + acctSchema.getM_CostType_ID(), acctSchema.get_ID(), + get_TrxName()); + for (MCost cost : costs) { + MPPOrderCost PP_Order_Cost = new MPPOrderCost(cost, get_ID(), get_TrxName()); + PP_Order_Cost.saveEx(); } - MPPOrderBOMLine[] lines = getLines(); - log.info("MPPOrderBOMLine[]" + lines.toString()); - - for (MPPOrderBOMLine line : lines) { - costs = MCost.getCosts(getCtx(), getAD_Client_ID(), getAD_Org_ID(), line.getM_Product_ID(), C_AcctSchema.getM_CostType_ID(), - C_AcctSchema_ID, get_TrxName()); - if (costs != null) { - for (MCost cost : costs) { - MPPOrderCost PP_Order_Cost = new MPPOrderCost(getCtx(), 0, get_TrxName()); - PP_Order_Cost.setPP_Order_ID(getPP_Order_ID()); - PP_Order_Cost.setC_AcctSchema_ID(cost.getC_AcctSchema_ID()); - PP_Order_Cost.setCumulatedAmt(cost.getCumulatedAmt()); - PP_Order_Cost.setCumulatedQty(cost.getCumulatedQty()); - PP_Order_Cost.setCurrentCostPriceLL(cost.getCurrentCostPriceLL()); - PP_Order_Cost.setCurrentCostPrice(cost.getCurrentCostPrice()); - PP_Order_Cost.setM_Product_ID(getM_Product_ID()); - PP_Order_Cost.setM_AttributeSetInstance_ID(cost.getM_AttributeSetInstance_ID()); - PP_Order_Cost.setM_CostElement_ID(cost.getM_CostElement_ID()); - PP_Order_Cost.save(); - } + for (MPPOrderBOMLine line : getLines()) { + costs = MCost.getCosts(getCtx(), getAD_Client_ID(), getAD_Org_ID(), line.getM_Product_ID(), + acctSchema.getM_CostType_ID(), acctSchema.get_ID(), + get_TrxName()); + for (MCost cost : costs) { + MPPOrderCost PP_Order_Cost = new MPPOrderCost(cost, get_ID(), get_TrxName()); + PP_Order_Cost.saveEx(); } } @@ -1199,116 +709,15 @@ public class MPPOrder extends X_PP_Order implements DocAction { return DocAction.STATUS_Completed; } // completeIt - public boolean isAvailable() { - StringBuffer sql = new StringBuffer("SELECT * FROM RV_PP_Order_Storage WHERE QtyOnHand - QtyRequiered < 0 AND PP_Order_ID=? "); - - PreparedStatement pstmt = null; - ResultSet rs = null; - try { - pstmt = DB.prepareStatement(sql.toString(), get_TrxName()); - pstmt.setInt(1, getPP_Order_ID()); - rs = pstmt.executeQuery(); - while (rs.next()) { - return false; - } - return true; - } - catch (Exception e) { - log.log(Level.SEVERE, "getLines - " + sql, e); - return false; - } - finally { - DB.close(rs, pstmt); - rs = null; - pstmt = null; - } + public boolean isAvailable() + { + String whereClause = "QtyOnHand - QtyRequiered < 0 AND PP_Order_ID=?"; + boolean notAvailable = new Query(getCtx(), X_RV_PP_Order_Storage.Table_Name, whereClause, get_TrxName()) + .setParameters(new Object[]{get_ID()}) + .match(); + return !notAvailable; } - /** - * Create Counter Document - */ - /* - private PP_Order createCounterDoc() - { - // Is this a counter doc ? - if (getRef_Order_ID() != 0) - return null; - - // Org Must be linked to BPartner - MOrg org = MOrg.get(getCtx(), getAD_Org_ID()); - int counterC_BPartner_ID = org.getLinkedC_BPartner_ID(); - if (counterC_BPartner_ID == 0) - return null; - // Business Partner needs to be linked to Org - MBPartner bp = new MBPartner (getCtx(), getC_BPartner_ID()); - int counterAD_Org_ID = bp.getAD_OrgBP_ID_Int(); - if (counterAD_Org_ID == 0) - return null; - - MBPartner counterBP = new MBPartner (getCtx(), counterC_BPartner_ID); - MOrgInfo counterOrgInfo = MOrgInfo.get(getCtx(), counterAD_Org_ID); - log.info("createCounterDoc - Counter BP=" + counterBP.getName()); - - // Document Type - int C_DocTypeTarget_ID = 0; - MDocTypeCounter counterDT = MDocTypeCounter.getCounterDocType(getCtx(), getC_DocType_ID()); - if (counterDT != null) - { - C_DocTypeTarget_ID = counterDT.getCounter_C_DocType_ID(); - log.fine("createCounterDoc - " + counterDT); - } - else // indirect - { - C_DocTypeTarget_ID = MDocTypeCounter.getCounterDocType_ID(getCtx(), getC_DocType_ID()); - log.fine("createCounterDoc - Indirect C_DocTypeTarget_ID=" + C_DocTypeTarget_ID); - } - // Deep Copy - MOrder counter = copyFrom (this, getDateOrdered(), - C_DocTypeTarget_ID, !isSOTrx(), true); - // - counter.setAD_Org_ID(counterAD_Org_ID); - counter.setM_Warehouse_ID(counterOrgInfo.getM_Warehouse_ID()); - // - counter.setBPartner(counterBP); - // Refernces (Should not be required - counter.setSalesRep_ID(getSalesRep_ID()); - counter.save(); - - // Update copied lines - MOrderLine[] counterLines = counter.getLines(true); - for (int i = 0; i < counterLines.length; i++) - { - MOrderLine counterLine = counterLines[i]; - counterLine.setOrder(counter); // copies header values (BP, etc.) - counterLine.setPrice(); - counterLine.setTax(); - counterLine.save(); - } - log.fine("createCounterDoc = " + counter); - - // Document Action - if (counterDT != null) - { - if (counterDT.getDocAction() != null) - { - counter.setDocAction(counterDT.getDocAction()); - counter.processIt(counterDT.getDocAction()); - counter.save(); - } - } - return counter; - } // createCounterDoc - */ - - /** - * Post Document - nothing - * @return true if success - */ - public boolean postIt() { - log.info("postIt - " + toString()); - return false; - } // postIt - /** * Void Document. * Set Qtys to 0 - Sales: reverse all documents @@ -1316,124 +725,16 @@ public class MPPOrder extends X_PP_Order implements DocAction { */ public boolean voidIt() { log.info("voidIt - " + toString()); - /* - MOrderLine[] lines = getLines(false); - for (int i = 0; i < lines.length; i++) - { - MOrderLine line = lines[i]; - BigDecimal old = line.getQtyOrdered(); - if (old.compareTo(Env.ZERO) != 0) - { - line.setQtyOrdered(Env.ZERO); - line.setLineNetAmt(Env.ZERO); - line.addDescription("Void (" + old + ")"); - line.save(); - } - } - // Clear Reservations - if (!reserveStock(null)) - { - m_processMsg = "Cannot unreserve Stock (void)"; - return false; - } - - if (!createReversals()) - return false; - */ - setProcessed(true); - setDocAction(DOCACTION_None); - return true; + return false; } // voidIt - /** - * Create Shipment/Invoice Reversals - * @param true if success - */ - /* - private boolean createReversals() - { - // Cancel only Sales - if (!isSOTrx()) - return true; - - log.fine("createReversals"); - StringBuffer info = new StringBuffer(); - - // Reverse All Shipments - info.append("@M_InOut_ID@:"); - MInOut[] shipments = getShipments(); - for (int i = 0; i < shipments.length; i++) - { - MInOut ship = shipments[i]; - // if closed - ignore - if (MInOut.DOCSTATUS_Closed.equals(ship.getDocStatus()) - || MInOut.DOCSTATUS_Reversed.equals(ship.getDocStatus()) - || MInOut.DOCSTATUS_Voided.equals(ship.getDocStatus()) ) - continue; - - // If not completed - void - otherwise reverse it - if (!MInOut.DOCSTATUS_Completed.equals(ship.getDocStatus())) - { - if (ship.voidIt()) - ship.setDocStatus(MInOut.DOCSTATUS_Voided); - } - else if (ship.reverseCorrectIt()) // completed shipment - { - ship.setDocStatus(MInOut.DOCSTATUS_Reversed); - info.append(" ").append(ship.getDocumentNo()); - } - else - { - m_processMsg = "Could not reverse Shipment " + ship; - return false; - } - ship.setDocAction(MInOut.DOCACTION_None); - ship.save(); - } // for all shipments - - // Reverse All Invoices - info.append(" - @C_Invoice_ID@:"); - MInvoice[] invoices = getInvoices(); - for (int i = 0; i < invoices.length; i++) - { - MInvoice invoice = invoices[i]; - // if closed - ignore - if (MInvoice.DOCSTATUS_Closed.equals(invoice.getDocStatus()) - || MInvoice.DOCSTATUS_Reversed.equals(invoice.getDocStatus()) - || MInvoice.DOCSTATUS_Voided.equals(invoice.getDocStatus()) ) - continue; - - // If not compleded - void - otherwise reverse it - if (!MInvoice.DOCSTATUS_Completed.equals(invoice.getDocStatus())) - { - if (invoice.voidIt()) - invoice.setDocStatus(MInvoice.DOCSTATUS_Voided); - } - else if (invoice.reverseCorrectIt()) // completed invoice - { - invoice.setDocStatus(MInvoice.DOCSTATUS_Reversed); - info.append(" ").append(invoice.getDocumentNo()); - } - else - { - m_processMsg = "Could not reverse Invoice " + invoice; - return false; - } - invoice.setDocAction(MInvoice.DOCACTION_None); - invoice.save(); - } // for all shipments - - m_processMsg = info.toString(); - return true; - } // createReversals - */ - /** * Close Document. * Cancel not delivered Qunatities * @return true if success */ - public boolean closeIt() { + public boolean closeIt() + { log.info(toString()); // Close Not delivered Qty - SO/PO @@ -1453,18 +754,9 @@ public class MPPOrder extends X_PP_Order implements DocAction { } }*/ - // 4Layers - Clear order quantities - if (!orderStock()) { - m_processMsg = "Cannot clear ordered quantities (close)"; - return false; - } - // 4Layers - end - - // Clear Reservations - if (!reserveStock(lines)) { - m_processMsg = "Cannot unreserve Stock (close)"; - return false; - } + orderStock(); // Clear Ordered Quantities + reserveStock(lines); // Clear Reservations + setProcessed(true); setDocAction(DOCACTION_None); return true; @@ -1494,49 +786,9 @@ public class MPPOrder extends X_PP_Order implements DocAction { */ public boolean reActivateIt() { log.info("reActivateIt - " + toString()); - - org.compiere.model.MDocType dt = MDocType.get(getCtx(), getC_DocType_ID()); - String DocSubTypeSO = dt.getDocSubTypeSO(); - - // PO - just re-open - if (!isSOTrx()) log.fine("reActivateIt - Existing documents not modified - " + dt); - // Reverse Direct Documents - if (MDocType.DOCSUBTYPESO_OnCreditOrder.equals(DocSubTypeSO) // (W)illCall(I)nvoice - || MDocType.DOCSUBTYPESO_WarehouseOrder.equals(DocSubTypeSO) // (W)illCall(P)ickup - || MDocType.DOCSUBTYPESO_POSOrder.equals(DocSubTypeSO)) // (W)alkIn(R)eceipt - { - //if (!createReversals()) - return false; - } - else { - log.fine("reActivateIt - Existing documents not modified - SubType=" + DocSubTypeSO); - } - - setDocAction(DOCACTION_Complete); - setProcessed(false); - return true; + return false; } // reActivateIt - /************************************************************************* - * Get Summary - * @return Summary of Document - */ - /* - public String getSummary() - { - StringBuffer sb = new StringBuffer(); - sb.append(getDocumentNo()); - // : Grand Total = 123.00 (#1) - sb.append(": "). - append(Msg.translate(getCtx(),"GrandTotal")).append("=").append(getGrandTotal()) - .append(" (#").append(getLines(true).length).append(")"); - // - Description - if (getDescription() != null && getDescription().length() > 0) - sb.append(" - ").append(getDescription()); - return sb.toString(); - } // getSummary - */ - /** * Get Document Owner (Responsible) * @return AD_User_ID @@ -1550,9 +802,8 @@ public class MPPOrder extends X_PP_Order implements DocAction { * @return amount */ - public java.math.BigDecimal getApprovalAmt() { - //return getGrandTotal(); - return new BigDecimal(0); + public BigDecimal getApprovalAmt() { + return Env.ZERO; } // getApprovalAmt public int getC_Currency_ID() { @@ -1567,10 +818,6 @@ public class MPPOrder extends X_PP_Order implements DocAction { return ""; } - /** - * Create PDF - * @return File or null - */ public File createPDF() { try { File temp = File.createTempFile(get_TableName() + get_ID() + "_", ".pdf"); @@ -1588,8 +835,9 @@ public class MPPOrder extends X_PP_Order implements DocAction { * @return file if success */ public File createPDF(File file) { - ReportEngine re = ReportEngine.get(getCtx(), ReportEngine.ORDER, getPP_Order_ID()); - if (re == null) return null; + ReportEngine re = ReportEngine.get(getCtx(), ReportEngine.MANUFACTURING_ORDER, getPP_Order_ID()); + if (re == null) + return null; return re.getPDF(file); } // createPDF @@ -1598,87 +846,23 @@ public class MPPOrder extends X_PP_Order implements DocAction { * @return document info (untranslated) */ public String getDocumentInfo() { - org.compiere.model.MDocType dt = MDocType.get(getCtx(), getC_DocType_ID()); + MDocType dt = MDocType.get(getCtx(), getC_DocType_ID()); return dt.getName() + " " + getDocumentNo(); } // getDocumentInfo - - public boolean setBOMLineQtys(MPPOrderBOMLine obl) { - BigDecimal QtyOrdered = getQtyOrdered(); - /*MPPOrderBOMLine[] obl = MPPOrder.getLines(getPP_Order_ID()); - for (int i = 0 ; i < obl.length ; i ++) - { - System.out.println("Product" + obl[i].getM_Product_ID()); - if (obl[i].isQtyPercentage()) - { - BigDecimal qty = obl[i].getQtyBatch().multiply(QtyOrdered); - if(obl[i].getComponentType().equals(obl[i].COMPONENTTYPE_Packing)) - obl[i].setQtyRequiered(qty.divide(new BigDecimal(100),0,qty.ROUND_UP)); - if (obl[i].getComponentType().equals(obl[i].COMPONENTTYPE_Component) || obl[i].getComponentType().equals(obl[i].COMPONENTTYPE_Phantom)) - obl[i].setQtyRequiered(qty.divide(new BigDecimal(100),4,qty.ROUND_UP)); - else if (obl[i].getComponentType().equals(obl[i].COMPONENTTYPE_Tools)) - obl[i].setQtyRequiered(obl[i].getQtyBOM()); - - } - else - { - if (obl[i].getComponentType().equals(obl[i].COMPONENTTYPE_Component) || obl[i].getComponentType().equals(obl[i].COMPONENTTYPE_Phantom)) - obl[i].setQtyRequiered(obl[i].getQtyBOM().multiply(QtyOrdered)); - if (obl[i].getComponentType().equals(obl[i].COMPONENTTYPE_Packing)) - obl[i].setQtyRequiered(obl[i].getQtyBOM().multiply(QtyOrdered)); - else if (obl[i].getComponentType().equals(obl[i].COMPONENTTYPE_Tools)) - obl[i].setQtyRequiered(obl[i].getQtyBOM()); - } - - // Set Scrap of Component - BigDecimal Scrap = obl[i].getScrap(); - - if (!Scrap.equals(Env.ZERO)) - { - Scrap = Scrap.divide(new BigDecimal(100),4,BigDecimal.ROUND_UP); - obl[i].setQtyRequiered(obl[i].getQtyRequiered().divide( Env.ONE.subtract(Scrap) , 4 ,BigDecimal.ROUND_HALF_UP )); - } - - //obl[i].save(trxName); - if (obl[i].getComponentType().equals(obl[i].COMPONENTTYPE_Phantom)) - { - obl[i].setQtyRequiered(Env.ZERO); - if(!obl[i].save(get_TrxName())) - { - throw new IllegalStateException("Could not Set Qty Line Manufacturing Order BOM"); - } - - } - }*/ - - if (obl.isQtyPercentage()) { - BigDecimal qty = obl.getQtyBatch().multiply(QtyOrdered); - if (obl.getComponentType().equals(obl.COMPONENTTYPE_Packing)) obl.setQtyRequiered(qty.divide(new BigDecimal(100), 8, qty.ROUND_UP)); - if (obl.getComponentType().equals(obl.COMPONENTTYPE_Component) || obl.getComponentType().equals(obl.COMPONENTTYPE_Phantom)) - obl.setQtyRequiered(qty.divide(new BigDecimal(100), 8, qty.ROUND_UP)); - else if (obl.getComponentType().equals(obl.COMPONENTTYPE_Tools)) obl.setQtyRequiered(obl.getQtyBOM()); - + + private void deletePO(String tableName, String whereClause, Object[] params) + { + // TODO: refactor this method and move it to org.compiere.model.Query class + POResultSet rs = new Query(getCtx(), tableName, whereClause, get_TrxName()) + .setParameters(params) + .scroll(); + try { + while(rs.hasNext()) { + rs.next().deleteEx(true); + } } - else { - if (obl.getComponentType().equals(obl.COMPONENTTYPE_Component) || obl.getComponentType().equals(obl.COMPONENTTYPE_Phantom)) - obl.setQtyRequiered(obl.getQtyBOM().multiply(QtyOrdered)); - if (obl.getComponentType().equals(obl.COMPONENTTYPE_Packing)) - obl.setQtyRequiered(obl.getQtyBOM().multiply(QtyOrdered)); - else if (obl.getComponentType().equals(obl.COMPONENTTYPE_Tools)) obl.setQtyRequiered(obl.getQtyBOM()); + finally { + rs.close(); } - - // Set Scrap of Component - BigDecimal Scrap = obl.getScrap(); - - if (!Scrap.equals(Env.ZERO)) { - Scrap = Scrap.divide(new BigDecimal(100), 8, BigDecimal.ROUND_UP); - obl.setQtyRequiered(obl.getQtyRequiered().divide(Env.ONE.subtract(Scrap), 8, BigDecimal.ROUND_HALF_UP)); - } - - //obl[i].save(trxName); - if (obl.getComponentType().equals(obl.COMPONENTTYPE_Phantom)) { - obl.setQtyRequiered(Env.ZERO); - } - - return true; } -} // MOrder +} // MPPOrder diff --git a/base/src/org/eevolution/model/MPPOrderBOM.java b/base/src/org/eevolution/model/MPPOrderBOM.java index b63378db9a..0453e99d04 100644 --- a/base/src/org/eevolution/model/MPPOrderBOM.java +++ b/base/src/org/eevolution/model/MPPOrderBOM.java @@ -24,9 +24,13 @@ import java.util.Properties; * * @author Victor Perez www.e-evolution.com * @version $Id: MOrder.java,v 1.40 2004/04/13 04:19:30 vpj-cd Exp $ + * + * @author Teo Sarca, www.arhipac.ro */ public class MPPOrderBOM extends X_PP_Order_BOM { + private static final long serialVersionUID = 1L; + /** * Default Constructor * @param ctx context @@ -51,6 +55,37 @@ public class MPPOrderBOM extends X_PP_Order_BOM { super (ctx, rs, trxName); } // MOrder + + /** + * Peer constructor + * @param bom + * @param PP_Order_ID + * @param trxName + */ + public MPPOrderBOM(MPPProductBOM bom, int PP_Order_ID, String trxName) + { + this(bom.getCtx(), 0, trxName); + // + setBOMType(bom.getBOMType()); + setBOMUse(bom.getBOMUse()); + setM_ChangeNotice_ID(bom.getM_ChangeNotice_ID()); + setHelp(bom.getHelp()); + //setCopyFrom(bom.getCopyFrom()); + setProcessing(bom.isProcessing()); + setHelp(bom.getHelp()); + setDescription(bom.getDescription()); + setM_AttributeSetInstance_ID(bom.getM_AttributeSetInstance_ID()); + setM_Product_ID(bom.getM_Product_ID()); + setName(bom.getName()); + setRevision(bom.getRevision()); + setValidFrom(bom.getValidFrom()); + setValidTo(bom.getValidTo()); + setValue(bom.getValue()); + setDocumentNo(bom.getDocumentNo()); + setC_UOM_ID(bom.getC_UOM_ID()); + // + setPP_Order_ID(PP_Order_ID); + } /** * String Representation diff --git a/base/src/org/eevolution/model/MPPOrderBOMLine.java b/base/src/org/eevolution/model/MPPOrderBOMLine.java index 8e955e2835..2b8dcca23a 100644 --- a/base/src/org/eevolution/model/MPPOrderBOMLine.java +++ b/base/src/org/eevolution/model/MPPOrderBOMLine.java @@ -28,6 +28,8 @@ import org.compiere.util.Env; * * @author Victor Perez www.e-evolution.com * @version $Id: MOrderLine.java,v 1.22 2004/03/22 07:15:03 vpj-cd Exp $ + * + * @author Teo Sarca, www.arhipac.ro */ public class MPPOrderBOMLine extends X_PP_Order_BOMLine { @@ -63,9 +65,50 @@ public class MPPOrderBOMLine extends X_PP_Order_BOMLine { super (ctx, rs,trxName); } // MOrderLine - + + /** + * Peer constructor + * @param bomLine + * @param PP_Order_ID + * @param PP_Order_BOM_ID + * @param M_Warehouse_ID + * @param trxName + */ + public MPPOrderBOMLine(MPPProductBOMLine bomLine, + int PP_Order_ID, int PP_Order_BOM_ID, int M_Warehouse_ID, + String trxName) + { + this(bomLine.getCtx(), 0, trxName); + + this.setPP_Order_BOM_ID(PP_Order_BOM_ID); + this.setPP_Order_ID(PP_Order_ID); + this.setM_Warehouse_ID(M_Warehouse_ID); + // + this.setM_ChangeNotice_ID(bomLine.getM_ChangeNotice_ID()); + this.setDescription(bomLine.getDescription()); + this.setHelp(bomLine.getHelp()); + this.setAssay(bomLine.getAssay()); + this.setQtyBatch(bomLine.getQtyBatch()); + this.setQtyBOM(bomLine.getQtyBOM()); + this.setIsQtyPercentage(bomLine.isQtyPercentage()); + this.setComponentType(bomLine.getComponentType()); + this.setC_UOM_ID(bomLine.getC_UOM_ID()); + this.setForecast(bomLine.getForecast()); + this.setIsCritical(bomLine.isCritical()); + this.setIssueMethod(bomLine.getIssueMethod()); + this.setLeadTimeOffset(bomLine.getLeadTimeOffset()); + this.setM_AttributeSetInstance_ID(bomLine.getM_AttributeSetInstance_ID()); + this.setM_Product_ID(bomLine.getM_Product_ID()); + this.setScrap(bomLine.getScrap()); + this.setValidFrom(bomLine.getValidFrom()); + this.setValidTo(bomLine.getValidTo()); + this.setBackflushGroup(bomLine.getBackflushGroup()); + } + private MPPOrder m_parent = null; - private MProduct m_product = null; + private MProduct m_product = null; + /** Qty used for exploding this BOM Line */ + private BigDecimal m_qtyToExplode = null; @Override @@ -78,26 +121,26 @@ public class MPPOrderBOMLine extends X_PP_Order_BOMLine int ii = DB.getSQLValue (get_TrxName(), sql, getPP_Order_ID()); setLine (ii); } + + // If Phantom, we need to explode this line (see afterSave): + if(newRecord && COMPONENTTYPE_Phantom.equals(getComponentType())) + { + m_qtyToExplode = getQtyRequiered(); + setQtyRequiered(Env.ZERO); + } + return true; } - - /************************************************************************** - * after Save - * @param newRecord new - * @return save - */ @Override - protected boolean afterSave(boolean newRecord, boolean success) { - - if (!newRecord) - return success; - //Qty Ordered to Phantom - BigDecimal QtyOrdered = getQtyRequiered(); + protected boolean afterSave(boolean newRecord, boolean success) + { + if (!success) + return false; log.fine(" Parent Product" + getM_Product_ID() + " getQtyBatch" + getQtyBatch() + " getQtyRequiered" + getQtyRequiered() + " QtyScrap" + getQtyScrap()); - //Phantom - if(getComponentType().equals(MPPProductBOMLine.COMPONENTTYPE_Phantom)) + // + if(m_qtyToExplode != null) { MProduct parent = MProduct.get(getCtx(), getM_Product_ID()); int PP_Product_BOM_ID = MPPProductBOM.getBOMSearchKey(getCtx(), parent); @@ -110,67 +153,16 @@ public class MPPOrderBOMLine extends X_PP_Order_BOMLine MPPProductBOMLine[] PP_Product_BOMline = bom.getLines(); for(int i = 0 ; i < PP_Product_BOMline.length ; i++ ) { - MPPOrderBOMLine PP_Order_BOMLine = new MPPOrderBOMLine(getCtx(), 0, get_TrxName()); - MProduct component = MProduct.get(getCtx(),PP_Product_BOMline[i].getM_Product_ID()); - PP_Order_BOMLine.setDescription(PP_Product_BOMline[i].getDescription()); - PP_Order_BOMLine.setHelp(PP_Product_BOMline[i].getHelp()); - PP_Order_BOMLine.setM_ChangeNotice_ID(PP_Product_BOMline[i].getM_ChangeNotice_ID()); - PP_Order_BOMLine.setAssay(PP_Product_BOMline[i].getAssay()); - PP_Order_BOMLine.setQtyBatch(PP_Product_BOMline[i].getQtyBatch()); - PP_Order_BOMLine.setQtyBOM(PP_Product_BOMline[i].getQtyBOM()); - PP_Order_BOMLine.setIsQtyPercentage(PP_Product_BOMline[i].isQtyPercentage()); - PP_Order_BOMLine.setComponentType(PP_Product_BOMline[i].getComponentType()); - PP_Order_BOMLine.setC_UOM_ID(PP_Product_BOMline[i].getC_UOM_ID()); - PP_Order_BOMLine.setForecast(PP_Product_BOMline[i].getForecast()); - PP_Order_BOMLine.setIsCritical(PP_Product_BOMline[i].isCritical()); - PP_Order_BOMLine.setIssueMethod(PP_Product_BOMline[i].getIssueMethod()); - PP_Order_BOMLine.setLeadTimeOffset(PP_Product_BOMline[i].getLeadTimeOffset()); - PP_Order_BOMLine.setM_AttributeSetInstance_ID(PP_Product_BOMline[i].getM_AttributeSetInstance_ID()); - PP_Order_BOMLine.setPP_Order_BOM_ID(getPP_Order_BOM_ID()); - PP_Order_BOMLine.setPP_Order_ID(getPP_Order_ID()); - PP_Order_BOMLine.setM_Product_ID(PP_Product_BOMline[i].getM_Product_ID()); - PP_Order_BOMLine.setScrap(PP_Product_BOMline[i].getScrap()); - PP_Order_BOMLine.setValidFrom(PP_Product_BOMline[i].getValidFrom()); - PP_Order_BOMLine.setValidTo(PP_Product_BOMline[i].getValidTo()); - PP_Order_BOMLine.setM_Warehouse_ID(getM_Warehouse_ID()); - - if (PP_Order_BOMLine.isQtyPercentage()) - { - BigDecimal qty = PP_Order_BOMLine.getQtyBatch().multiply(QtyOrdered); - log.fine("product:"+component.getName() +" Qty:"+qty + " QtyOrdered:"+ QtyOrdered + " PP_Order_BOMLine.getQtyBatch():" + PP_Order_BOMLine.getQtyBatch()); - if(PP_Order_BOMLine.getComponentType().equals(COMPONENTTYPE_Packing)) - PP_Order_BOMLine.setQtyRequiered(qty.divide(new BigDecimal(100),8,BigDecimal.ROUND_UP)); - else if (PP_Order_BOMLine.getComponentType().equals(COMPONENTTYPE_Component) || PP_Order_BOMLine.getComponentType().equals(COMPONENTTYPE_Phantom)) - PP_Order_BOMLine.setQtyRequiered(qty.divide(new BigDecimal(100),8,BigDecimal.ROUND_UP)); - else if (PP_Order_BOMLine.getComponentType().equals(COMPONENTTYPE_Tools)) - PP_Order_BOMLine.setQtyRequiered(PP_Order_BOMLine.getQtyBOM()); - - //System.out.println("PP_Order_BOMLinegetQtyRequiered" + PP_Order_BOMLine.getQtyRequiered()); - } - else - { - //System.out.println("product: "+product.getName() + " QtyOrdered:"+ QtyOrdered + " PP_Order_BOMLine.getQtyBOM():" + PP_Order_BOMLine.getQtyBOM()); - if (PP_Order_BOMLine.getComponentType().equals(COMPONENTTYPE_Component) || PP_Order_BOMLine.getComponentType().equals(COMPONENTTYPE_Phantom)) - PP_Order_BOMLine.setQtyRequiered(PP_Order_BOMLine.getQtyBOM().multiply(QtyOrdered)); - else if (PP_Order_BOMLine.getComponentType().equals(COMPONENTTYPE_Packing)) - PP_Order_BOMLine.setQtyRequiered(PP_Order_BOMLine.getQtyBOM().multiply(QtyOrdered)); - else if (PP_Order_BOMLine.getComponentType().equals(COMPONENTTYPE_Tools)) - PP_Order_BOMLine.setQtyRequiered(PP_Order_BOMLine.getQtyBOM()); - } - - // Set Scrap of Component - BigDecimal Scrap = PP_Order_BOMLine.getScrap(); - if (!Scrap.equals(Env.ZERO)) - { - Scrap = Scrap.divide(new BigDecimal(100),8,BigDecimal.ROUND_UP); - PP_Order_BOMLine.setQtyRequiered(PP_Order_BOMLine.getQtyRequiered().divide( Env.ONE.subtract(Scrap) , 8 ,BigDecimal.ROUND_HALF_UP )); - } - System.out.println("Cantidad Requerida" + PP_Order_BOMLine.getQtyRequiered()); - PP_Order_BOMLine.saveEx(); + MPPOrderBOMLine PP_Order_BOMLine = new MPPOrderBOMLine(PP_Product_BOMline[i], + getPP_Order_ID(), getPP_Order_BOM_ID(), + getM_Warehouse_ID(), + get_TrxName()); + PP_Order_BOMLine.setQtyOrdered(m_qtyToExplode); + PP_Order_BOMLine.saveEx(); } } - - }// end Phantom + m_qtyToExplode = null; + } return true; } @@ -197,4 +189,48 @@ public class MPPOrderBOMLine extends X_PP_Order_BOMLine return m_parent; } // getParent + public void setQtyOrdered(BigDecimal QtyOrdered) + { + // Set Qty Required + if (this.isQtyPercentage()) + { + BigDecimal qty = this.getQtyBatch().multiply(QtyOrdered); + if (this.getComponentType().equals(COMPONENTTYPE_Component) + || this.getComponentType().equals(COMPONENTTYPE_Phantom)) + { + this.setQtyRequiered(qty.divide(Env.ONEHUNDRED, 8, BigDecimal.ROUND_UP)); + } + else if (this.getComponentType().equals(COMPONENTTYPE_Packing)) + { + this.setQtyRequiered(qty.divide(Env.ONEHUNDRED, 8, BigDecimal.ROUND_UP)); + } + else if (this.getComponentType().equals(COMPONENTTYPE_Tools)) + { + this.setQtyRequiered(this.getQtyBOM()); + } + } + else + { + if (this.getComponentType().equals(COMPONENTTYPE_Component) + || this.getComponentType().equals(COMPONENTTYPE_Phantom)) + { + this.setQtyRequiered(this.getQtyBOM().multiply(QtyOrdered)); + } + else if (this.getComponentType().equals(COMPONENTTYPE_Packing)) + { + this.setQtyRequiered(this.getQtyBOM().multiply(QtyOrdered)); + } + else if (this.getComponentType().equals(COMPONENTTYPE_Tools)) + { + this.setQtyRequiered(this.getQtyBOM()); + } + } + + // Set Scrap of Component + BigDecimal Scrap = this.getScrap(); + if (Scrap.signum() != 0) { + Scrap = Scrap.divide(Env.ONEHUNDRED, 8, BigDecimal.ROUND_UP); + this.setQtyRequiered(this.getQtyRequiered().divide(Env.ONE.subtract(Scrap), 8, BigDecimal.ROUND_HALF_UP)); + } + } } diff --git a/base/src/org/eevolution/model/MPPOrderCost.java b/base/src/org/eevolution/model/MPPOrderCost.java index df4644f84e..de1d4401f5 100644 --- a/base/src/org/eevolution/model/MPPOrderCost.java +++ b/base/src/org/eevolution/model/MPPOrderCost.java @@ -16,12 +16,10 @@ package org.eevolution.model; -import java.util.*; -import java.sql.*; -import java.math.*; +import java.sql.ResultSet; +import java.util.Properties; -import org.compiere.util.*; -import org.compiere.model.*; +import org.compiere.model.MCost; /** * Order Model. @@ -31,9 +29,13 @@ import org.compiere.model.*; * * @author Victor Perez www.e-evolution.com * @version $Id: MOrder.java,v 1.40 2004/04/13 04:19:30 vpj-cd Exp $ + * + * @author Teo Sarca, www.arhipac.ro */ public class MPPOrderCost extends X_PP_Order_Cost { + private static final long serialVersionUID = 1L; + /** * Default Constructor * @param ctx context @@ -41,63 +43,36 @@ public class MPPOrderCost extends X_PP_Order_Cost */ public MPPOrderCost(Properties ctx, int PP_Order_Cost_ID,String trxName) { - super (ctx, PP_Order_Cost_ID,trxName); - // New - if ( PP_Order_Cost_ID == 0) - { - - } + super (ctx, PP_Order_Cost_ID, trxName); } // MOrder - - /** - * Load Constructor - * @param ctx context - * @param rs result set record + + /** + * Peer constructor + * @param cost + * @param PP_Order_ID */ - public MPPOrderCost(Properties ctx, MCost m_cost, int PP_Order_ID, String trxName) + public MPPOrderCost(MCost cost, int PP_Order_ID, String trxName) { - super (ctx, 0,trxName); - - setC_AcctSchema_ID(m_cost.getC_AcctSchema_ID()); - - setCumulatedAmt(m_cost.getCumulatedAmt()); - setCumulatedQty(m_cost.getCumulatedQty()); - setCurrentCostPrice(m_cost.getCurrentCostPrice()); - setCurrentCostPriceLL(m_cost.getCurrentCostPriceLL()); - setM_Product_ID(m_cost.getM_Product_ID()); - setM_CostElement_ID(m_cost.getM_CostElement_ID()); - setM_AttributeSetInstance_ID(m_cost.getM_AttributeSetInstance_ID()); - save(get_TrxName()); - - } // MOrder + this(cost.getCtx(), 0, trxName); + + setPP_Order_ID(PP_Order_ID); + setC_AcctSchema_ID(cost.getC_AcctSchema_ID()); + setCumulatedAmt(cost.getCumulatedAmt()); + setCumulatedQty(cost.getCumulatedQty()); + setCurrentCostPriceLL(cost.getCurrentCostPriceLL()); + setCurrentCostPrice(cost.getCurrentCostPrice()); + setM_Product_ID(cost.getM_Product_ID()); + setM_AttributeSetInstance_ID(cost.getM_AttributeSetInstance_ID()); + setM_CostElement_ID(cost.getM_CostElement_ID()); + } - - - /** * Load Constructor * @param ctx context * @param rs result set record */ - public MPPOrderCost(Properties ctx, ResultSet rs,String trxName) + public MPPOrderCost(Properties ctx, ResultSet rs, String trxName) { - super (ctx, rs,"PP_Order_Cost"); + super (ctx, rs, trxName); } // MOrder - - /** - * Overwrite Client/Org if required - * @param AD_Client_ID client - * @param AD_Org_ID org - */ - public void setClientOrg (int AD_Client_ID, int AD_Org_ID) - { - super.setClientOrg(AD_Client_ID, AD_Org_ID); - } // setClientOrg - - - // save - - - - -} // MOrder +} diff --git a/base/src/org/eevolution/model/MPPOrderNode.java b/base/src/org/eevolution/model/MPPOrderNode.java index ad73cf8fc7..4d6af345ab 100644 --- a/base/src/org/eevolution/model/MPPOrderNode.java +++ b/base/src/org/eevolution/model/MPPOrderNode.java @@ -17,6 +17,7 @@ package org.eevolution.model; import java.awt.Point; +import java.math.BigDecimal; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; @@ -27,18 +28,21 @@ import org.compiere.model.Query; import org.compiere.util.CCache; import org.compiere.util.Env; import org.compiere.util.Msg; +import org.compiere.wf.MWFNode; /** - * Workflow Node Model + * PP Order Workflow Node Model * * @author Jorg Janke * @version $Id: MWFNode.java,v 1.2 2006/07/30 00:51:05 jjanke Exp $ * - * @author Teo Sarca, SC ARHIPAC SERVICE SRL - *
  • BF [ 2041799 ] Can't delete PP_Order_Node (Not found=PP_Order_Node_Trl) + * @author Teo Sarca, http://www.arhipac.ro */ public class MPPOrderNode extends X_PP_Order_Node { + private static final long serialVersionUID = 1L; + + /** * Get WF Node from Cache * @param ctx context @@ -108,6 +112,62 @@ public class MPPOrderNode extends X_PP_Order_Node m_durationBaseMS = wf.getDurationBaseSec() * 1000; } // MPPOrderNode + /** + * Peer constructor + * @param wfNode + * @param PP_Order_Workflow + * @param qtyOrdered + * @param trxName + */ + public MPPOrderNode (MWFNode wfNode, MPPOrderWorkflow PP_Order_Workflow, + BigDecimal qtyOrdered, + String trxName) + { + this(wfNode.getCtx(), 0, trxName); + setPP_Order_ID(PP_Order_Workflow.getPP_Order_ID()); + setPP_Order_Workflow_ID(PP_Order_Workflow.getPP_Order_Workflow_ID()); + // + setAction(wfNode.getAction()); + setAD_WF_Node_ID(wfNode.getAD_WF_Node_ID()); + setAD_WF_Responsible_ID(wfNode.getAD_WF_Responsible_ID()); + setAD_Workflow_ID(wfNode.getAD_Workflow_ID()); + setCost(wfNode.getCost()); + setDuration(wfNode.getDuration()); + setEntityType(wfNode.getEntityType()); + setIsCentrallyMaintained(wfNode.isCentrallyMaintained()); + setJoinElement(wfNode.getJoinElement()); // X + setLimit(wfNode.getLimit()); + setName(wfNode.getName()); + setPriority(wfNode.getPriority()); + setSplitElement(wfNode.getSplitElement()); // X + setSubflowExecution(wfNode.getSubflowExecution()); + setValue(wfNode.getValue()); + setS_Resource_ID(wfNode.getS_Resource_ID()); + setSetupTime(wfNode.getSetupTime()); + setSetupTimeRequiered(wfNode.getSetupTime()); + BigDecimal time = new BigDecimal(wfNode.getDuration()).multiply(qtyOrdered); + setDurationRequiered(time.intValue()); + setMovingTime(wfNode.getMovingTime()); + setWaitingTime(wfNode.getWaitingTime()); + setWorkingTime(wfNode.getWorkingTime()); + setQueuingTime(wfNode.getQueuingTime()); + setXPosition(wfNode.getXPosition()); + setYPosition(wfNode.getYPosition()); + setDocAction(wfNode.getDocAction()); + setAD_Column_ID(wfNode.getAD_Column_ID()); + setAD_Form_ID(wfNode.getAD_Form_ID()); + setAD_Image_ID(wfNode.getAD_Image_ID()); + setAD_Window_ID(wfNode.getAD_Window_ID()); + setAD_Process_ID(wfNode.getAD_Process_ID()); + setAttributeName(wfNode.getAttributeName()); + setAttributeValue(wfNode.getAttributeValue()); + setC_BPartner_ID(wfNode.getC_BPartner_ID()); + setStartMode(wfNode.getStartMode()); + setFinishMode(wfNode.getFinishMode()); + setValidFrom(wfNode.getValidFrom()); + setValidTo(wfNode.getValidTo()); + } + /** * Load Constructor - save to cache * @param ctx context diff --git a/base/src/org/eevolution/model/MPPOrderNodeNext.java b/base/src/org/eevolution/model/MPPOrderNodeNext.java index 45df1c1b69..7e210e578c 100644 --- a/base/src/org/eevolution/model/MPPOrderNodeNext.java +++ b/base/src/org/eevolution/model/MPPOrderNodeNext.java @@ -16,22 +16,24 @@ *****************************************************************************/ package org.eevolution.model; -import java.sql.*; -import java.util.*; +import java.sql.ResultSet; +import java.util.Properties; -import org.compiere.model.*; -import org.compiere.process.*; -import java.util.logging.*; -import org.compiere.util.*; +import org.compiere.util.DB; +import org.compiere.wf.MWFNodeNext; /** - * Workflow Node Next - Transition + * PP Order Workflow Node Next - Transition * * @author Jorg Janke * @version $Id: MPPOrdeNodeNext.java,v 1.3 2006/10/06 00:42:24 jjanke Exp $ + * + * @author Teo Sarca, http://www.arhipac.ro */ public class MPPOrderNodeNext extends X_PP_Order_NodeNext { + private static final long serialVersionUID = 1L; + /** * Standard Costructor * @param ctx context @@ -75,8 +77,28 @@ public class MPPOrderNodeNext extends X_PP_Order_NodeNext setPP_Order_Next_ID(PP_Order_Next_ID); } // MPPOrderNodeNext - /** Transition Conditions */ - //private MWFNextCondition[] m_conditions = null; + /** + * Peer constructor + * @param wfNodeNext + * @param PP_Order_Node + * @param trxName + */ + public MPPOrderNodeNext (MWFNodeNext wfNodeNext, MPPOrderNode PP_Order_Node, String trxName) + { + this(wfNodeNext.getCtx(), 0, trxName); + setPP_Order_Node_ID(PP_Order_Node.get_ID()); + setPP_Order_ID(PP_Order_Node.getPP_Order_ID()); + setPP_Order_Next_ID(0); + // + setAD_WF_Node_ID(wfNodeNext.getAD_WF_Node_ID()); + setAD_WF_Next_ID(wfNodeNext.getAD_WF_Next_ID()); + setDescription(wfNodeNext.getDescription()); + setEntityType(wfNodeNext.getEntityType()); + setIsStdUserWorkflow(wfNodeNext.isStdUserWorkflow()); + setSeqNo(wfNodeNext.getSeqNo()); + setTransitionCode(wfNodeNext.getTransitionCode()); + } + /** From (Split Eleemnt) is AND */ public Boolean m_fromSplitAnd = null; /** To (Join Element) is AND */ @@ -109,11 +131,6 @@ public class MPPOrderNodeNext extends X_PP_Order_NodeNext return sb.toString (); } // toString - - - - - /** * Split Element is AND * @return Returns the from Split And. @@ -160,4 +177,12 @@ public class MPPOrderNodeNext extends X_PP_Order_NodeNext m_toJoinAnd = new Boolean(toJoinAnd); } // setToJoinAnd + public void setPP_Order_Next_ID() + { + final String sql = "SELECT PP_Order_Node_ID FROM PP_Order_Node " + + " WHERE PP_Order_ID=? AND AD_WF_Node_ID=? AND AD_Client_ID=?"; + int id = DB.getSQLValue(get_TrxName(), sql, getPP_Order_ID(), getAD_WF_Next_ID(), getAD_Client_ID()); + if (id > 0) + setPP_Order_Next_ID(id); + } } // MPPOrderNodeNext diff --git a/base/src/org/eevolution/model/MPPOrderWorkflow.java b/base/src/org/eevolution/model/MPPOrderWorkflow.java index 758b0a7d3b..7912373a83 100644 --- a/base/src/org/eevolution/model/MPPOrderWorkflow.java +++ b/base/src/org/eevolution/model/MPPOrderWorkflow.java @@ -26,16 +26,20 @@ import java.util.logging.Level; import org.compiere.model.MClient; import org.compiere.model.Query; import org.compiere.util.CCache; -import org.compiere.util.Env; +import org.compiere.wf.MWorkflow; /** - * WorkFlow Model + * PP Order WorkFlow Model * * @author Jorg Janke * @version $Id: MPPOrderWorkflow.java,v 1.4 2006/07/30 00:51:05 jjanke Exp $ + * + * @author Teo Sarca, http://www.arhipac.ro */ public class MPPOrderWorkflow extends X_PP_Order_Workflow { + private static final long serialVersionUID = 1L; + /** * Get Workflow from Cache * @param ctx context @@ -97,6 +101,50 @@ public class MPPOrderWorkflow extends X_PP_Order_Workflow super(ctx, rs, trxName); loadNodes(); } // Workflow + + /** + * Peer constructor + * @param workflow + * @param PP_Order_ID + * @param trxName + */ + public MPPOrderWorkflow (MWorkflow workflow, int PP_Order_ID, String trxName) + { + this(workflow.getCtx(), 0, trxName); + setPP_Order_ID(PP_Order_ID); + // + setValue(workflow.getValue()); + setWorkflowType(workflow.getWorkflowType()); + setQtyBatchSize(workflow.getQtyBatchSize()); + setName(workflow.getName()); + setAccessLevel(workflow.getAccessLevel()); + setAuthor(workflow.getAuthor()); + setDurationUnit(workflow.getDurationUnit()); + setDuration(workflow.getDuration()); + setEntityType(workflow.getEntityType()); + setIsDefault(workflow.isDefault()); + setPublishStatus(workflow.getPublishStatus()); + setVersion(workflow.getVersion()); + setCost(workflow.getCost()); + setWaitingTime(workflow.getWaitingTime()); + setWorkingTime(workflow.getWorkingTime()); + setAD_WF_Responsible_ID(workflow.getAD_WF_Responsible_ID()); + setAD_Workflow_ID(workflow.getAD_Workflow_ID()); + setLimit(workflow.getLimit()); + setPriority(workflow.getPriority()); + setValidateWorkflow(workflow.getValidateWorkflow()); + setS_Resource_ID(workflow.getS_Resource_ID()); + setQueuingTime(workflow.getQueuingTime()); + setSetupTime(workflow.getSetupTime()); + setMovingTime(workflow.getMovingTime()); + setProcessType(workflow.getProcessType()); + setAD_Table_ID(workflow.getAD_Table_ID()); + setAD_WF_Node_ID(workflow.getAD_WF_Node_ID()); + setAD_WorkflowProcessor_ID(workflow.getAD_WorkflowProcessor_ID()); + setDescription(workflow.getDescription()); + setValidFrom(workflow.getValidFrom()); + setValidTo(workflow.getValidTo()); + } /** WF Nodes */ private List m_nodes = null; @@ -104,17 +152,16 @@ public class MPPOrderWorkflow extends X_PP_Order_Workflow /** * Load All Nodes */ - private void loadNodes() + protected void loadNodes() { final String whereClause = MPPOrderNode.COLUMNNAME_PP_Order_Workflow_ID+"=? AND IsActive=?"; m_nodes = new Query(getCtx(), MPPOrderNode.Table_Name, whereClause, get_TrxName()) - .setParameters(new Object[]{get_ID(), "Y"}) - .list(); + .setParameters(new Object[]{get_ID(), "Y"}) + .list(); log.fine("#" + m_nodes.size()); } // loadNodes - - /************************************************************************** + /** * Get Number of Nodes * @return number of nodes */ @@ -401,23 +448,7 @@ public class MPPOrderWorkflow extends X_PP_Order_Workflow return sb.toString (); } // toString - /************************************************************************** - * Before Save - * @param newRecord new - * @return true - */ - protected boolean beforeSave (boolean newRecord) - { - //validate(); - return true; - } // beforeSave - - /** - * After Save. - * @param newRecord new record - * @param success success - * @return true if save complete (if not overwritten true) - */ + @Override protected boolean afterSave (boolean newRecord, boolean success) { log.fine("Success=" + success); @@ -432,9 +463,6 @@ public class MPPOrderWorkflow extends X_PP_Order_Workflow return success; } // afterSave - - - /** * Get Duration Base in Seconds * @return duration unit in seconds @@ -480,95 +508,4 @@ public class MPPOrderWorkflow extends X_PP_Order_Workflow return Calendar.YEAR; return Calendar.MINUTE; } // getDurationCalendarField - - - - - - - /************************************************************************** - * main - * @param args - */ - public static void main (String[] args) - { - org.compiere.Adempiere.startup(true); - - // Create Standard Document Process - MPPOrderWorkflow wf = new MPPOrderWorkflow(Env.getCtx(), 0, null); - wf.setValue ("Process_xx"); - wf.setName (wf.getValue()); - wf.setDescription("(Standard " + wf.getValue()); - wf.setEntityType (ENTITYTYPE_Dictionary); - wf.save(); - // - MPPOrderNode node10 = new MPPOrderNode (wf, "10", "(Start)"); - node10.setDescription("(Standard Node)"); - node10.setEntityType (ENTITYTYPE_Dictionary); - node10.setAction(MPPOrderNode.ACTION_WaitSleep); - //node10.setWaitTime(0); - node10.setPosition(5, 5); - node10.save(); - wf.setPP_Order_Node_ID(node10.getPP_Order_Node_ID()); - wf.save(); - - MPPOrderNode node20 = new MPPOrderNode (wf, "20", "(DocAuto)"); - node20.setDescription("(Standard Node)"); - node20.setEntityType (ENTITYTYPE_Dictionary); - node20.setAction(MPPOrderNode.ACTION_DocumentAction); - node20.setDocAction(MPPOrderNode.DOCACTION_None); - node20.setPosition(5, 120); - node20.save(); - MPPOrderNodeNext tr10_20 = new MPPOrderNodeNext(node10, node20.getPP_Order_Node_ID()); - tr10_20.setEntityType (ENTITYTYPE_Dictionary); - tr10_20.setDescription("(Standard Transition)"); - tr10_20.setSeqNo(100); - tr10_20.save(); - - MPPOrderNode node100 = new MPPOrderNode (wf, "100", "(DocPrepare)"); - node100.setDescription("(Standard Node)"); - node100.setEntityType (ENTITYTYPE_Dictionary); - node100.setAction(MPPOrderNode.ACTION_DocumentAction); - node100.setDocAction(MPPOrderNode.DOCACTION_Prepare); - node100.setPosition(170, 5); - node100.save(); - MPPOrderNodeNext tr10_100 = new MPPOrderNodeNext(node10, node100.getPP_Order_Node_ID()); - tr10_100.setEntityType (ENTITYTYPE_Dictionary); - tr10_100.setDescription("(Standard Approval)"); - tr10_100.setIsStdUserWorkflow(true); - tr10_100.setSeqNo(10); - tr10_100.save(); - - MPPOrderNode node200 = new MPPOrderNode (wf, "200", "(DocComplete)"); - node200.setDescription("(Standard Node)"); - node200.setEntityType (ENTITYTYPE_Dictionary); - node200.setAction(MPPOrderNode.ACTION_DocumentAction); - node200.setDocAction(MPPOrderNode.DOCACTION_Complete); - node200.setPosition(170, 120); - node200.save(); - MPPOrderNodeNext tr100_200 = new MPPOrderNodeNext(node100, node200.getPP_Order_Node_ID()); - tr100_200.setEntityType (ENTITYTYPE_Dictionary); - tr100_200.setDescription("(Standard Transition)"); - tr100_200.setSeqNo(100); - tr100_200.save(); - - - /** - Env.setContext(Env.getCtx(), "#AD_Client_ID ", "11"); - Env.setContext(Env.getCtx(), "#AD_Org_ID ", "11"); - Env.setContext(Env.getCtx(), "#AD_User_ID ", "100"); - // - int PP_Order_Workflow_ID = 115; // Requisition WF - int M_Requsition_ID = 100; - MRequisition req = new MRequisition (Env.getCtx(), M_Requsition_ID); - req.setDocStatus(DocAction.DOCSTATUS_Drafted); - req.save(); - Log.setTraceLevel(8); - System.out.println("---------------------------------------------------"); - MPPOrderWorkflow wf = MPPOrderWorkflow.get (Env.getCtx(), PP_Order_Workflow_ID); - **/ - // wf.start(M_Requsition_ID); - - } // main - } // MPPOrderWorkflow_ID diff --git a/base/src/org/eevolution/model/MPPProductBOM.java b/base/src/org/eevolution/model/MPPProductBOM.java index 4f6ae2a29c..2ed5e44eb2 100644 --- a/base/src/org/eevolution/model/MPPProductBOM.java +++ b/base/src/org/eevolution/model/MPPProductBOM.java @@ -37,7 +37,7 @@ import org.compiere.util.Env; * @author Victor Perez www.e-evolution.com * @version $Id: MOrder.java,v 1.40 2004/04/13 04:19:30 vpj-cd Exp $ * - * @author Teo Sarca, SC ARHIPAC SERVICE SRL + * @author Teo Sarca, http://www.arhipac.ro */ public class MPPProductBOM extends X_PP_Product_BOM { @@ -46,7 +46,7 @@ public class MPPProductBOM extends X_PP_Product_BOM /** Static Logger */ private static CLogger log = CLogger.getCLogger(MPPProductBOM.class); /** Cache */ - private static CCache s_cache = new CCache(Table_Name, 40, 5); // 5 minutes + private static CCache s_cache = new CCache(Table_Name, 40, 5); /** * Load/Get Product BOM by ID (cached) @@ -78,49 +78,8 @@ public class MPPProductBOM extends X_PP_Product_BOM */ public MPPProductBOM(Properties ctx, int PP_Product_BOM_ID,String trxName) { - super (ctx, PP_Product_BOM_ID,trxName); - // New - if ( PP_Product_BOM_ID == 0) - { - //setDocStatus(DOCSTATUS_Drafted); - //setDocAction (DOCACTION_Prepare); - // - //setDeliveryRule (DELIVERYRULE_Availability); - //setFreightCostRule (FREIGHTCOSTRULE_FreightIncluded); - //setInvoiceRule (INVOICERULE_Immediate); - //setPaymentRule(PAYMENTRULE_OnCredit); - //setPriorityRule (PRIORITYRULE_Medium); - //setDeliveryViaRule (DELIVERYVIARULE_Pickup); - // - //setIsDiscountPrinted (false); - //setIsSelected (false); - //setIsTaxIncluded (false); - //setIsSOTrx (true); - ///setIsDropShip(false); - //setSendEMail (false); - // - //setIsApproved(false); - //setIsPrinted(false); - //setIsCreditApproved(false); - //setIsDelivered(false); - //setIsInvoiced(false); - //setIsTransferred(false); - //setIsSelfService(false); - // - //setProcessed(false); - //setProcessing(false); - //setPosted(false); - - //setDateAcct (new Timestamp(System.currentTimeMillis())); - //setDatePromised (new Timestamp(System.currentTimeMillis())); - //setDateOrdered (new Timestamp(System.currentTimeMillis())); - - //setFreightAmt (Env.ZERO); - //setChargeAmt (Env.ZERO); - //setTotalLines (Env.ZERO); - //setGrandTotal (Env.ZERO); - } - } // MOrder + super (ctx, PP_Product_BOM_ID, trxName); + } // MPPProductBOM /** @@ -133,21 +92,6 @@ public class MPPProductBOM extends X_PP_Product_BOM super (ctx, rs,trxName); } // MOrder - - - - - /** - * Overwrite Client/Org if required - * @param AD_Client_ID client - * @param AD_Org_ID org - */ - public void setClientOrg (int AD_Client_ID, int AD_Org_ID) - { - super.setClientOrg(AD_Client_ID, AD_Org_ID); - } // setClientOrg - - /** * Copy Lines From other BOM * @param order order @@ -164,15 +108,6 @@ public class MPPProductBOM extends X_PP_Product_BOM MPPProductBOMLine line = new MPPProductBOMLine (this); PO.copyValues(fromLines[i], line, getAD_Client_ID(), getAD_Org_ID()); line.setPP_Product_BOM_ID(getPP_Product_BOM_ID()); - //line.setOrder(bom); - line.setPP_Product_BOMLine_ID(0); - // - //line.setQtyDelivered(Env.ZERO); - //line.setQtyInvoiced(Env.ZERO); - //line.setDateDelivered(null); - //line.setDateInvoiced(null); - //line.setRef_OrderLine_ID(0); - //line.setTax(); if (line.save(get_TrxName())) count++; } @@ -187,7 +122,8 @@ public class MPPProductBOM extends X_PP_Product_BOM * BUG #104 * @param lines */ - private void setLines(List lines) { + private void setLines(List lines) + { this.lines = lines; } @@ -272,28 +208,10 @@ public class MPPProductBOM extends X_PP_Product_BOM public static MPPProductBOM get(MProduct product, int ad_org_id, Timestamp valid, String trxName) { MPPProductBOM bom = get(product, ad_org_id, trxName); - // - // Validate date - if (bom != null) + if (bom != null && bom.isValidFromTo(valid)) { - boolean ValidFromBOM = true; - boolean ValidToBOM = true; - if (bom.getValidFrom() != null) - { - ValidFromBOM = valid.compareTo(bom.getValidFrom()) >= 0 ? true : false; - } - if (bom.getValidTo() != null ) - { - ValidToBOM = valid.compareTo(bom.getValidTo()) <= 0 ? true : false; - } - if(ValidFromBOM && ValidToBOM) - { - return bom; - } - else - return null; + return bom; } - return null; } // getBOM @@ -308,20 +226,9 @@ public class MPPProductBOM extends X_PP_Product_BOM MPPProductBOMLine[] bomlines = getLines(); // All BOM Lines List list = new ArrayList(); // Selected BOM Lines Only for (MPPProductBOMLine bl : bomlines) { - boolean ValidFromBOMLine = true; - boolean ValidToBOMLine = true; - if (bl.getValidFrom() != null) - { - ValidFromBOMLine = valid.compareTo(bl.getValidFrom()) >= 0 ? true : false; - } - if (bl.getValidTo() != null ) - { - ValidToBOMLine = valid.compareTo(bl.getValidTo()) <= 0 ? true : false; - } - if(ValidFromBOMLine && ValidToBOMLine) - { + if (bl.isValidFromTo(valid)) { list.add(bl); - } + } } // return list.toArray(new MPPProductBOMLine[list.size()]); @@ -333,12 +240,14 @@ public class MPPProductBOM extends X_PP_Product_BOM */ public MPPProductBOMLine[] getLines() { - // TODO: add caching support - String whereClause = MPPProductBOMLine.COLUMNNAME_PP_Product_BOM_ID+"=?"; - List list = new Query(getCtx(), MPPProductBOMLine.Table_Name, whereClause, get_TrxName()) + if (this.lines == null) + { + final String whereClause = MPPProductBOMLine.COLUMNNAME_PP_Product_BOM_ID+"=?"; + this.lines = new Query(getCtx(), MPPProductBOMLine.Table_Name, whereClause, get_TrxName()) .setParameters(new Object[]{getPP_Product_BOM_ID()}) .list(); - return list.toArray(new MPPProductBOMLine[list.size()]); + } + return this.lines.toArray(new MPPProductBOMLine[this.lines.size()]); } // getLines /** @@ -350,8 +259,8 @@ public class MPPProductBOM extends X_PP_Product_BOM { int AD_Client_ID = Env.getAD_Client_ID(ctx); String sql = "SELECT pb.PP_Product_BOM_ID FROM PP_Product_BOM pb" - +" WHERE pb.Value = ? AND pb.AD_Client_ID = ?"; - return DB.getSQLValue(null, sql, product.getValue(), AD_Client_ID); + +" WHERE pb.Value = ? AND pb.M_Product_ID=? AND pb.AD_Client_ID = ?"; + return DB.getSQLValue(null, sql, product.getValue(), product.get_ID(), AD_Client_ID); } /** @@ -364,7 +273,17 @@ public class MPPProductBOM extends X_PP_Product_BOM return new Query(product.getCtx(), Table_Name, "M_Product_ID=? AND Value=?", trxName) .setParameters(new Object[]{product.getM_Product_ID(), product.getValue()}) .first(); - } - + + public boolean isValidFromTo(Timestamp date) + { + Timestamp validFrom = getValidFrom(); + Timestamp validTo = getValidTo(); + + if (validFrom != null && date.before(validFrom)) + return false; + if (validTo != null && date.after(validTo)) + return false; + return true; + } } // MPPProductBOM diff --git a/base/src/org/eevolution/model/MPPProductBOMLine.java b/base/src/org/eevolution/model/MPPProductBOMLine.java index d0cdfe97ef..a0d603e448 100644 --- a/base/src/org/eevolution/model/MPPProductBOMLine.java +++ b/base/src/org/eevolution/model/MPPProductBOMLine.java @@ -18,6 +18,7 @@ package org.eevolution.model; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Timestamp; import java.util.Hashtable; import java.util.Properties; @@ -115,6 +116,18 @@ public class MPPProductBOMLine extends X_PP_Product_BOMLine } return true; } + + public boolean isValidFromTo(Timestamp date) + { + Timestamp validFrom = getValidFrom(); + Timestamp validTo = getValidTo(); + + if (validFrom != null && date.before(validFrom)) + return false; + if (validTo != null && date.after(validTo)) + return false; + return true; + } } class ProductLowLevelCalculator { diff --git a/base/src/org/eevolution/tools/DateTimeUtil.java b/base/src/org/eevolution/tools/DateTimeUtil.java index 98cf6d3d9e..e1ca7a9a7b 100644 --- a/base/src/org/eevolution/tools/DateTimeUtil.java +++ b/base/src/org/eevolution/tools/DateTimeUtil.java @@ -18,92 +18,73 @@ package org.eevolution.tools; import java.sql.Timestamp; import java.util.Calendar; import java.util.GregorianCalendar; -import java.util.*; /** -* @author Gunther Hoppe, tranSIT GmbH Ilmenau/Germany -* @version 1.0, October 14th 2005 -*/ + * @author Gunther Hoppe, tranSIT GmbH Ilmenau/Germany + * @version 1.0, October 14th 2005 + */ public class DateTimeUtil { - public static long getTimeDifference(Timestamp time1 , Timestamp time2) { + /** + * @param time1 + * @param time2 + * @return time difference (time2 - time1) in millis + */ + public static long getTimeDifference(Timestamp time1 , Timestamp time2) + { + return time2.getTime() - time1.getTime(); + } - GregorianCalendar gc1 = new GregorianCalendar(); - gc1.setTimeInMillis(time1.getTime()); - GregorianCalendar gc2 = new GregorianCalendar(); - gc2.setTimeInMillis(time2.getTime()); - long l1 = gc1.getTime().getTime(); - long l2 = gc2.getTime().getTime(); - - return l2 - l1; - } - - public static Timestamp[] getDayBorders(Timestamp dateTime, Timestamp timeSlotStart, Timestamp timeSlotFinish) { - return new Timestamp[] { - getDayBorder(dateTime, timeSlotStart, false), getDayBorder(dateTime, timeSlotFinish, true), }; } - - public static Timestamp getDayBorder(Timestamp dateTime, Timestamp timeSlot, boolean end) { - - GregorianCalendar gc = new GregorianCalendar(); - gc.setTimeInMillis(dateTime.getTime()); - dateTime.setNanos(0); - - if(timeSlot != null) { - - timeSlot.setNanos(0); - GregorianCalendar gcTS = new GregorianCalendar(); - gcTS.setTimeInMillis(timeSlot.getTime()); - - gc.set(Calendar.HOUR_OF_DAY, gcTS.get(Calendar.HOUR_OF_DAY)); - gc.set(Calendar.MINUTE, gcTS.get(Calendar.MINUTE)); - gc.set(Calendar.SECOND, gcTS.get(Calendar.SECOND)); - gc.set(Calendar.MILLISECOND, gcTS.get(Calendar.MILLISECOND)); - } - else if(end) { - - gc.set(Calendar.HOUR_OF_DAY, 23); - gc.set(Calendar.MINUTE, 59); - gc.set(Calendar.SECOND, 59); - gc.set(Calendar.MILLISECOND, 999); - } - else { - - gc.set(Calendar.MILLISECOND, 0); - gc.set(Calendar.SECOND, 0); - gc.set(Calendar.MINUTE, 0); - gc.set(Calendar.HOUR_OF_DAY, 0); - } - return new Timestamp(gc.getTimeInMillis()); - } - public static Timestamp[] getDayBorders(Timestamp dateTime) { - return getDayBorders(dateTime, null, null); } - - /*public static Timestamp incrementDay(Timestamp dateTime) { - + + /** + * Returns the day border by combining the date part from dateTime and time part form timeSlot. + * If timeSlot is null, then first milli of the day will be used (if end == false) or last milli of the day (if end == true). + * + * @param dateTime + * @param timeSlot + * @param end + * @return + */ + public static Timestamp getDayBorder(Timestamp dateTime, Timestamp timeSlot, boolean end) + { GregorianCalendar gc = new GregorianCalendar(); gc.setTimeInMillis(dateTime.getTime()); - gc.add(Calendar.DAY_OF_MONTH, 1); - + dateTime.setNanos(0); + + if(timeSlot != null) { + timeSlot.setNanos(0); + + GregorianCalendar gcTS = new GregorianCalendar(); + gcTS.setTimeInMillis(timeSlot.getTime()); + + gc.set(Calendar.HOUR_OF_DAY, gcTS.get(Calendar.HOUR_OF_DAY)); + gc.set(Calendar.MINUTE, gcTS.get(Calendar.MINUTE)); + gc.set(Calendar.SECOND, gcTS.get(Calendar.SECOND)); + gc.set(Calendar.MILLISECOND, gcTS.get(Calendar.MILLISECOND)); + } + else if(end) { + gc.set(Calendar.HOUR_OF_DAY, 23); + gc.set(Calendar.MINUTE, 59); + gc.set(Calendar.SECOND, 59); + gc.set(Calendar.MILLISECOND, 999); + } + else { + gc.set(Calendar.MILLISECOND, 0); + gc.set(Calendar.SECOND, 0); + gc.set(Calendar.MINUTE, 0); + gc.set(Calendar.HOUR_OF_DAY, 0); + } return new Timestamp(gc.getTimeInMillis()); } - - public static Timestamp decrementDay(Timestamp dateTime) { - - GregorianCalendar gc = new GregorianCalendar(); - gc.setTimeInMillis(dateTime.getTime()); - gc.add(Calendar.DAY_OF_MONTH, -1); - - return new Timestamp(gc.getTimeInMillis()); - }*/ }