Skip to content

Commit

Permalink
Se agrega validación para sobregiro de documentos por cobrar a través de
Browse files Browse the repository at this point in the history
notas de crédito
  • Loading branch information
yamelsenih committed Apr 17, 2024
1 parent 52438fd commit 9ae6c54
Show file tree
Hide file tree
Showing 6 changed files with 494 additions and 5 deletions.
135 changes: 135 additions & 0 deletions core/src/main/java/base/org/erpya/lve/model/validator/CreditMemo.java
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;
}
}
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;
}
}
4 changes: 3 additions & 1 deletion core/src/main/java/base/org/erpya/lve/util/LVEUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,15 @@ public class LVEUtil {
/** Validate Value for Multi Client */
public static final String ENABLE_MULTI_CLIENT = "ENABLE_MULTI_CLIENT";
/** Commercial Activity License */
public static final String COLUMNNAME_LVE_CommercialActivityLicense = "LVE_CommercialActivityLicense";
public static final String LVE_CommercialActivityLicense = "LVE_CommercialActivityLicense";
/** Default Statement Charge */
public static final String COLUMNNAME_LVE_DefaultStatementCharge_ID = "LVE_DefaultStatementCharge_ID";
/** Automatic Debt Note Invoice Referenced */
public static final String COLUMNNAME_LVE_AutoDebitInvoice_ID = "LVE_AutoDebitInvoice_ID";
/** Automatic Debt Note Invoice Referenced */
public static final String COLUMNNAME_LVE_AllowOverPayInvoice = "LVE_AllowOverPayInvoice";
/** Allows overwrite document */
public static final String LVE_AllowOverdraftReference = "LVE_AllowOverdraftReference";
/**
* Process Business Partner Value
* @param value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ private boolean createTXTFile (Writer writer, ReportEngine engine) {
String businessPartnerCode = null;
if(organizationInfo.get_ValueAsInt("WH_BPartner_ID") > 0) {
MBPartner businessPartner = new MBPartner(getCtx(), organizationInfo.get_ValueAsInt("WH_BPartner_ID"), transactionName);
businessPartnerCode = businessPartner.get_ValueAsString(LVEUtil.COLUMNNAME_LVE_CommercialActivityLicense);
businessPartnerCode = businessPartner.get_ValueAsString(LVEUtil.LVE_CommercialActivityLicense);
}
data = String.format("%1$" + 5 + "s", Optional.ofNullable(businessPartnerCode).orElse("")).replace(" ", "0");
}
} else if(printFormatItem.getName().equals("CodigoProveedor")) {
MInvoice invoice = getInvoice(value, transactionName);
if(invoice != null) {
MBPartner businessPartner = new MBPartner(getCtx(), invoice.getC_BPartner_ID(), transactionName);
String businessPartnerCode = Optional.ofNullable(businessPartner.get_ValueAsString(LVEUtil.COLUMNNAME_LVE_CommercialActivityLicense)).orElse("");
String businessPartnerCode = Optional.ofNullable(businessPartner.get_ValueAsString(LVEUtil.LVE_CommercialActivityLicense)).orElse("");
data = String.format("%1$" + 5 + "s", businessPartnerCode).replace(" ", "0");
}
} else if(printFormatItem.getName().equals("NombreProveedor")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ private boolean createTXTFile (Writer writer, ReportEngine engine) {
String businessPartnerCode = null;
if(organizationInfo.get_ValueAsInt("WH_BPartner_ID") > 0) {
MBPartner businessPartner = new MBPartner(getCtx(), organizationInfo.get_ValueAsInt("WH_BPartner_ID"), transactionName);
businessPartnerCode = businessPartner.get_ValueAsString(LVEUtil.COLUMNNAME_LVE_CommercialActivityLicense);
businessPartnerCode = businessPartner.get_ValueAsString(LVEUtil.LVE_CommercialActivityLicense);
}
data = String.format("%1$" + 5 + "s", Optional.ofNullable(businessPartnerCode).orElse("")).replace(" ", "0");
}
} else if(printFormatItem.getName().equals("CodigoProveedor")) {
MInvoice invoice = getInvoice(value, transactionName);
if(invoice != null) {
MBPartner businessPartner = new MBPartner(getCtx(), invoice.getC_BPartner_ID(), transactionName);
String businessPartnerCode = Optional.ofNullable(businessPartner.get_ValueAsString(LVEUtil.COLUMNNAME_LVE_CommercialActivityLicense)).orElse("");
String businessPartnerCode = Optional.ofNullable(businessPartner.get_ValueAsString(LVEUtil.LVE_CommercialActivityLicense)).orElse("");
data = String.format("%1$" + 5 + "s", businessPartnerCode).replace(" ", "0");
}
} else if(printFormatItem.getName().equals("NombreProveedor")) {
Expand Down
Loading

0 comments on commit 9ae6c54

Please sign in to comment.