Exemplo n.º 1
0
        public ActionResult CreateProductCompetitorLedger(int?ProductCompetitorID, int ProductCompareSourceID, string InTaxPrice, string IncludeShippingCost, string Competitor, int ProductID, long Price, string Stock)
        {
            //create ProductCompetitorMapping first and use specified sourceID for creating ProductCompetitorPrice
            //use that ID for creating the Ledger



            using (var unit = GetUnitOfWork())
            {
                try
                {
                    var newMapping = new ProductCompetitorMapping()
                    {
                        ProductCompareSourceID = ProductCompareSourceID,
                        InTaxPrice             = InTaxPrice == "on" ? true : false,
                        IncludeShippingCost    = IncludeShippingCost == "on" ? true : false,
                        Competitor             = Competitor,
                        ProductCompetitorID    = ProductCompetitorID.HasValue ? ProductCompetitorID : null
                    };
                    unit.Service <ProductCompetitorMapping>().Create(newMapping);

                    var newCompetitorPrice = new ProductCompetitorPrice()
                    {
                        CreationTime             = DateTime.Now.ToUniversalTime(),
                        ProductCompetitorMapping = newMapping,
                        ProductID  = ProductID,
                        Price      = Price,
                        Stock      = Stock.ToString(),
                        LastImport = DateTime.Now.ToUniversalTime()
                    };
                    unit.Service <ProductCompetitorPrice>().Create(newCompetitorPrice);

                    var newLedger = new ProductCompetitorLedger()
                    {
                        CreationTime           = DateTime.Now.ToUniversalTime(),
                        ProductCompetitorPrice = newCompetitorPrice,
                        Stock     = Stock.ToString(),
                        Price     = Price,
                        CreatedBy = Concentrator.Objects.Web.Client.User.UserID
                    };
                    unit.Service <ProductCompetitorLedger>().Create(newLedger);

                    unit.Save();

                    return(Success("Sucessfully created ProductCompetitorLedger"));
                }
                catch (Exception ex)
                {
                    return(Failure("Failed to create ProductCompetitorLedger", ex));
                }
            }
        }
Exemplo n.º 2
0
        protected ProductCompetitorPrice SyncCompetitorPrice(string competitor, string price, int CompareProductID, IUnitOfWork unit, int productCompareSourceID)
        {
            competitor = competitor.Replace("\"", string.Empty).Trim();
            //competitor
            ProductCompetitorMapping pc = (from c in ProductCompetitors
                                           where c.Competitor.Trim() == competitor.Trim()
                                           select c).FirstOrDefault();

            if (pc == null)
            {
                pc = new ProductCompetitorMapping()
                {
                    Competitor = competitor.Trim(), ProductCompareSourceID = productCompareSourceID, ProductCompetitorMappingID = -1
                };
                unit.Scope.Repository <ProductCompetitorMapping>().Add(pc);
                unit.Save();
                ProductCompetitors.Add(pc);
            }
            //check price
            unit.Save();
            ProductCompetitorPrice pcp = (from c in unit.Scope.Repository <ProductCompetitorPrice>().GetAll()
                                          where
                                          c.CompareProductID == CompareProductID &&
                                          c.ProductCompetitorMapping.Competitor == competitor.Trim()
                                          select c).FirstOrDefault();

            unit.Save();
            if (pcp == null)
            {
                string VendorItemNumber = unit.Scope.Repository <ProductCompare>().GetSingle(c => c.CompareProductID == CompareProductID).VendorItemNumber;
                var    Product          = unit.Scope.Repository <Product>().GetSingle(p => p.VendorItemNumber == VendorItemNumber);
                int    productID;
                if (Product != null)
                {
                    productID = Product.ProductID;
                }
                else
                {
                    Product                  = new Product();
                    Product.BrandID          = -1;
                    Product.VendorItemNumber = VendorItemNumber;
                    unit.Scope.Repository <Product>().Add(Product);
                    unit.Save();
                    productID = Product.ProductID;
                }
                pcp = new ProductCompetitorPrice()
                {
                    ProductCompetitorMappingID = pc.ProductCompetitorMappingID,
                    CompareProductID           = CompareProductID,
                    Price      = decimal.Parse(price, NumberStyles.Any, Cultures),
                    LastImport = DateTime.Now,
                    ProductID  = productID
                };
                unit.Scope.Repository <ProductCompetitorPrice>().Add(pcp);
                unit.Save();
            }
            else
            {
                if (pcp.Price != decimal.Parse(price, NumberStyles.Any, Cultures))
                {
                    pcp = unit.Scope.Repository <ProductCompetitorPrice>().GetSingle(c => c.CompareProductID == CompareProductID &&
                                                                                     c.ProductCompetitorMapping.Competitor == competitor.Trim());

                    pcp.Price = decimal.Parse(price, NumberStyles.Any, Cultures);

                    unit.Save();
                }
            }
            ((IFunctionScope)unit.Scope).Repository().UpdateProductCompetitorPrice(pcp.CompareProductID.Value, pcp.ProductCompetitorMappingID);

            return(pcp);
        }
