IDEMPIERE-118 Average Costing: Negative inventory.

This commit is contained in:
Heng Sin Low 2012-03-19 00:19:06 +08:00
parent 57baa80708
commit 7da289a794
3 changed files with 52 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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);