private void RefreshFreeItemLines(PXCache sender)
        {
            if (sender.Graph.IsImport && !sender.Graph.IsMobile)
            {
                return;
            }

            Dictionary <int, decimal> groupedByInventory = new Dictionary <int, decimal>();

            //PXSelectBase<Discount> select = new PXSelect<Discount,
            //	Where<Discount.orderType, Equal<Current<CROpportunity.orderType>>,
            //	And<Discount.orderNbr, Equal<Current<CROpportunity.orderNbr>>,
            //	//And<Discount.skipDiscount, NotEqual<boolTrue>>
            //	>>>(_Graph);

            foreach (Discount item in Discounts.Select())
            {
                if (item.FreeItemID != null && item.SkipDiscount != true)
                {
                    if (groupedByInventory.ContainsKey(item.FreeItemID.Value))
                    {
                        groupedByInventory[item.FreeItemID.Value] += item.FreeItemQty ?? 0;
                    }
                    else
                    {
                        groupedByInventory.Add(item.FreeItemID.Value, item.FreeItemQty ?? 0);
                    }
                }
            }

            bool freeItemChanged = false;

            #region Delete Unvalid FreeItems
            foreach (Detail line in Details.Cache.Cached)
            {
                var status = Details.Cache.GetStatus(line);
                if (status == PXEntryStatus.Deleted || status == PXEntryStatus.InsertedDeleted)
                {
                    continue;
                }

                if (line.IsFree != true)
                {
                    continue;
                }

                if (line.ManualDisc == false && line.InventoryID != null)
                {
                    if (groupedByInventory.ContainsKey(line.InventoryID.Value))
                    {
                        if (groupedByInventory[line.InventoryID.Value] == 0)
                        {
                            Details.Cache.Delete(line);
                            freeItemChanged = true;
                        }
                    }
                    else
                    {
                        Details.Cache.Delete(line);
                        freeItemChanged = true;
                    }
                }
            }

            #endregion

            foreach (KeyValuePair <int, decimal> kv in groupedByInventory)
            {
                object source;
                var    freeLine = GetFreeLineByItemID(kv.Key, out source);

                if (freeLine == null)
                {
                    if (kv.Value > 0)
                    {
                        Detail line = new Detail
                        {
                            InventoryID = kv.Key,
                            IsFree      = true,
                            Quantity    = kv.Value
                        };
                        //line.ShipComplete = SOShipComplete.CancelRemainder;
                        //Need to ckeck
                        Details.Cache.Insert(line);


                        freeItemChanged = true;
                    }
                }
                else
                {
                    Detail copy = (Detail)Details.Cache.CreateCopy(freeLine);
                    copy.Quantity = kv.Value;
                    Details.Cache.Update(copy);
                    freeItemChanged = true;
                }
            }

            if (freeItemChanged)
            {
                Details.View.RequestRefresh();
            }
        }
 /// <summary>Sets the default discount account and subaccount for the specified detail line. You must override this method in the implementation class for a particular
 /// graph.</summary>
 /// <param name="discount">The detail line.</param>
 protected abstract void DefaultDiscountAccountAndSubAccount(Detail discount);