Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4433,7 +4433,7 @@ codeunit 12 "Gen. Jnl.-Post Line"
GenJnlLine, TempDimPostingBuffer, AdjAmount, SaveEntryNo, GetCustomerReceivablesAccount(GenJnlLine, CustPostingGr));

OnPostDtldCustLedgEntriesOnAfterCreateGLEntriesForTotalAmounts(TempGLEntryBuf, GlobalGLEntry, NextTransactionNo);
PostReceivableDocs(GenJnlLine, CustPostingGr);
PostReceivableDocs(GenJnlLine, CustPostingGr, SaveEntryNo);

DtldCVLedgEntryBuf.DeleteAll();
end;
Expand Down Expand Up @@ -8466,25 +8466,40 @@ codeunit 12 "Gen. Jnl.-Post Line"
exit;
end;

local procedure PostReceivableDocs(GenJnlLine: Record "Gen. Journal Line"; CustPostingGr: Record "Customer Posting Group")
local procedure PostReceivableDocs(GenJnlLine: Record "Gen. Journal Line"; CustPostingGr: Record "Customer Posting Group"; var SaveEntryNo: Integer)
var
DocAmountAddCurr: Decimal;
GLAccNo: Code[20];
GLEntry: Record "G/L Entry";
NeedsSaveEntryNoFix: Boolean;
begin
if (DocAmountLCY <> 0) or (DiscDocAmountLCY <> 0) or (CollDocAmountLCY <> 0) or (RejDocAmountLCY <> 0) or
(DiscRiskFactAmountLCY <> 0) or (DiscUnriskFactAmountLCY <> 0) or (CollFactAmountLCY <> 0)
then
if NextEntryNo2 = NextEntryNo then
NextEntryNo := NextEntryNo - 1;
//
NeedsSaveEntryNoFix := (SaveEntryNo <> 0) and (NextEntryNo2 <> NextEntryNo);
if not NeedsSaveEntryNoFix then
if (DocAmountLCY <> 0) or (DiscDocAmountLCY <> 0) or (CollDocAmountLCY <> 0) or (RejDocAmountLCY <> 0) or
(DiscRiskFactAmountLCY <> 0) or (DiscUnriskFactAmountLCY <> 0) or (CollFactAmountLCY <> 0)
then
if NextEntryNo2 = NextEntryNo then
NextEntryNo := NextEntryNo - 1;
if DocAmountLCY <> 0 then begin
if GenJnlLine."Currency Code" = AddCurrency.Code then
DocAmountAddCurr := GenJnlLine.Amount
else
DocAmountAddCurr := DocAmtCalcAddCurrency(GenJnlLine, DocAmountLCY);
GLAccNo := GetGLAccountForReceivableDocs(GenJnlLine, CustPostingGr, DocAmountLCY, CollDocAmountLCY);
CreateGLEntryBalAcc(
GenJnlLine, GLAccNo, DocAmountLCY, DocAmountAddCurr,
GenJnlLine."Bal. Account Type", GenJnlLine."Bal. Account No.");
if NeedsSaveEntryNoFix then begin
DocAmountAddCurr := DocAmountAddCurr - GenJnlLine."VAT Amount";
InitGLEntry(GenJnlLine, GLEntry, GLAccNo, DocAmountLCY, DocAmountAddCurr, true, true,
CalcAmountSrcCurr(GenJnlLine, DocAmountLCY));
GLEntry."Bal. Account Type" := GenJnlLine."Bal. Account Type";
GLEntry."Bal. Account No." := GenJnlLine."Bal. Account No.";
UpdateGLEntryNo(GLEntry."Entry No.", SaveEntryNo);
InsertGLEntry(GenJnlLine, GLEntry, true);
OnMoveGenJournalLine(GenJnlLine, GLEntry.RecordId);
end else
CreateGLEntryBalAcc(
GenJnlLine, GLAccNo, DocAmountLCY, DocAmountAddCurr,
GenJnlLine."Bal. Account Type", GenJnlLine."Bal. Account No.");
end;
if DiscDocAmountLCY <> 0 then begin
CustPostingGr.TestField("Discted. Bills Acc.");
Expand Down
88 changes: 88 additions & 0 deletions src/Layers/ES/Tests/Local/CarteraRecvBasicScenarios.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,77 @@
Assert.AreEqual(CarteraDoc."Cust./Vendor Bank Acc. Code", SEPADirectDebitMandate."Customer Bank Account Code", CustVendorBankAccountErr);
end;

[Test]
[HandlerFunctions('ConfirmHandler,MessageHandler')]
[Scope('OnPrem')]
procedure UnapplyAndRevertSucceedsAfterPaymentWithCurrencyFactorChange()
var
SalesHeader: Record "Sales Header";
GenJournalBatch: Record "Gen. Journal Batch";
GenJournalLine: Record "Gen. Journal Line";
CustLedgerEntry: Record "Cust. Ledger Entry";
CurrencyExchangeRate: Record "Currency Exchange Rate";
CurrencyCode: Code[10];
PostedInvoiceNo: Code[20];
PaymentDate: Date;
TransactionNo: Integer;
begin
// [SCENARIO] Unapply and revert transaction succeeds after payment with changed currency factor for Cartera bill
Initialize();

