Spanish cash discount setup creates an unbalance error when posting
Scenario:
Posting fails with message: "The transactions on voucher XXX do not balance as per <Date>" when using Spanish cash discount settings.
Resolution:
To resolve this issue you can make the following changes to \Classes\LedgerJournalEngine\write and \Classes\LedgerJournalCashDiscService
- Edit the write method of the LedgerJournalEngine class as follows:
...
if (ledgerJournalCashDiscService && ledgerJournalCashDiscService.invoiceId())
{
this.updateCashDisc(_ledgerJournalTrans, _updateCashDisc);
ledgerJournalCashDiscService.adjustPennDiffCashDiscAmount(); //line added
}
...
- Create a new method "adjustPennDiffCashDiscAmount" with the following code:
void adjustPennDiffCashDiscAmount()
{
LedgerJournalTrans ledgerJournalTrans;
DiscAmount pennyDiff;
DiscAmount cashDiscRaw;
DiscAmount cashDisc;
DiscAmount cashDiscMax = 0;
Recid cashDiscMaxLine = 0;
;
while select AmountCurDebit, AmountCurCredit, CashDiscAmount, CurrencyCode from ledgerJournalTrans
where ledgerJournalTrans.JournalNum == ledgerJournalId
&& ledgerJournalTrans.Voucher == voucher
&& ledgerJournalTrans.AccountType != LedgerJournalACType::Cust
&& ledgerJournalTrans.AccountType != LedgerJournalACType::Vend
&& ledgerJournalTrans.AccountType != LedgerJournalACType::Bank
&& ledgerJournalTrans.RecId != ledgerJournalTransMain.RecId
{
cashDiscRaw = cashDiscRaw + this.cashDiscBaseAmount(ledgerJournalTrans) * cashDiscPercent / 100;
cashDisc = cashDisc + Currency::amount(ledgerJournalTrans.CashDiscAmount, ledgerJournalTrans.currencyCode);
if (abs(ledgerJournalTrans.CashDiscAmount) > cashDiscMax)
{
cashDiscMax = abs(ledgerJournalTrans.CashDiscAmount);
cashDiscMaxLine = ledgerJournalTrans.RecId;
}
}
pennyDiff = Currency::amount(cashDiscRaw - cashDisc, ledgerJournalTrans.currencyCode);
if (pennyDiff && cashDiscMaxLine)
{
select forupdate ledgerJournalTrans
where ledgerJournalTrans.RecId == cashDiscMaxLine;
ledgerJournalTrans.CashDiscAmount += pennyDiff;
ledgerJournalTrans.CashDiscAmount = Currency::amount(ledgerJournalTrans.CashDiscAmount, ledgerJournalTrans.currencyCode);
ttsbegin;
ledgerJournalTrans.update();
ttscommit;
}
}