private void UpdateProductPrice(string sellerItemJson, SellerItem sellerItem, int userCode)
 {
     ParcelService ps = new ParcelService();
     string TokenJSON = ps.GetTokenJSON(userCode);//"{\"brucewl1964\": 62, \"williamgerardit\": 64}";
     int pageSize = 20;
     string response = GetProductRank(sellerItemJson, pageSize, TokenJSON, userCode);
     if (!string.IsNullOrEmpty(response))
     {
         ps.UpdateProductPrice(response, sellerItem);
     }
 }
    public FindItemsByKeywordsResponse SearchItems(SellerItem sellerItem, int pageSize, int pageNumber)
    {
        // Creating an object to the BestMatchService class
        CustomFindingService service = new CustomFindingService();
        service.Url = ConfigurationManager.AppSettings["FindingService"];

        // Creating request object for FindBestMatchItemDetailsByKeywords API
        FindItemsByKeywordsRequest request = new FindItemsByKeywordsRequest();

        // Setting the required property values
        request.keywords = sellerItem.Keywords;

        // Setting the pagination
        PaginationInput pagination = new PaginationInput();
        pagination.entriesPerPageSpecified = true;
        pagination.entriesPerPage = pageSize;
        pagination.pageNumberSpecified = true;
        pagination.pageNumber = pageNumber;
        request.paginationInput = pagination;
        ItemFilter filter;
        List<ItemFilter> filters = new List<ItemFilter>();

        /*ListingType*/
        string ListingTypeFilter = null;
        if (sellerItem.Is_Fixed_Price == true)
            ListingTypeFilter = "FixedPrice";

        if (sellerItem.Is_Auctions == true)
            ListingTypeFilter += ",AuctionWithBIN";

        if (ListingTypeFilter != null)
        {
            filter = new ItemFilter();
            filter.name = ItemFilterType.ListingType;
            filter.value = ListingTypeFilter.Split(',');
            filters.Add(filter);
        }

        /*ReturnsAcceptedOnly*/
        if (sellerItem.Is_Returns_Accepted == true)
        {
            filter = new ItemFilter();
            filter.name = ItemFilterType.ReturnsAcceptedOnly;
            filter.value = new string[] { "true" };
            filters.Add(filter);
        }

        /*LocatedIn*/
        //if (sellerItem.Is_Location_AU == true)
        //{
        //    filter = new ItemFilter();
        //    filter.name = ItemFilterType.LocatedIn;
        //    filter.value = new string[] { "AU" };
        //    filters.Add(filter);
        //}
        if (!string.IsNullOrEmpty(sellerItem.LocatedIn))
        {
            filter = new ItemFilter();
            filter.name = ItemFilterType.LocatedIn;
            filter.value = new string[] { sellerItem.LocatedIn.ToUpper() };
            filters.Add(filter);
        }
        /*HideDuplicateItems*/
        if (sellerItem.Is_Hide_Duplicates == true)
        {
            filter = new ItemFilter();
            filter.name = ItemFilterType.HideDuplicateItems;
            filter.value = new string[] { "true" };
            filters.Add(filter);
        }
        /*TopRatedSellerOnly*/
        if (sellerItem.Is_Hide_Duplicates == true)
        {
            filter = new ItemFilter();
            filter.name = ItemFilterType.TopRatedSellerOnly;
            filter.value = new string[] {"true"};
            filters.Add(filter);
        }
        else
        {
            /*Specify one or more seller names. Search results will
             include items from the specified sellers only.
             The Seller item filter cannot be used together with either the
             ExcludeSeller or TopRatedSellerOnly item filters. */
            if (!string.IsNullOrEmpty(sellerItem.Inclued_Sellers))
            {
                filter = new ItemFilter();
                filter.name = ItemFilterType.Seller;
                filter.value = sellerItem.Inclued_Sellers.Split(',');
                filters.Add(filter);
            }
            else if (!string.IsNullOrEmpty(sellerItem.Exclude_Sellers))
            {
                filter = new ItemFilter();
                filter.name = ItemFilterType.ExcludeSeller;
                filter.value = sellerItem.Exclude_Sellers.Split(',');
                filters.Add(filter);
            }
        }
        /*MinQuantity*/
        if (sellerItem.Minimum_Quantity > 0)
        {
            filter = new ItemFilter();
            filter.name = ItemFilterType.MinQuantity;
            filter.value = new string[] { sellerItem.Minimum_Quantity.ToString() };
            filters.Add(filter);
        }
        /*MaxQuantity*/
        if (sellerItem.Maximum_Quantity > 0)
        {
            filter = new ItemFilter();
            filter.name = ItemFilterType.MaxQuantity;
            filter.value = new string[] { sellerItem.Maximum_Quantity.ToString() };
            filters.Add(filter);
        }
        /*MinPrice*/
        if (sellerItem.Minimum_Price >= 0)
        {
            filter = new ItemFilter();
            filter.name = ItemFilterType.MinPrice;
            filter.value = new string[] { sellerItem.Minimum_Price.ToString() };
            filters.Add(filter);
        }
        /*MaxPrice*/
        if (sellerItem.Maximum_Price > 0)
        {
            filter = new ItemFilter();
            filter.name = ItemFilterType.MaxPrice;
            filter.value = new string[] { sellerItem.Maximum_Price.ToString() };
            filters.Add(filter);
        }
        /*FeedbackScoreMin*/
        if (sellerItem.Minimum_Feedback >= 0)
        {
            filter = new ItemFilter();
            filter.name = ItemFilterType.FeedbackScoreMin;
            filter.value = new string[] {sellerItem.Minimum_Feedback.ToString()};
            filters.Add(filter);
        }
        /*FeedbackScoreMax*/
        if (sellerItem.Maximum_Feedback > 0)
        {
            filter = new ItemFilter();
            filter.name = ItemFilterType.FeedbackScoreMax;
            filter.value = new string[] { sellerItem.Maximum_Feedback.ToString() };
            filters.Add(filter);
        }
        /*Condition*/
        if (!string.IsNullOrEmpty(sellerItem.Include_Condtion_Codes))
        {
            filter = new ItemFilter();
            filter.name = ItemFilterType.Condition;
            filter.value = sellerItem.Include_Condtion_Codes.Split(',');
            filters.Add(filter);
        }
        /*Cateogry*/
        if (!string.IsNullOrEmpty(sellerItem.Exclude_Category_Codes))
        {
            filter = new ItemFilter();
            filter.name = ItemFilterType.ExcludeCategory;
            filter.value = sellerItem.Exclude_Category_Codes.Split(',');
            filters.Add(filter);
        }

        if(filters.Count > 0)
            request.itemFilter = filters.ToArray();
        request.sortOrder = SortOrderType.BestMatch;
        request.sortOrderSpecified = true;
        // Creating response object
        FindItemsByKeywordsResponse response = service.findItemsByKeywords(request);
        return response;
    }
    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>
 /// Deprecated Method for adding a new object to the SellerItems EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToSellerItems(SellerItem sellerItem)
 {
     base.AddObject("SellerItems", sellerItem);
 }
 /// <summary>
 /// Create a new SellerItem object.
 /// </summary>
 /// <param name="item_Code">Initial value of the Item_Code property.</param>
 public static SellerItem CreateSellerItem(global::System.Int32 item_Code)
 {
     SellerItem sellerItem = new SellerItem();
     sellerItem.Item_Code = item_Code;
     return sellerItem;
 }
