SupportCategory: Simple Object Designerchange \”Total Excl. VAT\”\’s calculate formula
Alan Cheung asked 5 months ago
can I change \"Total Excl. VAT\"\'s calculate formula?i added few custom fields in \"Purchase Order Subform\".\"Item Discount Amount Excl. VAT.\" sum with item\'s \"Item Discount Amount\".the \"Inv. Discount Amount\" can sum with \"Custom Discount Amount\" and \"Item Discount Amount Excl. VAT.\"finally, i hope the \"Total Excl. VAT\" & \"Total Incl. VAT\" can minus \"Inv. Discount Amount\".tableextension 50158 AddItemDiscountField extends \"Purchase Line\"{ fields { field(147; \"Item Discount Amount\"; Decimal) { AutoFormatExpression = \"Currency Code\"; AutoFormatType = 1; CaptionClass = GetCaptionClass(FieldNo(\"Item Discount Amount\")); Caption = \'Item Discount Amount\'; Editable = true; trigger OnValidate() var ItemDiscount: Decimal; IsHandled: Boolean; Currency: Record Currency; PurchHeader: Record \"Purchase Header\"; begin IsHandled := false; OnBeforeValidateItemDiscountAmount(Rec, xRec, CurrFieldNo, IsHandled, Currency); if IsHandled then exit; TestField(\"Item Discount Amount\"); GetPurchHeader(); ItemDiscount := Round(\"Item Discount Amount\", Currency.\"Amount Rounding Precision\"); OnValidateLineAmountOnAfterCalcItemDiscountAmount(Rec, ItemDiscount); CheckItemDiscountAmount(ItemDiscount); end; } field(148; CustomDiscountAmount; Decimal) { AutoFormatExpression = \"Currency Code\"; AutoFormatType = 1; CaptionClass = GetCaptionClass(FieldNo(CustomDiscountAmount)); Caption = \'Custom Discount Amount\'; } } [IntegrationEvent(false, false)] local procedure OnBeforeValidateItemDiscountAmount(var PurchaseLine: Record \"Purchase Line\"; xPurchaseLine: Record \"Purchase Line\"; CurrentFieldNo: Integer; var IsHandled: Boolean; Currency: Record Currency) begin end; [IntegrationEvent(false, false)] local procedure OnValidateLineAmountOnAfterCalcItemDiscountAmount(var PurchaseLine: Record \"Purchase Line\"; var ItemDiscount: Decimal) begin end; local procedure CheckItemDiscountAmount(ItemDiscount: Decimal) var IsHandled: Boolean; PurchHeader: Record \"Purchase Header\"; PurchLine: Record \"Purchase Line\"; TotalDiscount: Decimal; testAmountInvalidErr: Label \'You have set the line amount to a value that results in a discount that is not valid. Consider increasing the unit cost instead.\'; begin IsHandled := false; OnBeforeItemDiscountAmount(Rec, ItemDiscount, IsHandled, CurrFieldNo); if IsHandled then exit; if \"Line Amount\" > 0 then if \"Line Amount\" < ItemDiscount then Error(testAmountInvalidErr); end; [IntegrationEvent(false, false)] local procedure OnBeforeItemDiscountAmount(var PurchaseLine: Record \"Purchase Line\"; ItemDiscount: Decimal; var IsHandled: Boolean; CalledByFieldNo: Integer) begin end;}tableextension 50162 DiscountAmount extends \"Purchase Header\"{ fields { field(180; ItemDiscountIncludingVAT; Decimal) { AutoFormatExpression = \"Currency Code\"; AutoFormatType = 1; CalcFormula = Sum(\"Purchase Line\".\"Item Discount Amount\" where(\"Document Type\" = field(\"Document Type\"), \"Document No.\" = field(\"No.\"))); Caption = \'Item Discount Including VAT\'; FieldClass = FlowField; Editable = false; } field(181; CustomDiscountAmount; Decimal) { AutoFormatExpression = \"Currency Code\"; AutoFormatType = 1; CalcFormula = Sum(\"Purchase Line\".CustomDiscountAmount where(\"Document Type\" = field(\"Document Type\"), \"Document No.\" = field(\"No.\"))); Caption = \'Custom Discount Amount\'; FieldClass = FlowField; Editable = false; } }}pageextension 50161 SubformAddField extends \"Purchase Order Subform\"{ layout { modify(\"Invoice Discount Amount\") { Editable = false; } addafter(\"Line Amount\") { field(\"Item Discount Amount Excl. VAT\"; Rec.\"Item Discount Amount\") { ApplicationArea = Suite; BlankZero = true; Editable = NOT IsBlankNumber; Enabled = NOT IsBlankNumber; ToolTip = \'Specifies the discount amount to use to calculate the discount for purchases.\'; } } addbefore(Control19) { field(ItemDiscountAmount; TotalPurchaseHeader.ItemDiscountIncludingVAT) { ApplicationArea = Suite; AutoFormatExpression = Currency.Code; AutoFormatType = 1; Caption = \'Item Discount Amount Excl. VAT\'; Editable = false; ToolTip = \'Specifies a discount amount that is deducted from the value of the Total Incl. VAT field, based on purchase lines where the Allow Invoice Disc. field is selected. You can enter or change the amount manually.\'; } field(CustomDiscountAmount; TotalPurchaseHeader.CustomDiscountAmount) { ApplicationArea = Suite; AutoFormatExpression = Currency.Code; AutoFormatType = 1; Caption = \'Custom Discount Amount\'; Editable = true; ToolTip = \'Specifies a discount amount that is deducted from the value of the Total Incl. VAT field, based on purchase lines where the Allow Invoice Disc. field is selected. You can enter or change the amount manually.\'; trigger OnValidate() begin if TotalPurchaseHeader.CustomDiscountAmount + TotalPurchaseHeader.ItemDiscountIncludingVAT > TotalPurchaseLine.Amount then Error( \'amount netgative amount\' ); TotalAmountLessItemDiscount(); end; } } } trigger OnAfterGetCurrRecord() begin TotalPurchaseHeader.FindSet(); if TotalPurchaseHeader.FindFirst() then begin TotalPurchaseHeader.CalcFields(CustomDiscountAmount); TotalPurchaseHeader.CalcFields(ItemDiscountIncludingVAT); end; GetTotalsPurchaseHeader(); CalculateTotals(); UpdateEditableOnRow(); UpdateTypeText(); TotalAmountLessItemDiscount(); end; procedure GetTotalsPurchaseHeader() var DocumentTotals: Codeunit \"Document Totals\"; Currency: Record Currency; begin DocumentTotals.GetTotalPurchaseHeaderAndCurrency(Rec, TotalPurchaseHeader, Currency); end; procedure TotalAmountLessItemDiscount() var SumDiscountAmount: Decimal; begin SumDiscountAmount := TotalPurchaseHeader.CustomDiscountAmount + TotalPurchaseHeader.ItemDiscountIncludingVAT; InvoiceDiscountAmount := SumDiscountAmount; end; var Currency: Record Currency; TotalsUpToDate: Boolean; CustomDiscountAmount: Decimal;}