// [GIVEN] Posted sales invoice with Cartera customer in FCY (creates bill entries)
CurrencyCode := LibraryCarteraCommon.CreateCarteraCurrency(true, false, false);
PostedInvoiceNo := PostCarteraSalesInvoiceWithItem(SalesHeader, CurrencyCode);

// [GIVEN] Find the bill entry for the posted invoice
CustLedgerEntry.SetRange("Document No.", PostedInvoiceNo);
CustLedgerEntry.SetRange("Customer No.", SalesHeader."Sell-to Customer No.");
CustLedgerEntry.SetFilter("Bill No.", '<>%1', '');
CustLedgerEntry.FindFirst();

// [GIVEN] Create a second exchange rate for the payment date (different rate to cause gain/loss)
PaymentDate := CalcDate('<+1M>', WorkDate());
LibraryERM.CreateExchRate(CurrencyExchangeRate, CurrencyCode, PaymentDate);
CurrencyExchangeRate.Validate("Exchange Rate Amount", 1);
CurrencyExchangeRate.Validate("Relational Exch. Rate Amount", LibraryRandom.RandDecInRange(2, 5, 2));
CurrencyExchangeRate.Validate("Adjustment Exch. Rate Amount", CurrencyExchangeRate."Exchange Rate Amount");
CurrencyExchangeRate.Validate("Relational Adjmt Exch Rate Amt", CurrencyExchangeRate."Relational Exch. Rate Amount");
CurrencyExchangeRate.Modify(true);

// [GIVEN] Payment journal applied to the bill and posted
CustLedgerEntry.CalcFields("Remaining Amount");
LibraryERM.SelectGenJnlBatch(GenJournalBatch);
LibraryERM.ClearGenJournalLines(GenJournalBatch);
LibraryERM.CreateGeneralJnlLineWithBalAcc(
GenJournalLine, GenJournalBatch."Journal Template Name", GenJournalBatch.Name,
GenJournalLine."Document Type"::Payment,
GenJournalLine."Account Type"::Customer, SalesHeader."Sell-to Customer No.",
GenJournalLine."Bal. Account Type"::"G/L Account", LibraryERM.CreateGLAccountNo(),
-Abs(CustLedgerEntry."Remaining Amount"));
GenJournalLine.Validate("Posting Date", PaymentDate);
GenJournalLine.Validate("Applies-to Bill No.", CustLedgerEntry."Bill No.");
GenJournalLine.Validate("Applies-to Doc. Type", GenJournalLine."Applies-to Doc. Type"::Bill);
GenJournalLine.Validate("Applies-to Doc. No.", PostedInvoiceNo);
GenJournalLine.Modify(true);
LibraryERM.PostGeneralJnlLine(GenJournalLine);

// [GIVEN] Find payment entry
CustLedgerEntry.Reset();
LibraryERM.FindCustomerLedgerEntry(CustLedgerEntry, CustLedgerEntry."Document Type"::Payment, GenJournalLine."Document No.");
TransactionNo := CustLedgerEntry."Transaction No.";

// [WHEN] Unapply entries and revert transaction
LibraryERM.UnapplyCustomerLedgerEntry(CustLedgerEntry);
LibraryERM.ReverseTransaction(TransactionNo);

// [THEN] No error occurs and entries are successfully reversed
CustLedgerEntry.Reset();
CustLedgerEntry.SetRange("Customer No.", SalesHeader."Sell-to Customer No.");
CustLedgerEntry.SetRange("Document No.", GenJournalLine."Document No.");
CustLedgerEntry.SetRange(Reversed, true);
Assert.RecordIsNotEmpty(CustLedgerEntry);
end;

local procedure Initialize()
begin
LibraryVariableStorage.Clear();
Expand Down Expand Up @@ -1762,6 +1833,23 @@
exit(LibrarySales.PostSalesDocument(SalesHeader, true, true));
end;

local procedure PostCarteraSalesInvoiceWithItem(var SalesHeader: Record "Sales Header"; CurrencyCode: Code[10]): Code[20]
var
Customer: Record Customer;
CustomerBankAccount: Record "Customer Bank Account";
Item: Record Item;
SalesLine: Record "Sales Line";
begin
LibraryCarteraReceivables.CreateCarteraCustomer(Customer, CurrencyCode);
LibraryCarteraReceivables.CreateCustomerBankAccount(Customer, CustomerBankAccount);
LibraryInventory.CreateItem(Item);
Item.Validate("Unit Price", LibraryRandom.RandDec(100, 2));
Item.Modify(true);
LibrarySales.CreateSalesHeader(SalesHeader, SalesHeader."Document Type"::Invoice, Customer."No.");
LibrarySales.CreateSalesLine(SalesLine, SalesHeader, SalesLine.Type::Item, Item."No.", LibraryRandom.RandInt(100));
exit(LibrarySales.PostSalesDocument(SalesHeader, true, true));
end;

local procedure UpdateCustLedgEntryPaymentCode(DocumentNo: Code[20]; CustomerNo: Code[20]; PaymentMethodCode: Code[20]; IsBill: Boolean): Code[20]
var
CustLedgerEntry: Record "Cust. Ledger Entry";
Expand Down
Loading