Skip to content
Open
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
43 changes: 41 additions & 2 deletions src/Layers/APAC/BaseApp/Sales/Posting/SalesPost.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -7771,8 +7771,8 @@ codeunit 80 "Sales-Post"
if SalesLine.Type <> SalesLine.Type::" " then
if SalesLine."Prepayment Line" then begin
TempPrepmtVATAmtBuf.Get(0, '', SalesLine."Line No.");
TempPrepmtVATAmtBuf.Amount := SalesLine."Amount Including VAT" - SalesLine.Amount;
TempPrepmtVATAmtBuf."VAT Base Amount" := SalesLine."VAT Base Amount";
TempPrepmtVATAmtBuf.Amount := -SalesLine."Amount Including VAT" + SalesLine.Amount;
TempPrepmtVATAmtBuf."VAT Base Amount" := -SalesLine."VAT Base Amount";
TempPrepmtVATAmtBuf.Modify();
end else
if not SalesLine."System-Created Entry" then
Expand All @@ -7788,6 +7788,44 @@ codeunit 80 "Sales-Post"
end;
end;

local procedure DividePrepmtVATDeducted(var SalesLine: Record "Sales Line")
var
PrepmtVATReminder: Decimal;
PrepmtVATBaseReminder: Decimal;
Ratio: Decimal;
begin
if TempPrepmtVATAmtBuf.FindSet() then
repeat
TempPrepmtLineNoBuf.SetRange("New Line Number", TempPrepmtVATAmtBuf."Line No.");
if TempPrepmtLineNoBuf.FindSet() then
repeat
SalesLine.Get(SalesLine."Document Type", SalesLine."Document No.", TempPrepmtLineNoBuf."Old Line Number");
Ratio := SalesLine."Prepmt Amt to Deduct" / TempPrepmtVATAmtBuf."Prepmt Amt to Deduct";
DistributeAmount(
TempPrepmtVATAmtBuf.Amount, Ratio,
SalesLine."Prepmt. VAT Amount Deducted", PrepmtVATReminder);
DistributeAmount(
TempPrepmtVATAmtBuf."VAT Base Amount", Ratio,
SalesLine."Prepmt. VAT Base Deducted", PrepmtVATBaseReminder);
SalesLine.Modify();
until TempPrepmtLineNoBuf.Next() = 0;
until TempPrepmtVATAmtBuf.Next() = 0;
TempPrepmtVATAmtBuf.DeleteAll();
TempPrepmtLineNoBuf.Reset();
TempPrepmtLineNoBuf.DeleteAll();
end;

local procedure DistributeAmount(AmountToDistibute: Decimal; Ratio: Decimal; var Amount: Decimal; var Reminder: Decimal)
var
DistibutedAmount: Decimal;
DistibutedAmountRnded: Decimal;
begin
DistibutedAmount := Reminder + AmountToDistibute * Ratio;
DistibutedAmountRnded := Round(DistibutedAmount, Currency."Amount Rounding Precision");
Reminder := DistibutedAmount - DistibutedAmountRnded;
Amount := Amount + DistibutedAmountRnded;
end;

local procedure UpdateTaxForPostedDoc(var SalesHeader: Record "Sales Header")
var
SalesInvHeader: Record "Sales Invoice Header";
Expand Down Expand Up @@ -8608,6 +8646,7 @@ codeunit 80 "Sales-Post"
OnBeforePostUpdateOrderLine(SalesHeader, TempSalesLineGlobal, SuppressCommit, SalesSetup);

ResetTempLines(TempSalesLine);
DividePrepmtVATDeducted(TempSalesLine);
TempSalesLine.SetRange("Prepayment Line", false);
TempSalesLine.SetFilter(Quantity, '<>0');
OnPostUpdateOrderLineOnAfterSetFilters(TempSalesLine);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,7 @@ codeunit 442 "Sales-Post Prepayments"
PrepmtAmtToInvTotal := SalesLine."Prepmt. Line Amount" - SalesLine."Prepmt. Amt. Inv.";
if SalesLine.FindSet() then
repeat
DeductedVATBaseAmount := 0;
PrepmtAmt := PrepmtAmount(SalesLine, DocumentType);
if PrepmtAmt <> 0 then begin
if IsFullGST(SalesLine) then
Expand Down Expand Up @@ -2001,6 +2002,8 @@ codeunit 442 "Sales-Post Prepayments"
SalesLine."Prepmt Amt to Deduct" := 0;
SalesLine."Prepmt VAT Diff. to Deduct" := 0;
SalesLine."Prepayment VAT Difference" := 0;
SalesLine."Prepmt. VAT Amount Deducted" := 0;
SalesLine."Prepmt. VAT Base Deducted" := 0;
OnUpdateSalesDocumentOnBeforeModifyCreditMemoSalesLine(SalesLine);
SalesLine.Modify();
until SalesLine.Next() = 0;
Expand Down
69 changes: 69 additions & 0 deletions src/Layers/APAC/Tests/Local/ERMGSTOnPrepaymentsII.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,60 @@ codeunit 141027 "ERM GST On Prepayments II"
GeneralLedgerSetup."Enable GST (Australia)", GeneralLedgerSetup."GST Report", GeneralLedgerSetup."Full GST on Prepayment");
end;

