/// <summary>
 /// Deprecated Method for adding a new object to the PricingHistories EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToPricingHistories(PricingHistory pricingHistory)
 {
     base.AddObject("PricingHistories", pricingHistory);
 }
    public void UpdateProductPrice(string productRankResponse, SellerItem sellerItem)
    {
        EbayServiceBL service = new EbayServiceBL((int)sellerItem.User_Code, (int)sellerItem.Country_Code);
        double? averagePrice = null;
        double newPrice;
        double? AvgORLow = null;
        double floorPrice = Convert.ToDouble(sellerItem.Floor_Price);
        double ceilingPrice = Convert.ToDouble(sellerItem.Ceiling_Price);
        int? rank = null;
        double? shippingCost = null;

        List<EbaySearchItem> searchItems = (List<EbaySearchItem>)Common.Deserialize(productRankResponse, typeof(List<EbaySearchItem>));
        string ItemID = sellerItem.Item_ID;

        EbaySearchItem myItem = searchItems.FirstOrDefault(f => f.ItemID == ItemID);
        if (myItem != null)
        {
            shippingCost = myItem.ShippingCost;
            rank = searchItems.IndexOf(myItem);
        }
        else
        {
            var myItemResponse = service.SearchItemsByID(ItemID);
            var myItemresult = myItemResponse.searchResult;
            if (myItemresult != null && myItemresult.count > 0)
            {
                var myItemSearched = myItemresult.item.FirstOrDefault(f => f.itemId == ItemID);
                shippingCost = myItemSearched.shippingInfo.shippingServiceCost != null ? myItemSearched.shippingInfo.shippingServiceCost.Value : 0;
                //shippingCost = 0;
            }
        }

        List<EbaySearchItem> excludedMySearchItems = searchItems.Where(w => w.IsMyProduct == false).ToList();
        EbaySearchItem minPriceSearchItem = excludedMySearchItems.OrderBy(o => o.TotalCostIncludingShipping).FirstOrDefault();
        EbaySearchItem maxPriceSearchItem = excludedMySearchItems.OrderByDescending(o => o.TotalCostIncludingShipping).FirstOrDefault();

        /*Get averagePrice Price From Seached Items Excluded User's Items*/

        if (excludedMySearchItems != null)
            averagePrice = excludedMySearchItems.Sum(s => s.TotalCostIncludingShipping) / excludedMySearchItems.Count;

        /*Set New Price Of Item According to Algo which have been selected by user*/

        if (sellerItem.Algo == Convert.ToString((int)Common.Algo.Lowest))
        {
            if (sellerItem.Less_To_Lowest_Price == null)
                AvgORLow = minPriceSearchItem != null ? (double?)(minPriceSearchItem.TotalCostIncludingShipping - 0.1) : null;
            else
                AvgORLow = minPriceSearchItem != null ? (double?)(minPriceSearchItem.TotalCostIncludingShipping - Convert.ToDouble(sellerItem.Less_To_Lowest_Price)) : null;
                //AvgORLow = minPriceSearchItem != null ? (double?)(minPriceSearchItem.TotalCost - (minPriceSearchItem.TotalCost * Convert.ToDouble(sellerItem.Less_To_Lowest_Price))) : null; it was before 9th april 2014
        }
        else if (sellerItem.Algo == Convert.ToString((int)Common.Algo.Average))
            AvgORLow = averagePrice;
        else if (sellerItem.Algo == Convert.ToString((int)Common.Algo.MatchLowest))
            AvgORLow = minPriceSearchItem.TotalCostIncludingShipping;
        else
            AvgORLow = null;

        /*IF AvgORLow is set and not equals to null means price will be updated */

        if (AvgORLow != null)
        {
            newPrice = AvgORLow >= floorPrice &&
                       AvgORLow <= ceilingPrice ?
                       Convert.ToDouble(AvgORLow) :
                       AvgORLow > ceilingPrice ?
                       ceilingPrice :
                       floorPrice;

            if (sellerItem.User.Automation_Include_Shipping == true && sellerItem.User.Country1.Country_Abbr.ToUpper() == sellerItem.LocatedIn.ToUpper())
            {
                if (shippingCost == null)
                    return;

                newPrice = newPrice - Convert.ToDouble(shippingCost);
            }

            if (sellerItem.Is_Round_To_Nearest == true)
                newPrice = Math.Floor(newPrice / 0.10) * 0.10;

            if (sellerItem.Current_Price == Convert.ToDecimal(newPrice))
                return;

            string result = service.ReviseEbayItem(sellerItem.Item_ID, (double)newPrice, service.UserTokens[(int)sellerItem.User_Account_Code]);

            /*In case of success service returns null*/
            if (string.IsNullOrEmpty(result))
            {

                DataModelEntities context = new DataModelEntities();
                SellerItem si = context.SellerItems.First(f => f.Item_Code == sellerItem.Item_Code);

                PricingHistory ph = new PricingHistory();
                ph.Algo = si.Algo;
                ph.Keyword = si.Keywords;
                ph.Item_Code = si.Item_Code;
                ph.Old_Price = si.Current_Price;
                ph.New_Price = Convert.ToDecimal(newPrice);
                ph.Created_Date = System.DateTime.Now;
                ph.Currency = sellerItem.Currency;
                context.PricingHistories.AddObject(ph);

                si.Current_Price = Convert.ToDecimal(newPrice);
                si.Item_Rank = rank != null ? rank + 1 : null;

                context.SaveChanges();
                if (sellerItem.Floor_Price == Convert.ToDecimal(newPrice))
                {
                    /*Send Floor limit Reaced Notification*/
                    System.Threading.Thread t = new System.Threading.Thread(() => SendFloorLimitReachedAlert(sellerItem));
                    t.Start();

                }

                if (sellerItem.Ceiling_Price == Convert.ToDecimal(newPrice))
                {
                    /*Send Floor limit Reaced Notification*/
                    System.Threading.Thread t = new System.Threading.Thread(() => SendFloorLimitReachedAlert(sellerItem));
                    t.Start();

                }
                Logging.WriteLog(LogType.Info, sellerItem.Item_ID + " Price revised.");
            }
            else
                Logging.WriteLog(LogType.Error, result);
        }
    }
 /// <summary>
 /// Create a new PricingHistory object.
 /// </summary>
 /// <param name="price_History_Code">Initial value of the Price_History_Code property.</param>
 public static PricingHistory CreatePricingHistory(global::System.Int32 price_History_Code)
 {
     PricingHistory pricingHistory = new PricingHistory();
     pricingHistory.Price_History_Code = price_History_Code;
     return pricingHistory;
 }