diff --git a/src/Layers/BE/BaseApp/Local/BankMgt/Payment/SuggestVendorPaymentsEB.Report.al b/src/Layers/BE/BaseApp/Local/BankMgt/Payment/SuggestVendorPaymentsEB.Report.al index ad6aad1115..322305e3a9 100644 --- a/src/Layers/BE/BaseApp/Local/BankMgt/Payment/SuggestVendorPaymentsEB.Report.al +++ b/src/Layers/BE/BaseApp/Local/BankMgt/Payment/SuggestVendorPaymentsEB.Report.al @@ -119,11 +119,20 @@ report 2000019 "Suggest Vendor Payments EB" ApplicationArea = Basic, Suite; Caption = 'Take Payment Discounts'; ToolTip = 'Specifies if you want the batch job to include vendor ledger entries for which you can receive a payment discount.'; + + trigger OnValidate() + begin + if (IncPmtDiscount) and (PmtDiscDueDate = 0D) then + PmtDiscDueDate := WorkDate() + else + PmtDiscDueDate := 0D; + end; } field(PmtDiscDueDate; PmtDiscDueDate) { ApplicationArea = Basic, Suite; Caption = 'Payment Discount Date'; + Editable = IncPmtDiscount; ToolTip = 'Specifies the date that will be used to calculate the payment discount.'; } field(MaximumAmount; MaximumAmount) @@ -151,8 +160,10 @@ report 2000019 "Suggest Vendor Payments EB" begin if DueDate = 0D then DueDate := WorkDate(); - if PmtDiscDueDate = 0D then - PmtDiscDueDate := WorkDate(); + if (IncPmtDiscount) and (PmtDiscDueDate = 0D) then + PmtDiscDueDate := WorkDate() + else + PmtDiscDueDate := 0D; if PostingDate = 0D then PostingDate := WorkDate(); IncCreditMemos := true; diff --git a/src/Layers/BE/Tests/Local/ERMPaymentJournal.Codeunit.al b/src/Layers/BE/Tests/Local/ERMPaymentJournal.Codeunit.al index c428613efe..c838cf7089 100644 --- a/src/Layers/BE/Tests/Local/ERMPaymentJournal.Codeunit.al +++ b/src/Layers/BE/Tests/Local/ERMPaymentJournal.Codeunit.al @@ -34,24 +34,25 @@ codeunit 144003 "ERM Payment Journal" end; var - LibraryReportDataset: Codeunit "Library - Report Dataset"; - LibraryRandom: Codeunit "Library - Random"; + Assert: Codeunit Assert; + LibraryDimension: Codeunit "Library - Dimension"; LibraryERM: Codeunit "Library - ERM"; - LibraryUtility: Codeunit "Library - Utility"; LibraryPurchase: Codeunit "Library - Purchase"; + LibraryRandom: Codeunit "Library - Random"; + LibraryReportDataset: Codeunit "Library - Report Dataset"; LibrarySales: Codeunit "Library - Sales"; - Assert: Codeunit Assert; + LibrarySetupStorage: Codeunit "Library - Setup Storage"; LibraryTestInitialize: Codeunit "Library - Test Initialize"; + LibraryUtility: Codeunit "Library - Utility"; LibraryVariableStorage: Codeunit "Library - Variable Storage"; - LibraryDimension: Codeunit "Library - Dimension"; - LibrarySetupStorage: Codeunit "Library - Setup Storage"; isInitialized: Boolean; refExportProtocolType: Option Domestic,International; IncorrectNumberOfDimErr: Label 'Incorrect number of dimensions.'; + LettersTxt: Label 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', Locked = true; WrongNumberOfLinesErr: Label 'Wrong number of Payment Journal Lines.'; - WrongStatusOfLineErr: Label 'Wrong status of Payment Journal Line.'; + WrongPmtDiscErr: Label 'Wrong %1 on the payment journal line.', Comment = '%1 = Field Caption'; WrongStatusOfBatchErr: Label 'Wrong status of Payment Journal Batch.'; - LettersTxt: Label 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', Locked = true; + WrongStatusOfLineErr: Label 'Wrong status of Payment Journal Line.'; [Test] [Scope('OnPrem')] @@ -635,6 +636,76 @@ codeunit 144003 "ERM Payment Journal" VerifyGenJnlLinesWithSameDescription(GenJnlBatch, PaymentJnlLine."Payment Message", 2); end; + [Test] + [HandlerFunctions('SuggestVendorPaymentsTakeDiscRPH')] + procedure SuggestVendorPaymentsWithoutTakingPaymentDiscount() + var + PaymentJournalLine: Record "Payment Journal Line"; + PurchaseHeader: Record "Purchase Header"; + VendorLedgerEntry: Record "Vendor Ledger Entry"; + VendorNo: Code[20]; + begin + // [SCENARIO 637839] Payment discount is not taken when "Take Payment Discounts" is disabled in the "Suggest Vendor Payments EB" report. + Initialize(); + + // [GIVEN] Create a Vendor with Payment terms with Payment Discount. + VendorNo := CreateVendorWithPaymentDiscount(); + + // [GIVEN] Create and post a Purchase Invoice. + CreateAndPostPurchaseDocument(PurchaseHeader, PurchaseHeader."Document Type"::Invoice, VendorNo); + + // [GIVEN] Find the Vendor Ledger Entry for the posted Purchase Invoice. + FindInvoiceVendorLedgerEntry(VendorLedgerEntry, VendorNo); + + // [WHEN] Run "Suggest Vendor Payments EB" with "Take Payment Discounts" = FALSE. + LibraryVariableStorage.Enqueue(false); + SuggestVendorPayments(VendorNo); + + // [WHEN] Filter the Payment Journal Line for the Vendor. + FilterEBPaymentJournalLine(PaymentJournalLine, VendorNo, PaymentJournalLine.Status::Created); + PaymentJournalLine.FindFirst(); + + // [THEN] Verify that the "Pmt. Discount Date" is Blank, Pmt Discount is not taken, in Payment Journal Line. + Assert.AreEqual(0D, PaymentJournalLine."Pmt. Discount Date", StrSubstNo(WrongPmtDiscErr, PaymentJournalLine."Pmt. Discount Date")); + Assert.AreEqual(0, PaymentJournalLine."Pmt. Disc. Possible", StrSubstNo(WrongPmtDiscErr, PaymentJournalLine."Pmt. Disc. Possible")); + LibraryVariableStorage.AssertEmpty(); + end; + + [Test] + [HandlerFunctions('SuggestVendorPaymentsTakeDiscRPH')] + procedure SuggestVendorPaymentsTakingPaymentDiscount() + var + PaymentJournalLine: Record "Payment Journal Line"; + PurchaseHeader: Record "Purchase Header"; + VendorLedgerEntry: Record "Vendor Ledger Entry"; + VendorNo: Code[20]; + begin + // [SCENARIO 637839] Payment discount is taken when "Take Payment Discounts" is enabled in the "Suggest Vendor Payments EB" report. + Initialize(); + + // [GIVEN] Create a Vendor with Payment terms with Payment Discount. + VendorNo := CreateVendorWithPaymentDiscount(); + + // [GIVEN] Create and post a Purchase Invoice. + CreateAndPostPurchaseDocument(PurchaseHeader, PurchaseHeader."Document Type"::Invoice, VendorNo); + + // [GIVEN] Find the Vendor Ledger Entry for the posted Purchase Invoice. + FindInvoiceVendorLedgerEntry(VendorLedgerEntry, VendorNo); + + // [WHEN] Run "Suggest Vendor Payments EB" with "Take Payment Discounts" = TRUE. + LibraryVariableStorage.Enqueue(true); + SuggestVendorPayments(VendorNo); + + // [WHEN] Filter the Payment Journal Line for the Vendor. + FilterEBPaymentJournalLine(PaymentJournalLine, VendorNo, PaymentJournalLine.Status::Created); + PaymentJournalLine.FindFirst(); + + // [THEN] Verify that the "Pmt. Discount Date" and Pmt Discount is taken, in Payment Journal Line. + Assert.AreEqual(WorkDate(), PaymentJournalLine."Pmt. Discount Date", StrSubstNo(WrongPmtDiscErr, PaymentJournalLine."Pmt. Discount Date")); + Assert.AreEqual(PaymentJournalLine."Pmt. Disc. Possible", Abs(VendorLedgerEntry."Remaining Pmt. Disc. Possible"), StrSubstNo(WrongPmtDiscErr, PaymentJournalLine."Pmt. Disc. Possible")); + LibraryVariableStorage.AssertEmpty(); + end; + local procedure Initialize() begin LibraryTestInitialize.OnTestInitialize(CODEUNIT::"ERM Payment Journal"); @@ -1488,6 +1559,26 @@ codeunit 144003 "ERM Payment Journal" Assert.RecordCount(GenJournalLine, ExpectedCount); end; + local procedure CreateVendorWithPaymentDiscount(): Code[20] + var + PaymentTerms: Record "Payment Terms"; + Vendor: Record Vendor; + begin + LibraryERM.CreatePaymentTermsDiscount(PaymentTerms, false); + LibraryPurchase.CreateVendor(Vendor); + Vendor.Validate("Payment Terms Code", PaymentTerms.Code); + Vendor.Modify(true); + + exit(Vendor."No."); + end; + + local procedure FindInvoiceVendorLedgerEntry(var VendorLedgerEntry: Record "Vendor Ledger Entry"; VendorNo: Code[20]) + begin + VendorLedgerEntry.SetRange("Document Type", VendorLedgerEntry."Document Type"::Invoice); + VendorLedgerEntry.SetRange("Vendor No.", VendorNo); + VendorLedgerEntry.FindFirst(); + end; + [RequestPageHandler] [Scope('OnPrem')] procedure ExportPaymentJournalLinesHandler(var FileDomesticPayments: TestRequestPage "File Domestic Payments") @@ -1532,5 +1623,13 @@ codeunit 144003 "ERM Payment Journal" EBPaymentJournalTemplates.FILTER.SetFilter(Name, LibraryVariableStorage.DequeueText()); EBPaymentJournalTemplates.OK().Invoke(); end; + + [RequestPageHandler] + procedure SuggestVendorPaymentsTakeDiscRPH(var SuggestVendorPaymentsEB: TestRequestPage "Suggest Vendor Payments EB") + begin + SuggestVendorPaymentsEB.DueDate.SetValue(CalcDate('<3M>', WorkDate())); + SuggestVendorPaymentsEB.IncPmtDiscount.SetValue(LibraryVariableStorage.DequeueBoolean()); + SuggestVendorPaymentsEB.OK().Invoke(); + end; }