예제 #1
0
        public static AlterOrderResponse AlterOrder_Admin(AlterOrderRequestModel source)
        {
            using (var dbContext = new DeliversEntities())
            {
                var response = new AlterOrderResponse {
                    isSuccesss = true
                };
                var totalAmount = source.Items.Sum(i => ItemDetailsService.GetItemDetailLocalById(i.itemId).Price *i.quantity);
                if (totalAmount <= 0)
                {
                    response.isSuccesss = false;
                    response.Message    = "Total amount can not be 0";
                    return(response);
                }
                var order = dbContext.Orders.FirstOrDefault(o => o.Id == source.OrderId);
                if (order != null)
                {
                    if (order.Status == OrderHistoryEnu.Deliverd.Value || order.Status == OrderHistoryEnu.PickedUp.Value)
                    {
                        response.isSuccesss = false;
                        response.Message    = "Can not change order with status: " + order.Status;
                        return(response);
                    }

                    if (order.Amount != totalAmount)
                    {
                        order.Amount    = totalAmount;
                        order.UpdatedAt = CommonService.GetSystemTime();
                        if (order.OrderDetails != null && order.OrderDetails.Any())
                        {
                            foreach (var det in order.OrderDetails)
                            {
                                var newQ = source.Items.FirstOrDefault(i => i.itemId == det.ItemId).quantity;
                                if (newQ != det.Quantity)
                                {
                                    det.Quantity = newQ;
                                }
                            }
                            dbContext.OrderHistories.Add(new OrderHistory {
                                DateTime  = CommonService.GetSystemTime(),
                                IsCurrent = false,
                                OrderId   = source.OrderId,
                                Status    = OrderHistoryEnu.OrderAltered.Value,
                                Comments  = source.Comments
                            });
                            dbContext.SaveChanges();
                        }
                    }
                }
                return(response);
            }
        }
예제 #2
0
 public static string Place(PlaceOrderRequestModel request, string orderBy)
 {
     using (var dbContext = new DeliversEntities())
     {
         var totalAmount =
             request.Items.Sum(i => ItemDetailsService.GetItemDetailLocalById(i.ItemId).Price *i.Quantity);
         var order = new Order
         {
             Address       = request.Address,
             Instructions  = request.Instructions,
             Status        = OrderHistoryEnu.WaitingForPickup.Value,
             OrderBy       = orderBy,
             Amount        = totalAmount,
             DateTime      = DateTime.Now,
             EstimatedTime = "45 minutes away",
         };
         dbContext.Orders.Add(order);
         foreach (var item in request.Items)
         {
             var itmObj = new OrderDetail
             {
                 OrderId  = order.Id,
                 ItemId   = item.ItemId,
                 Quantity = item.Quantity,
             };
             dbContext.OrderDetails.Add(itmObj);
         }
         dbContext.OrderHistories.Add(new OrderHistory
         {
             OrderId  = order.Id,
             DateTime = DateTime.Now,
             MovedBy  = orderBy,
             Status   = OrderHistoryEnu.WaitingForPickup.Value
         });
         dbContext.SaveChanges();
         return(order.EstimatedTime);
     }
 }
예제 #3
0
        public static PlaceOrderResponseModel Place(PlaceOrderRequestModel request, string orderBy)
        {
            DbGeography userLoc       = null;
            var         orderstatus   = OrderHistoryEnu.Placed.Value;
            var         estimatedTime = "45 minutes away";
            var         orderIds      = new List <string>();

            if (!string.IsNullOrEmpty(request.Cords) && request.Cords != "")
            {
                var latlng = request.Cords.Split('_').ToList();
                if (latlng.Count == 2)
                {
                    userLoc = CommonService.ConvertLatLonToDbGeography(latlng[1], latlng[0]); // lat _ lng
                }
            }


            using (var dbContext = new DeliversEntities())
            {
                foreach (var item in request.Items)
                {
                    var dbItem = dbContext.ItemDetails.FirstOrDefault(i => i.Id == item.ItemId);
                    if (dbItem != null)
                    {
                        item.RestId = dbItem.ListItemId;
                    }
                }
            }


            var rest_groups = request.Items.GroupBy(item => item.RestId).ToList();
            var serial      = Guid.NewGuid().ToString().Substring(0, 6).ToUpper();

            using (var dbContext = new DeliversEntities())
            {
                foreach (var localG in rest_groups)
                {
                    var localList   = localG.ToList();
                    var totalAmount = localList.Sum(i => ItemDetailsService.GetItemDetailLocalById(i.ItemId).Price *i.Quantity);
                    var order       = new Order
                    {
                        Address       = request.Address,
                        Instructions  = request.Instructions,
                        Status        = orderstatus,
                        OrderBy       = orderBy,
                        Amount        = totalAmount,
                        DateTime      = CommonService.GetSystemTime(),
                        Cords         = userLoc,
                        EstimatedTime = estimatedTime,
                        SerialNo      = serial,
                        PickedBy      = Guid.Empty.ToString(),
                        UpdatedAt     = CommonService.GetSystemTime(),
                        DeliveryCost  = CommonService.GetDeliveryAmount()
                    };
                    dbContext.Orders.Add(order);

                    dbContext.OrderHistories.Add(new OrderHistory
                    {
                        OrderId   = order.Id,
                        DateTime  = CommonService.GetSystemTime(),
                        Status    = orderstatus,
                        IsCurrent = true
                    });
                    foreach (var item in localList)
                    {
                        var itmObj = new OrderDetail
                        {
                            OrderId  = order.Id,
                            ItemId   = item.ItemId,
                            Quantity = item.Quantity,
                            RestId   = item.RestId
                        };
                        dbContext.OrderDetails.Add(itmObj);
                    }
                    dbContext.SaveChanges();

                    // generate notification
                    NotificationService.ProcessNotificationRequest(orderstatus, order.Id);
                    orderIds.Add(order.Id.ToString());
                }

                return(new PlaceOrderResponseModel {
                    EstimatedTime = estimatedTime, OrderIds = orderIds, SerailNo = serial
                });
            }
        }