public ActionResult SaveAddNewOrder(Daily_ChicCut_Pre_OrderModel model, List <Daily_ChicCut_Pre_OrderDetailViewModel> details = null) { try { if (ModelState.IsValid) { using (TransactionScope ts = new TransactionScope()) { var currentTime = DateTime.Now; model.CreatedDate = currentTime; // model.CreatedUserId = currentAccount.UserName; _context.Entry(model).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); //decimal SumCOGSOfOrderDetail = 0; string ServicesName = ""; int count = 0; #region Chi tiết đơn hàng if (details != null) { foreach (var item in details) { count++; ServicesName += item.ServiceName; if (count < details.Count) { ServicesName += " ; "; } Daily_ChicCut_Pre_OrderDetailModel itemAdd = new Daily_ChicCut_Pre_OrderDetailModel() { ServiceCategoryId = item.ServiceCategoryId, // COGS = COGSCal, MinPrice = item.MinPrice, MaxPrice = item.MaxPrice, Qty = item.Qty, //UnitCOGS = COGSCal * item.Qty, MinUnitPrice = item.MinPrice * item.Qty, MaxUnitPrice = item.MaxPrice * item.Qty, PreOrderId = model.PreOrderId }; _context.Entry(itemAdd).State = System.Data.Entity.EntityState.Added; } } #endregion // model.SumCOGSOfOrderDetail = SumCOGSOfOrderDetail; model.ServicesName = ServicesName; _context.Entry(model).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); model.PreOrderCode = (model.PreOrderId % 1000).ToString("D3"); #region // Gửi SMS cho khách hàng SendSMSRepository SMSRepo = new SendSMSRepository(); string name = model.FullName.LastIndexOf(' ') > 0 ? model.FullName.Substring(model.FullName.LastIndexOf(' ') + 1) : model.FullName; name = Library.ConvertToNoMarkString(name); if (!string.IsNullOrEmpty(name)) { name = name.First().ToString().ToUpper() + name.Substring(1).ToLower(); } string mes = string.Format( "Cam on {0} {1} dat hen tai Chic Cut, thoi gian hen la: {2:HH:mm dd/MM/yyyy}. {0} vui long den dung gio da hen.", model.Gender.HasValue && model.Gender.Value ? "anh" : "chi", name, model.AppointmentTime ); AY_SMSCalendar smsModel = new AY_SMSCalendar() { EndDate = DateTime.Now, isSent = true, NumberOfFailed = 0, SMSContent = mes, SMSTo = model.Phone }; _context.Entry(smsModel).State = System.Data.Entity.EntityState.Added; #endregion _context.SaveChanges(); SMSRepo.SendSMSModel(smsModel); _context.SaveChanges(); ts.Complete(); return(Content("success")); } } else { return(Content("Vui lòng kiểm tra lại thông tin không hợp lệ")); } } catch { return(Content("Xảy ra lỗi trong quá trình thêm mới đơn hàng")); } }
//CreateListInner public ActionResult _DailyChicCutOrderDetailInnerInfo(List <Daily_ChicCut_Pre_OrderDetailViewModel> details = null, int?ServiceCategoryId = null, int?PreOrderId = null) { if (details == null) { details = new List <Daily_ChicCut_Pre_OrderDetailViewModel>(); } /*Kiểm tra xem Danh sách đã có dịch vụ đó chưa : + Có rồi: Tăng Qty + Cogs + UnitPrice + Chưa có: Thêm mới. */ if (ServiceCategoryId.HasValue) { var itemExist = details.Where(p => p.ServiceCategoryId == ServiceCategoryId).FirstOrDefault(); if (itemExist != null) //Đã có dịch vụ đó { var index = details.FindIndex(c => c.ServiceCategoryId == ServiceCategoryId); details[index] = new Daily_ChicCut_Pre_OrderDetailViewModel() { ServiceCategoryId = itemExist.ServiceCategoryId, // COGS = itemExist.COGS, MinPrice = itemExist.MinPrice, MaxPrice = itemExist.MaxPrice, Qty = itemExist.Qty + 1, // UnitCOGS = itemExist.COGS * (itemExist.Qty + 1), MinUnitPrice = itemExist.MinPrice * (itemExist.Qty + 1), MaxUnitPrice = itemExist.MaxUnitPrice * (itemExist.Qty + 1), ServiceName = itemExist.ServiceName }; } else //Chưa có dịch vụ đó { #region Chưa có dịch vụ đó /* Tính giá vốn * List<int> ProductIdLst = (from QMaster in _context.Master_ChicCut_QuantificationMasterModel * join QDetail in _context.Master_ChicCut_QuantificationDetailModel on QMaster.QuantificationMasterId equals QDetail.QuantificationMasterId * where QMaster.ServiceId == ServiceId * select QDetail.ProductId.Value).ToList(); * var LstProductModel = _context.ProductModel.Where(p => ProductIdLst.Contains(p.ProductId)).ToList(); * decimal COGSCal = (LstProductModel != null && LstProductModel.Count > 0) ? LstProductModel.Sum(p => p.COGS.HasValue ? p.COGS : 0).Value : 0; */ // from p in _context.Master_ChicCut_ServiceModel //join sc in _context.Master_ChicCut_ServiceCategoryModel on p.ServiceCategoryId equals sc.ServiceCategoryId //where p.Gender == Gender && p.Actived == true && p.Price > 0 //group p by p.ServiceCategoryId into P_Groupby // nhóm bảng nào + theo cột nào + ra bảng mới //orderby (P_Groupby.Key) //select new Master_ChicCut_ServiceViewModel() //{ // ServiceCategoryId = P_Groupby.Key, // MinPrice = P_Groupby.Min(p => p.Price.Value), // MaxPrice = P_Groupby.Max(p => p.Price.Value), // ServiceName = _context.Master_ChicCut_ServiceCategoryModel.Where(p => p.ServiceCategoryId == P_Groupby.Key).Select(p => p.ServiceName).FirstOrDefault() //} var item = ( from p in _context.Master_ChicCut_ServiceModel join sc in _context.Master_ChicCut_ServiceCategoryModel on p.ServiceCategoryId equals sc.ServiceCategoryId where p.Actived == true && p.Price > 0 && p.ServiceCategoryId == ServiceCategoryId group p by p.ServiceCategoryId into P_Groupby // nhóm bảng nào + theo cột nào + ra bảng mới select new Daily_ChicCut_Pre_OrderDetailViewModel() { ServiceCategoryId = P_Groupby.Key, //COGS = 0, MinPrice = P_Groupby.Min(p => p.Price.Value), MaxPrice = P_Groupby.Max(p => p.Price.Value), Qty = 1, ServiceName = _context.Master_ChicCut_ServiceCategoryModel.Where(p => p.ServiceCategoryId == P_Groupby.Key).Select(p => p.ServiceName).FirstOrDefault(), MinUnitPrice = P_Groupby.Min(p => p.Price.Value), MaxUnitPrice = P_Groupby.Max(p => p.Price.Value) }).FirstOrDefault(); details.Add(item); #endregion } if (PreOrderId.HasValue && PreOrderId.Value > 0)//Sửa đơn hàng: + Chưa có dịch vụ đó: Thêm mới { // + Có rồi: Update Qty, TT, Vốn, Sum.. #region //Sửa đơn hàng: using (TransactionScope ts = new TransactionScope()) { var itemOrderDetailModel = _context.Daily_ChicCut_Pre_OrderDetailModel.Where(p => p.ServiceCategoryId == ServiceCategoryId && p.PreOrderId == PreOrderId).FirstOrDefault(); if (itemOrderDetailModel != null) // Update { //itemOrderDetailModel.COGS = itemOrderDetailModel.COGS; //itemOrderDetailModel.UnitCOGS = itemOrderDetailModel.COGS * (itemOrderDetailModel.Qty + 1); itemOrderDetailModel.MinUnitPrice = itemExist.MinPrice * (itemExist.Qty + 1); itemOrderDetailModel.MaxUnitPrice = itemExist.MaxUnitPrice * (itemExist.Qty + 1); itemOrderDetailModel.Qty++; _context.Entry(itemOrderDetailModel).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); } else // Chưa có { #region Thêm mới /* Tính giá vốn */ //List<int> ProductIdLst = (from QMaster in _context.Master_ChicCut_QuantificationMasterModel // join QDetail in _context.Master_ChicCut_QuantificationDetailModel on QMaster.QuantificationMasterId equals QDetail.QuantificationMasterId // where QMaster.ServiceId == ServiceId // select QDetail.ProductId.Value).ToList(); //var LstProductModel = _context.ProductModel.Where(p => ProductIdLst.Contains(p.ProductId)).ToList(); //decimal COGSCal = (LstProductModel != null && LstProductModel.Count > 0) ? LstProductModel.Sum(p => p.COGS.HasValue ? p.COGS : 0).Value : 0; var ServiceLstModel = _context.Master_ChicCut_ServiceModel.Where(p => p.ServiceCategoryId == ServiceCategoryId).ToList(); var itemAdd = new Daily_ChicCut_Pre_OrderDetailModel() { PreOrderId = PreOrderId, ServiceCategoryId = ServiceLstModel.Select(p => p.ServiceCategoryId).FirstOrDefault(), // COGS = itemExist.COGS, MinPrice = ServiceLstModel.Where(p => p.Price > 0).Min(p => p.Price), MaxPrice = ServiceLstModel.Where(p => p.Price > 0).Max(p => p.Price), Qty = 1, // UnitCOGS = itemExist.COGS * (itemExist.Qty + 1), MinUnitPrice = ServiceLstModel.Where(p => p.Price > 0).Min(p => p.Price), MaxUnitPrice = ServiceLstModel.Where(p => p.Price > 0).Max(p => p.Price), }; _context.Entry(itemAdd).State = System.Data.Entity.EntityState.Added; _context.SaveChanges(); #endregion } var model = _context.Daily_ChicCut_Pre_OrderModel.Where(p => p.PreOrderId == PreOrderId).FirstOrDefault(); if (model != null)// Cập nhật Master { var lstDetail = model.Daily_ChicCut_Pre_OrderDetailModel.ToList(); //model.SumCOGSOfOrderDetail = lstDetail.Sum(p => p.UnitCOGS.HasValue ? p.UnitCOGS.Value : 0); model.MinSumPriceOfOrderDetail = lstDetail.Sum(p => p.MinUnitPrice.HasValue ? p.MinUnitPrice.Value : 0); model.MaxSumPriceOfOrderDetail = lstDetail.Sum(p => p.MaxUnitPrice.HasValue ? p.MaxUnitPrice.Value : 0); model.MinTotalBillDiscount = model.BillDiscount.HasValue ? ((model.BillDiscountTypeId == 1 ? model.BillDiscount : (model.BillDiscount / 100) * lstDetail.Sum(p => p.MinUnitPrice.HasValue ? p.MinUnitPrice.Value : 0))) : 0; model.MaxTotalBillDiscount = model.BillDiscount.HasValue ? ((model.BillDiscountTypeId == 1 ? model.BillDiscount : (model.BillDiscount / 100) * lstDetail.Sum(p => p.MaxUnitPrice.HasValue ? p.MaxUnitPrice.Value : 0))) : 0; model.MinTotal = model.MinSumPriceOfOrderDetail - model.MinTotalBillDiscount; model.MaxTotal = model.MaxSumPriceOfOrderDetail - model.MaxTotalBillDiscount; _context.Entry(model).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); ts.Complete(); } } #endregion } } return(PartialView(details)); }