Codeunit 30188 Shpfy Update Item

Shopify Connector, Microsoft.Integration.Shopify

src/Apps/W1/Shopify/App/src/Products/Codeunits/ShpfyUpdateItem.Codeunit.al162 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.Integration.Shopify;using Microsoft.Inventory.Item;using Microsoft.Purchases.Vendor;/// <summary>/// Codeunit Shpfy Update Item (ID 30188)./// </summary>codeunit 30188 "Shpfy Update Item"{    Access = Internal;    Permissions =        tabledata Item = rim,        tabledata "Item Category" = r,        tabledata "Item Variant" = rim,        tabledata Vendor = r;    TableNo = "Shpfy Variant";    var        Shop: Record "Shpfy Shop";        ProductEvents: Codeunit "Shpfy Product Events";    trigger OnRun()    var        Item: Record Item;        ItemVariant: Record "Item Variant";        ShopifyProduct: Record "Shpfy Product";        CreateItem: Codeunit "Shpfy Create Item";        IsHandled: Boolean;    begin        if ShopifyProduct.Get(Rec."Product Id") then begin            SetShop(ShopifyProduct."Shop Code");            if Item.GetBySystemId(Rec."Item SystemId") then begin                ProductEvents.OnBeforeUpdateItem(Shop, ShopifyProduct, Rec, Item, IsHandled);                if not IsHandled then                    if DoUpdateItem(ShopifyProduct, Rec, Item) then                        ProductEvents.OnAfterUpdateItem(Shop, ShopifyProduct, Rec, Item);                IsHandled := false;                if (not IsNullGuid(Rec."Item Variant SystemId")) and ItemVariant.GetBySystemId(Rec."Item Variant SystemId") then begin                    ProductEvents.OnBeforeUpdateItemVariant(Shop, ShopifyProduct, Rec, Item, ItemVariant, IsHandled);                    if not IsHandled then begin                        CreateItem.CreateReferences(ShopifyProduct, Rec, Item, ItemVariant);                        if DoUpdateItemVariant(Rec, ItemVariant) then                            ProductEvents.OnAfterUpdateItemVariant(Shop, ShopifyProduct, Rec, Item, ItemVariant);                    end;                end else begin                    Clear(ItemVariant);                    CreateItem.CreateReferences(ShopifyProduct, Rec, Item, ItemVariant);                end;            end;        end;    end;    /// <summary>     /// Do Update Item.    /// </summary>    /// <param name="ShopifyProduct">Parameter of type Record "Shopify Product".</param>    /// <param name="ShopifyVariant">Parameter of type Record "Shopify Variant".</param>    /// <param name="Item">Parameter of type Record Item.</param>    /// <returns>Return value of type Boolean.</returns>    local procedure DoUpdateItem(var ShopifyProduct: Record "Shpfy Product"; var ShopifyVariant: Record "Shpfy Variant"; var Item: Record Item): Boolean    var        ItemCategory: Record "Item Category";        Vendor: Record Vendor;        CreateItem: Codeunit "Shpfy Create Item";        ProcessOrder: Codeunit "Shpfy Process Order";        IsModified: Boolean;        IsModifiedByEvent: Boolean;        TariffNo: Code[20];        CountryRegionCode: Code[10];    begin        if Item.Description <> ShopifyProduct.Title then begin            IsModified := true;            Item.Description := ShopifyProduct.Title;        end;        if ShopifyProduct."Product Type" <> '' then begin            ItemCategory.SetFilter(Description, '@' + ShopifyProduct."Product Type");            if ItemCategory.FindFirst() then                if Item."Item Category Code" <> ItemCategory.Code then begin                    IsModified := true;                    Item."Item Category Code" := ItemCategory.Code;                end;        end;        if ShopifyProduct.Vendor <> '' then begin            Vendor.SetFilter(Name, '@' + ShopifyProduct.Vendor);            if Vendor.FindFirst() then                if Item."Vendor No." <> Vendor."No." then begin                    IsModified := true;                    Item."Vendor No." := Vendor."No.";                end;        end;        if Shop."Sync HS Code and Country" then begin            // Only apply a value that resolves to an existing BC Tariff Number / Country/Region, so an            // unmapped or blank Shopify value never wipes an existing value on the Business Central item.            TariffNo := CreateItem.GetTariffNo(ShopifyVariant."Tariff No.");            if (TariffNo <> '') and (Item."Tariff No." <> TariffNo) then begin                Item.Validate("Tariff No.", TariffNo);                IsModified := true;            end;            CountryRegionCode := ProcessOrder.GetCountryCode(ShopifyVariant."Country/Region of Origin Code");            if (CountryRegionCode <> '') and (Item."Country/Region of Origin Code" <> CountryRegionCode) then begin                Item.Validate("Country/Region of Origin Code", CountryRegionCode);                IsModified := true;            end;        end;        ProductEvents.OnDoUpdateItemBeforeModify(Shop, ShopifyProduct, Item, IsModifiedByEvent);        if IsModified or IsModifiedByEvent then            exit(Item.Modify());        exit(false);    end;    /// <summary>     /// Do Update Item Variant.    /// </summary>    /// <param name="ShopifyVariant">Parameter of type Record "Shopify Variant".</param>    /// <param name="ItemVariant">Parameter of type Record "Item Variant".</param>    /// <returns>Return value of type Boolean.</returns>    local procedure DoUpdateItemVariant(ShopifyVariant: Record "Shpfy Variant"; var ItemVariant: Record "Item Variant"): Boolean    var        IsModified: Boolean;        IsModifiedByEvent: Boolean;    begin        if ItemVariant.Description <> ShopifyVariant.Title then begin            ItemVariant.Description := ShopifyVariant.Title;            IsModified := true;        end;        ProductEvents.OnDoUpdateItemVariantBeforeModify(Shop, ShopifyVariant, ItemVariant, IsModifiedByEvent);        if IsModified or IsModifiedByEvent then            exit(ItemVariant.Modify());        exit(false);    end;    /// <summary>     /// Set Shop.    /// </summary>    /// <param name="Code">Parameter of type Code[20].</param>    internal procedure SetShop(Code: Code[20])    begin        if (Code <> '') and (Shop.Code <> Code) then begin            Clear(Shop);            Shop.Get(Code);        end;    end;    /// <summary>     /// Set Shop.    /// </summary>    /// <param name="ShopifyShop">Parameter of type Record "Shopify Shop".</param>    internal procedure SetShop(ShopifyShop: Record "Shpfy Shop")    begin        Shop := ShopifyShop;    end;}