/// <summary> /// /// </summary> /// <param name="sale_id"></param> public void CreateBackStock(int backsale_id,List<BOrder> orders,int backSaleStatus) { KuanMaiEntities db = new KuanMaiEntities(); try { if (orders == null || orders.Count == 0) { throw new KMJXCException("没有退货信息"); } Back_Sale dbbackSale = (from bsale in db.Back_Sale where bsale.Back_Sale_ID == backsale_id select bsale).FirstOrDefault<Back_Sale>(); if (dbbackSale == null) { throw new KMJXCException("退货单信息不存在"); } //dbbackSale.Status = backSaleStatus; var bdetails = from bsd in db.Back_Sale_Detail where bsd.Back_Sale_ID == backsale_id select bsd; string[] order_id = (from o in orders select o.Order_ID).ToArray<string>(); if (order_id != null) { bdetails = bdetails.Where(d => order_id.Contains(d.Order_ID)); } List<Sale_Detail> saleDetails=(from s in db.Sale_Detail where order_id.Contains(s.Mall_Order_ID) select s).ToList<Sale_Detail>(); List<Back_Sale_Detail> backSaleDetails = bdetails.ToList<Back_Sale_Detail>(); //Check if current sale trade has leave stock records //if the sale doesn't have leave stock, so no need to back stock Leave_Stock leave_Stock=(from ls in db.Leave_Stock where ls.Sale_ID==dbbackSale.Sale_ID select ls).FirstOrDefault<Leave_Stock>(); if (leave_Stock!=null) { List<Leave_Stock_Detail> leaveDetails=(from ld in db.Leave_Stock_Detail where ld.Leave_Stock_ID==leave_Stock.Leave_Stock_ID select ld).ToList<Leave_Stock_Detail>(); BBackStock backStock = new BBackStock(); backStock.BackSaleID = dbbackSale.Back_Sale_ID; backStock.BackSale = new BBackSale() { ID = dbbackSale.Back_Sale_ID }; if (backSaleStatus == 1 || backSaleStatus==2) { backStock.UpdateStock = true; } else { backStock.UpdateStock = false; } backStock.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); backStock.BackDateTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); backStock.Created_By = new BUser() { ID = this.CurrentUser.ID }; if (!string.IsNullOrEmpty(dbbackSale.Description)) { backStock.Description = dbbackSale.Description + "<br/> 生成了退库单"; } else { backStock.Description = "生成了退库单"; } backStock.Shop = new BShop() { ID = dbbackSale.Shop_ID }; //collect back stock details info from leave stock details backStock.Details = new List<BBackStockDetail>(); foreach (Back_Sale_Detail bsd in backSaleDetails) { BOrder order=(from o in orders where bsd.Order_ID == o.Order_ID select o).FirstOrDefault<BOrder>(); Sale_Detail saleDetail=(from s in saleDetails where s.Mall_Order_ID==bsd.Order_ID select s).FirstOrDefault<Sale_Detail>(); Leave_Stock_Detail leaveStockDetail = (from lsd in leaveDetails where lsd.Order_ID == bsd.Order_ID select lsd).FirstOrDefault<Leave_Stock_Detail>(); if (leaveStockDetail == null) { continue; } BBackStockDetail bsDetail = new BBackStockDetail(); bsDetail.Price = leaveStockDetail.Price; bsDetail.Quantity = leaveStockDetail.Quantity; if (order != null && order.Quantity > 0 && order.Quantity<=leaveStockDetail.Quantity) { bsDetail.Quantity = order.Quantity; } bsDetail.ProductID = leaveStockDetail.Product_ID; bsDetail.ParentProductID = leaveStockDetail.Parent_Product_ID; bsDetail.Batch = new BStockBatch() { ID = leaveStockDetail.Batch_ID }; bsDetail.StoreHouse = new BStoreHouse() { ID = leaveStockDetail.StoreHouse_ID }; backStock.Details.Add(bsDetail); bsd.Status = backSaleStatus; //5 means refound and successfully back to sock saleDetail.Status1 = (int)SaleDetailStatus.REFOUND_HANDLED; saleDetail.SyncResultMessage = "退货已经处理"; } if (backStock.Details.Count > 0) { this.CreateBackStock(backStock); } } db.SaveChanges(); } catch (KMJXCException kex) { throw kex; } catch(Exception ex) { throw ex; } finally { db.Dispose(); } }
/// <summary> /// /// </summary> /// <param name="backSaleId"></param> /// <param name="backSaleDetailId"></param> /// <param name="productId"></param> private void CreateBackStock(BBackStock backStock) { List<BBackStockDetail> details = backStock.Details; if (this.CurrentUserPermission.HANDLE_BACK_SALE == 0) { throw new KMJXCException("没有权限进行退货退库存操作"); } if (backStock == null) { throw new KMJXCException("输入错误", ExceptionLevel.SYSTEM); } if (backStock.BackSale == null || backStock.BackSale.ID <= 0) { throw new KMJXCException("请选择退货单进行退库存操作", ExceptionLevel.SYSTEM); } if (details == null) { throw new KMJXCException("没有选择产品进行退库存"); } using(KuanMaiEntities db = new KuanMaiEntities()) { using (TransactionScope tran = new TransactionScope()) { Back_Stock dbBackStock = (from dbStock in db.Back_Stock where dbStock.Back_Sale_ID == backStock.BackSale.ID select dbStock).FirstOrDefault<Back_Stock>(); if (dbBackStock == null) { dbBackStock = new Back_Stock(); if (backStock.BackDateTime > 0) { dbBackStock.Back_Date = backStock.BackDateTime; } else { dbBackStock.Back_Date = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); } dbBackStock.Back_Sale_ID = backStock.BackSale.ID; dbBackStock.Back_Sock_ID = 0; dbBackStock.Description = backStock.Description; dbBackStock.Shop_ID = this.Shop.Shop_ID; if (backStock.Created > 0) { dbBackStock.Created = backStock.Created; } else { dbBackStock.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); } dbBackStock.User_ID = this.CurrentUser.ID; db.Back_Stock.Add(dbBackStock); db.SaveChanges(); } if (dbBackStock.Back_Sock_ID > 0) { foreach (BBackStockDetail detail in details) { Back_Stock_Detail dbDetail = new Back_Stock_Detail(); dbDetail.Back_Stock_ID = dbBackStock.Back_Sock_ID; dbDetail.Price = detail.Price; dbDetail.Product_ID = detail.ProductID; dbDetail.Parent_Product_ID = detail.ParentProductID; dbDetail.Quantity = detail.Quantity; if (detail.Batch != null) { dbDetail.Batch_ID = detail.Batch.ID; } if (detail.StoreHouse != null) { dbDetail.StoreHouse_ID = detail.StoreHouse.ID; } //Update stock pile if (backStock.UpdateStock) { Stock_Pile pile = (from spile in db.Stock_Pile where spile.Product_ID == dbDetail.Product_ID && spile.StockHouse_ID == detail.StoreHouse.ID && spile.Batch_ID == dbDetail.Batch_ID select spile).FirstOrDefault<Stock_Pile>(); if (pile != null) { pile.Quantity = pile.Quantity + dbDetail.Quantity; } Product 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>(); if (product != null) { product.Quantity += dbDetail.Quantity; } //1表示退库并更新了库存 dbDetail.Status = 1; } db.Back_Stock_Detail.Add(dbDetail); } db.SaveChanges(); } else { throw new KMJXCException("退库存操作失败"); } tran.Complete(); } } }