Exemplo n.º 1
0
 public static bool AccessTest(ProductShop productShop, ModelGeneralAttribute attr)
 {
     if (attr.Acl)
     {
         if (LS.CurrentShop != null)
         {
             return(productShop.ShopID == LS.CurrentShop.ID);
         }
         return(false);
     }
     ;
     return(true);
 }
        public static ProductSkuMap OnUpdating(ProductSkuMap item)
        {
            if (!string.IsNullOrEmpty(item.ProductSKU))
            {
                //check if shop product in DB

                var sid               = item.ShopID;
                var sku               = item.ProductSKU;
                var product           = LS.CurrentEntityContext.Products.AsNoTracking().FirstOrDefault(x => x.SKU == sku);
                int productCategoryID = 0;
                if (product != null)
                {
                    var pid         = product.ID;
                    var productShop = LS.CurrentEntityContext.ProductShopMap.AsNoTracking().FirstOrDefault(x => x.ProductID == pid && x.ShopID == sid);


                    if (productShop == null)
                    {
                        //import
                        DeleteOldProductShop(item.ID, item.ShopID);
                        var newProductShop = new ProductShop();
                        newProductShop.CreateDate = DateTime.Now;
                        newProductShop.Price      = item.Price;
                        if (product.UnitsPerPackage.HasValue && product.UnitsPerPackage.Value > 0)
                        {
                            newProductShop.PriceByUnit = newProductShop.Price / product.UnitsPerPackage.Value;
                        }
                        newProductShop.ProductID    = product.ID;
                        newProductShop.Quantity     = item.Quantity;
                        newProductShop.QuantityType = ProductQuantityType.NotCheck;
                        newProductShop.ShopID       = sid;


                        bool needAddCategory = true;
                        productCategoryID = product.CategoryID;
                        //category check
                        if (productCategoryID > 0)
                        {
                            var shopCategories = LS.CurrentEntityContext.ShopCategories.AsNoTracking().Where(x => x.ShopID == sid);
                            var categories     = LS.CurrentEntityContext.Categories.AsNoTracking();

                            if (!shopCategories.Any(x => x.CategoryID == productCategoryID))
                            {
                                //create and add
                                var shopCat = new ShopCategory()
                                {
                                    CategoryID   = productCategoryID,
                                    DisplayOrder = 1000,
                                    Published    = true,
                                    ShopID       = sid
                                };
                                LS.CurrentEntityContext.ShopCategories.Add(shopCat);
                                //check if parent cat in shop map
                                var cat = categories.FirstOrDefault(x => x.ID == productCategoryID);
                                if (cat != null && cat.ParentCategoryID > 0)
                                {
                                    int parentCategoryID = cat.ParentCategoryID;
                                    if (!shopCategories.Any(x => x.CategoryID == parentCategoryID))
                                    {
                                        //create and add
                                        var shopCatParent = new ShopCategory()
                                        {
                                            CategoryID   = parentCategoryID,
                                            DisplayOrder = 1000,
                                            Published    = true,
                                            ShopID       = sid
                                        };
                                        LS.CurrentEntityContext.ShopCategories.Add(shopCatParent);
                                    }
                                }
                            }
                            else
                            {
                                newProductShop.NotInCategory = true;
                                if (needAddCategory)//run only one time
                                {
                                    var otherCategory = categories.FirstOrDefault(x => x.Name == "שונות" && x.ParentCategoryID == 0);
                                    if (otherCategory == null)
                                    {
                                        otherCategory = new Category()
                                        {
                                            DisplayOrder = 1000000,
                                            Name         = "שונות",
                                            Published    = true,
                                        };
                                        LS.CurrentEntityContext.Categories.Add(otherCategory);
                                    }
                                    var catshopmap = shopCategories.FirstOrDefault(x => x.CategoryID == otherCategory.ID);
                                    if (catshopmap == null)
                                    {
                                        var otherShopCategory = new ShopCategory()
                                        {
                                            CategoryID   = otherCategory.ID,
                                            DisplayOrder = 1000000,
                                            Published    = true,
                                            ShopID       = sid
                                        };
                                        LS.CurrentEntityContext.ShopCategories.Add(otherShopCategory);
                                    }
                                    else
                                    {
                                        if (!catshopmap.Published)
                                        {
                                            var catshopMapAttached = LS.CurrentEntityContext.ShopCategories.FirstOrDefault(x => x.ID == catshopmap.ID);
                                            if (catshopMapAttached != null)
                                            {
                                                catshopMapAttached.Published = true;
                                            }
                                        }
                                    }
                                    needAddCategory = false;
                                }
                            }
                        }



                        LS.CurrentEntityContext.ProductShopMap.Add(newProductShop);
                        item.Imported = true;
                    }
                    else
                    {
                        item.Imported = true;
                    }
                }
            }
            //delete product from shop
            if (string.IsNullOrEmpty(item.ProductSKU))
            {
                item.Imported = false;
                DeleteOldProductShop(item.ID, item.ShopID);
            }
            return(item);
        }