Exemplo n.º 3
0
        public SlurpResult Process()
        {
            log.InfoFormat("Start task for {0}", PluginType.Name);


            using (var unit = ServiceLocator.Current.GetInstance <IUnitOfWork>())
            {
                var QueueItem = unit.Scope.Repository <SlurpQueue>().GetSingle(x => x.QueueID == QueueID);
                QueueItem.StartTime = DateTime.Now;

                ProductID = QueueItem.ProductID;

                SlurpResult result = new SlurpResult(QueueItem.Product.VendorItemNumber);
                try
                {
                    var plugin = (ISlurpSite)Activator.CreateInstance(PluginType);

                    result = plugin.Process(QueueItem.Product.VendorItemNumber);

                    result.SiteName = PluginType.Name;

                    #region Update Results

                    if (result.ProductStatus == ProductStatus.Valid)
                    {
                        var mappingRepo = unit.Scope.Repository <ProductCompetitorMapping>();
                        var priceRepo   = unit.Scope.Repository <ProductCompetitorPrice>();

                        var mappings = mappingRepo.GetAll(x => x.ProductCompareSourceID == QueueItem.ProductCompareSourceID).ToList();
                        var prices   = priceRepo.GetAll(x => x.ProductCompetitorMapping.ProductCompareSourceID == QueueItem.ProductCompareSourceID).ToList();


                        foreach (var shop in result.Shops)
                        {
                            var mapping = mappings.SingleOrDefault(c => c.Competitor == shop.Name.Trim());
                            if (mapping == null)
                            {
                                //new
                                mapping = new ProductCompetitorMapping()
                                {
                                    Competitor = shop.Name.Trim(), ProductCompareSourceID = QueueItem.ProductCompareSourceID, ProductCompetitorID = null
                                };

                                mappingRepo.Add(mapping);
                                //                context.SubmitChanges();

                                mappings.Add(mapping);
                            }



                            var newPrice  = shop.TotalPrice ?? 0;
                            var stockInfo = shop.Delivery.ToString();

                            //price insert

                            ProductCompetitorPrice price = prices.SingleOrDefault(p => p.ProductCompetitorMappingID == mapping.ProductCompetitorMappingID &&
                                                                                  p.ProductID == QueueItem.ProductID);
                            if (price == null)
                            {
                                price = new ProductCompetitorPrice()
                                {
                                    ProductCompetitorMapping = mapping,
                                    ProductID    = QueueItem.ProductID,
                                    Price        = newPrice,
                                    Stock        = stockInfo,
                                    CreationTime = DateTime.Now,
                                    LastImport   = DateTime.Now.AddDays(-30)
                                };
                                priceRepo.Add(price);
                            }
                            else
                            {
                                //ledger

                                //check for diff
                                if (price.Price != newPrice || price.Stock != stockInfo)
                                {
                                    ProductCompetitorLedger ledger = new ProductCompetitorLedger()
                                    {
                                        Price = price.Price,
                                        Stock = price.Stock,
                                        ProductCompetitorPriceID = price.ProductCompetitorPriceID,
                                        CreationTime             = DateTime.Now
                                    };

                                    price.Price = newPrice;
                                    price.Stock = shop.Delivery.ToString();

                                    unit.Scope.Repository <ProductCompetitorLedger>().Add(ledger);
                                }
                            }
                        }

                        CompleteItem(unit, QueueItem);
                        //unit.Save();
                    }
                    else
                    {
                        CompleteItem(unit, QueueItem);
                    }

                    #endregion


                    log.DebugFormat("Sleeping for {0} seconds.", SleepTime);
                    Thread.Sleep(SleepTime * 1000);
                }
                catch (Exception ex)
                {
                    log.Error("Error during processing", ex);
                    result          = new SlurpResult(QueueItem.Product.VendorItemNumber);
                    result.SiteName = PluginType.Name;

                    CompleteItem(unit, QueueItem);
                }
                finally
                {
                    WriteLog(result);
                }



                return(result);
            }
        }