diff --git a/src/Apps/W1/WithholdingTax/Test/src/ERMWithholdingTaxTestsII.Codeunit.al b/src/Apps/W1/WithholdingTax/Test/src/ERMWithholdingTaxTestsII.Codeunit.al index 58c0c74c25f..9f23ffc57ae 100644 --- a/src/Apps/W1/WithholdingTax/Test/src/ERMWithholdingTaxTestsII.Codeunit.al +++ b/src/Apps/W1/WithholdingTax/Test/src/ERMWithholdingTaxTestsII.Codeunit.al @@ -46,6 +46,7 @@ codeunit 148322 "ERM Withholding Tax Tests II" WHTProdPostGroupNotCopiedErr: Label 'Withholding Tax Prod. Post. Group must be copied from the applied Invoice to the Payment line.'; EmployeeOnlyOptionErr: Label 'The %1 option can be used only when the withholding tax is for employees.', Comment = '%1 = field caption'; ThresholdPeriodNotAllowedErr: Label 'The %1 can be specified only when %2 is %3 or %4.', Comment = '%1 = Withholding Threshold Period field caption, %2 = Withholding Threshold Base field caption, %3 = Category in Period option, %4 = Total in Period option'; + GenJnlTemplateNotFoundErr: Label 'A general journal template with Type Purchases does not exist.'; IsInitialized: Boolean; [Test] @@ -764,6 +765,58 @@ codeunit 148322 "ERM Withholding Tax Tests II" Assert.AreEqual(WHTPostingSetup."WHT Threshold Period"::Month, WHTPostingSetup."WHT Threshold Period", ValueMustBeSameMsg); end; + [Test] + [Scope('OnPrem')] + [HandlerFunctions('ConfirmHandler')] + procedure PostingWHTPaymentWithoutPurchaseJournalTemplateErr() + var + BankAccount: Record "Bank Account"; + GenJournalLine: Record "Gen. Journal Line"; + GenJournalLine2: Record "Gen. Journal Line"; + GenJournalTemplate: Record "Gen. Journal Template"; + VATPostingSetup: Record "VAT Posting Setup"; + WHTBusPostingGroup: Record "Wthldg. Tax Bus. Post. Group"; + WHTPostingSetup: Record "Withholding Tax Posting Setup"; + WHTProdPostingGroup: Record "Wthldg. Tax Prod. Post. Group"; + DocumentNo: Code[20]; + begin + // [FEATURE] [AI Test] + // [SCENARIO 647253] Posting a WHT payment without a purchase journal template results in an error. + Initialize(); + + // [GIVEN] A posted purchase invoice with withholding tax. + UpdateLocalFunctionalitiesOnGeneralLedgerSetup(true); + LibraryERM.FindVATPostingSetup(VATPostingSetup, VATPostingSetup."VAT Calculation Type"::"Normal VAT"); + LibraryWithholdingTax.CreateWHTBusinessPostingGroup(WHTBusPostingGroup); + LibraryWithholdingTax.CreateWHTProductPostingGroup(WHTProdPostingGroup); + CreateGeneralJournalLineWithBalAccountType( + GenJournalLine, GenJournalLine."Document Type"::Invoice, CreateVendor(VATPostingSetup."VAT Bus. Posting Group", WHTBusPostingGroup.Code), '', + '', GenJournalLine."Bal. Account Type"::"G/L Account", CreateGLAccountWithVATBusPostingGroup(VATPostingSetup, WHTProdPostingGroup.Code), + -LibraryRandom.RandDecInRange(100, 200, 2)); + UpdateGenJournalLineWHTAbsorbBase(GenJournalLine); + FindWHTPostingSetup(WHTPostingSetup, GenJournalLine."Wthldg. Tax Bus. Post. Group", GenJournalLine."Wthldg. Tax Prod. Post. Group", ''); + LibraryERM.PostGeneralJnlLine(GenJournalLine); + DocumentNo := FindVendorLedgerEntry(GenJournalLine."Account No."); + + // [GIVEN] A payment journal line applied to the posted invoice. + LibraryERM.CreateBankAccount(BankAccount); + CreateGeneralJournalLineWithBalAccountType( + GenJournalLine2, GenJournalLine."Document Type"::Payment, GenJournalLine."Account No.", DocumentNo, + '', GenJournalLine2."Bal. Account Type"::"Bank Account", BankAccount."No.", -FindVendorLedgerEntryAmount(DocumentNo)); + GenJournalLine2.Validate("Wthldg. Tax Prod. Post. Group", WHTPostingSetup."Wthldg. Tax Prod. Post. Group"); + GenJournalLine2.Modify(true); + + // [GIVEN] No general journal template of type Purchases exists. + GenJournalTemplate.SetRange(Type, GenJournalTemplate.Type::Purchases); + GenJournalTemplate.DeleteAll(true); + + // [WHEN] Post the payment journal. + asserterror LibraryERM.PostGeneralJnlLine(GenJournalLine2); + + // [THEN] The missing purchase journal template error is raised. + Assert.ExpectedError(GenJnlTemplateNotFoundErr); + end; + local procedure Initialize() var VATPostingSetup: Record "VAT Posting Setup"; diff --git a/src/Apps/W1/WithholdingTax/app/src/Purchase/Transaction/WithholdingTaxMgmt.Codeunit.al b/src/Apps/W1/WithholdingTax/app/src/Purchase/Transaction/WithholdingTaxMgmt.Codeunit.al index d6294bec1b0..05a05dd7f4b 100644 --- a/src/Apps/W1/WithholdingTax/app/src/Purchase/Transaction/WithholdingTaxMgmt.Codeunit.al +++ b/src/Apps/W1/WithholdingTax/app/src/Purchase/Transaction/WithholdingTaxMgmt.Codeunit.al @@ -78,6 +78,7 @@ codeunit 6785 "Withholding Tax Mgmt." WithholdingMinInvNotConsistentErr: Label 'You cannot post a transaction using different Withholding Tax minimum invoice amounts on lines.'; DiffWithholdingPostGroupsErr: Label 'The Withholding Tax posting groups are different and thus the entries cannot be apply.'; MissingRevenueTypeErr: Label 'The Withholding Tax Entry you are trying to process contains WHT Revenue Type `%1`. Please add this value to your Withholding Revenue Types and post again.', Comment = '%1 = Withholding Revenue Type'; + GenJnlTemplateNotFoundErr: Label 'A general journal template with Type Purchases does not exist.'; MustbeNegativeLbl: Label 'must be positive.'; OneLbl: Label 'ONE'; TwoLbl: Label 'TWO'; @@ -4240,10 +4241,12 @@ codeunit 6785 "Withholding Tax Mgmt." if Source = Source::Vendor then GenJnlTemplate.SetRange(Type, GenJnlTemplate.Type::Purchases); - if GenJnlTemplate.FindFirst() then + if GenJnlTemplate.FindFirst() then begin if GenJnlLine."Journal Template Name" <> GenJnlTemplate.Name then if WithholdingTaxEntry2.Amount <> 0 then InsertWithholdingTaxPostingBuffer(WithholdingTaxEntry2, GenJnlLine, 0, AmountWithDisc); + end else + Error(GenJnlTemplateNotFoundErr); end; until (WithholdingTaxEntry.Next() = 0);