Table extension 20512 Subc. Purchase Line

Subcontracting, Microsoft.Manufacturing.Subcontracting

src/Apps/W1/Subcontracting/App/src/Purchase/SubcPurchaseLine.TableExt.al253 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.Manufacturing.Subcontracting;using Microsoft.Foundation.UOM;using Microsoft.Inventory.Item;using Microsoft.Inventory.Location;using Microsoft.Manufacturing.Document;using Microsoft.Manufacturing.Routing;using Microsoft.Manufacturing.WorkCenter;using Microsoft.Purchases.Document;using Microsoft.Warehouse.Document;tableextension 20512 "Subc. Purchase Line" extends "Purchase Line"{    fields    {        modify("Operation No.")        {            trigger OnAfterValidate()            begin#if not CLEAN29#pragma warning disable AL0432                if not SubcFeatureFlagHandler.IsSubcontractingEnabled() then#pragma warning restore AL0432                    exit;#endif                SetSubcontractingLineType();            end;        }        modify("Work Center No.")        {            trigger OnAfterValidate()            begin#if not CLEAN29#pragma warning disable AL0432                if not SubcFeatureFlagHandler.IsSubcontractingEnabled() then#pragma warning restore AL0432                    exit;#endif                SetSubcontractingLineType();            end;        }        field(20543; "Subc. Prod. Order No."; Code[20])        {            Caption = 'Prod. Order No. (Sub)';            DataClassification = CustomerContent;            Editable = false;            TableRelation = "Production Order"."No." where(Status = const(Released));        }        field(20544; "Subc. Prod. Order Line No."; Integer)        {            Caption = 'Prod. Order Line No. (Sub)';            DataClassification = CustomerContent;            Editable = false;            TableRelation = "Prod. Order Line"."Line No." where(Status = const(Released),                                                                 "Prod. Order No." = field("Subc. Prod. Order No."));        }        field(20545; "Subc. Routing No."; Code[20])        {            Caption = 'Routing No. (Sub)';            DataClassification = CustomerContent;            Editable = false;            TableRelation = "Routing Header";        }        field(20546; "Subc. Rtng Reference No."; Integer)        {            Caption = 'Routing Reference No. (Sub)';            DataClassification = CustomerContent;            Editable = false;        }        field(20547; "Subc. Operation No."; Code[10])        {            Caption = 'Operation No. (Sub)';            DataClassification = CustomerContent;            Editable = false;            TableRelation = "Prod. Order Routing Line"."Operation No." where(Status = const(Released),                                                                              "Prod. Order No." = field("Prod. Order No."),                                                                              "Routing No." = field("Subc. Routing No."),                                                                              "Routing Reference No." = field("Subc. Rtng Reference No."));        }        field(20548; "Subc. Work Center No."; Code[20])        {            Caption = 'Work Center No. (Sub)';            DataClassification = CustomerContent;            Editable = false;            TableRelation = "Work Center";        }        field(20549; "Subc. Purchase Line Type"; Enum "Subc. Purchase Line Type")        {            Caption = 'Subcontracting Line Type';            DataClassification = CustomerContent;            Editable = false;        }        field(20550; "Subc.Whse.Outstanding Quantity"; Decimal)        {            AccessByPermission = TableData Location = R;            AutoFormatType = 0;            BlankZero = true;            CalcFormula = sum("Warehouse Receipt Line"."Qty. Outstanding" where("Source Type" = const(39),#pragma warning disable AL0603                                                                                        "Source Subtype" = field("Document Type"),#pragma warning restore AL0603                                                                                        "Source No." = field("Document No."),                                                                                        "Source Line No." = field("Line No.")));            Caption = 'Whse. Outstanding Quantity';            DecimalPlaces = 0 : 5;            Editable = false;            FieldClass = FlowField;        }        field(20560; "Transfer WIP Item"; Boolean)        {            Caption = 'Transfer WIP Item';            DataClassification = CustomerContent;            ToolTip = 'Specifies whether this purchase line is linked to a WIP item transfer operation.';        }    }    keys    {        key(SubcPurchLineKey; "Subc. Purchase Line Type") { }    }#if not CLEAN29    var#pragma warning disable AL0432        SubcFeatureFlagHandler: Codeunit "Subc. Feature Flag Handler";#pragma warning restore AL0432#endif    procedure GetQuantityPerUOM(): Decimal    var        ItemUnitofMeasure: Record "Item Unit of Measure";    begin#if not CLEAN29#pragma warning disable AL0432        if not SubcFeatureFlagHandler.IsSubcontractingEnabled() then#pragma warning restore AL0432            exit(0);#endif        ItemUnitofMeasure.SetLoadFields("Qty. per Unit of Measure");        ItemUnitofMeasure.Get("No.", "Unit of Measure Code");        exit(ItemUnitofMeasure."Qty. per Unit of Measure");    end;    procedure GetQuantityBase(): Decimal    var        ItemUnitofMeasure: Record "Item Unit of Measure";    begin#if not CLEAN29#pragma warning disable AL0432        if not SubcFeatureFlagHandler.IsSubcontractingEnabled() then#pragma warning restore AL0432            exit(0);#endif        ItemUnitofMeasure.SetLoadFields("Qty. per Unit of Measure");        ItemUnitofMeasure.Get("No.", "Unit of Measure Code");        exit(Round(Quantity * ItemUnitofMeasure."Qty. per Unit of Measure", 0.00001));    end;    /// <summary>    /// Calculates the base quantity from a quantity    /// </summary>    /// <param name="SourceQuantity">Quantity from which the base quantity is calculated</param>    /// <param name="BasedOnField">Field on which the calculation is based</param>    /// <param name="FromFieldName">Name of the field from which the calculation starts</param>    /// <param name="ToFieldName">Name of the field to which the calculation is applied</param>    /// <returns>Calculated base quantity</returns>    internal procedure CalcBaseQtyFromQuantity(SourceQuantity: Decimal; BasedOnField: Text; FromFieldName: Text; ToFieldName: Text) BaseQuantityToReturn: Decimal    var        Item: Record Item;        UOMMgt: Codeunit "Unit of Measure Management";        QtyPerUoM: Decimal;    begin#if not CLEAN29#pragma warning disable AL0432        if not SubcFeatureFlagHandler.IsSubcontractingEnabled() then#pragma warning restore AL0432            exit(0);#endif        Testfield(Type, "Purchase Line Type"::Item);        Item.Get(Rec."No.");        QtyPerUoM := UOMMgt.GetQtyPerUnitOfMeasure(Item, Rec."Unit of Measure Code");        BaseQuantityToReturn := UOMMgt.CalcBaseQty(Rec."No.", Rec."Variant Code", Rec."Unit of Measure Code", SourceQuantity, QtyPerUoM, Rec."Qty. Rounding Precision (Base)", BasedOnField, FromFieldName, ToFieldName);    end;    /// <summary>    /// Determine if the purchase line is a subcontracting line    /// </summary>    /// <param name="ProdOrderLine">The production order line to check</param>    /// <returns>True if the line is a subcontracting line, false otherwise</returns>    internal procedure IsSubcontractingLine(var ProdOrderLine: Record "Prod. Order Line"): Boolean    var        ProductionOrder: Record "Production Order";        ProdOrderRoutingLine: Record "Prod. Order Routing Line";        IsValidLine: Boolean;    begin#if not CLEAN29#pragma warning disable AL0432        if not SubcFeatureFlagHandler.IsSubcontractingEnabled() then#pragma warning restore AL0432            exit(false);#endif        if Rec."Operation No." = '' then            exit(false);        ProductionOrder.SetLoadFields("Source Type");        ProdOrderRoutingLine.SetLoadFields(SystemId);        IsValidLine := ProdOrderLine.Get("Production Order Status"::Released, Rec."Prod. Order No.", Rec."Prod. Order Line No.");        IsValidLine := IsValidLine and ProductionOrder.Get("Production Order Status"::Released, Rec."Prod. Order No.");        IsValidLine := IsValidLine and ProdOrderRoutingLine.Get("Production Order Status"::Released, Rec."Prod. Order No.", Rec."Routing Reference No.", Rec."Routing No.", Rec."Operation No.");        exit(IsValidLine);    end;    /// <summary>    /// Determines if the purchase line is a subcontracting line with the last operation    /// </summary>    /// <param name="ProdOrderLine">The production order line to check</param>    /// <returns>True if the line is a subcontracting line with the last operation, false otherwise</returns>    internal procedure IsSubcontractingLineWithLastOperation(var ProdOrderLine: Record "Prod. Order Line"): Boolean    var        ProdOrderRoutingLine: Record "Prod. Order Routing Line";        IsValidLine: Boolean;    begin#if not CLEAN29#pragma warning disable AL0432        if not SubcFeatureFlagHandler.IsSubcontractingEnabled() then#pragma warning restore AL0432            exit(false);#endif        ProdOrderRoutingLine.SetLoadFields("Next Operation No.");        IsValidLine := ProdOrderRoutingLine.Get("Production Order Status"::Released, Rec."Prod. Order No.", Rec."Routing Reference No.", Rec."Routing No.", Rec."Operation No.");        IsValidLine := IsValidLine and (ProdOrderRoutingLine."Next Operation No." = '');        exit(IsSubcontractingLine(ProdOrderLine) and IsValidLine);    end;    local procedure SetSubcontractingLineType()    var        ProdOrderLine: Record "Prod. Order Line";    begin        case true of            not IsSubcontractingLine(ProdOrderLine):                Rec."Subc. Purchase Line Type" := Rec."Subc. Purchase Line Type"::None;            IsSubcontractingLineWithLastOperation(ProdOrderLine):                Rec."Subc. Purchase Line Type" := Rec."Subc. Purchase Line Type"::LastOperation;            else                Rec."Subc. Purchase Line Type" := Rec."Subc. Purchase Line Type"::NotLastOperation;        end;    end;}