/// <summary> /// 保存单据 /// </summary> public static void SaveBill <T, TDetail>(BillBO <T, TDetail> bo, DbContext dbContext, bool specifcCreateTime = false) where T : BillBase where TDetail : BillDetailBase { if (!specifcCreateTime) { bo.Bill.CreateTime = DateTime.Now; } var tcontext = dbContext.Set <T>(); if (string.IsNullOrEmpty(bo.Bill.Code)) { bo.Bill.Code = BillHelper.GenerateBillCode <T>(bo.Bill.OrganizationID, dbContext); } tcontext.Add(bo.Bill); dbContext.SaveChanges(); var dcontext = dbContext.Set <TDetail>(); foreach (var d in bo.Details) { d.BillID = bo.Bill.ID; dcontext.Add(d); } dbContext.SaveChanges(); }
public OPResult SaveBillDelivery(BillDeliveryBO bo) { using (var dbContext = new DistributionEntities()) { using (TransactionScope scope = new TransactionScope()) { try { bo.Bill.Code = BillHelper.GenerateBillCode <BillDelivery>(bo.Bill.ToOrganizationID, dbContext, o => o.ToOrganizationID == bo.Bill.ToOrganizationID); BillHelper.SaveBill <BillDelivery, BillDeliveryDetails>(bo, dbContext); var storeout = BillHelper.GenerateStoreOut(bo); BillHelper.SaveBillStoreOut(storeout, dbContext); if (bo.FundAccount != null && bo.FundAccount.NeedIn != 0) { bo.FundAccount.RefrenceBillCode = bo.Bill.Code; dbContext.OrganizationFundAccount.Add(bo.FundAccount); } if (bo.Bill.IsWriteDownOrder) { //冲减订单 bo.Details.ForEach(d => dbContext.Database.ExecuteSqlCommand("exec UpdateOrderWhenDelivery {0}, {1},{2}", bo.Bill.ToOrganizationID, d.ProductID, d.Quantity)); } dbContext.SaveChanges(); scope.Complete(); } catch (Exception e) { return(new OPResult { IsSucceed = false, Message = "保存失败,失败原因:\n" + e.Message }); } } } return(new OPResult { IsSucceed = true, Message = "保存成功!" }); }
public OPResult <BillRetail> SaveBillRetail(BillRetailBO bo) { //bo.Bill.CreateTime = DateTime.Now; using (var dbContext = new DistributionEntities()) { if (bo.RefrenseVIPUpTactics != null && bo.VIPPointRecord != null) { IEnumerable <int> kindIDs = bo.RefrenseVIPUpTactics.Select(o => o.Tactic.FormerKindID).ToList(); IEnumerable <VIPCardKindMapping> mappings = dbContext.VIPCardKindMapping.Where(o => o.CardID == bo.VIPPointRecord.VIPID && kindIDs.Contains(o.KindID)).ToList(); foreach (var mapping in mappings) { var t = bo.RefrenseVIPUpTactics.First(o => o.Tactic.FormerKindID == mapping.KindID); mapping.KindID = t.Tactic.AfterKindID; if (t.Tactic.CutPoint != 0) { dbContext.VIPPointTrack.Add(new VIPPointTrack { CreateTime = DateTime.Now, Point = (-1 * t.Tactic.CutPoint), VIPID = bo.VIPPointRecord.VIPID, Remark = "VIP升级产生," + t.Description }); } } } using (TransactionScope scope = new TransactionScope()) { try { bo.Bill.Code = BillHelper.GenerateBillCode <BillRetail>(bo.Bill.OrganizationID, dbContext); BillHelper.SaveBill <BillRetail, BillRetailDetails>(bo, dbContext, bo.SpecifcCreateTime); foreach (var storeout in bo.BillStoreOuts) { storeout.Bill.CreateTime = DateTime.Now; storeout.Bill.RefrenceBillCode = bo.Bill.Code; BillHelper.SaveBillStoreOut(storeout, dbContext); } foreach (var storing in bo.BillStorings) { storing.Bill.CreateTime = DateTime.Now; storing.Bill.RefrenceBillCode = bo.Bill.Code; BillHelper.SaveBillStoring(storing, dbContext); } if (bo.VIPPointRecord != null) { bo.VIPPointRecord.Remark = "零售单产生,小票号" + bo.Bill.Code; dbContext.VIPPointTrack.Add(bo.VIPPointRecord); if (bo.Bill.PredepositPay != 0) { VIPPredepositTrack predepositTrack = new VIPPredepositTrack { RefrenceBillCode = bo.Bill.Code, Kind = false, OrganizationID = bo.Bill.OrganizationID, CreatorID = bo.Bill.CreatorID, CreateTime = DateTime.Now, ConsumeMoney = bo.Bill.PredepositPay, VIPID = bo.VIPPointRecord.VIPID, Remark = "零售单产生" }; dbContext.VIPPredepositTrack.Add(predepositTrack); } } if (bo.VIPBirthdayConsumption != null) { bo.VIPBirthdayConsumption.ConsumeDay = DateTime.Now.Date; dbContext.VIPBirthdayConsumption.Add(bo.VIPBirthdayConsumption); } dbContext.SaveChanges(); scope.Complete(); } catch (Exception e) { return(new OPResult <BillRetail> { IsSucceed = false, Message = "保存失败,失败原因:\n" + e.Message }); } } } return(new OPResult <BillRetail> { IsSucceed = true, Message = "保存成功!", Result = bo.Bill }); }