# Updating Future items in stock

**URL:** <https://forum.litium.com/t/updating-future-items-in-stock/1668>\
**Category:** Questions\
**Created:** [September 29, 2020, 8:14am UTC](https://forum.litium.com/t/updating-future-items-in-stock/1668 "2020-09-29T08:14:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimmy\_haridy](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jimmy\_haridy](https://forum.litium.com/u/jimmy_haridy)\
**Post date:** [September 29, 2020, 8:14am UTC](https://forum.litium.com/t/updating-future-items-in-stock/1668/1 "2020-09-29T08:14:40Z")

</div>

Hi, I wonder how I can update future items in stock correctly? Now if I want to update future items I need to call the update two times for it to work? If I only call the update method once, only the current stock updates, but not the future items. Se code below:  
Litium version: 7.4.0

```auto
            foreach (var stockItem in stockItems)
            {
                var stockitemToUpdate = stockItem.MakeWritableClone();
                // Check if the stock item has future inventory / inventories
                if (stockitemToUpdate.CustomData is FutureInventoryItems futureInventoryItems)
                {
                    // loop through the future inventories and reduce the future stocks
                    foreach (var futureItem in futureInventoryItems.FutureItems)
                    {
                        futureItem.InStockQuantity -= item.Quantity;
                    }
                    stockitemToUpdate.CustomData = futureInventoryItems;
                }
                stockitemToUpdate.InStockQuantity -= item.Quantity;
                using (Solution.Instance.SystemToken.Use(“OrderUtilities.ReduceStockBalance”))
                {
                    {
                        _inventoryItemService.Update(stockitemToUpdate);
                        _inventoryItemService.Update(stockitemToUpdate);
                    }
                }
            }

```

---

<div class="post-metadata">

**Author:** ![steve.redstorm](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/steve.redstorm/32/14_2.png) [@steve.redstorm](https://forum.litium.com/u/steve.redstorm)\
**Post date:** [September 29, 2020, 8:38am UTC](https://forum.litium.com/t/updating-future-items-in-stock/1668/2 "2020-09-29T08:38:26Z")

</div>

It should be enough with calling the update method once.  
You got 2 extra brackets inside using statement.

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [September 29, 2020, 9:10am UTC](https://forum.litium.com/t/updating-future-items-in-stock/1668/3 "2020-09-29T09:10:21Z")

</div>

Can you share the implementation for the `FutureInventoryItems`-class?

---

<div class="post-metadata">

**Author:** ![jimmy\_haridy](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jimmy\_haridy](https://forum.litium.com/u/jimmy_haridy)\
**Post date:** [September 29, 2020, 9:42am UTC](https://forum.litium.com/t/updating-future-items-in-stock/1668/4 "2020-09-29T09:42:49Z")

</div>

```auto
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Litium.ComponentModel;

namespace Columbus.Application.Inventory
{
    public class FutureInventoryItem : IReadOnly
    {
        private bool _isReadOnly { get; set; }
        public int ErpStatus { get; set; }
        public decimal InStockQuantity { get; set; }
        public DateTimeOffset AvailableDateTime { get; set; }

        bool IReadOnly.IsReadOnly => _isReadOnly;
        public void MakeReadOnly()
        {
            _isReadOnly = true;
        }

        public object MakeWritableClone()
        {
            return new FutureInventoryItem
            {
                ErpStatus = this.ErpStatus,
                InStockQuantity = this.InStockQuantity,
                AvailableDateTime = this.AvailableDateTime
            };
        }
    }
    public class FutureInventoryItems : IReadOnly
    {
        private bool _isReadOnly { get; set; }
        public List<FutureInventoryItem> FutureItems { get; set; }
        bool IReadOnly.IsReadOnly => _isReadOnly;

        public FutureInventoryItems()
        {
            FutureItems = new List<FutureInventoryItem>();
        }

        public void MakeReadOnly()
        {
            _isReadOnly = true;
        }
        public object MakeWritableClone()
        {
            return new FutureInventoryItems
            {
                FutureItems = this.FutureItems
            };
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [September 29, 2020, 1:34pm UTC](https://forum.litium.com/t/updating-future-items-in-stock/1668/5 "2020-09-29T13:34:58Z")

</div>

The problem is that your `FutureInventoryItems` and `FutureInventoryItem` classes are not implementing the `IReadOnly` interface in the correct way.

Example the `FutureInventoryItems.MakeWritableClone()` is returning an object that referensing to the same `FutureItems` collection (not a clone of the collection but the same instance) so when the cloned version is changed, also the object in cache will be changed. During update operation the item in cache and the updated item is compared and they are the same (the reference to the same for the `FutureItems` collection) and the objects will be the same.

If we take for example the `TextOption` that exists in the product we also have an inner collection; `Items` that are of the interface `IList<Item>` instead of the `List<Item>`. The reason to use the interface one is that when the object will be read only, the `IList<>` will be a read only version and otherwise a `List<>`.

```cs
using System.Collections.Generic;
using Litium.ComponentModel;

namespace Litium.FieldFramework.FieldTypes
{
    /// <summary>
    /// Class TextOption.
    /// </summary>
    /// <seealso cref="Litium.ComponentModel.ObjectBase" />
    public class TextOption : ObjectBase
    {
        private IList<Item> _items = new List<Item>();
        private bool _multiSelect;
        private bool _manualSort;

        /// <summary>
        /// Gets or sets the items.
        /// </summary>
        /// <value>The items.</value>
        public virtual IList<Item> Items
        {
            get { return _items; }
            set
            {
                ThrowIfReadOnly();
                _items = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [multi select].
        /// </summary>
        /// <value><c>true</c> if [multi select]; otherwise, <c>false</c>.</value>
        public virtual bool MultiSelect
        {
            get { return _multiSelect; }
            set
            {
                ThrowIfReadOnly();
                _multiSelect = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [manual sort].
        /// </summary>
        /// <value><c>true</c> if [manual sort]; otherwise, <c>false</c>.</value>
        public virtual bool ManualSort
        {
            get { return _manualSort; }
            set
            {
                ThrowIfReadOnly();
                _manualSort = value;
            }
        }

        /// <summary>
        /// Class Item.
        /// </summary>
        /// <seealso cref="Litium.ComponentModel.ObjectBase" />
        public class Item : ObjectBase
        {
            private IDictionary<string, string> _name = new Dictionary<string, string>();
            private string _value;

            /// <summary>
            /// Gets or sets the name.
            /// </summary>
            /// <value>The name.</value>
            public virtual IDictionary<string, string> Name
            {
                get { return _name; }
                set
                {
                    ThrowIfReadOnly();
                    _name = value;
                }
            }

            /// <summary>
            /// Gets or sets the value.
            /// </summary>
            /// <value>The value.</value>
            public virtual string Value
            {
                get { return _value; }
                set
                {
                    ThrowIfReadOnly();
                    _value = value;
                }
            }
        }
    }
}

```

`ObjectBase` and `ObjectBase<>` are base classes that have helper methods to transform all the properties from/to read only so the manual creation of the object is not needed.

---

<div class="post-metadata">

**Author:** ![jimmy\_haridy](https://avatars.discourse-cdn.com/v4/letter/j/bb73d2/32.png) [@jimmy\_haridy](https://forum.litium.com/u/jimmy_haridy)\
**Post date:** [October 1, 2020, 6:40am UTC](https://forum.litium.com/t/updating-future-items-in-stock/1668/6 "2020-10-01T06:40:08Z")

</div>

Thanks, we forgot to clone the object properly in makewritableclone()

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)\
**Post date:** [October 29, 2020, 6:40am UTC](https://forum.litium.com/t/updating-future-items-in-stock/1668/7 "2020-10-29T06:40:11Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
