/// <summary> /// /// </summary> /// <param name="backSale"></param> /// <param name="details"></param> public void CreateBackSale(BBackSale backSale) { if (backSale == null) { return; } List<BBackSaleDetail> details = backSale.Details; if (this.CurrentUserPermission.ADD_BACK_STOCK == 0) { throw new KMJXCException("没有权限进行退货操作"); } if (backSale.Sale == null || string.IsNullOrEmpty(backSale.Sale.Sale_ID)) { throw new KMJXCException("请选择一个销售单进行退货"); } if (details == null) { throw new KMJXCException(""); } using (KuanMaiEntities db = new KuanMaiEntities()) { int[] cspids = (from c in this.DBChildShops select c.Shop_ID).ToArray<int>(); List<Product> products = (from p in db.Product where p.Shop_ID == this.Shop.Shop_ID || p.Shop_ID == this.Main_Shop.Shop_ID || cspids.Contains(p.Shop_ID) select p).ToList<Product>(); Back_Sale dbbackSale = new Back_Sale(); dbbackSale.Back_Date = backSale.BackTime; dbbackSale.Back_Sale_ID = 0; dbbackSale.Description = backSale.Description; dbbackSale.Sale_ID = backSale.Sale.Sale_ID; dbbackSale.Shop_ID = this.Shop.Shop_ID; dbbackSale.User_ID = this.CurrentUser.ID; db.Back_Sale.Add(dbbackSale); if (dbbackSale.Back_Sale_ID <= 0) { throw new KMJXCException("退货单创建失败"); } List<BBackStockDetail> bdetails = new List<BBackStockDetail>(); foreach (BBackSaleDetail sDetail in details) { Back_Sale_Detail dbSaleDetail = new Back_Sale_Detail(); dbSaleDetail.Back_Sale_ID = dbbackSale.Back_Sale_ID; dbSaleDetail.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbSaleDetail.Description = sDetail.Description; dbSaleDetail.Parent_Product_ID = sDetail.ParentProductID; if (dbSaleDetail.Parent_Product_ID == 0) { dbSaleDetail.Parent_Product_ID = (from p in products where p.Product_ID == sDetail.ProductID select p.Parent_ID).FirstOrDefault<int>(); } dbSaleDetail.Price = sDetail.Price; dbSaleDetail.Product_ID = sDetail.ProductID; dbSaleDetail.Quantity = sDetail.Quantity; dbSaleDetail.Status = 0; db.Back_Sale_Detail.Add(dbSaleDetail); } db.SaveChanges(); if (backSale.GenerateBackStock) { //stockManager.CreateBackStock(dbbackSale.Back_Sale_ID, backSale.UpdateStock); } db.SaveChanges(); } }
/// <summary> /// The back sales won't be created while the sales don't has corresponding leave stocks /// Example:Customers refounded before expressed /// </summary> /// <param name="trades"></param> private void HandleBackTrades(List<BSale> trades, Shop shop) { if (trades == null || trades.Count == 0 || shop==null) { return; } KuanMaiEntities db = new KuanMaiEntities(); try { string[] sale_ids=(from trade in trades select trade.Sale_ID).ToArray<string>(); List<Leave_Stock> leave_Stocks=(from ls in db.Leave_Stock where sale_ids.Contains(ls.Sale_ID) select ls).ToList<Leave_Stock>(); int[] ls_ids=(from ls in leave_Stocks select ls.Leave_Stock_ID).ToArray<int>(); List<Leave_Stock_Detail> leave_stock_Details=(from lsd in db.Leave_Stock_Detail where ls_ids.Contains(lsd.Leave_Stock_ID) select lsd).ToList<Leave_Stock_Detail>(); List<Back_Sale> back_Sales=(from backSale in db.Back_Sale where sale_ids.Contains(backSale.Sale_ID) select backSale).ToList<Back_Sale>(); int[] bs_ids=(from bs in back_Sales select bs.Back_Sale_ID).ToArray<int>(); List<Back_Sale_Detail> back_Sale_Details=(from bsd in db.Back_Sale_Detail where bs_ids.Contains(bsd.Back_Sale_ID) select bsd).ToList<Back_Sale_Detail>(); List<Sale_Detail> saleDetails=(from sd in db.Sale_Detail where sale_ids.Contains(sd.Mall_Trade_ID) select sd).ToList<Sale_Detail>(); foreach (BSale trade in trades) { Leave_Stock ls=(from leaveStock in leave_Stocks where leaveStock.Sale_ID==trade.Sale_ID select leaveStock).FirstOrDefault<Leave_Stock>(); //no leave stock no need to create back sale if (ls == null) { continue; } Back_Sale bSale=(from b_Sale in back_Sales where b_Sale.Sale_ID==trade.Sale_ID select b_Sale).FirstOrDefault<Back_Sale>(); bool isNewBackSale = false; double totalRefound = 0; if (bSale == null) { isNewBackSale = true; bSale = new Back_Sale(); bSale.Back_Date = trade.Synced; bSale.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); bSale.Description = ""; bSale.Sale_ID = trade.Sale_ID; bSale.Shop_ID = shop.Shop_ID; bSale.Status = 0; bSale.User_ID = this.CurrentUser.ID; } if (isNewBackSale) { db.Back_Sale.Add(bSale); db.SaveChanges(); } if (trade.Orders != null) { foreach (BOrder order in trade.Orders) { Sale_Detail sale_detail=(from odetail in saleDetails where odetail.Mall_Order_ID==order.Order_ID select odetail).FirstOrDefault<Sale_Detail>(); if (sale_detail == null) { continue; } //1- refound succeed //0- is normal if (!order.Refound) { continue; } Back_Sale_Detail dbSaleDetail = (from bsd in back_Sale_Details where bsd.Order_ID==order.Order_ID select bsd).FirstOrDefault<Back_Sale_Detail>(); Leave_Stock_Detail dbLeaveStockDetail=(from dblsd in leave_stock_Details where dblsd.Order_ID==order.Order_ID select dblsd).FirstOrDefault<Leave_Stock_Detail>(); //no need to create back sale detail while no leave stock detail if (dbSaleDetail == null && dbLeaveStockDetail!=null) { dbSaleDetail = new Back_Sale_Detail(); dbSaleDetail.Order_ID = order.Order_ID; dbSaleDetail.Back_Sale_ID = bSale.Back_Sale_ID; dbSaleDetail.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbSaleDetail.Description = "同步商城订单时处理有退货的订单(有出库单详细信息)"; dbSaleDetail.Parent_Product_ID = dbLeaveStockDetail.Parent_Product_ID; dbSaleDetail.Price = order.Price; dbSaleDetail.Product_ID = dbLeaveStockDetail.Product_ID; dbSaleDetail.Quantity = order.Quantity; dbSaleDetail.Status = 0; dbSaleDetail.Refound = order.Amount; totalRefound += order.Amount; db.Back_Sale_Detail.Add(dbSaleDetail); sale_detail.Status1 = (int)SaleDetailStatus.REFOUNDED_WAIT_HANDLE; } } bSale.Amount = totalRefound; } } base.CreateActionLog(new BUserActionLog() { Shop=new BShop{ ID=shop.Shop_ID}, Action = new BUserAction() { Action_ID = UserLogAction.CREATE_BACK_SALE }, Description = "同步商城订单到进销存,成功调用商城API,有退货交易,进销存退货单成功创建" }); db.SaveChanges(); } catch (Exception ex) { } finally { db.Dispose(); } }