-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Se agrega validación para sobregiro de documentos por cobrar a través de
notas de crédito
- Loading branch information
1 parent
52438fd
commit 9ae6c54
Showing
6 changed files
with
494 additions
and
5 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
core/src/main/java/base/org/erpya/lve/model/validator/CreditMemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/************************************************************************************ | ||
* Copyright (C) 2012-2018 E.R.P. Consultores y Asociados, C.A. * | ||
* Contributor(s): Yamel Senih ysenih@erpya.com * | ||
* This program is free software: you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation, either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* 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, see <https://www.gnu.org/licenses/>. * | ||
************************************************************************************/ | ||
package org.erpya.lve.model.validator; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.adempiere.core.domains.models.I_C_Invoice; | ||
import org.adempiere.exceptions.AdempiereException; | ||
import org.compiere.model.MClient; | ||
import org.compiere.model.MConversionRate; | ||
import org.compiere.model.MCurrency; | ||
import org.compiere.model.MDocType; | ||
import org.compiere.model.MInvoice; | ||
import org.compiere.model.MTax; | ||
import org.compiere.model.ModelValidationEngine; | ||
import org.compiere.model.ModelValidator; | ||
import org.compiere.model.PO; | ||
import org.compiere.util.CLogger; | ||
import org.compiere.util.Env; | ||
import org.compiere.util.Msg; | ||
import org.erpya.lve.util.LVEUtil; | ||
|
||
/** | ||
* Write here your change comment | ||
* @author Yamel Senih ysenih@erpya.com | ||
* | ||
*/ | ||
public class CreditMemo implements ModelValidator { | ||
|
||
/** Logger */ | ||
private static CLogger log = CLogger.getCLogger(CreditMemo.class); | ||
/** Client */ | ||
private int clientId = -1; | ||
|
||
@Override | ||
public void initialize(ModelValidationEngine engine, MClient client) { | ||
// client = null for global validator | ||
if (client != null) { | ||
clientId = client.getAD_Client_ID(); | ||
log.info(client.toString()); | ||
} else { | ||
log.info("Initializing global validator: " + this.toString()); | ||
} | ||
// Add Persistence for IsDefault values | ||
engine.addDocValidate(I_C_Invoice.Table_Name, this); | ||
} | ||
|
||
@Override | ||
public int getAD_Client_ID() { | ||
return clientId; | ||
} | ||
|
||
@Override | ||
public String login(int AD_Org_ID, int AD_Role_ID, int AD_User_ID) { | ||
log.info("AD_User_ID=" + AD_User_ID); | ||
return null; | ||
} | ||
|
||
@Override | ||
public String modelChange(PO entity, int type) throws Exception { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String docValidate(PO entity, int timing) { | ||
if(timing == TIMING_BEFORE_COMPLETE) { | ||
if(entity.get_TableName().equals(I_C_Invoice.Table_Name)) { | ||
MInvoice creditMemo = (MInvoice) entity; | ||
MDocType documentType = MDocType.get(entity.getCtx(), creditMemo.getC_DocTypeTarget_ID()); | ||
if(creditMemo.isSOTrx() | ||
&& creditMemo.isCreditMemo() | ||
&& !creditMemo.isReversal() | ||
&& creditMemo.get_ValueAsBoolean(LVEUtil.COLUMNNAME_IsFiscalDocument) | ||
&& !documentType.get_ValueAsBoolean(LVEUtil.LVE_AllowOverdraftReference)) { | ||
Map<Integer, BigDecimal> documentsToAllocate = new HashMap<>(); | ||
Arrays.asList(creditMemo.getLines()) | ||
.stream() | ||
.filter(creditMemoLine -> creditMemoLine.get_ValueAsInt(LVEUtil.COLUMNNAME_InvoiceToAllocate_ID) != 0) | ||
.forEach(creditMemoLine -> { | ||
MInvoice sourceInvoice = new MInvoice(creditMemoLine.getCtx(), creditMemoLine.get_ValueAsInt(LVEUtil.COLUMNNAME_InvoiceToAllocate_ID), creditMemoLine.get_TrxName()); | ||
BigDecimal amountToAllocate = creditMemoLine.getLineNetAmt(); | ||
// Add Tax if exists | ||
if(creditMemoLine.getC_Tax_ID() > 0) { | ||
MTax tax = MTax.get(creditMemoLine.getCtx(), creditMemoLine.getC_Tax_ID()); | ||
amountToAllocate = amountToAllocate.add(tax.calculateTax(amountToAllocate, creditMemoLine.isTaxIncluded(), creditMemoLine.getPrecision())); | ||
} | ||
// Convert It | ||
amountToAllocate = MConversionRate.convert(creditMemo.getCtx(), amountToAllocate, creditMemo.getC_Currency_ID(), sourceInvoice.getC_Currency_ID(), creditMemo.getDateAcct(), creditMemo.getC_ConversionType_ID(), creditMemo.getAD_Client_ID(), creditMemo.getAD_Org_ID()); | ||
if(documentsToAllocate.containsKey(sourceInvoice.getC_Invoice_ID())) { | ||
amountToAllocate = amountToAllocate.add(documentsToAllocate.get(sourceInvoice.getC_Invoice_ID())); | ||
} | ||
documentsToAllocate.put(sourceInvoice.getC_Invoice_ID(), amountToAllocate); | ||
}); | ||
// Validate | ||
StringBuffer message = new StringBuffer(); | ||
documentsToAllocate.entrySet().forEach(entry -> { | ||
MInvoice sourceInvoice = new MInvoice(creditMemo.getCtx(), entry.getKey(), creditMemo.get_TrxName()); | ||
BigDecimal openAmount = Optional.ofNullable(sourceInvoice.getOpenAmt()).orElse(Env.ZERO); | ||
if(openAmount.compareTo(entry.getValue()) < 0) { | ||
message.append(Msg.getMsg(creditMemo.getCtx(), "LVE.InvoiceOverdraft", | ||
new Object[] { | ||
sourceInvoice.getDocumentNo(), | ||
sourceInvoice.getGrandTotal(), | ||
openAmount, | ||
entry.getValue(), | ||
MCurrency.get(sourceInvoice.getCtx(), sourceInvoice.getC_Currency_ID()).getISO_Code(), | ||
openAmount.subtract(entry.getValue()) | ||
})); | ||
} | ||
}); | ||
if(message.length() > 0) { | ||
throw new AdempiereException(message.toString()); | ||
} | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
core/src/main/java/base/org/erpya/lve/setup/CreditMemoValidation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/****************************************************************************** | ||
* Product: Adempiere ERP & CRM Smart Business Solution * | ||
* 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. * | ||
* For the text or an alternative of this public license, you may reach us * | ||
* Copyright (C) 2003-2015 E.R.P. Consultores y Asociados, C.A. * | ||
* All Rights Reserved. * | ||
* Contributor(s): Yamel Senih www.erpya.com * | ||
*****************************************************************************/ | ||
package org.erpya.lve.setup; | ||
|
||
import java.util.Properties; | ||
|
||
import org.compiere.model.Query; | ||
import org.adempiere.core.domains.models.X_AD_ModelValidator; | ||
import org.erpya.lve.model.validator.CreditMemo; | ||
import org.spin.util.ISetupDefinition; | ||
|
||
/** | ||
* Testing class for setup | ||
* @author Yamel Senih, ysenih@erpya.com, ERPCyA http://www.erpya.com | ||
*/ | ||
public class CreditMemoValidation implements ISetupDefinition { | ||
|
||
private static final String SETUP_DESCRIPTION = "(*Created from Setup Automatically*)"; | ||
private static final String SETUP_UUID = "(*AutomaticSetup*)"; | ||
|
||
@Override | ||
public String doIt(Properties context, String transactionName) { | ||
// Add Model Validator | ||
createModelValidator(context, transactionName); | ||
// financial management | ||
return "@AD_SetupDefinition_ID@ @Ok@"; | ||
} | ||
|
||
/** | ||
* Create Model Vaidator | ||
* @param context | ||
* @param transactionName | ||
* @return | ||
*/ | ||
private X_AD_ModelValidator createModelValidator(Properties context, String transactionName) { | ||
X_AD_ModelValidator modelValidator = new Query(context, X_AD_ModelValidator.Table_Name, X_AD_ModelValidator.COLUMNNAME_ModelValidationClass + " = ?", transactionName) | ||
.setParameters(CreditMemo.class.getName()) | ||
.setClient_ID() | ||
.<X_AD_ModelValidator>first(); | ||
// Validate | ||
if(modelValidator != null | ||
&& modelValidator.getAD_ModelValidator_ID() > 0) { | ||
return modelValidator; | ||
} | ||
// | ||
modelValidator = new X_AD_ModelValidator(context, 0, transactionName); | ||
modelValidator.setName("Fiscal Credit Memo Validation"); | ||
modelValidator.setEntityType("LVE"); | ||
modelValidator.setDescription(SETUP_DESCRIPTION); | ||
modelValidator.setSeqNo(200); | ||
modelValidator.setModelValidationClass(CreditMemo.class.getName()); | ||
modelValidator.setUUID(SETUP_UUID); | ||
modelValidator.setIsDirectLoad(true); | ||
modelValidator.saveEx(); | ||
return modelValidator; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.