[Test]
[Scope('OnPrem')]
procedure GSTOnSalesPrepmtCrMemoAfterPartialFinalInvoice()
var
GeneralLedgerSetup: Record "General Ledger Setup";
SalesCrMemoHeader: Record "Sales Cr.Memo Header";
SalesHeader: Record "Sales Header";
SalesInvoiceHeader: Record "Sales Invoice Header";
SalesLine: Record "Sales Line";
DocumentNo: Code[20];
DeductedVATAmount: Decimal;
DeductedVATBase: Decimal;
PrepmtInvVATAmount: Decimal;
begin
// [SCENARIO 8896] GST on a Sales Prepayment Credit Memo posted after a partial final Invoice excludes the prepayment GST that the partial Invoice already deducted.

// [GIVEN] Full GST On Prepayment. Create Sales Order, post Sales Prepayment Invoice and make payment for the same.
Initialize();
GeneralLedgerSetup.Get();
UpdateLocalFunctionalitiesOnGeneralLedgerSetup(true, true, true); // TRUE for Enable GST, GST Reports, Full GST On Prepayment.
CreateSalesOrderAndUpdateGeneralPostingSetup(SalesLine, '', LibraryRandom.RandDec(10, 2), false); // Taking blank value for Currency Code, random value for Prepayment Pct., FALSE for Prices Including VAT.
PostPaymentForSalesPrepaymentInvoice(SalesHeader, SalesLine);
FindPrepaymentSalesInvoiceHeader(SalesInvoiceHeader, SalesHeader."Sell-to Customer No.");
SalesInvoiceHeader.CalcFields(Amount);
PrepmtInvVATAmount := SalesInvoiceHeader."Amount Including VAT" - SalesInvoiceHeader.Amount;

// [GIVEN] Post a partial final Invoice, deducting part of the prepayment and its GST.
UpdateQuantityToInvoiceOnSalesLine(SalesLine);
DocumentNo := LibrarySales.PostSalesDocument(SalesHeader, true, true); // Post as Ship and Invoice.
GetPrepmtDeductedVATOnSalesInvoice(DocumentNo, DeductedVATAmount, DeductedVATBase);

// [THEN] The Sales Order line stores the prepayment GST deducted by the partial Invoice.
SalesLine.Get(SalesLine."Document Type", SalesLine."Document No.", SalesLine."Line No.");
Assert.AreNearlyEqual(
DeductedVATAmount, SalesLine."Prepmt. VAT Amount Deducted", LibraryERM.GetAmountRoundingPrecision(), AmountMustMatchMsg);
Assert.AreNearlyEqual(
DeductedVATBase, SalesLine."Prepmt. VAT Base Deducted", LibraryERM.GetAmountRoundingPrecision(), AmountMustMatchMsg);

// [WHEN] Post the Sales Prepayment Credit Memo.
LibrarySales.PostSalesPrepaymentCrMemo(SalesHeader);

// [THEN] The Credit Memo GST equals the prepaid GST minus the GST already deducted by the partial Invoice.
SalesHeader.Get(SalesHeader."Document Type", SalesHeader."No.");
SalesCrMemoHeader.Get(SalesHeader."Last Prepmt. Cr. Memo No.");
SalesCrMemoHeader.CalcFields(Amount, "Amount Including VAT");
Assert.AreNearlyEqual(
PrepmtInvVATAmount - DeductedVATAmount, SalesCrMemoHeader."Amount Including VAT" - SalesCrMemoHeader.Amount,
LibraryERM.GetAmountRoundingPrecision(), AmountMustMatchMsg);

// Tear Down.
UpdateLocalFunctionalitiesOnGeneralLedgerSetup(
GeneralLedgerSetup."Enable GST (Australia)", GeneralLedgerSetup."GST Report", GeneralLedgerSetup."Full GST on Prepayment");
end;

[Test]
[Scope('OnPrem')]
procedure GLEntryAfterPostPartialPurchInvAndPrepaymentInv()
Expand Down Expand Up @@ -1509,6 +1563,21 @@ codeunit 141027 "ERM GST On Prepayments II"
SalesInvoiceHeader.CalcFields("Amount Including VAT");
end;

local procedure GetPrepmtDeductedVATOnSalesInvoice(DocumentNo: Code[20]; var DeductedVATAmount: Decimal; var DeductedVATBase: Decimal)
var
SalesInvoiceLine: Record "Sales Invoice Line";
begin
DeductedVATAmount := 0;
DeductedVATBase := 0;
SalesInvoiceLine.SetRange("Document No.", DocumentNo);
SalesInvoiceLine.SetRange("Prepayment Line", true);
SalesInvoiceLine.FindSet();
repeat
DeductedVATAmount += SalesInvoiceLine.Amount - SalesInvoiceLine."Amount Including VAT"; // Deduction lines are negative, so this yields the reversed GST as a positive amount.
DeductedVATBase -= SalesInvoiceLine."VAT Base Amount";
until SalesInvoiceLine.Next() = 0;
end;

local procedure GetPayableAccount(No: Code[20]): Code[20]
var
Vendor: Record Vendor;
Expand Down
Loading