Sunday, 3 May 2020

Create Item and setup using x++ code


    public void CreateItemAndSetup()
    {
        InventTable                                     inventTable;
        InventTableModule                               inventTableModule;
        InventItemSetupSupplyType                       inventItemSetupSupplyType;
        InventModelGroupItem                            inventModelGroupItem;
        InventItemGroupItem                             inventItemGroupItem;
        NumberSequenceTable                             numberSequenceTable;
        EcoResProduct                                   ecoResProduct;
        EcoResDistinctProduct                           ecoResDistinctProduct;
        EcoResProductIdentifier                         ecoResProductIdentifier;
        EcoResStorageDimensionGroupProduct              ecoResStorageDimensionGroupProduct;
        EcoResTrackingDimensionGroupProduct             ecoResTrackingDimensionGroupProduct;
        EcoResStorageDimensionGroupItem                 ecoResStorageDimensionGroupItem;
        EcoResTrackingDimensionGroupItem                ecoResTrackingDimensionGroupItem;
        DataAreaId                                      _dataAreaId=curext();
        ItemId                                          _itemid;
        ItemName                                        _itemname;


        if (EcoResProduct::findByProductNumber(_itemid).RecId == 0)
        {
            //Product
            ecoResDistinctProduct.clear();
            ecoResDistinctProduct.initValue();
            ecoResDistinctProduct.ProductType = EcoResProductType::Item;
            ecoResDistinctProduct.DisplayProductNumber  = _itemid;
            ecoResDistinctProduct.SearchName            = _itemname;

            if (ecoResDistinctProduct.validateWrite())
            {
                ecoResDistinctProduct.insert();

                ecoResProductIdentifier.clear();

                ecoResProductIdentifier.initValue();
                ecoResProductIdentifier.ProductNumber   = _itemid;
                ecoResProductIdentifier.Product         = ecoResDistinctProduct.RecId;
                ecoResProductIdentifier.insert();

                //Released Product

                select firstOnly ecoResProduct
                    where EcoResProduct.DisplayProductNumber == _itemid;

                //Storage dimension group

                ecoResStorageDimensionGroupProduct.clear();
                ecoResStorageDimensionGroupProduct.initValue();
                ecoResStorageDimensionGroupProduct.Product               = ecoResProduct.RecId;
                ecoResStorageDimensionGroupProduct.StorageDimensionGroup = EcoResStorageDimensionGroup::findByDimensionGroupName("SiteWH").RecId;

                if (ecoResStorageDimensionGroupProduct.validateWrite())
                {
                    ecoResStorageDimensionGroupProduct.insert();
                }

                //Tracking dimension group
                ecoResTrackingDimensionGroupProduct.clear();
                ecoResTrackingDimensionGroupProduct.initValue();
                ecoResTrackingDimensionGroupProduct.Product                = ecoResProduct.RecId;
                ecoResTrackingDimensionGroupProduct.TrackingDimensionGroup = EcoResTrackingDimensionGroup::findByDimensionGroupName("None").RecId;

                if (ecoResTrackingDimensionGroupProduct.validateWrite())
                {
                    ecoResTrackingDimensionGroupProduct.insert();
                }

                if (_itemname)
                {
                    EcoResProductTranslation::createOrUpdateTranslation(ecoResDistinctProduct.RecId, _itemname, _itemname);
                }

                inventTable.clear();
                inventTable.initValue();
                inventTable.initFromEcoResProduct(ecoResProduct);
                inventTable.ItemId      = ecoResProduct.productNumber();
                inventTable.NameAlias   = ecoResProduct.SearchName;
                inventTable.insert(true);

                // Create inventTableModules
                inventTableModule.clear();
                inventTableModule.initValue();
                inventTableModule.ItemId        = inventTable.ItemId;
                inventTableModule.ModuleType    = ModuleInventPurchSales::Invent;
                inventTableModule.Price         = 10;
                inventTableModule.insert();

                inventTableModule.clear();
                inventTableModule.initValue();
                inventTableModule.ItemId        = inventTable.ItemId;
                inventTableModule.ModuleType    = ModuleInventPurchSales::Purch;
                inventTableModule.insert();

                inventTableModule.clear();
                inventTableModule.initValue();
                inventTableModule.ItemId        = inventTable.ItemId;
                inventTableModule.ModuleType    = ModuleInventPurchSales::Sales;
                inventTableModule.insert();

                //Inventory model group
                inventModelGroupItem.clear();
                inventModelGroupItem.initValue();
                inventModelGroupItem.ItemDataAreaId = inventTable.dataAreaId;
                inventModelGroupItem.ItemId         = inventTable.ItemId;
                inventModelGroupItem.ModelGroupId   = "FIFO";
                inventModelGroupItem.ModelGroupDataAreaId = curext();
                inventModelGroupItem.insert();


                //Item group
                inventItemGroupItem.clear();
                inventItemGroupItem.initValue();
                inventItemGroupItem.ItemDataAreaId = inventTable.dataAreaId;
                inventItemGroupItem.ItemId         = inventTable.ItemId;
                inventItemGroupItem.ItemGroupId    = "Sevices";
                inventItemGroupItem.ItemGroupDataAreaId = curext();
                inventItemGroupItem.insert();

                //Create inventItemLocation
                InventItemLocation::createDefault(inventTable.ItemId);

                // Creates a new item default order type for the product that is released.
                inventItemSetupSupplyType.clear();
                inventItemSetupSupplyType.initValue();
                inventItemSetupSupplyType.ItemId            = inventTable.ItemId;
                inventItemSetupSupplyType.ItemDataAreaId    = inventTable.DataAreaId;
                inventItemSetupSupplyType.insert();

                //create relationship tables to dimension groups.
                ecoResStorageDimensionGroupProduct  = EcoResStorageDimensionGroupProduct::findByProduct(ecoResProduct.RecId);
                ecoResTrackingDimensionGroupProduct = EcoResTrackingDimensionGroupProduct::findByProduct(ecoResProduct.RecId);

                // mandatory storage dimension group for the product
                if (ecoResStorageDimensionGroupProduct.RecId)
                {
                    ecoResStorageDimensionGroupItem.clear();
                    ecoResStorageDimensionGroupItem.ItemDataAreaId = inventTable.DataAreaId;
                    ecoResStorageDimensionGroupItem.ItemId = inventTable.ItemId;
                    ecoResStorageDimensionGroupItem.StorageDimensionGroup = ecoResStorageDimensionGroupProduct.StorageDimensionGroup;
                    ecoResStorageDimensionGroupItem.insert();
                }

                // mandatory tracking dimension group for the product

                if (ecoResTrackingDimensionGroupProduct.RecId)
                {
                    ecoResTrackingDimensionGroupItem.clear();
                    ecoResTrackingDimensionGroupItem.ItemDataAreaId = inventTable.DataAreaId;
                    ecoResTrackingDimensionGroupItem.ItemId = inventTable.ItemId;
                    ecoResTrackingDimensionGroupItem.TrackingDimensionGroup = ecoResTrackingDimensionGroupProduct.TrackingDimensionGroup;
                    ecoResTrackingDimensionGroupItem.insert();
                }

                info(strfmt("Product successfully released to UsMf legal entity"));

            }

        }

    }


       /// <summary>
    ///  To create or update service orderable attribute YES or NO
    /// </summary>

    public void createOrUpdateProductAttribute()
    {
        EcoResProduct                       product;
        EcoResDistinctProductVariant        variant;
        EcoResProductInstanceValue          ecoResProductInstanceValue;
        EcoResAttributeType                 ecoResAttributeType;
        EcoResAttribute                     ecoResAttribute;
        EcoResAttributeValue                ecoResAttributeValue;
        EcoResBooleanValue                  ecoResBooleanValue;
        ItemId                              itemId;
        EcoResCategoryName                  categoryName;
        RefRecId                            itemRecID;
        str                                 attributeName;
        Boolean                             attributeValue;

        itemId          = this.ItemNumber;
        attributeName   = "@BC:ServiceOrderableLbl";
        attributeValue  = this.BCServiceOrderable;
        categoryName    = this.BCEcoResCategoryName;

        product = EcoResProduct::findByProductNumber(itemId);

        //Get Product by Itemid
        if(product)
        {
            ttsbegin;

            if(product.getInstanceRelationType() == tableStr(EcoResDistinctProductVariant))
            {
                variant = product as EcoResDistinctProductVariant;
                product = EcoResProduct::find(variant.ProductMaster);
            }

            //Find records in ecoResProductInstanceValue if not insert new
            ecoResProductInstanceValue = EcoResProductInstanceValue::findByProduct(product.recid);

            if(!ecoResProductInstanceValue)
            {
                //insert records in ecoResProductInstanceValue
                ecoResProductInstanceValue.product = product.Recid;
                ecoResProductInstanceValue.insert();
            }

            //Check if record exists in EcoResAttributeValue
            ecoResAttributeType     = EcoResAttributeType::findByName("Boolean");
            ecoResAttribute         = EcoResAttribute::findByName(attributeName, ecoResAttributeType.RecId);
            ecoResAttributeValue    = EcoResAttributeValue::findByInstanceAttribute(ecoResProductInstanceValue.recid, ecoResAttribute.recid, true);
           
            if(ecoResAttributeValue)
            {
                //If EcoResBooleanValue exists, check it doesn't have the same value
                if(EcoResBooleanValue::find(ecoResAttributeValue.Value).BooleanValue != attributeValue)
                {
                    //create a record in EcoresBooleanvalue
                    ecoResBooleanValue.clear();
                    ecoResBooleanValue.BooleanValue = attributeValue;
                    ecoResBooleanValue.insert();
  
                    //Update ecoResAttribute
                    ecoResAttributeValue.Value = ecoResBooleanValue.recid;
                    ecoResAttributeValue.update();
                }
            }

            //if ecoresAttributeValue not exists
            else
            {
                //Always is necessary to insert a new EcoResValue record per attribute (1:1)
                ecoResBooleanValue.clear();
                ecoResBooleanValue.BooleanValue = attributeValue;
                ecoResBooleanValue.insert();
 
                //insert record in ecoResAttributeValue
                ecoResAttributeValue.clear();
                ecoResAttributeValue.InstanceValue  = ecoResProductInstanceValue.recid;
                ecoResAttributeValue.Attribute      = ecoResAttribute.recid;
                ecoResAttributeValue.Value              = ecoResBooleanValue.Recid;
                ecoResAttributeValue.insert();
                
            }
            ttsCommit;
        }
    }


    /// <summary>
    ///  To create product categories 
    /// </summary>
    public void createProductCategory()
    {
        EcoResCategoryHierarchy     ecoResCategoryHierarchy;
        EcoResCategory              ecoResCategory;
        EcoResProductCategory       selectedEcoResProductCategory;
        EcoResProduct               ecoResProduct;
        EcoResTextValue             textValue;

        ecoResCategoryHierarchy = EcoResCategoryHierarchy::findByName(this.BCEcoResCategoryHierarchyName);
        ecoResCategory          = EcoResCategory::findByName(this.BCEcoResCategoryName, ecoResCategoryHierarchy.Recid);

        if (ecoResCategory.RecId)
        {
            ecoResProduct = InventTable::find(this.ItemNumber).Product();

            select  firstonly selectedEcoResProductCategory
                where selectedEcoResProductCategory.CategoryHierarchy == ecoResCategory.CategoryHierarchy
                &&  selectedEcoResProductCategory.Product             == ecoResProduct.RecId;

            if (selectedEcoResProductCategory.RecId)
            {
                selectedEcoResProductCategory.selectForUpdate(true);

                ttsbegin;
                selectedEcoResProductCategory.Category = ecoResCategory.RecId;
                selectedEcoResProductCategory.update();
                ttscommit;
            }
            else
            {
                EcoResProductCategory::insertEcoResProductCategory(ecoResCategory, ecoResProduct);
            }
        }
    }