diff --git a/src/Apps/W1/WithholdingTax/Test/src/ERMWithholdingTaxTestsII.Codeunit.al b/src/Apps/W1/WithholdingTax/Test/src/ERMWithholdingTaxTestsII.Codeunit.al index e087179e4c..f3c5a0edec 100644 --- a/src/Apps/W1/WithholdingTax/Test/src/ERMWithholdingTaxTestsII.Codeunit.al +++ b/src/Apps/W1/WithholdingTax/Test/src/ERMWithholdingTaxTestsII.Codeunit.al @@ -42,6 +42,7 @@ codeunit 148322 "ERM Withholding Tax Tests II" ValueMustBeSameMsg: Label 'Value must be same.'; WHTAccountCodeEmptyErr: Label '%1 must have a value in Withholding Tax Posting Setup: Withholding Tax Bus. Post. Group=%2, Withholding Tax Prod. Post. Group=%3. It cannot be zero or empty.', Comment = '%1 = Field Caption, %2 = WHT Business Posting Group Code, %3 = WHT Product Posting Group Code'; + WHTProdPostGroupNotCopiedErr: Label 'Withholding Tax Prod. Post. Group must be copied from the applied Invoice to the Payment line.'; IsInitialized: Boolean; [Test] @@ -595,6 +596,47 @@ codeunit 148322 "ERM Withholding Tax Tests II" VerifyPostingNoSeriesOnePostedDoc('B0', 6, 2); end; + [Test] + [Scope('OnPrem')] + [HandlerFunctions('ConfirmHandler')] + procedure WithholdingTaxProductPostingGroupIsCopiedToWithholdingTaxEntry() + var + GenJournalLine: Record "Gen. Journal Line"; + 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"; + WHTEntry: Record "Withholding Tax Entry"; + begin + // [SCENARIO 639524] Withholding Tax Product Posting Group is transferred from the General Journal Line to the Withholding Tax Entry created when the Invoice is posted. + Initialize(); + + // [GIVEN] Local functionalities enabled, VAT Posting Setup, Withholding Tax Business Posting Group and Withholding Tax Product Posting Group. + UpdateLocalFunctionalitiesOnGeneralLedgerSetup(true); + LibraryERM.FindVATPostingSetup(VATPostingSetup, VATPostingSetup."VAT Calculation Type"::"Normal VAT"); + LibraryWithholdingTax.CreateWHTBusinessPostingGroup(WHTBusPostingGroup); + LibraryWithholdingTax.CreateWHTProductPostingGroup(WHTProdPostingGroup); + + // [GIVEN] A General Journal Line for a Withholding Tax liable Vendor. + 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)); + + // [GIVEN] The Withholding Tax Absorb Base is updated and the Withholding Tax Posting Setup is found for the General Journal Line. + UpdateGenJournalLineWHTAbsorbBase(GenJournalLine); + FindWHTPostingSetup(WHTPostingSetup, GenJournalLine."Wthldg. Tax Bus. Post. Group", GenJournalLine."Wthldg. Tax Prod. Post. Group", ''); + + // [WHEN] The Invoice is posted. + LibraryERM.PostGeneralJnlLine(GenJournalLine); + + // [THEN] The Withholding Tax Entry has the same Withholding Tax Product Posting Group as the source General Journal Line. + WHTEntry.SetRange("Document Type", WHTEntry."Document Type"::Invoice); + WHTEntry.SetRange("Bill-to/Pay-to No.", GenJournalLine."Account No."); + WHTEntry.FindFirst(); + Assert.AreEqual(WHTProdPostingGroup.Code, WHTEntry."Wthldg. Tax Prod. Post. Group", WHTProdPostGroupNotCopiedErr); + end; + local procedure Initialize() var VATPostingSetup: Record "VAT Posting Setup"; diff --git a/src/Apps/W1/WithholdingTax/app/src/Purchase/Transaction/WithholdingTaxJnlSubscriber.Codeunit.al b/src/Apps/W1/WithholdingTax/app/src/Purchase/Transaction/WithholdingTaxJnlSubscriber.Codeunit.al index 0b495a8881..bc08a0dfb6 100644 --- a/src/Apps/W1/WithholdingTax/app/src/Purchase/Transaction/WithholdingTaxJnlSubscriber.Codeunit.al +++ b/src/Apps/W1/WithholdingTax/app/src/Purchase/Transaction/WithholdingTaxJnlSubscriber.Codeunit.al @@ -1683,6 +1683,36 @@ codeunit 6786 "Withholding Tax Jnl Subscriber" PostUnrealizedWHT(GenJournalLine, GenJnlPostLine); end; + + [EventSubscriber(ObjectType::Page, Page::"Create Payment", OnBeforeUpdateGnlJnlLineDimensionsFromVendorPayment, '', false, false)] + local procedure AssignWHTPostingGroupOnCreatePayment(var GenJournalLine: Record "Gen. Journal Line"; TempVendorPaymentBuffer: Record "Vendor Payment Buffer" temporary) + var + VendorLedgerEntry: Record "Vendor Ledger Entry"; + WithholdingTaxEntry: Record "Withholding Tax Entry"; + begin + if CheckWithholdingTaxDisabled() then + exit; + + if GenJournalLine."Applies-to ID" = '' then + exit; + + if TempVendorPaymentBuffer."Vendor Ledg. Entry No." = 0 then + exit; + + VendorLedgerEntry.SetLoadFields("Document No.", "Vendor No."); + VendorLedgerEntry.Get(TempVendorPaymentBuffer."Vendor Ledg. Entry No."); + + WithholdingTaxEntry.SetLoadFields("Wthldg. Tax Prod. Post. Group"); + WithholdingTaxEntry.SetRange("Document No.", VendorLedgerEntry."Document No."); + WithholdingTaxEntry.SetRange("Transaction Type", WithholdingTaxEntry."Transaction Type"::Purchase); + WithholdingTaxEntry.SetRange("Document Type", WithholdingTaxEntry."Document Type"::Invoice); + WithholdingTaxEntry.SetRange("Bill-to/Pay-to No.", VendorLedgerEntry."Vendor No."); + if not WithholdingTaxEntry.FindFirst() then + exit; + + GenJournalLine."Wthldg. Tax Prod. Post. Group" := WithholdingTaxEntry."Wthldg. Tax Prod. Post. Group"; + end; + local procedure PostUnrealizedWHT(var GenJnlLine: Record "Gen. Journal Line"; var GenJnlPostLine: Codeunit "Gen. Jnl.-Post Line") var GenJournalLineWHT: Record "Gen. Journal Line";