Exemplo n.º 6
0
    public void SaveEbayUserItems(int UserCode)
    {
        EbayServiceBL service = new EbayServiceBL(UserCode);
        DataModelEntities context = new DataModelEntities();
        DateTime today = DateTime.Today;
        if (service.UserTokens != null)
        {
            foreach (KeyValuePair<int, string> account in service.UserTokens)
            {
                try
                {
                   // List<Seller_Item> items = service.GetUserItems(account.Value);
                    List<Seller_Item> items = service.GetUserItemsList(account.Value);
                    foreach (Seller_Item item in items)
                    {
                        SellerItem sellerItem = context.SellerItems.FirstOrDefault(a => a.Is_Active == true && a.User_Account_Code == account.Key && a.Item_ID == item.ItemID && a.Country_Code == service.countryID);
                        if (sellerItem == null)
                        {
                            if (item.EndDate.Date > today.Date)
                            {
                                /*Save to seller item*/
                                SellerItem dbItem = new SellerItem();
                                dbItem.BIN_Price = (decimal)item.BinPrice;
                                dbItem.Created_Date = DateTime.Now;
                                dbItem.Current_Price = (decimal)item.CurrentPrice;
                                dbItem.End_Date = item.EndDate;
                                dbItem.Is_Active = true;
                                dbItem.Item_ID = item.ItemID;
                                dbItem.Item_Name = item.ItemName;
                                dbItem.Start_Date = item.StartDate;
                                dbItem.Is_Automated = false;
                                dbItem.User_Code = UserCode;
                                dbItem.User_Account_Code = account.Key;
                                dbItem.Picture_URL = item.PictureURL;
                                dbItem.Item_View_URL = item.ItemViewURL;
                                dbItem.Is_Promo_Item = item.IsPromoItem;
                                dbItem.Item_Category_ID = item.CategoryID;
                                dbItem.Item_Category_Name = item.CategoryName;
                                dbItem.Currency = item.Currency;
                                dbItem.Country_Code = item.CountryCode;
                                dbItem.LocatedIn = item.CountryShortCode;
                                dbItem.Current_Sales = item.CurrentSales;
                                dbItem.QuantityAvailable = item.QuantityAvailable;
                                context.SellerItems.AddObject(dbItem);
                            }
                        }
                        else
                        {

                            if (item.EndDate.Date > today.Date)
                            {
                                sellerItem.User_Account_Code = account.Key;
                                sellerItem.BIN_Price = (decimal)item.BinPrice;
                                sellerItem.Current_Price = (decimal)item.CurrentPrice;
                                sellerItem.End_Date = item.EndDate;
                                sellerItem.Item_Name = item.ItemName;
                                sellerItem.Start_Date = item.StartDate;
                                sellerItem.Is_Promo_Item = item.IsPromoItem;
                                sellerItem.Picture_URL = item.PictureURL;
                                sellerItem.Item_View_URL = item.ItemViewURL;
                                sellerItem.Item_Category_ID = item.CategoryID;
                                sellerItem.Item_Category_Name = item.CategoryName;
                                if (item.IsPromoItem == true && sellerItem.Is_Automated == true)
                                    sellerItem.Is_Automated = false;
                                sellerItem.Currency = item.Currency;
                                sellerItem.Country_Code = item.CountryCode;
                                sellerItem.Current_Sales = item.CurrentSales;
                                sellerItem.QuantityAvailable = item.QuantityAvailable;

                                ItemTitle currentTitle = context.ItemTitles.FirstOrDefault(f => f.ItemId == sellerItem.Item_Code && (f.Is_Current == true || f.Title.ToLower() == sellerItem.Item_Name.ToLower()));

                                if (currentTitle != null)
                                {
                                    if(context.TitleHistories.Any(a => a.Item_Code == sellerItem.Item_Code))
                                    {
                                       var totalSales = context.GetSalesForOldTitles(sellerItem.Item_Code, sellerItem.Item_Name).FirstOrDefault();
                                       currentTitle.TotalSales = totalSales.TotalSales;

                                    }
                                }

                            }
                            else
                            {
                                sellerItem.Is_Automated = false;
                                sellerItem.Is_Active = false;
                            }

                        }

                        if (1 == 1)
                        {
                            Decimal weightMajor = item.Weight != null ? Convert.ToDecimal(item.Weight) * 1000 : 0; //Weight Major comes in Kgs so converted in to gms
                            Decimal weightMinor = item.Weight != null ? Convert.ToDecimal(item.WeightMinor) : 0; //Weight Minor comes in gms
                            Decimal weight = (weightMajor + weightMinor) / 1000; // Added both major and minor and converted into kgs

                            Item partmaster = context.Items.FirstOrDefault(a => a.Item_ID == item.ItemID && a.UserCode == UserCode && a.Country_Code == service.countryID);
                            if (partmaster == null)
                            {
                                partmaster = new Item();
                                partmaster.Item_ID = item.ItemID;
                                partmaster.CustomLabel = item.CustomLabel;
                                partmaster.Description = item.ItemName;
                                partmaster.UserCode = UserCode;
                                partmaster.User_Account_Code = account.Key;
                                partmaster.Height = item.Height != null ? item.Height.ToString() : string.Empty;
                                partmaster.Length = item.Length != null ? item.Length.ToString() : string.Empty;
                                partmaster.Width = item.Width != null ? item.Width.ToString() : string.Empty;
                                partmaster.Weight = item.Weight != null || item.WeightMinor != null ? weight.ToString("0.00") : string.Empty;
                                partmaster.Balance_Quantity = item.Quantity;
                                partmaster.Current_Price = Convert.ToDecimal(item.CurrentPrice);
                                partmaster.BIN_Price = Convert.ToDecimal(item.BinPrice);
                                partmaster.Picture_URL = item.PictureURL;
                                partmaster.Item_View_URL = item.ItemViewURL;
                                partmaster.Item_Category_ID = item.CategoryID;
                                partmaster.Item_Category_Name = item.CategoryName;
                                partmaster.Start_Date = item.StartDate;
                                partmaster.End_Date = item.EndDate;
                                partmaster.User_Account_Code = account.Key;
                                partmaster.Detail_Description = item.Discription;
                                partmaster.Country_Code = item.CountryCode;
                                context.AddToItems(partmaster);
                            }
                            else
                            {
                                partmaster.CustomLabel = item.CustomLabel;
                                partmaster.Description = item.ItemName;
                                partmaster.UserCode = UserCode;
                                partmaster.User_Account_Code = account.Key;

                                if (string.IsNullOrEmpty(partmaster.Height))
                                    partmaster.Height = item.Height != null ? item.Height.ToString() : string.Empty;

                                if (string.IsNullOrEmpty(partmaster.Length))
                                    partmaster.Length = item.Length != null ? item.Length.ToString() : string.Empty;

                                if (string.IsNullOrEmpty(partmaster.Width))
                                    partmaster.Width = item.Width != null ? item.Width.ToString() : string.Empty;

                                if (string.IsNullOrEmpty(partmaster.Weight))
                                    partmaster.Weight = item.Weight != null || item.WeightMinor != null ? weight.ToString("0.00") : string.Empty;

                                partmaster.Height = item.Height != null ? item.Height.ToString() : string.Empty;
                                partmaster.Length = item.Length != null ? item.Length.ToString() : string.Empty;
                                partmaster.Width = item.Width != null ? item.Width.ToString() : string.Empty;
                                partmaster.Weight = item.Weight != null || item.WeightMinor != null ? weight.ToString("0.00") : string.Empty;
                                partmaster.Current_Price = Convert.ToDecimal(item.CurrentPrice);
                                partmaster.BIN_Price = Convert.ToDecimal(item.BinPrice);
                                partmaster.Picture_URL = item.PictureURL;
                                partmaster.Item_View_URL = item.ItemViewURL;
                                partmaster.Item_Category_ID = item.CategoryID;
                                partmaster.Item_Category_Name = item.CategoryName;
                                partmaster.Start_Date = item.StartDate;
                                partmaster.User_Account_Code = account.Key;
                                partmaster.End_Date = item.EndDate;
                                partmaster.Balance_Quantity = item.Quantity;
                                partmaster.Country_Code = item.CountryCode;

                            }
                        }
                        context.SaveChanges();
                    }
                    //context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Logging.WriteLog(LogType.Critical, ex.ToString());
                }

            }

        }
    }