public OrderViewDTO CreateOrder(string username, int productId) { var order = new OrderDTO(); int userId = _user.FindUser(username).ID; order.UserID = userId; order.StoreId = _product.GetById(productId).StoreID; order.OrderCode = CodeGenerator.RandomString(6); order.CreatedDate = DateTime.Now; order.ModifiedDate = DateTime.Now; order.StatusID = ORDERING; order.ModifiedID = userId; var cart = _repo.Create(order.Translate <OrderDTO, Order>()).Translate <Order, OrderViewDTO>(); _detail.AddItem(productId, cart.ID); return(cart); }
public bool CheckOut(OrderViewDTO order, string username) { var dto = new OrderDTO(); int userId = _user.FindUser(username).ID; dto.ID = order.ID; dto.ModifiedDate = DateTime.Now; dto.ModifiedID = userId; dto.Note = order.Note; dto.OrderCode = order.OrderCode; dto.StatusID = PROCESSING; dto.Total = 0; dto.UserID = userId; dto.CityID = _city.GetByName(order.City).Id; dto.CountryID = _country.GetByName(order.Country).Id; var DisString = order.District.Split('.'); if (DisString.Length > 1) { dto.DistrictID = _district.GetByName(DisString[1]).Id; } else if (DisString.Length == 1) { dto.DistrictID = _district.GetByName(DisString[0]).Id; } dto.Address = order.Address; if (order.StoreId != null) { dto.StoreId = order.StoreId.Value; dto.Total = dto.Price; foreach (var detail in order.details) { dto.Total = dto.Total + (detail.Quantity * detail.Price); dto.Price = dto.Price + (detail.Quantity * detail.Weight); } dto.Price = dto.Price * 5; var result = _repo.UpdateOrder(dto.Translate <OrderDTO, Order>()); if (result) { var mess = new MessageDTO(); mess.Description = "Order " + order.ID + " is checked out."; mess.FromID = userId; mess.SentID = _repo.GetById(order.ID).Store.UserID; mess.MessageType = STORE_ORDER; mess.DataID = order.StoreId; _message.CreateMessage(mess); } return(result); } else { var details = new ConcurrentDictionary <int, OrderDetail>(); foreach (var d in _detail.GetByOrderId(order.ID).ToList()) { details.TryAdd(d.ID, d); } try { foreach (var d in details) { var newOrder = new OrderDTO(); newOrder.OrderCode = order.OrderCode; newOrder.StatusID = PROCESSING; newOrder.ModifiedDate = DateTime.Now; newOrder.ModifiedID = 1; newOrder.CreatedDate = DateTime.Now; newOrder.UserID = null; newOrder = _repo.Create(newOrder.Translate <OrderDTO, Order>()).Translate <Order, OrderDTO>(); newOrder.Address = dto.Address; newOrder.CityID = dto.CityID; newOrder.DistrictID = dto.DistrictID; newOrder.CountryID = dto.CountryID; newOrder.StoreId = d.Value.Product.StoreID; newOrder.ParentId = order.ID; newOrder.Total = newOrder.Price; var childDetails = _detail.GetByStoreId(d.Value.Product.StoreID, order.ID); foreach (var c in childDetails) { var child = _detail.GetById(c.ID); child.ModifiedDate = DateTime.Now; child.OrderID = newOrder.ID; _detail.EditDetail(child); var temp = d.Value; newOrder.Total = newOrder.Total + (c.Quantity.Value * c.Product.Price.Value); newOrder.Price = newOrder.Price + (c.Quantity.Value * c.Product.Weight); details.TryRemove(c.ID, out temp); } newOrder.Price = 5 * newOrder.Price; _repo.UpdateOrder(newOrder.Translate <OrderDTO, Order>()); var mess = new MessageDTO(); mess.Description = "checked out order " + newOrder.ID; mess.FromID = userId; mess.SentID = _repo.GetById(newOrder.ID).Store.UserID; mess.MessageType = STORE_ORDER; mess.DataID = newOrder.StoreId; _message.CreateMessage(mess); dto.Total = dto.Total + newOrder.Total; } dto.StoreId = null; dto.ParentId = null; var result = _repo.UpdateOrder(dto.Translate <OrderDTO, Order>()); return(result); } catch (Exception e) { Console.WriteLine(e.StackTrace); return(false); } } }