ALAppExtensions icon indicating copy to clipboard operation
ALAppExtensions copied to clipboard

[Event Request] Table 37 "Sales Line", Functions CalcVATAmountLines, UpdateVATOnLines, ShowItemChargeAssgnt, SetSalesHeader; OnAfterInitializeCurreny

Open nd2403 opened this issue 5 years ago • 1 comments

Hello, we need to influence the price rounding precision for Swiss customers. Thank you in advance!

    procedure CalcVATAmountLines(QtyType: Option General,Invoicing,Shipping; var SalesHeader: Record "Sales Header"; var SalesLine: Record "Sales Line"; var VATAmountLine: Record "VAT Amount Line"; IncludePrepayments: Boolean)
    var
        TotalVATAmount: Decimal;
        QtyToHandle: Decimal;
        AmtToHandle: Decimal;
        RoundingLineInserted: Boolean;
    begin
        if IsCalcVATAmountLinesHandled(SalesHeader, SalesLine, VATAmountLine, QtyType) then
            exit;

        Currency.Initialize(SalesHeader."Currency Code");
// - new
        OnAfterInitializeCurreny(Rec, SalesHeader, Currency);
// + new

        VATAmountLine.DeleteAll();

        with SalesLine do begin
            SetRange("Document Type", SalesHeader."Document Type");
            SetRange("Document No.", SalesHeader."No.");
            OnCalcVATAmountLinesOnAfterSetFilters(SalesLine, SalesHeader);
            if FindSet then
                repeat
                    if not ZeroAmountLine(QtyType) then begin
                        if (Type = Type::"G/L Account") and not "Prepayment Line" then
                            RoundingLineInserted := (("No." = GetCPGInvRoundAcc(SalesHeader)) and "System-Created Entry") or RoundingLineInserted;
                        if "VAT Calculation Type" in
                           ["VAT Calculation Type"::"Reverse Charge VAT", "VAT Calculation Type"::"Sales Tax"]
                        then
                            "VAT %" := 0;
                        if not VATAmountLine.Get(
                             "VAT Identifier", "VAT Calculation Type", "Tax Group Code", false, "Line Amount" >= 0)
                        then
                            VATAmountLine.InsertNewLine(
                              "VAT Identifier", "VAT Calculation Type", "Tax Group Code", false, "VAT %", "Line Amount" >= 0, false);

                        // - 01.02.16 IS ND#0207209
                        if IsBoqPrintCall and ("Document Type" in ["Document Type"::Invoice, "Document Type"::"Credit Memo"]) then begin
                            VATAmountLine."VAT Clause Code" := "VAT Clause Code";
                            MODIFY;
                        end;
                        // + 01.02.16 IS ND#0207209

                        case QtyType of
                            QtyType::General:
                                begin
                                    VATAmountLine.Quantity += "Quantity (Base)";
                                    VATAmountLine.SumLine(
                                      "Line Amount", "Inv. Discount Amount", "VAT Difference", "Allow Invoice Disc.", "Prepayment Line");
                                end;
                            QtyType::Invoicing:
                                begin
                                    case true of
                                        ("Document Type" in ["Document Type"::Order, "Document Type"::Invoice]) and
                                      (not SalesHeader.Ship) and SalesHeader.Invoice and (not "Prepayment Line"):
                                            if "Shipment No." = '' then begin
                                                QtyToHandle := GetAbsMin("Qty. to Invoice", "Qty. Shipped Not Invoiced");
                                                VATAmountLine.Quantity += GetAbsMin("Qty. to Invoice (Base)", "Qty. Shipped Not Invd. (Base)");
                                            end else begin
                                                QtyToHandle := "Qty. to Invoice";
                                                VATAmountLine.Quantity += "Qty. to Invoice (Base)";
                                            end;
                                        IsCreditDocType() and (not SalesHeader.Receive) and SalesHeader.Invoice:
                                            if "Return Receipt No." = '' then begin
                                                QtyToHandle := GetAbsMin("Qty. to Invoice", "Return Qty. Rcd. Not Invd.");
                                                VATAmountLine.Quantity += GetAbsMin("Qty. to Invoice (Base)", "Ret. Qty. Rcd. Not Invd.(Base)");
                                            end else begin
                                                QtyToHandle := "Qty. to Invoice";
                                                VATAmountLine.Quantity += "Qty. to Invoice (Base)";
                                            end;
                                        else begin
                                                QtyToHandle := "Qty. to Invoice";
                                                VATAmountLine.Quantity += "Qty. to Invoice (Base)";
                                            end;
                                    end;

                                    if IncludePrepayments then
                                        AmtToHandle := GetLineAmountToHandleInclPrepmt(QtyToHandle)
                                    else
                                        AmtToHandle := GetLineAmountToHandle(QtyToHandle);
                                    if SalesHeader."Invoice Discount Calculation" <> SalesHeader."Invoice Discount Calculation"::Amount then
                                        VATAmountLine.SumLine(
                                          AmtToHandle, Round("Inv. Discount Amount" * QtyToHandle / Quantity, Currency."Amount Rounding Precision"),
                                          "VAT Difference", "Allow Invoice Disc.", "Prepayment Line")
                                    else
                                        VATAmountLine.SumLine(
                                          AmtToHandle, "Inv. Disc. Amount to Invoice", "VAT Difference", "Allow Invoice Disc.", "Prepayment Line");
                                end;
                            QtyType::Shipping:
                                begin
                                    if "Document Type" in
                                       ["Document Type"::"Return Order", "Document Type"::"Credit Memo"]
                                    then begin
                                        QtyToHandle := "Return Qty. to Receive";
                                        VATAmountLine.Quantity += "Return Qty. to Receive (Base)";
                                    end else begin
                                        QtyToHandle := "Qty. to Ship";
                                        VATAmountLine.Quantity += "Qty. to Ship (Base)";
                                    end;
                                    if IncludePrepayments then
                                        AmtToHandle := GetLineAmountToHandleInclPrepmt(QtyToHandle)
                                    else
                                        AmtToHandle := GetLineAmountToHandle(QtyToHandle);
                                    VATAmountLine.SumLine(
                                      AmtToHandle, Round("Inv. Discount Amount" * QtyToHandle / Quantity, Currency."Amount Rounding Precision"),
                                      "VAT Difference", "Allow Invoice Disc.", "Prepayment Line");
                                end;
                        end;
                        TotalVATAmount += "Amount Including VAT" - Amount;
                        OnCalcVATAmountLinesOnAfterCalcLineTotals(VATAmountLine, SalesHeader, SalesLine, Currency, QtyType, TotalVATAmount);
                    end;
                until Next() = 0;
        end;

        VATAmountLine.UpdateLines(
          TotalVATAmount, Currency, SalesHeader."Currency Factor", SalesHeader."Prices Including VAT",
          SalesHeader."VAT Base Discount %", SalesHeader."Tax Area Code", SalesHeader."Tax Liable", SalesHeader."Posting Date");

        if RoundingLineInserted and (TotalVATAmount <> 0) then
            if GetVATAmountLineOfMaxAmt(VATAmountLine, SalesLine) then begin
                VATAmountLine."VAT Amount" += TotalVATAmount;
                VATAmountLine."Amount Including VAT" += TotalVATAmount;
                VATAmountLine."Calculated VAT Amount" += TotalVATAmount;
                VATAmountLine.Modify();
            end;

        OnAfterCalcVATAmountLines(SalesHeader, SalesLine, VATAmountLine, QtyType);
    end;
    procedure UpdateVATOnLines(QtyType: Option General,Invoicing,Shipping; var SalesHeader: Record "Sales Header"; var SalesLine: Record "Sales 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
        if IsUpdateVATOnLinesHandled(SalesHeader, SalesLine, VATAmountLine, QtyType) then
            exit;

        LineWasModified := false;
        if QtyType = QtyType::Shipping then
            exit;

        Currency.Initialize(SalesHeader."Currency Code");
// - new
        OnAfterInitializeCurreny(Rec, SalesHeader, Currency);
// + new

        TempVATAmountLineRemainder.DeleteAll();

        with SalesLine do begin
            SetRange("Document Type", SalesHeader."Document Type");
            SetRange("Document No.", SalesHeader."No.");
            LockTable();
            if FindSet then
                repeat
                    if not ZeroAmountLine(QtyType) and
                       ((SalesHeader."Document Type" <> SalesHeader."Document Type"::Invoice) or ("Prepmt. Amt. Inv." = 0))
                    then begin
                        DeferralAmount := GetDeferralAmount();
                        VATAmountLine.Get("VAT Identifier", "VAT Calculation Type", "Tax Group Code", false, "Line Amount" >= 0);
                        if VATAmountLine.Modified then begin
                            if not TempVATAmountLineRemainder.Get(
                                 "VAT Identifier", "VAT Calculation Type", "Tax Group Code", false, "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
                                    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(SalesLine, SalesHeader);
                            if QtyType = QtyType::General then begin
                                if SalesHeader."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;
                                    OnUpdateVATOnLinesOnBeforeCalculateNewAmount(
                                      Rec, SalesHeader, VATAmountLine, TempVATAmountLineRemainder, NewAmountIncludingVAT, VATAmount);
                                    NewAmount :=
                                      Round(NewAmountIncludingVAT, Currency."Amount Rounding Precision") -
                                      Round(VATAmount, Currency."Amount Rounding Precision");
                                    NewVATBaseAmount :=
                                      Round(
                                        NewAmount * (1 - SalesHeader."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 - SalesHeader."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;
                                    OnUpdateVATOnLinesOnBeforeCalculateNewAmount(
                                      Rec, SalesHeader, VATAmountLine, TempVATAmountLineRemainder, NewAmount, VATAmount);
                                    NewAmountIncludingVAT := NewAmount + Round(VATAmount, Currency."Amount Rounding Precision");
                                end;
                                OnUpdateVATOnLinesOnAfterCalculateNewAmount(
                                  Rec, SalesHeader, 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");
                            end;
                            OnUpdateVATOnLinesOnAfterCalculateAmounts(SalesLine, SalesHeader);

                            if QtyType = QtyType::General then begin
                                if not "Prepayment Line" then
                                    UpdatePrepmtAmounts();
                                UpdateBaseAmounts(NewAmount, Round(NewAmountIncludingVAT, Currency."Amount Rounding Precision"), NewVATBaseAmount);
                            end;
                            InitOutstanding();
                            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(SalesHeader, SalesLine, VATAmountLine, QtyType);
    end;
    procedure ShowItemChargeAssgnt()
    var
        ItemChargeAssgntSales: Record "Item Charge Assignment (Sales)";
        AssignItemChargeSales: Codeunit "Item Charge Assgnt. (Sales)";
        ItemChargeAssgnts: Page "Item Charge Assignment (Sales)";
        ItemChargeAssgntLineAmt: Decimal;
        IsHandled: Boolean;
    begin
        Get("Document Type", "Document No.", "Line No.");
        TestField("No.");
        TestField(Quantity);

        if Type <> Type::"Charge (Item)" then begin
            Message(ItemChargeAssignmentErr);
            exit;
        end;

        GetSalesHeader();
        Currency.Initialize(SalesHeader."Currency Code");
// - new
        OnAfterInitializeCurreny(Rec, SalesHeader, Currency);
// + new
        if ("Inv. Discount Amount" = 0) and ("Line Discount Amount" = 0) and
           (not SalesHeader."Prices Including VAT")
        then
            ItemChargeAssgntLineAmt := "Line Amount"
        else
            if SalesHeader."Prices Including VAT" then
                ItemChargeAssgntLineAmt :=
                  Round(CalcLineAmount / (1 + "VAT %" / 100), Currency."Amount Rounding Precision")
            else
                ItemChargeAssgntLineAmt := CalcLineAmount;

        ItemChargeAssgntSales.Reset();
        ItemChargeAssgntSales.SetRange("Document Type", "Document Type");
        ItemChargeAssgntSales.SetRange("Document No.", "Document No.");
        ItemChargeAssgntSales.SetRange("Document Line No.", "Line No.");
        ItemChargeAssgntSales.SetRange("Item Charge No.", "No.");
        if not ItemChargeAssgntSales.FindLast then begin
            ItemChargeAssgntSales."Document Type" := "Document Type";
            ItemChargeAssgntSales."Document No." := "Document No.";
            ItemChargeAssgntSales."Document Line No." := "Line No.";
            ItemChargeAssgntSales."Item Charge No." := "No.";
            ItemChargeAssgntSales."Unit Cost" :=
              Round(ItemChargeAssgntLineAmt / Quantity, Currency."Unit-Amount Rounding Precision");
        end;

        IsHandled := false;
        OnShowItemChargeAssgntOnBeforeCalcItemCharge(Rec, ItemChargeAssgntLineAmt, Currency, IsHandled, ItemChargeAssgntSales);
        if not IsHandled then
            ItemChargeAssgntLineAmt :=
              Round(ItemChargeAssgntLineAmt * ("Qty. to Invoice" / Quantity), Currency."Amount Rounding Precision");

        if IsCreditDocType() then
            AssignItemChargeSales.CreateDocChargeAssgn(ItemChargeAssgntSales, "Return Receipt No.")
        else
            AssignItemChargeSales.CreateDocChargeAssgn(ItemChargeAssgntSales, "Shipment No.");
        Clear(AssignItemChargeSales);
        Commit();

        ItemChargeAssgnts.Initialize(Rec, ItemChargeAssgntLineAmt);
        ItemChargeAssgnts.RunModal;
        CalcFields("Qty. to Assign");
    end;
procedure SetSalesHeader(NewSalesHeader: Record "Sales Header")
    begin
        SalesHeader := NewSalesHeader;
        OnBeforeSetSalesHeader(SalesHeader);

        if SalesHeader."Currency Code" = '' then
            Currency.InitRoundingPrecision
        else begin
            SalesHeader.TestField("Currency Factor");
            Currency.Get(SalesHeader."Currency Code");
            Currency.TestField("Amount Rounding Precision");
        end;
// - new
        OnAfterInitializeCurreny(Rec, SalesHeader, Currency);
// + new
    end;
    [IntegrationEvent(false, false)]
    local procedure OnAfterInitializeCurreny(var SalesLine: Record "Sales Line"; var SalesHeader: Record "Sales Header"; var Currency: Record Currency)
    begin
    end;

nd2403 avatar Nov 23 '21 14:11 nd2403

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. 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 Nov 29 '21 06:11 JesperSchulz