Beispiel #1
0
        /// <summary>
        /// Update supplier products
        /// </summary>
        /// <param name="product_ids"></param>
        /// <param name="supplier_id"></param>
        public void UpdateSupplierProducts(int[] product_ids, int supplier_id)
        {
            if (supplier_id <= 0)
            {
                throw new KMJXCException("更新供应商产品时必须输入供应商编号");
            }

            if (this.CurrentUserPermission.UPDATE_SUPPLIER_PRODUCT == 0)
            {
                throw new KMJXCException("没有权限更新供应商产品");
            }

            using (KuanMaiEntities db = new KuanMaiEntities())
            {
                Supplier supplier = (from s in db.Supplier where s.Supplier_ID == supplier_id select s).FirstOrDefault<Supplier>();
                if (supplier == null)
                {
                    throw new KMJXCException("编号为:" + supplier_id + " 的供应商信息不存在");
                }

                if (this.Shop.Shop_ID == this.Main_Shop.Shop_ID)
                {
                    int[] child_shops = (from c in this.ChildShops select c.ID).ToArray<int>();
                    if (this.Shop.Shop_ID != supplier.Shop_ID && !child_shops.Contains(supplier.Shop_ID))
                    {
                        throw new KMJXCException("不能操作其他店铺的供应商,只能使用主店铺和子店铺的供应商");
                    }
                }
                else
                {
                    if (supplier.Shop_ID != this.Shop.Shop_ID && supplier.Shop_ID != this.Main_Shop.Shop_ID)
                    {
                        throw new KMJXCException("不能操作其他店铺的供应商,只能使用主店铺和子店铺的供应商");
                    }
                }

                List<Product_Supplier> products = (from s in db.Product_Supplier where s.Supplier_ID == supplier_id select s).ToList<Product_Supplier>();
                if(product_ids==null || product_ids.Length<=0)
                {
                    foreach (Product_Supplier p in products)
                    {
                        p.Enabled = false;
                    }

                    db.SaveChanges();
                }else
                {
                    for (int i = 0; i < product_ids.Length; i++)
                    {
                        Product_Supplier ps=(from s in products where s.Product_ID==product_ids[i] select s).FirstOrDefault<Product_Supplier>();
                        if (ps == null)
                        {
                            ps = new Product_Supplier();
                            ps.Supplier_ID = supplier_id;
                            ps.Product_ID = product_ids[i];
                            ps.Enabled = true;
                            ps.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                            ps.Created_By = this.CurrentUser.ID;
                            db.Product_Supplier.Add(ps);
                        }
                        else
                        {
                            ps.Enabled = true;
                        }
                    }

                    db.SaveChanges();
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Update product information
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public bool UpdateProduct(ref BProduct bproduct)
        {
            BProduct product = bproduct;
            if (this.CurrentUserPermission.UPDATE_PRODUCT == 0)
            {
                throw new KMJXCException("没有权限更新产品");
            }

            bool result = false;
            KuanMaiEntities db = new KuanMaiEntities();
            try
            {
                Product dbProduct=(from pdt in db.Product where pdt.Product_ID==product.ID select pdt).FirstOrDefault<Product>();
                if (dbProduct == null)
                {
                    throw new KMJXCException("您要修改的产品信息不存在");
                }

                if (this.Shop.Shop_ID != this.Main_Shop.Shop_ID)
                {
                    if (dbProduct.Shop_ID == this.Main_Shop.Shop_ID)
                    {
                        throw new KMJXCException("您不能修改主店铺产品");
                    }

                    if (dbProduct.Shop_ID != this.Shop.Shop_ID)
                    {
                        throw new KMJXCException("您不能其他主店铺产品");
                    }
                }
                else
                {
                    int[] child_shops=(from c in this.DBChildShops select c.Shop_ID).ToArray<int>();
                    if (dbProduct.Shop_ID != this.Main_Shop.Shop_ID && !child_shops.Contains(dbProduct.Shop_ID))
                    {
                        throw new KMJXCException("您无法修改其他店铺的产品,只能修改主店铺或者子店铺产品");
                    }
                }

                dbProduct.Name = product.Title;
                dbProduct.Description = product.Description;
                if (product.Category != null)
                {
                    dbProduct.Product_Class_ID = product.Category.ID;
                }
                dbProduct.Update_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                dbProduct.Update_User_ID = this.CurrentUser.ID;

                //update images
                if (product.Images!=null && product.Images.Count > 0)
                {
                    List<Image> existedImages=(from img in db.Image where img.ProductID==product.ID select img).ToList<Image>();
                    //Update new uploaded images
                    foreach (Image newimg in product.Images)
                    {
                        Image tmp = (from eted in existedImages where eted.ID == newimg.ID select eted).FirstOrDefault<Image>();
                        if (tmp == null)
                        {
                            Image newone = (from ni in db.Image where ni.ID == newimg.ID select ni).FirstOrDefault<Image>();
                            newone.ProductID = product.ID;
                            db.Image.Add(newone);
                        }
                    }

                    //Remove deleted images
                    string rootPath = product.FileRootPath;
                    foreach (Image oldImg in existedImages)
                    {

                        Image tmp = (from eted in product.Images where eted.ID == oldImg.ID select eted).FirstOrDefault<Image>();
                        if (tmp == null)
                        {
                            db.Image.Remove(oldImg);
                            if (rootPath != null && System.IO.File.Exists(rootPath + oldImg.Path))
                            {
                                System.IO.File.Delete(rootPath + oldImg.Path);
                            }
                        }
                    }
                }

                //update suppliers
                if (product.Suppliers != null)
                {
                    foreach (Supplier s in product.Suppliers)
                    {
                        Product_Supplier ps = new Product_Supplier() { Product_ID = product.ID, Supplier_ID = s.Supplier_ID, Enabled=true,Created=DateTimeUtil.ConvertDateTimeToInt(DateTime.Now),Created_By=this.CurrentUser.ID };
                        db.Product_Supplier.Add(ps);
                    }
                }

                //update children
                List<Product> children=(from p in db.Product where p.Parent_ID==dbProduct.Product_ID select p).ToList<Product>();
                foreach (Product child in children)
                {
                    child.Name = dbProduct.Name;
                    child.Product_Class_ID = dbProduct.Product_Class_ID;
                    child.Description = dbProduct.Description;
                }

                db.SaveChanges();

                if (product.Children != null && product.Children.Count > 0)
                {
                    foreach (BProduct child in product.Children)
                    {
                        if (child.ID == 0)
                        {
                            //create new child product with properties
                            child.Parent = product;
                            child.Children = null;
                            this.CreateProduct(child);
                        }
                        else
                        {
                            //Update properties
                            if (child.Properties != null && child.Properties.Count > 0)
                            {
                                List<Product_Specifications> properties = (from prop in db.Product_Specifications
                                                                           where prop.Product_ID == child.ID
                                                                           select prop).ToList<Product_Specifications>();

                                List<Product_Specifications> newProps = new List<Product_Specifications>();
                                if (properties.Count > 0)
                                {
                                    //current just support edit existed property's value, doesn't support deleting property
                                    foreach (BProductProperty p in child.Properties)
                                    {
                                        Product_Specifications psprop = (from ep in properties where ep.Product_Spec_ID == p.PID  select ep).FirstOrDefault<Product_Specifications>();
                                        if (psprop == null)
                                        {
                                            //cretae new property for existed product
                                            psprop = new Product_Specifications() { Product_ID=child.ID, Product_Spec_ID=p.PID, Product_Spec_Value_ID=p.PVID, Created=DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), User_ID=this.CurrentUser.ID };
                                            db.Product_Specifications.Add(psprop);
                                        }
                                        else
                                        {
                                            //update existed property's value
                                            if (psprop.Product_Spec_Value_ID != p.PVID)
                                            {
                                                psprop.Product_Spec_Value_ID = p.PVID;
                                            }
                                        }
                                    }

                                    db.SaveChanges();
                                }
                            }
                        }
                    }
                }

                bproduct = this.GetProductFullInfo(product.ID);

                base.CreateActionLog(new BUserActionLog() { Shop = new BShop { ID = bproduct.Shop.Shop_ID }, Action = new BUserAction() { Action_ID = UserLogAction.UPDATE_PRODUCT }, Description = "商品编号:" + bproduct.ID+ "\n商品名称:" + bproduct.Title });
                result = true;

            }
            catch (KMJXCException kex)
            {
                throw kex;
            }
            catch
            {
            }
            finally
            {
                db.Dispose();
            }
            return result;
        }
Beispiel #3
0
        /// <summary>
        /// Set product supplier
        /// </summary>
        /// <param name="product_id">Product ID</param>
        /// <param name="supplier_id">Supplier ID</param>
        /// <returns>TRUE/FALSE</returns>
        public bool SetProductSupplier(int product_id, int supplier_id)
        {
            bool result = false;

            if (this.CurrentUserPermission.ADD_SUPPLIER == 0)
            {
                throw new KMJXCException("没有创建供应商的权限");
            }

            if (product_id == 0 || supplier_id == 0)
            {
                throw new KMJXCException("创建产品供应商时,产品和供应商都必须选择");
            }

            using (KuanMaiEntities db = new KuanMaiEntities())
            {
                Product_Supplier pps = (from p in db.Product_Supplier where p.Product_ID == product_id && p.Supplier_ID == supplier_id select p).FirstOrDefault<Product_Supplier>();
                if (pps != null)
                {
                    throw new KMJXCException("");
                }
                Product_Supplier ps = new Product_Supplier();
                ps.Product_ID = product_id;
                ps.Supplier_ID = supplier_id;
                db.Product_Supplier.Add(ps);
                db.SaveChanges();
                result = true;
            }
            return result;
        }
Beispiel #4
0
        /// <summary>
        /// Create single parent or child product which can contains Properties combination
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public void CreateProduct(BProduct product)
        {
            if (this.CurrentUserPermission.ADD_PRODUCT == 0)
            {
                throw new KMJXCException("没有权限创建产品");
            }

            Product dbProduct = new Product();
            dbProduct.Code = product.Code;
            dbProduct.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
            dbProduct.Name = product.Title;
            dbProduct.Parent_ID = product.ParentID;
            dbProduct.Quantity = 0;
            dbProduct.Description = product.Description;
            if (product.Category != null)
            {
                dbProduct.Product_Class_ID = product.Category.ID;
            }
            dbProduct.Product_ID = 0;
            dbProduct.Price = product.Price;
            if (product.Unit != null)
            {
                dbProduct.Product_Unit_ID = product.Unit.Product_Unit_ID;
            }
            dbProduct.Shop_ID = this.Shop.Shop_ID;
            dbProduct.User_ID = this.MainUser.ID;

            if (product.Parent != null && product.Parent.ID > 0)
            {
                dbProduct.Parent_ID = product.Parent.ID;
            }

            using (KuanMaiEntities db = new KuanMaiEntities())
            {
                db.Product.Add(dbProduct);
                db.SaveChanges();
                if (dbProduct.Product_ID <= 0)
                {
                    throw new KMJXCException("产品创建失败");
                }
                product.ID = dbProduct.Product_ID;
                //Update product images
                if (product.Images != null && product.Images.Count > 0)
                {
                    List<int> img_ids = new List<int>();
                    foreach (Image img in product.Images)
                    {
                        img_ids.Add(img.ID);
                    }

                    List<Image> dbImages=(from img in db.Image where img_ids.Contains(img.ID) select img).ToList<Image>();
                    foreach (Image image in dbImages)
                    {
                        image.ProductID = product.ID;
                    }

                    db.SaveChanges();
                }
                Stock_Batch batch = null;
                if (dbProduct.Parent_ID == 0)
                {
                    batch = new Stock_Batch() { Name = "P0", ProductID = dbProduct.Product_ID, Price = 0, ShopID = dbProduct.Shop_ID, Desc = "", Created_By = this.CurrentUser.ID };
                    batch.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    batch.ParentProductID = dbProduct.Parent_ID;
                    db.Stock_Batch.Add(batch);
                    db.SaveChanges();
                }
                else
                {
                    batch=(from b in db.Stock_Batch where b.ProductID==dbProduct.Parent_ID select b).FirstOrDefault<Stock_Batch>();
                }

                Store_House defaultStoreHouse = null;
                List<Store_House> storeHouses = (from h in db.Store_House where (h.Shop_ID == dbProduct.Shop_ID || h.Shop_ID==this.Main_Shop.Shop_ID) select h).ToList<Store_House>();

                if (storeHouses.Count == 0)
                {
                    defaultStoreHouse = new Store_House();
                    defaultStoreHouse.Shop_ID = dbProduct.Shop_ID;
                    defaultStoreHouse.Title = "默认仓库";
                    defaultStoreHouse.Address = "";
                    defaultStoreHouse.Phone = "";
                    defaultStoreHouse.User_ID = this.CurrentUser.ID;
                    defaultStoreHouse.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    defaultStoreHouse.Default = true;
                    defaultStoreHouse.Guard = 0;
                    db.Store_House.Add(defaultStoreHouse);
                    db.SaveChanges();
                }
                else
                {
                    defaultStoreHouse = (from h in storeHouses where h.Default == true select h).FirstOrDefault<Store_House>();
                    if (defaultStoreHouse == null)
                    {
                        defaultStoreHouse = storeHouses[0];
                        defaultStoreHouse.Default = true;
                    }
                }

                Stock_Pile stockPile = new Stock_Pile();
                stockPile.LastLeave_Time = 0;
                stockPile.Price = 0;
                stockPile.Product_ID = product.ID;
                stockPile.Quantity = 0;
                stockPile.Shop_ID = dbProduct.Shop_ID;
                stockPile.StockHouse_ID = defaultStoreHouse.StoreHouse_ID;
                stockPile.StockPile_ID = 0;
                stockPile.Batch_ID = batch.ID;

                db.Stock_Pile.Add(stockPile);

                if (product.Properties != null && product.Properties.Count > 0)
                {
                    foreach (BProductProperty pro in product.Properties)
                    {
                        Product_Specifications ps = new Product_Specifications();
                        ps.Product_ID = dbProduct.Product_ID;
                        ps.Product_Spec_ID = pro.PID;
                        ps.Product_Spec_Value_ID = pro.PVID;
                        ps.User_ID = this.CurrentUser.ID;
                        ps.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                        db.Product_Specifications.Add(ps);
                    }

                    //db.SaveChanges();
                }

                if (product.Suppliers != null)
                {
                    foreach (Supplier su in product.Suppliers)
                    {
                        Product_Supplier ps = new Product_Supplier();
                        ps.Product_ID = product.ID;
                        ps.Supplier_ID = su.Supplier_ID;
                        db.Product_Supplier.Add(ps);
                    }

                    //db.SaveChanges();
                }

                if (product.Children != null)
                {
                    foreach (BProduct p in product.Children)
                    {
                        if (p.Parent == null)
                        {
                            p.ParentID = dbProduct.Product_ID;
                            p.Parent = new BProduct() { ID = product.ID };
                        }
                        p.Children = null;
                        this.CreateProduct(p);
                    }
                }
                else
                {
                    base.CreateActionLog(new BUserActionLog() { Shop = new BShop { ID = dbProduct.Shop_ID }, Action = new BUserAction() { Action_ID = UserLogAction.CREATE_PRODUCT }, Description = "" });
                }

                db.SaveChanges();
            }
        }