public IQueryable <OrderDetailViewModel> GetOrderDetailByOrder(SearchViewModel model) { var result = Queryable().Where(x => x.Delete == false && ((!model.From.HasValue) || (DbFunctions.TruncateTime(x.CreatDate) >= DbFunctions.TruncateTime(model.From))) && ((!model.To.HasValue) || (DbFunctions.TruncateTime(x.CreatDate) <= DbFunctions.TruncateTime(model.To)))). Select(x => new OrderDetailViewModel { ID = x.Id, ProductID = x.ProductId, ProductName = x.ProductName, Quantity = x.Quantity, Price = x.Price, Serial = x.Serial, CreatDate = x.CreatDate, IsGift = x.IsGift, UserName = x.UserAccount.UserName, OrderCode = x.Order.Code, OrderID = x.Order.Id }).ToList(); if (result != null) { foreach (var item in result) { var r = _orderStatisticService.Queryable().Where(x => x.Delete == false && x.OrderDetailId == item.ID).ToList(); if (r != null) { foreach (var item1 in r) { var a = new OrderStatisticViewModel(); a.ProductAttributeName = item1.ProductAttributeName; a.ProductAttributeId = item1.ProductAttributeId; a.ProductAttributeNote = item1.ProductAttributeNote; a.Serial = item1.Serial; a.ID = item1.Id; item.OrderStatistics.Add(a); } } } } return(result.AsQueryable()); }
public bool Update(OrderViewModel model, string CurrentId) { //tìm phiếu order var Phieu = _repository.Find(model.ID); if (Phieu != null) { Phieu.Delete = false; //xóa dữ liệu trong Detail và Stattic var statistic = _orderStatisticService.Queryable().Where(x => x.OrderId == model.ID).ToList(); if (statistic != null) { foreach (var item in statistic) { _repositoryStatistic.Delete(item.Id); } } //xóa dữ liệu trong Stattic var detail = _orderDetailService.Queryable().Where(x => x.Order.Id == Phieu.Id).ToList(); if (detail != null) { foreach (var item in detail) { _repositoryDetail.Delete(item.Id); } } //Tạo phiếu mới var data = new Order(); data.Code = Phieu.Code; data.Customer = _customerService.Find(model.CustomerID); data.StatusOrder = model.StatusOrder; data.UserAccount = _userRepository.Find(CurrentId); data.Note = model.Note; data.IsGift = model.IsGift; data.CreatDate = Phieu.CreatDate; data.LastModifiedDate = DateTime.Now; data.Appointment = model.Appointment; data.SaleEmployeeName = model.SaleEmployeeName; data.SaleEmployeeID = model.SaleEmployeeID; data.TypeOrder = model.TypeOrder; data.Source = model.Source; OrderDetail o; if (model.OrderDetails != null) { data.OrderDetails = new List <OrderDetail>(); foreach (var item in model.OrderDetails) { o = new OrderDetail(); o = new OrderDetail() { ProductId = item.ProductID, ProductName = item.ProductName, Quantity = item.Quantity, Price = item.Price, IsGift = item.IsGift, Serial = item.Serial, CreatDate = Phieu.CreatDate, LastModifiedDate = DateTime.Now, UserAccount = _userRepository.Find(CurrentId), Delete = false }; data.OrderDetails.Add(o); item.ID = o.Id; } } data.Total = model.OrderDetails.Sum(x => x.Quantity * x.Price); _repository.Insert(data); model.ID = data.Id; //Thêm data vào bảng OrderStatisticService foreach (var item in model.OrderDetails) { //nếu sản phẩm có thuộc tính if (item.OrderStatistics.Count != 0) { foreach (var item1 in item.OrderStatistics) { var dataProduct = new OrderStatistic(); dataProduct.OrderId = model.ID; dataProduct.ProductAttributeId = item1.ID; dataProduct.ProductAttributeName = item1.ProductAttributeName; dataProduct.ProductAttributeNote = item1.ProductAttributeNote; dataProduct.CreatDate = Phieu.CreatDate; dataProduct.LastModifiedDate = DateTime.Now; dataProduct.ProductId = item.ProductID; dataProduct.UserAccount = _userRepository.Find(CurrentId); dataProduct.OrderDetailId = item.ID; dataProduct.Serial = item.Serial; _repositoryStatistic.Insert(dataProduct); } } else { var dataProduct = new OrderStatistic(); dataProduct.OrderId = model.ID; dataProduct.CreatDate = Phieu.CreatDate; dataProduct.LastModifiedDate = DateTime.Now; dataProduct.ProductId = item.ProductID; dataProduct.UserAccount = _userRepository.Find(CurrentId); dataProduct.OrderDetailId = item.ID; dataProduct.Serial = item.Serial; _repositoryStatistic.Insert(dataProduct); } } //Xóa phiếu _repository.Delete(Phieu.Id); } return(true); }