Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix NPE on missing currency conversion #10196

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ CurrencyId getBaseCurrencyId(
OrgId adOrgId);

@Deprecated
@Nullable
@NonNull
BigDecimal convertBase(
BigDecimal amt,
CurrencyId currencyFromId,
Expand All @@ -86,7 +86,7 @@ BigDecimal convertBase(
@NonNull OrgId orgId);

@Deprecated
@Nullable
@NonNull
BigDecimal convert(
BigDecimal amt,
CurrencyId currencyFromId,
Expand All @@ -107,20 +107,22 @@ BigDecimal convert(
* @return converted amount
*/
@Deprecated
@NonNull
BigDecimal convert(
BigDecimal amt,
CurrencyId currencyFromId,
CurrencyId currencyToId,
@NonNull ClientId clientId,
@NonNull OrgId orgId);

@Nullable
@NonNull
CurrencyConversionResult convert(
CurrencyConversionContext conversionCtx,
BigDecimal amt,
CurrencyId currencyFromId,
CurrencyId currencyToId);

@NonNull
default CurrencyConversionResult convert(
@NonNull final CurrencyConversionContext conversionCtx,
@NonNull final Money amt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import de.metas.i18n.AdMessageKey;
import de.metas.i18n.ITranslatableString;
import de.metas.i18n.TranslatableStrings;
import lombok.NonNull;
import org.adempiere.exceptions.AdempiereException;

import javax.annotation.Nullable;
import java.time.LocalDate;

/**
Expand All @@ -41,19 +43,19 @@ public class NoCurrencyRateFoundException extends AdempiereException
private static final AdMessageKey MSG = AdMessageKey.of("NoCurrencyConversion");

public NoCurrencyRateFoundException(
final CurrencyCode currencyFrom,
final CurrencyCode currencyTo,
final LocalDate conversionDate,
final ConversionTypeMethod conversionTypeMethod)
@NonNull final CurrencyCode currencyFrom,
@NonNull final CurrencyCode currencyTo,
@Nullable final LocalDate conversionDate,
@Nullable final ConversionTypeMethod conversionTypeMethod)
{
super(buildMsg(currencyFrom, currencyTo, conversionDate, conversionTypeMethod));
}

private static ITranslatableString buildMsg(
final CurrencyCode currencyFrom,
final CurrencyCode currencyTo,
final LocalDate conversionDate,
final ConversionTypeMethod conversionTypeMethod)
@NonNull final CurrencyCode currencyFrom,
@NonNull final CurrencyCode currencyTo,
@Nullable final LocalDate conversionDate,
@Nullable final ConversionTypeMethod conversionTypeMethod)
{
return TranslatableStrings.builder()
.appendADMessage(MSG).append(" ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public BigDecimal convertBase(
return convert(amt, currencyFromId, currencyToId, convDate, conversionTypeId, clientId, orgId);
}

@Nullable
@NonNull
@Override
public final BigDecimal convert(
final BigDecimal amt,
Expand All @@ -120,14 +120,10 @@ public final BigDecimal convert(
amt,
currencyFromId,
currencyToId);
if (conversionResult == null)
{
return null;
}
return conversionResult.getAmount();
}

@Nullable
@NonNull
@Override
public final CurrencyConversionResult convert(
@NonNull final CurrencyConversionContext conversionCtx,
Expand All @@ -153,8 +149,11 @@ else if (CurrencyId.equals(currencyFromId, currencyToId))
final CurrencyRate currencyRate = getCurrencyRateOrNull(conversionCtx, currencyFromId, currencyToId);
if (currencyRate == null)
{
// TODO: evaluate if we can throw an exception here
return null;
throw new NoCurrencyRateFoundException(
currencyDAO.getCurrencyCodeById(currencyFromId),
currencyDAO.getCurrencyCodeById(currencyToId),
conversionCtx.getConversionDate(),
currencyDAO.getConversionTypeMethodById(conversionCtx.getConversionTypeId()));
}

conversionRateBD = currencyRate.getConversionRate();
Expand All @@ -179,7 +178,7 @@ private CurrencyPrecision getStdPrecision(final CurrencyId currencyId)
}

@Override
@Nullable
@NonNull
public final BigDecimal convert(
final BigDecimal amt,
final CurrencyId currencyFromId,
Expand All @@ -188,7 +187,7 @@ public final BigDecimal convert(
@NonNull final OrgId orgId)
{
final CurrencyConversionContext conversionCtx = createCurrencyConversionContext(
(LocalDate)null, // convDate
null, // convDate
(CurrencyConversionTypeId)null, // C_ConversionType_ID,
clientId,
orgId);
Expand All @@ -197,10 +196,6 @@ public final BigDecimal convert(
amt,
currencyFromId,
currencyToId);
if (conversionResult == null)
{
return null;
}
return conversionResult.getAmount();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,6 @@ public String prepareIt()
(CurrencyConversionTypeId)null,
ClientId.ofRepoId(getAD_Client_ID()),
OrgId.ofRepoId(getAD_Org_ID()));
if (amt == null)
{
m_processMsg = "No Conversion Rate found - @C_CashLine_ID@= " + line.getLine();
return IDocument.STATUS_Invalid;
}
difference = difference.add(amt);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@
import de.metas.bpartner.service.IBPartnerStatsBL;
import de.metas.bpartner.service.IBPartnerStatsDAO;
import de.metas.cache.CCache;
import de.metas.currency.Currency;
import de.metas.currency.CurrencyPrecision;
import de.metas.currency.ICurrencyBL;
import de.metas.currency.ICurrencyDAO;
import de.metas.document.DocTypeId;
import de.metas.document.IDocTypeDAO;
import de.metas.document.engine.DocStatus;
import de.metas.document.engine.IDocument;
import de.metas.document.engine.IDocumentBL;
import de.metas.document.sequence.IDocumentNoBuilder;
import de.metas.document.sequence.IDocumentNoBuilderFactory;
import de.metas.i18n.AdMessageKey;
import de.metas.i18n.Msg;
import de.metas.invoice.InvoiceId;
import de.metas.invoice.service.IInvoiceBL;
Expand Down Expand Up @@ -101,8 +98,6 @@
@SuppressWarnings("serial")
public class MInvoice extends X_C_Invoice implements IDocument
{
private static final AdMessageKey ERR_NoBaseConversionBetweenCurrencies = AdMessageKey.of("NoBaseConversionBetweenCurrencies");

/**
* Get Payments Of BPartner
*
Expand Down Expand Up @@ -1184,32 +1179,6 @@ else if (!isSOTrx()
}
} // for all lines

// verify that we can deal with the invoice's currency
// Update total revenue and balance / credit limit (reversed on AllocationLine.processIt)
final BigDecimal invAmt = Services.get(ICurrencyBL.class).convertBase(
getGrandTotal(true), // CM adjusted
CurrencyId.ofRepoId(getC_Currency_ID()),
TimeUtil.asLocalDate(getDateAcct()),
CurrencyConversionTypeId.ofRepoIdOrNull(getC_ConversionType_ID()),
ClientId.ofRepoId(getAD_Client_ID()),
OrgId.ofRepoId(getAD_Org_ID()));

if (invAmt == null)
{
final Currency currency = Services.get(ICurrencyDAO.class).getById(CurrencyId.ofRepoId(getC_Currency_ID()));
final Currency currencyTo = Services.get(ICurrencyBL.class).getBaseCurrency(ClientId.ofRepoId(getAD_Client_ID()), OrgId.ofRepoId(getAD_Org_ID()));

final IBPartnerDAO bpartnerDAO = Services.get(IBPartnerDAO.class);
final I_C_BPartner bpartner = bpartnerDAO.getById(getC_BPartner_ID());

throw new AdempiereException(
ERR_NoBaseConversionBetweenCurrencies,
bpartner.getName(),
bpartner.getValue(),
currency.getCurrencyCode().toThreeLetterCode(),
currencyTo.getCurrencyCode().toThreeLetterCode());
}

// Update Project
if (isSOTrx() && getC_Project_ID() > 0)
{
Expand All @@ -1227,10 +1196,6 @@ else if (!isSOTrx()
ClientId.ofRepoId(getAD_Client_ID()),
OrgId.ofRepoId(getAD_Org_ID()));
}
if (amt == null)
{
throw new AdempiereException("Could not convert C_Currency_ID=" + getC_Currency_ID() + " to Project C_Currency_ID=" + C_CurrencyTo_ID);
}
BigDecimal newAmt = project.getInvoicedAmt();
if (newAmt == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public CostAmount convertToAcctSchemaCurrency(
return CostAmount.of(result.getAmount(), acctCurrencyId);
}

@NonNull
public CurrencyConversionResult convert(
final CurrencyConversionContext conversionCtx,
final Money price,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,6 @@ private ProductPrice getInOutLinePriceConverted(@NonNull final InOutAndLineId in
Env.getClientId(),
Env.getOrgId());

if (shipmentLinePriceConverted == null)
{
throw new AdempiereException("Please, add a conversion between the following currencies: " + priceActual.getCurrencyId() + ", " + currencyId);
}

return priceActual.toBuilder().money(Money.of(shipmentLinePriceConverted, currencyId)).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import de.metas.quantity.Quantity;
import de.metas.uom.IUOMConversionBL;
import de.metas.uom.UOMConversionContext;
import de.metas.util.Check;
import de.metas.util.Services;
import de.metas.util.lang.Percent;
import de.metas.util.time.SystemTime;
Expand Down Expand Up @@ -101,12 +100,7 @@ public Money convertMoneyToCurrency(
money.getCurrencyId(),
targetCurrencyId);

final BigDecimal convertedAmount = Check.assumeNotNull(
conversionResult.getAmount(),
"CurrencyConversion from currencyId={} to currencyId={} needs to work; currencyConversionContext={}, currencyConversionResult={}",
money.getCurrencyId(), targetCurrencyId, currencyConversionContext, conversionResult);

return Money.of(convertedAmount, targetCurrencyId);
return Money.of(conversionResult.getAmount(), targetCurrencyId);
}

public Money percentage(@NonNull final Percent percent, @NonNull final Money input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ private Money convertFreightRate(@Nullable final Money freightAmt, @NonNull fina
Env.getClientId(),
Env.getOrgId());

if (freightAmtConverted == null)
{
throw new AdempiereException("Please, add a conversion between the following currencies: " + freightCostCurrencyId + ", " + orderCurrencyId);
}

final Money convertedFreightAmt = Money.of(freightAmtConverted, orderCurrencyId);

return convertedFreightAmt;
Expand Down