ALAppExtensions icon indicating copy to clipboard operation
ALAppExtensions copied to clipboard

[Event Request] Table 39 "Purchase Line".UpdateVATOnLines

Open Softstore opened this issue 5 years ago • 1 comments

New Event to give the possibility to changes/create new fields for VAT Diference ( ND Diferente for PT localization)

procedure UpdateVATOnLines(QtyType: Option General,Invoicing,Shipping; var PurchHeader: Record "Purchase Header"; var PurchLine: Record "Purchase Line"; var VATAmountLine: Record "VAT Amount Line") LineWasModified: Boolean var TempVATAmountLineRemainder: Record "VAT Amount Line" temporary; Currency: Record Currency; NewAmount: Decimal; NewAmountIncludingVAT: Decimal; NewVATBaseAmount: Decimal; VATAmount: Decimal; VATDifference: Decimal; InvDiscAmount: Decimal; LineAmountToInvoice: Decimal; LineAmountToInvoiceDiscounted: Decimal; DeferralAmount: Decimal; begin LineWasModified := false; if QtyType = QtyType::Shipping then exit; if PurchHeader."Currency Code" = '' then Currency.InitRoundingPrecision else Currency.Get(PurchHeader."Currency Code");

    TempVATAmountLineRemainder.DeleteAll();

    with PurchLine do begin
        SetRange("Document Type", PurchHeader."Document Type");
        SetRange("Document No.", PurchHeader."No.");
        OnUpdateVATOnLinesOnAfterSetFilters(PurchLine, PurchHeader);
        LockTable();
        if FindSet then
            repeat
                if not ZeroAmountLine(QtyType) then begin
                    DeferralAmount := GetDeferralAmount;
                    VATAmountLine.Get("VAT Identifier", "VAT Calculation Type", "Tax Group Code", "Use Tax", "Line Amount" >= 0);
                    if VATAmountLine.Modified then begin
                        if not TempVATAmountLineRemainder.Get(
                             "VAT Identifier", "VAT Calculation Type", "Tax Group Code", "Use Tax", "Line Amount" >= 0)
                        then begin
                            TempVATAmountLineRemainder := VATAmountLine;
                            TempVATAmountLineRemainder.Init();
                            TempVATAmountLineRemainder.Insert();
                        end;

                        if QtyType = QtyType::General then
                            LineAmountToInvoice := "Line Amount"
                        else
                            LineAmountToInvoice :=
                              Round("Line Amount" * "Qty. to Invoice" / Quantity, Currency."Amount Rounding Precision");

                        if "Allow Invoice Disc." then begin
                            if (VATAmountLine."Inv. Disc. Base Amount" = 0) or (LineAmountToInvoice = 0) then
                                InvDiscAmount := 0
                            else begin
                                if QtyType = QtyType::General then
                                    LineAmountToInvoice := "Line Amount"
                                else
                                    LineAmountToInvoice :=
                                      Round("Line Amount" * "Qty. to Invoice" / Quantity, Currency."Amount Rounding Precision");
                                LineAmountToInvoiceDiscounted :=
                                  VATAmountLine."Invoice Discount Amount" * LineAmountToInvoice /
                                  VATAmountLine."Inv. Disc. Base Amount";
                                TempVATAmountLineRemainder."Invoice Discount Amount" :=
                                  TempVATAmountLineRemainder."Invoice Discount Amount" + LineAmountToInvoiceDiscounted;
                                InvDiscAmount :=
                                  Round(
                                    TempVATAmountLineRemainder."Invoice Discount Amount", Currency."Amount Rounding Precision");
                                TempVATAmountLineRemainder."Invoice Discount Amount" :=
                                  TempVATAmountLineRemainder."Invoice Discount Amount" - InvDiscAmount;
                            end;
                            if QtyType = QtyType::General then begin
                                "Inv. Discount Amount" := InvDiscAmount;
                                CalcInvDiscToInvoice();
                            end else
                                "Inv. Disc. Amount to Invoice" := InvDiscAmount;
                        end else
                            InvDiscAmount := 0;

                        OnUpdateVATOnLinesOnBeforeCalculateAmounts(PurchLine, PurchHeader);
                        if QtyType = QtyType::General then begin
                            if PurchHeader."Prices Including VAT" then begin
                                if (VATAmountLine.CalcLineAmount = 0) or ("Line Amount" = 0) then begin
                                    VATAmount := 0;
                                    NewAmountIncludingVAT := 0;
                                end else begin
                                    VATAmount :=
                                      TempVATAmountLineRemainder."VAT Amount" +
                                      VATAmountLine."VAT Amount" * CalcLineAmount / VATAmountLine.CalcLineAmount;
                                    NewAmountIncludingVAT :=
                                      TempVATAmountLineRemainder."Amount Including VAT" +
                                      VATAmountLine."Amount Including VAT" * CalcLineAmount / VATAmountLine.CalcLineAmount;
                                end;
                                NewAmount :=
                                  Round(NewAmountIncludingVAT, Currency."Amount Rounding Precision") -
                                  Round(VATAmount, Currency."Amount Rounding Precision");
                                NewVATBaseAmount :=
                                  Round(
                                    NewAmount * (1 - PurchHeader."VAT Base Discount %" / 100),
                                    Currency."Amount Rounding Precision");
                            end else begin
                                if "VAT Calculation Type" = "VAT Calculation Type"::"Full VAT" then begin
                                    VATAmount := CalcLineAmount();
                                    NewAmount := 0;
                                    NewVATBaseAmount := 0;
                                end else begin
                                    NewAmount := CalcLineAmount();
                                    NewVATBaseAmount :=
                                      Round(
                                        NewAmount * (1 - PurchHeader."VAT Base Discount %" / 100),
                                        Currency."Amount Rounding Precision");
                                    if VATAmountLine."VAT Base" = 0 then
                                        VATAmount := 0
                                    else
                                        VATAmount :=
                                          TempVATAmountLineRemainder."VAT Amount" +
                                          VATAmountLine."VAT Amount" * NewAmount / VATAmountLine."VAT Base";
                                end;
                                NewAmountIncludingVAT := NewAmount + Round(VATAmount, Currency."Amount Rounding Precision");
                            end;
                            OnUpdateVATOnLinesOnAfterCalculateNewAmount(
                                Rec, PurchHeader, VATAmountLine, TempVATAmountLineRemainder, NewAmountIncludingVAT, VATAmount,
                                NewAmount, NewVATBaseAmount);
                        end else begin
                            if VATAmountLine.CalcLineAmount = 0 then
                                VATDifference := 0
                            else
                                VATDifference :=
                                  TempVATAmountLineRemainder."VAT Difference" +
                                  VATAmountLine."VAT Difference" * (LineAmountToInvoice - InvDiscAmount) / VATAmountLine.CalcLineAmount;
                            if LineAmountToInvoice = 0 then
                                "VAT Difference" := 0
                            else
                                "VAT Difference" := Round(VATDifference, Currency."Amount Rounding Precision");
                            **//------------------------------------------------- 
                            OnUpdateVATOnLinesOnAfterCalculateDiference(
                               Rec, PurchHeader, VATAmountLine, TempVATAmountLineRemainder, QtyType);
                            //-------------------------------------------------** 
                        end;
                        OnUpdateVATOnLinesOnAfterCalculateAmounts(PurchLine, PurchHeader);

                        if QtyType = QtyType::General then begin
                            if not "Prepayment Line" then
                                UpdatePrepmtAmounts();
                            UpdateBaseAmounts(NewAmount, Round(NewAmountIncludingVAT, Currency."Amount Rounding Precision"), NewVATBaseAmount);
                        end;
                        InitOutstanding();
                        if not ((Type = Type::"Charge (Item)") and ("Quantity Invoiced" <> "Qty. Assigned")) then begin
                            SetUpdateFromVAT(true);
                            UpdateUnitCost();
                        end;
                        if Type = Type::"Charge (Item)" then
                            UpdateItemChargeAssgnt();
                        Modify;
                        LineWasModified := true;

                        if ("Deferral Code" <> '') and (DeferralAmount <> GetDeferralAmount()) then
                            UpdateDeferralAmounts();

                        TempVATAmountLineRemainder."Amount Including VAT" :=
                          NewAmountIncludingVAT - Round(NewAmountIncludingVAT, Currency."Amount Rounding Precision");
                        TempVATAmountLineRemainder."VAT Amount" := VATAmount - NewAmountIncludingVAT + NewAmount;
                        TempVATAmountLineRemainder."VAT Difference" := VATDifference - "VAT Difference";
                        OnUpdateVATOnLinesOnBeforeTempVATAmountLineRemainderModify(Rec, TempVATAmountLineRemainder, VATAmount, NewVATBaseAmount);
                        TempVATAmountLineRemainder.Modify();
                    end;
                end;
            until Next = 0;
    end;

    OnAfterUpdateVATOnLines(PurchHeader, PurchLine, VATAmountLine, QtyType);
end;

[IntegrationEvent(false, false)] local procedure OnUpdateVATOnLinesOnAfterCalculateNewAmount(var PurchaseLine: Record "Purchase Line"; PurchaseHeader: Record "Purchase Header"; VATAmountLine: Record "VAT Amount Line"; VATAmountLineReminder: Record "VAT Amount Line";QtyType: Option General,Invoicing,Shipping;) begin end;

Softstore avatar Jan 27 '21 22:01 Softstore

Thanks for reporting this. We agree, and we’ll publish a fix asap, either in an update for the current version or in the next major release. We will update this issue with information about availability. Please do not reply to this, as we do not monitor closed issues. If you have follow-up questions or requests, please create a new issue where you reference this one.

JesperSchulz avatar Feb 01 '21 15:02 JesperSchulz