/// <summary> /// Add one enter stock detail record /// </summary> /// <param name="detail"></param> /// <returns></returns> public bool EnterStockDetail(int stockId,BEnterStockDetail detail) { bool result = false; if (this.CurrentUserPermission.ADD_ENTER_STOCK == 0) { throw new KMJXCException("没有新增入库单的权限"); } if (detail.EnterStock == null && stockId<=0) { throw new KMJXCException("必须选择入库单"); } KuanMaiEntities db = new KuanMaiEntities(); if (stockId > 0) { Enter_Stock dbStock= (from st in db.Enter_Stock where st.Enter_Stock_ID == stockId select st).FirstOrDefault<Enter_Stock>(); detail.EnterStock = new BEnterStock() { ID = stockId, StoreHouse = new BStoreHouse() { ID = dbStock.StoreHouse_ID } }; } else { if (detail.EnterStock.ID <= 0) { throw new KMJXCException("必须选择入库单"); } } if (detail.Product == null) { throw new KMJXCException("必须指定商品"); } if (detail.Quantity == 0) { throw new KMJXCException("数量必须大于零"); } try { Enter_Stock_Detail dbDetail = new Enter_Stock_Detail(); dbDetail.Create_Date = detail.Created; dbDetail.Enter_Stock_ID = detail.EnterStock.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; db.Enter_Stock_Detail.Add(dbDetail); db.SaveChanges(); //update stock pile Stock_Pile stockPile = (from sp in db.Stock_Pile where sp.Product_ID == dbDetail.Product_ID && sp.StockHouse_ID==detail.EnterStock.StoreHouse.ID select sp).FirstOrDefault<Stock_Pile>(); if (stockPile != null) { stockPile.Quantity = stockPile.Quantity + dbDetail.Quantity; stockPile.Price = dbDetail.Price; if (stockPile.First_Enter_Time == 0) { stockPile.First_Enter_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); } } result = true; } catch { } finally { if (db != null) { db.Dispose(); } } return result; }
/// <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; }