/// <summary> /// /// </summary> /// <param name="product"></param> /// <param name="props"></param> /// <returns></returns> private bool CreateProperties(Product product, List<BProductProperty> props) { if (this.CurrentUserPermission.UPDATE_PRODUCT == 0) { throw new KMJXCException("没有权限更新产品"); } if (product == null || product.Product_ID <= 0) { throw new KMJXCException("产品不存在"); } bool result = false; Product newProduct = new Product(); newProduct.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); newProduct.Code = product.Code; newProduct.Name = product.Name; newProduct.Parent_ID = product.Parent_ID; newProduct.Price = product.Price; newProduct.Product_Class_ID = product.Product_Class_ID; newProduct.Product_ID = 0; newProduct.Product_Unit_ID = product.Product_Unit_ID; newProduct.Shop_ID = product.Shop_ID; newProduct.User_ID = this.CurrentUser.ID; using (KuanMaiEntities db = new KuanMaiEntities()) { db.Product.Add(newProduct); db.SaveChanges(); if (newProduct.Product_ID <= 0) { throw new KMJXCException("创建属性失败"); } Stock_Pile stockPile = new Stock_Pile(); stockPile.LastLeave_Time = 0; stockPile.Price = 0; stockPile.Product_ID = newProduct.Product_ID; stockPile.Quantity = 0; stockPile.Shop_ID = this.Shop.Shop_ID; stockPile.StockHouse_ID = 0; stockPile.StockPile_ID = 0; this.stockManager.CreateDefaultStockPile(stockPile); if (props != null) { foreach (BProductProperty p in props) { Product_Specifications ps = new Product_Specifications(); ps.User_ID = this.CurrentUser.ID; ps.Created = (int)newProduct.Create_Time; ps.Product_ID = newProduct.Product_ID; ps.Product_Spec_ID = p.PID; ps.Product_Spec_Value_ID = p.PVID; db.Product_Specifications.Add(ps); } db.SaveChanges(); } result = true; } return result; }
/// <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(); } }
/// <summary> /// Add multiple stock detail records /// </summary> /// <param name="stock"></param> /// <returns></returns> public bool CreateEnterStockDetails(Enter_Stock dbstock,List<BEnterStockDetail> details,bool updateStock=false) { bool result = false; if (this.CurrentUserPermission.ADD_ENTER_STOCK == 0) { throw new KMJXCException("没有新增入库单产品信息的权限"); } if (dbstock.Enter_Stock_ID <= 0) { throw new KMJXCException(""); } if (details == null) { throw new KMJXCException("输入错误",ExceptionLevel.SYSTEM); } KuanMaiEntities db = new KuanMaiEntities(); List<Stock_Pile> stockPiles = (from sp in db.Stock_Pile where sp.Shop_ID == dbstock.Shop_ID select sp).ToList<Stock_Pile>(); int totalQuantity = 0; List<Stock_Batch> batches=(from sb in db.Stock_Batch where sb.ShopID==dbstock.Shop_ID select sb ).ToList<Stock_Batch>(); foreach (BEnterStockDetail detail in details) { Product tmp = (from p in db.Product where p.Product_ID == detail.Product.ID select p).FirstOrDefault<Product>(); if (tmp == null) { continue; } Enter_Stock_Detail dbDetail = new Enter_Stock_Detail(); dbDetail.Create_Date = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbDetail.Enter_Stock_ID = dbstock.Enter_Stock_ID; dbDetail.Have_Invoice = detail.Invoiced; dbDetail.Invoice_Amount = detail.InvoiceAmount; dbDetail.Invoice_Num = detail.InvoiceNumber; dbDetail.Price = detail.Price; dbDetail.Product_ID = detail.Product.ID; dbDetail.Quantity = (int)detail.Quantity; totalQuantity += dbDetail.Quantity; int parentProductId = dbDetail.Product_ID; if (tmp.Parent_ID > 0) { parentProductId = tmp.Parent_ID; } dbDetail.Parent_Product_ID = parentProductId; List<Stock_Batch> pBatches = (from b in batches where b.ProductID == parentProductId select b).ToList<Stock_Batch>(); Stock_Batch batch = (from b in pBatches where b.Price==dbDetail.Price select b).FirstOrDefault<Stock_Batch>(); if (batch == null) { if (pBatches.Count == 1 && pBatches[0].Price == 0) { batch = pBatches[0]; batch.Price = dbDetail.Price; batch.Name = "P" + batch.Price.ToString("0.00"); } else { batch = new Stock_Batch() { ProductID = parentProductId, ParentProductID = 0, Price = dbDetail.Price, ShopID = dbstock.Shop_ID, Desc = "" }; batch.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); batch.Created_By = this.CurrentUser.ID; batch.Name = "P" + batch.Price.ToString("0.00"); db.Stock_Batch.Add(batch); db.SaveChanges(); } } dbDetail.Batch_ID = batch.ID; db.Enter_Stock_Detail.Add(dbDetail); if (updateStock) { //update stock pile Stock_Pile zeroPile = (from sp in stockPiles where sp.Product_ID == dbDetail.Product_ID && sp.StockHouse_ID == dbstock.StoreHouse_ID && sp.Quantity==0 select sp).FirstOrDefault<Stock_Pile>(); if (zeroPile != null && zeroPile.Batch_ID!=batch.ID) { db.Stock_Pile.Remove(zeroPile); db.SaveChanges(); } Stock_Pile stockPile = (from sp in stockPiles where sp.Product_ID == dbDetail.Product_ID && sp.StockHouse_ID == dbstock.StoreHouse_ID && sp.Batch_ID==dbDetail.Batch_ID select sp).FirstOrDefault<Stock_Pile>(); if (stockPile == null) { stockPile = new Stock_Pile(); stockPile.Product_ID = dbDetail.Product_ID; stockPile.Shop_ID = this.Shop.Shop_ID; stockPile.StockHouse_ID = dbstock.StoreHouse_ID; stockPile.Quantity = dbDetail.Quantity; stockPile.Price = dbDetail.Price; stockPile.Batch_ID = dbDetail.Batch_ID; stockPile.First_Enter_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Stock_Pile.Add(stockPile); } else { stockPile.Quantity = stockPile.Quantity + dbDetail.Quantity; stockPile.Price = dbDetail.Price; } Product product = null; if (tmp.Parent_ID > 0) { product = (from p in db.Product join p1 in db.Product on p.Product_ID equals p1.Parent_ID where p1.Product_ID == dbDetail.Product_ID select p).FirstOrDefault<Product>(); } else if (tmp.Parent_ID == 0) { product = tmp; } if (product != null) { product.Quantity += dbDetail.Quantity; } } } try { db.SaveChanges(); result = true; } catch(Exception ex) { throw new KMJXCException(ex.Message, ExceptionLevel.SYSTEM); } finally { db.Dispose(); } return result; }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> public bool UpdateProductStockByEnterStock(int id) { bool result = false; if (this.CurrentUserPermission.UPDATE_ENTERSTOCK_TO_PRODUCT_STOCK == 0) { throw new KMJXCException("没有权限更新入库单到库存"); } using (KuanMaiEntities db = new KuanMaiEntities()) { Enter_Stock stock=(from dbstock in db.Enter_Stock where dbstock.Enter_Stock_ID==id select dbstock).FirstOrDefault<Enter_Stock>(); if (stock == null) { throw new KMJXCException("编号为:"+id+" 的入库单不存在"); } List<Enter_Stock_Detail> details=(from d in db.Enter_Stock_Detail where d.Enter_Stock_ID==id select d).ToList<Enter_Stock_Detail>(); foreach (Enter_Stock_Detail eDetail in details) { Product tmp=(from p in db.Product where p.Product_ID==eDetail.Product_ID select p).FirstOrDefault<Product>(); if (tmp == null) { continue; } Stock_Pile stockPile = (from sp in db.Stock_Pile where sp.Product_ID == eDetail.Product_ID && sp.StockHouse_ID == stock.StoreHouse_ID && sp.Batch_ID == eDetail.Batch_ID select sp).FirstOrDefault<Stock_Pile>(); if (stockPile == null) { stockPile = new Stock_Pile(); stockPile.Product_ID = eDetail.Product_ID; stockPile.Shop_ID = this.Shop.Shop_ID; stockPile.StockHouse_ID = stock.StoreHouse_ID; stockPile.Quantity = eDetail.Quantity; stockPile.Price = eDetail.Price; stockPile.First_Enter_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); stockPile.Batch_ID = eDetail.Batch_ID; db.Stock_Pile.Add(stockPile); } else { stockPile.Quantity = stockPile.Quantity + eDetail.Quantity; stockPile.Price = eDetail.Price; } Product product = null; if (tmp.Parent_ID > 0) { product = (from p in db.Product join p1 in db.Product on p.Product_ID equals p1.Parent_ID where p1.Product_ID == eDetail.Product_ID select p).FirstOrDefault<Product>(); } else { product = tmp; } if (product != null) { product.Quantity += eDetail.Quantity; } } stock.Status = 1; db.SaveChanges(); result = true; } return result; }
/// <summary> /// /// </summary> /// <param name="product_id"></param> public void CreateDefaultStockPile(Stock_Pile stockPile) { if (stockPile == null) { throw new KMJXCException(""); } if (stockPile.Shop_ID < 0) { throw new KMJXCException(""); } if (stockPile.Product_ID <= 0) { throw new KMJXCException(""); } if (stockPile.Price < 0) { throw new KMJXCException(""); } //if (stockPile.StockHouse_ID <= 0) //{ // throw new KMJXCException(""); //} using (KuanMaiEntities db = new KuanMaiEntities()) { stockPile.First_Enter_Time = 0; stockPile.LastLeave_Time = 0; db.Stock_Pile.Add(stockPile); db.SaveChanges(); } }
/// <summary> /// /// </summary> /// <param name="products"></param> /// <param name="mapProduct"></param> private void CreateProductsByMallProducts(List<BMallProduct> products,bool mapProduct=false) { if (products == null || products.Count == 0) { return; } List<BProperty> properties = new List<BProperty>(); int shop_id = products[0].Shop.ID; using (KuanMaiEntities db = new KuanMaiEntities()) { string[] dbMallPdtIds=(from p in products select p.ID).ToArray<string>(); List<Mall_Product> dbMallProcuts = (from p in db.Mall_Product where dbMallPdtIds.Contains(p.Mall_ID) select p).ToList<Mall_Product>(); List<Product> dbProducts = new List<Product>(); List<Product_Spec> existedProperties = null; var tmpProperties = from p in db.Product_Spec select p; var tmpDbProducts = from p in db.Product where p.Parent_ID == 0 select p; if (shop_id == this.Main_Shop.Shop_ID) { int[] child_shop_ids = (from c in this.ChildShops select c.ID).ToArray<int>(); tmpProperties = tmpProperties.Where(p => (child_shop_ids.Contains(p.Shop_ID) || p.Shop_ID == shop_id)); tmpDbProducts = tmpDbProducts.Where(p => (child_shop_ids.Contains(p.Shop_ID) || p.Shop_ID == shop_id)); } else { tmpProperties = tmpProperties.Where(p =>(p.Shop_ID == shop_id || p.Shop_ID==this.Main_Shop.Shop_ID)); tmpDbProducts = tmpDbProducts.Where(p => (p.Shop_ID == shop_id || p.Shop_ID == this.Main_Shop.Shop_ID)); } dbProducts = tmpDbProducts.ToList<Product>(); existedProperties = tmpProperties.ToList<Product_Spec>(); int[] prop_ids=(from prop in existedProperties select prop.Product_Spec_ID).ToArray<int>(); List<Product_Spec_Value> existedPropValues=(from pv in db.Product_Spec_Value where prop_ids.Contains(pv.Product_Spec_ID) select pv).ToList<Product_Spec_Value>(); Store_House defaultStoreHouse = null; List<Store_House> storeHouses=(from h in db.Store_House where h.Shop_ID==shop_id select h).ToList<Store_House>(); if (storeHouses.Count == 0) { defaultStoreHouse = new Store_House(); defaultStoreHouse.Shop_ID = shop_id; defaultStoreHouse.Title = "默认仓库"; defaultStoreHouse.User_ID = this.CurrentUser.ID; defaultStoreHouse.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); defaultStoreHouse.Default = true; 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; } } foreach (BMallProduct product in products) { Mall_Product dbMallProduct=(from p in dbMallProcuts where p.Mall_ID==product.ID select p).FirstOrDefault<Mall_Product>(); Product dbProduct = (from p in dbProducts where p.MallProduct == product.ID select p).FirstOrDefault<Product>(); if (dbProduct != null) { if (!string.IsNullOrEmpty(dbMallProduct.Title) && dbMallProduct.Title!=dbProduct.Name) { dbProduct.Name = dbMallProduct.Title; } dbMallProduct.CreatedProduct = true; db.SaveChanges(); continue; } dbProduct = new Product(); dbProduct.Code = product.Code; dbProduct.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbProduct.Description = dbProduct.Description; dbProduct.MallProduct = product.ID; dbProduct.Name = product.Title; dbProduct.Parent_ID = 0; dbProduct.Price = product.Price; dbProduct.Product_Class_ID = 0; //dbProduct.Quantity = (int)product.Quantity; dbProduct.Shop_ID = product.Shop.ID; dbProduct.Update_Time = dbProduct.Create_Time; dbProduct.Update_User_ID = this.CurrentUser.ID; dbProduct.User_ID = this.CurrentUser.ID; dbProduct.Wastage = 0; db.Product.Add(dbProduct); db.SaveChanges(); if (dbProduct.Product_ID <= 0) { continue; } Stock_Batch 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(); Stock_Pile stockPile = new Stock_Pile(); stockPile.LastLeave_Time = 0; stockPile.Price = 0; stockPile.Product_ID = dbProduct.Product_ID; stockPile.Quantity = 0; stockPile.Shop_ID = product.Shop.ID; stockPile.StockHouse_ID = defaultStoreHouse.StoreHouse_ID; stockPile.StockPile_ID = 0; stockPile.Batch_ID = batch.ID; db.Stock_Pile.Add(stockPile); if (product.Skus != null) { foreach (BMallSku sku in product.Skus) { Product dbChildProduct = new Product(); dbChildProduct.Code = product.Code; dbChildProduct.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbChildProduct.Description = dbProduct.Description; dbChildProduct.MallProduct = sku.SkuID; dbChildProduct.Name = product.Title; dbChildProduct.Parent_ID = dbProduct.Product_ID; dbChildProduct.Price = product.Price; dbChildProduct.Product_Class_ID = 0; //dbChildProduct.Quantity = (int)sku.Quantity; dbChildProduct.Shop_ID = product.Shop.ID; dbChildProduct.Update_Time = dbChildProduct.Create_Time; dbChildProduct.Update_User_ID = this.CurrentUser.ID; dbChildProduct.User_ID = this.CurrentUser.ID; dbChildProduct.Wastage = 0; db.Product.Add(dbChildProduct); db.SaveChanges(); if (dbChildProduct.Product_ID <= 0) { continue; } if (string.IsNullOrEmpty(sku.PropertiesName)) { continue; } Stock_Pile skustockPile = new Stock_Pile(); skustockPile.LastLeave_Time = 0; skustockPile.Price = 0; skustockPile.Product_ID = dbChildProduct.Product_ID; skustockPile.Quantity = 0; skustockPile.Shop_ID = product.Shop.ID; skustockPile.StockHouse_ID = defaultStoreHouse.StoreHouse_ID; skustockPile.StockPile_ID = 0; skustockPile.Batch_ID = batch.ID; db.Stock_Pile.Add(skustockPile); string[] props = sku.PropertiesName.Split(';'); foreach (string prop in props) { string[] values = prop.Split(':'); Product_Spec property = (from p in existedProperties where (p.Mall_PID == values[0] || p.Name==values[2]) select p).FirstOrDefault<Product_Spec>(); if (property == null) { property=new Product_Spec(); property.Name = values[2]; property.Mall_PID = values[0]; property.Shop_ID = shop_id; property.User_ID = this.CurrentUser.ID; property.Product_Class_ID = 0; property.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Product_Spec.Add(property); db.SaveChanges(); if (property.Product_Spec_ID > 0) { existedProperties.Add(property); } } Product_Spec_Value propValue=(from v in existedPropValues where (v.Mall_PVID==values[2] || v.Name==values[3]) select v ).FirstOrDefault<Product_Spec_Value>(); if(propValue==null) { propValue=new Product_Spec_Value(){ Product_Spec_ID=property.Product_Spec_ID, Mall_PVID=values[1], Name=values[3], Created=DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), User_ID=this.CurrentUser.ID}; db.Product_Spec_Value.Add(propValue); db.SaveChanges(); if (propValue.Product_Spec_Value_ID > 0) { existedPropValues.Add(propValue); } } if (property.Product_Spec_ID > 0 && propValue.Product_Spec_Value_ID > 0) { Product_Specifications ps = new Product_Specifications(); ps.Product_ID = dbChildProduct.Product_ID; ps.Product_Spec_ID = property.Product_Spec_ID; ps.Product_Spec_Value_ID = propValue.Product_Spec_Value_ID; db.Product_Specifications.Add(ps); } } } } dbMallProduct.CreatedProduct = true; } db.SaveChanges(); } }