Table 6101 E-Document Purchase Line

E-Document Core, Microsoft.eServices.EDocument.Processing.Import.Purchase

src/Apps/W1/EDocument/App/src/Processing/Import/Purchase/EDocumentPurchaseLine.Table.al521 lines, Copyright (c) Microsoft Corporation. MIT

// ------------------------------------------------------------------------------------------------// Copyright (c) Microsoft Corporation. All rights reserved.// Licensed under the MIT License. See License.txt in the project root for license information.// ------------------------------------------------------------------------------------------------namespace Microsoft.eServices.EDocument.Processing.Import.Purchase;using Microsoft.eServices.EDocument;using Microsoft.eServices.EDocument.Processing;using Microsoft.eServices.EDocument.Processing.Import;using Microsoft.Finance.AllocationAccount;using Microsoft.Finance.Deferral;using Microsoft.Finance.Dimension;using Microsoft.Finance.GeneralLedger.Account;using Microsoft.Finance.VAT.Setup;using Microsoft.FixedAssets.FixedAsset;using Microsoft.Foundation.UOM;using Microsoft.Inventory.Item;using Microsoft.Inventory.Item.Catalog;using Microsoft.Projects.Resources.Resource;using Microsoft.Purchases.Document;using Microsoft.Purchases.History;using Microsoft.Purchases.Vendor;using Microsoft.Utilities;using System.Log;using System.Reflection;using System.Utilities;table 6101 "E-Document Purchase Line"{    DataClassification = CustomerContent;    ReplicateData = false;    InherentEntitlements = RIMDX;    InherentPermissions = RIMDX;    fields    {        field(1; "E-Document Entry No."; Integer)        {            Caption = 'E-Document Entry No.';            TableRelation = "E-Document"."Entry No";            DataClassification = SystemMetadata;            Editable = false;        }        field(2; "Line No."; Integer)        {            Caption = 'Line No.';            DataClassification = SystemMetadata;            Editable = false;        }        #region External data - Purchase fields [3-100]        field(3; "Date"; Date)        {            Caption = 'Date';            Editable = false;        }        field(4; "Product Code"; Text[100])        {            Caption = 'Product Code';            Editable = false;        }        field(5; "Description"; Text[100])        {            Caption = 'Description';            ToolTip = 'Specifies the description.';            Editable = false;        }        field(6; "Quantity"; Decimal)        {            Caption = 'Quantity';            ToolTip = 'Specifies the quantity.';            Editable = false;            DecimalPlaces = 0 : 5;            AutoFormatType = 0;        }        field(7; "Unit of Measure"; Text[50])        {            Caption = 'Unit of Measure';            Editable = false;        }        field(8; "Unit Price"; Decimal)        {            AutoFormatExpression = Rec."Currency Code";            AutoFormatType = 2;            Caption = 'Unit Price';            ToolTip = 'Specifies the direct unit cost.';            Editable = false;        }        field(9; "Sub Total"; Decimal)        {            AutoFormatExpression = Rec."Currency Code";            AutoFormatType = 2;            Caption = 'Sub Total';            ToolTip = 'Specifies the line subtotal.';        }        field(10; "Total Discount"; Decimal)        {            Caption = 'Total Discount';            ToolTip = 'Specifies the line discount.';            AutoFormatExpression = Rec."Currency Code";            AutoFormatType = 2;        }        field(11; "VAT Rate"; Decimal)        {            Caption = 'VAT Rate';            Editable = false;            AutoFormatType = 0;        }        field(12; "Currency Code"; Code[10])        {            Caption = 'Currency Code';            Editable = false;        }        #endregion Purchase fields        #region Business Central Data - Validated fields [101-200]        field(101; "[BC] Purchase Line Type"; Enum "Purchase Line Type")        {            Caption = 'Type';            ToolTip = 'Specifies the type of entity that will be posted for this purchase line, such as Item, Resource, or G/L Account.';            trigger OnValidate()            begin                POMatchingValidation();                Rec."[BC] Purchase Type No." := '';            end;        }        field(102; "[BC] Purchase Type No."; Code[20])        {            Caption = 'No.';            ToolTip = 'Specifies what you''re selling. The options vary, depending on what you choose in the Type field.';            TableRelation = if ("[BC] Purchase Line Type" = const(" ")) "Standard Text"            else            if ("[BC] Purchase Line Type" = const("G/L Account")) "G/L Account" where("Direct Posting" = const(true))            else            if ("[BC] Purchase Line Type" = const("Fixed Asset")) "Fixed Asset"            else            if ("[BC] Purchase Line Type" = const("Charge (Item)")) "Item Charge"            else            if ("[BC] Purchase Line Type" = const(Item)) Item            else            if ("[BC] Purchase Line Type" = const("Allocation Account")) "Allocation Account"            else            if ("[BC] Purchase Line Type" = const(Resource)) Resource;            trigger OnValidate()            begin                SetDescriptionFromLineTypeNo();                POMatchingValidation();                ClearUnitOfMeasureAndVariantNotValidForNo();            end;        }        field(103; "[BC] Unit of Measure"; Code[20])        {            Caption = 'Unit of Measure';            ToolTip = 'Specifies the unit of measure code.';            TableRelation = "Unit of Measure";        }        field(104; "[BC] Deferral Code"; Code[10])        {            Caption = 'Deferral Code';            ToolTip = 'Specifies the deferral code.';            TableRelation = "Deferral Template";        }        field(105; "[BC] Shortcut Dimension 1 Code"; Code[20])        {            CaptionClass = '1,2,1';            Caption = 'Shortcut Dimension 1 Code';            ToolTip = 'Specifies the code for Shortcut Dimension 1, which is one of two global dimension codes that you set up in the General Ledger Setup window.';            TableRelation = "Dimension Value".Code where("Global Dimension No." = const(1),                                                          Blocked = const(false));        }        field(106; "[BC] Shortcut Dimension 2 Code"; Code[20])        {            CaptionClass = '1,2,2';            Caption = 'Shortcut Dimension 2 Code';            ToolTip = 'Specifies the code for Shortcut Dimension 2, which is one of two global dimension codes that you set up in the General Ledger Setup window.';            TableRelation = "Dimension Value".Code where("Global Dimension No." = const(2),                                                          Blocked = const(false));        }#pragma warning disable AS0086        field(107; "[BC] Item Reference No."; Code[50])#pragma warning restore AS0086        {            Caption = 'Item Reference No.';            ToolTip = 'Specifies the item reference number.';            TableRelation = "Item Reference"."Reference No." where("Unit of Measure" = field("[BC] Unit of Measure"), "Variant Code" = field("[BC] Variant Code"));        }        field(108; "[BC] Variant Code"; Code[10])        {            Caption = 'Variant Code';            ToolTip = 'Specifies the variant code.';            TableRelation = "Item Variant".Code where("Item No." = field("[BC] Purchase Type No."));        }        field(109; "[BC] Dimension Set ID"; Integer)        {            Caption = 'Dimension Set ID';            Editable = false;            TableRelation = "Dimension Set Entry";            trigger OnLookup()            begin                Rec.LookupDimensions();            end;            trigger OnValidate()            begin                DimMgt.UpdateGlobalDimFromDimSetID("[BC] Dimension Set ID", "[BC] Shortcut Dimension 1 Code", "[BC] Shortcut Dimension 2 Code");            end;        }        field(110; "[BC] VAT Prod. Posting Group"; Code[20])        {            Caption = 'VAT Prod. Posting Group';            ToolTip = 'Specifies the VAT product posting group resolved from the extracted VAT rate.';            TableRelation = "VAT Product Posting Group";            trigger OnLookup()            var                Vendor: Record Vendor;                VATPostingSetup: Record "VAT Posting Setup";                EDocPurchDocHelper: Codeunit "E-Doc. Purch. Doc. Helper";            begin                Vendor := Rec.GetBCVendor();                if Vendor."No." = '' then                    exit;                EDocPurchDocHelper.SetNormalReverseChargeFilter(VATPostingSetup, Vendor."VAT Bus. Posting Group");                if Page.RunModal(Page::"VAT Posting Setup", VATPostingSetup) = Action::LookupOK then                    Validate("[BC] VAT Prod. Posting Group", VATPostingSetup."VAT Prod. Posting Group");            end;        }        #endregion Validated fields        #region Metadata fields [201-300]        field(201; "E-Doc. Purch. Line History Id"; Integer)        {            Caption = 'E-Doc. Purch. Line History Id';            ToolTip = 'Specifies the ID of the e-document purchase line history.';            TableRelation = "E-Doc. Purchase Line History"."Entry No.";            DataClassification = SystemMetadata;        }        #endregion Metadata fields    }    keys    {        key(PK; "E-Document Entry No.", "Line No.")        {            Clustered = true;        }        key(Key1; "[BC] Purchase Type No.")        {        }    }    trigger OnDelete()    var        EDocPOMatching: Codeunit "E-Doc. PO Matching";    begin        EDocPOMatching.RemoveAllMatchesForEDocumentLine(Rec);    end;    var        DimMgt: Codeunit DimensionManagement;    local procedure SetDescriptionFromLineTypeNo()    begin        if Rec.Description <> '' then            exit;        Rec.Description := GetMatchedEntityName();    end;    /// <summary>    /// Clears the unit of measure and variant code when they are not valid for the entity in "[BC] Purchase Type No.",    /// so that a reassigned line does not keep values from the previous entity that would fail when the purchase line is created.    /// </summary>    local procedure ClearUnitOfMeasureAndVariantNotValidForNo()    var        ItemUnitOfMeasure: Record "Item Unit of Measure";        ItemVariant: Record "Item Variant";        ResourceUnitOfMeasure: Record "Resource Unit of Measure";    begin        if Rec."[BC] Purchase Type No." = '' then            exit;        if Rec."[BC] Variant Code" <> '' then begin            ItemVariant.SetLoadFields(Code);            if (Rec."[BC] Purchase Line Type" <> "Purchase Line Type"::Item) or not ItemVariant.Get(Rec."[BC] Purchase Type No.", Rec."[BC] Variant Code") then                Rec."[BC] Variant Code" := '';        end;        if Rec."[BC] Unit of Measure" = '' then            exit;        case Rec."[BC] Purchase Line Type" of            "Purchase Line Type"::Item:                begin                    ItemUnitOfMeasure.SetLoadFields(Code);                    if not ItemUnitOfMeasure.Get(Rec."[BC] Purchase Type No.", Rec."[BC] Unit of Measure") then                        Rec."[BC] Unit of Measure" := '';                end;            "Purchase Line Type"::Resource:                begin                    ResourceUnitOfMeasure.SetLoadFields(Code);                    if not ResourceUnitOfMeasure.Get(Rec."[BC] Purchase Type No.", Rec."[BC] Unit of Measure") then                        Rec."[BC] Unit of Measure" := '';                end;        end;    end;    /// <summary>    /// Returns the name of the entity (Item, G/L Account, Resource, etc.) that this line is matched to,    /// resolved from the "[BC] Purchase Line Type" and "[BC] Purchase Type No." fields    /// </summary>    internal procedure GetMatchedEntityName() EntityName: Text[100]    var        Item: Record Item;        GLAccount: Record "G/L Account";        AllocationAccount: Record "Allocation Account";        FixedAsset: Record "Fixed Asset";        Resource: Record Resource;        ItemCharge: Record "Item Charge";    begin        if Rec."[BC] Purchase Type No." = '' then            exit('');        case Rec."[BC] Purchase Line Type" of            "Purchase Line Type"::Item:                begin                    Item.SetLoadFields(Description);                    if Item.Get(Rec."[BC] Purchase Type No.") then                        EntityName := Item.Description;                end;            "Purchase Line Type"::"G/L Account":                begin                    GLAccount.SetLoadFields(Name);                    if GLAccount.Get(Rec."[BC] Purchase Type No.") then                        EntityName := GLAccount.Name;                end;            "Purchase Line Type"::"Allocation Account":                begin                    AllocationAccount.SetLoadFields(Name);                    if AllocationAccount.Get(Rec."[BC] Purchase Type No.") then                        EntityName := AllocationAccount.Name;                end;            "Purchase Line Type"::"Fixed Asset":                begin                    FixedAsset.SetLoadFields(Description);                    if FixedAsset.Get(Rec."[BC] Purchase Type No.") then                        EntityName := FixedAsset.Description;                end;            "Purchase Line Type"::Resource:                begin                    Resource.SetLoadFields(Name);                    if Resource.Get(Rec."[BC] Purchase Type No.") then                        EntityName := Resource.Name;                end;            "Purchase Line Type"::"Charge (Item)":                begin                    ItemCharge.SetLoadFields(Description);                    if ItemCharge.Get(Rec."[BC] Purchase Type No.") then                        EntityName := ItemCharge.Description;                end;        end;        OnAfterGetMatchedEntityName(Rec, EntityName);    end;    [IntegrationEvent(false, false)]    local procedure OnAfterGetMatchedEntityName(EDocumentPurchaseLine: Record "E-Document Purchase Line"; var EntityName: Text[100])    begin    end;    local procedure POMatchingValidation()    var        EDocPOMatching: Codeunit "E-Doc. PO Matching";        ConfirmMgt: Codeunit "Confirm Management";        LineMatchedMsg: Label 'This e-document line is already matched to a purchase order line. Do you want to continue? This will remove the match(es).';    begin        if not EDocPOMatching.IsEDocumentLineMatchedToAnyPOLine(Rec) then            exit;        if not ConfirmMgt.GetResponse(LineMatchedMsg) then            Error('');        EDocPOMatching.RemoveAllMatchesForEDocumentLine(Rec);    end;    procedure GetNextLineNo(EDocumentEntryNo: Integer): Integer    var        EDocumentPurchaseLine: Record "E-Document Purchase Line";    begin        EDocumentPurchaseLine.SetRange("E-Document Entry No.", EDocumentEntryNo);        if EDocumentPurchaseLine.FindLast() then            exit(EDocumentPurchaseLine."Line No." + 10000);        exit(10000);    end;    /// <summary>    /// Returns any additional columns defined for this line in a human-readable format.    /// </summary>    /// <returns></returns>    internal procedure AdditionalColumnsDisplayText() AdditionalColumns: Text    var        EDocPurchLineFieldSetup: Record "ED Purchase Line Field Setup";        EDocPurchLineField: Record "E-Document Line - Field";        Field: Record Field;        AdditionalColumnValue: Text;    begin        if not EDocPurchLineFieldSetup.FindSet() then            exit;        repeat            if Field.Get(Database::"Purch. Inv. Line", EDocPurchLineFieldSetup."Field No.") then;            if AdditionalColumns <> '' then                AdditionalColumns += ', ';            AdditionalColumns += Field.FieldName;            AdditionalColumns += ': ';            EDocPurchLineField.Get(Rec, EDocPurchLineFieldSetup);            AdditionalColumnValue := EDocPurchLineField.GetValueAsText();            if AdditionalColumnValue = '' then                AdditionalColumnValue := '-';            AdditionalColumns += AdditionalColumnValue;        until EDocPurchLineFieldSetup.Next() = 0;    end;    internal procedure LookupDimensions(): Boolean    var        OldDimSetID: Integer;    begin        OldDimSetID := Rec."[BC] Dimension Set ID";        Rec."[BC] Dimension Set ID" := DimMgt.EditDimensionSet(            Rec, Rec."[BC] Dimension Set ID", StrSubstNo('%1 %2', Rec."E-Document Entry No.", Rec."Line No."),            Rec."[BC] Shortcut Dimension 1 Code", Rec."[BC] Shortcut Dimension 2 Code");        DimMgt.UpdateGlobalDimFromDimSetID(Rec."[BC] Dimension Set ID", Rec."[BC] Shortcut Dimension 1 Code", Rec."[BC] Shortcut Dimension 2 Code");        if OldDimSetID <> Rec."[BC] Dimension Set ID" then            Rec.Modify();        exit(OldDimSetID <> Rec."[BC] Dimension Set ID");    end;    internal procedure GetFromLinkedPurchaseLine(PurchaseLine: Record "Purchase Line"): Boolean    var        EDocumentRecordLink: Record "E-Doc. Record Link";    begin        Clear(Rec);        EDocumentRecordLink.SetRange("Source Table No.", Database::"E-Document Purchase Line");        EDocumentRecordLink.SetRange("Target Table No.", Database::"Purchase Line");        EDocumentRecordLink.SetRange("Target SystemId", PurchaseLine.SystemId);        if EDocumentRecordLink.FindFirst() then            exit(Rec.GetBySystemId(EDocumentRecordLink."Source SystemId"));    end;    internal procedure GetLinkedPurchaseLine(): Record "Purchase Line"    var        EDocumentRecordLink: Record "E-Doc. Record Link";        PurchaseLine: Record "Purchase Line";    begin        EDocumentRecordLink.SetRange("Source Table No.", Database::"E-Document Purchase Line");        EDocumentRecordLink.SetRange("Target Table No.", Database::"Purchase Line");        EDocumentRecordLink.SetRange("Source SystemId", Rec.SystemId);        if EDocumentRecordLink.FindFirst() then            if PurchaseLine.GetBySystemId(EDocumentRecordLink."Target SystemId") then                exit(PurchaseLine);    end;    procedure GetEDocumentPurchaseHeader() EDocumentPurchaseHeader: Record "E-Document Purchase Header"    begin        if EDocumentPurchaseHeader.Get(Rec."E-Document Entry No.") then;    end;    procedure GetBCVendor(): Record Vendor    begin        exit(GetEDocumentPurchaseHeader().GetBCVendor());    end;    internal procedure LogVATRateMismatch(VendVATBusPostingGroupCode: Code[20]; VATRate: Decimal)    var        VATPostingSetup: Record "VAT Posting Setup";        EDocPurchDocHelper: Codeunit "E-Doc. Purch. Doc. Helper";        ActivityLog: Codeunit "Activity Log Builder";        VATPostingSetupRef: RecordRef;        Reasoning: Text[250];        VATRateMismatchReasonLbl: Label 'VAT rate %1% extracted from the document could not be matched to a VAT Posting Setup for vendor''s VAT Business Posting Group %2.', Comment = '%1 = extracted VAT rate %, %2 = VAT Bus. Posting Group code';        VATRateMismatchTitleLbl: Label 'VAT Posting Setup for %1', Comment = '%1 = VAT Bus. Posting Group code';    begin        EDocPurchDocHelper.SetNormalReverseChargeFilter(VATPostingSetup, VendVATBusPostingGroupCode);        VATPostingSetupRef.GetTable(VATPostingSetup);        Reasoning := CopyStr(StrSubstNo(VATRateMismatchReasonLbl, Rec."VAT Rate", VendVATBusPostingGroupCode), 1, MaxStrLen(Reasoning));        ActivityLog            .Init(Database::"E-Document Purchase Line", Rec.FieldNo("[BC] VAT Prod. Posting Group"), Rec.SystemId)            .SetExplanation(Reasoning)            .SetType(Enum::"Activity Log Type"::AL)            .SetReferenceSource(Page::"VAT Posting Setup", VATPostingSetupRef)            .SetReferenceTitle(StrSubstNo(VATRateMismatchTitleLbl, VendVATBusPostingGroupCode))            .Log();    end;    internal procedure LogVATRateResolved(VendVATBusPostingGroupCode: Code[20]; VATRate: Decimal)    var        VATPostingSetup: Record "VAT Posting Setup";        EDocPurchDocHelper: Codeunit "E-Doc. Purch. Doc. Helper";        ActivityLog: Codeunit "Activity Log Builder";        VATPostingSetupRef: RecordRef;        Reasoning: Text[250];        VATRateResolvedReasonLbl: Label 'VAT rate %1% extracted from the document was matched to VAT Product Posting Group %2 for vendor''s VAT Business Posting Group %3.', Comment = '%1 = extracted VAT rate %, %2 = resolved VAT Prod. Posting Group code, %3 = VAT Bus. Posting Group code';        VATRateResolvedTitleLbl: Label 'VAT Posting Setup for %1', Comment = '%1 = VAT Bus. Posting Group code';    begin        EDocPurchDocHelper.SetNormalReverseChargeFilter(VATPostingSetup, VendVATBusPostingGroupCode);        VATPostingSetupRef.GetTable(VATPostingSetup);        Reasoning := CopyStr(StrSubstNo(VATRateResolvedReasonLbl, VATRate, Rec."[BC] VAT Prod. Posting Group", VendVATBusPostingGroupCode), 1, MaxStrLen(Reasoning));        ActivityLog            .Init(Database::"E-Document Purchase Line", Rec.FieldNo("[BC] VAT Prod. Posting Group"), Rec.SystemId)            .SetExplanation(Reasoning)            .SetType(Enum::"Activity Log Type"::AL)            .SetReferenceSource(Page::"VAT Posting Setup", VATPostingSetupRef)            .SetReferenceTitle(StrSubstNo(VATRateResolvedTitleLbl, VendVATBusPostingGroupCode))            .Log();    end;}