diff --git a/org.adempiere.base/src/org/adempiere/exceptions/AverageCostingNegativeQtyException.java b/org.adempiere.base/src/org/adempiere/exceptions/AverageCostingNegativeQtyException.java new file mode 100644 index 0000000000..0421e5b606 --- /dev/null +++ b/org.adempiere.base/src/org/adempiere/exceptions/AverageCostingNegativeQtyException.java @@ -0,0 +1,36 @@ +/****************************************************************************** + * Product: iDempiere ERP & CRM Smart Business Solution * + * Copyright (C) 2012 Heng Sin Low * + * Copyright (C) 2012 www.iDempiere.org * + * This program is free software; you can redistribute it and/or modify it * + * under the terms version 2 of the GNU General Public License as published * + * by the Free Software Foundation. This program is distributed in the hope * + * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * You should have received a copy of the GNU General Public License along * + * with this program; if not, write to the Free Software Foundation, Inc., * + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * + *****************************************************************************/ +package org.adempiere.exceptions; + +/** + * + * @author hengsin + * + */ +public class AverageCostingNegativeQtyException extends AdempiereException { + + /** + * generated serial version id + */ + private static final long serialVersionUID = 4165497320719149773L; + + public AverageCostingNegativeQtyException() { + super(); + } + + public AverageCostingNegativeQtyException(String message) { + super(message); + } +} diff --git a/org.adempiere.base/src/org/compiere/acct/Doc.java b/org.adempiere.base/src/org/compiere/acct/Doc.java index e463585776..84387501f0 100644 --- a/org.adempiere.base/src/org/compiere/acct/Doc.java +++ b/org.adempiere.base/src/org/compiere/acct/Doc.java @@ -28,6 +28,7 @@ import java.util.Iterator; import java.util.Properties; import java.util.logging.Level; +import org.adempiere.exceptions.AverageCostingNegativeQtyException; import org.compiere.model.MAccount; import org.compiere.model.MAcctSchema; import org.compiere.model.MConversionRate; @@ -516,6 +517,12 @@ public abstract class Doc p_Status = postLogic (); } } + catch (AverageCostingNegativeQtyException e) + { + log.log(Level.INFO, e.getLocalizedMessage(), e); + p_Status = STATUS_NotPosted; + p_Error = e.toString(); + } catch (Exception e) { log.log(Level.SEVERE, "", e); @@ -525,7 +532,7 @@ public abstract class Doc String validatorMsg = null; // Call validator on before post - if (!p_Status.equals(STATUS_Error)) { + if (p_Status.equals(STATUS_Posted)) { validatorMsg = ModelValidationEngine.get().fireDocValidate(getPO(), ModelValidator.TIMING_BEFORE_POST); if (validatorMsg != null) { p_Status = STATUS_Error; @@ -536,7 +543,7 @@ public abstract class Doc // commitFact p_Status = postCommit (p_Status); - if (!p_Status.equals(STATUS_Error)) { + if (p_Status.equals(STATUS_Posted)) { validatorMsg = ModelValidationEngine.get().fireDocValidate(getPO(), ModelValidator.TIMING_AFTER_POST); if (validatorMsg != null) { p_Status = STATUS_Error; diff --git a/org.adempiere.base/src/org/compiere/model/MCost.java b/org.adempiere.base/src/org/compiere/model/MCost.java index 9b53af0cd7..5c524ba763 100644 --- a/org.adempiere.base/src/org/compiere/model/MCost.java +++ b/org.adempiere.base/src/org/compiere/model/MCost.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.Properties; import java.util.logging.Level; +import org.adempiere.exceptions.AverageCostingNegativeQtyException; import org.adempiere.exceptions.DBException; import org.compiere.Adempiere; import org.compiere.util.CLogger; @@ -1458,6 +1459,12 @@ public class MCost extends X_M_Cost { amt = amt.multiply(new BigDecimal(-1.00d)); } + + if (getCurrentQty().add(qty).signum() < 0) + { + throw new AverageCostingNegativeQtyException("Product(ID)="+getM_Product_ID()+", Current Qty="+getCurrentQty()+", Trx Qty="+qty); + } + BigDecimal oldSum = getCurrentCostPrice().multiply(getCurrentQty()); BigDecimal newSum = amt; // is total already BigDecimal sumAmt = oldSum.add(newSum);