Exemplo n.º 1
0
        public override async Task <IActionResult> GetById(Guid id)
        {
            try
            {
                var userID = HttpContext.User.Identity.Name;
                if (userID == null)
                {
                    return(StatusCode(StatusCodes.Status401Unauthorized));
                }
                //if (!user.IsVerified) return StatusCode(StatusCodes.Status426UpgradeRequired);
                ApplicationUser user = await _context.Set <ApplicationUser>().SingleOrDefaultAsync(item => item.UserName == userID);

                if (user == null)
                {
                    return(BadRequest("Token is not related to any Account"));
                }

                var order = await _context.Set <Order>()
                            .Include(x => x.OrderItems)
                            .ThenInclude(x => x.Item)
                            .SingleOrDefaultAsync(x => x.ID == id);

                if (order == null)
                {
                    return(NotFound("id is not realted to any order"));
                }
                if (order.AccountID != user.AccountID)
                {
                    return(BadRequest("This Order is not related to the authorized user"));
                }
                if (order.Number == null)
                {
                    order.GenerateCode(_context as NavaraDbContext);
                    await _context.SaveChangesAsync();
                }

                var data = new OrderModel()
                {
                    ID             = order.ID,
                    Name           = order.Name,
                    FromTime       = order.FromTime?.ToShortTimeString(),
                    Location       = order.Location,
                    LocationRemark = order.LocationRemark,
                    Mobile         = order.Mobile,
                    Remark         = order.Remark,
                    ToTime         = order.ToTime?.ToShortTimeString(),
                    Code           = order.Code,
                    Date           = order.CreationDate,
                    TotalPrices    = order.OrderItems.Sum(y => (y.UnitPrice ?? 0) * (y.Quantity ?? 1)),
                    TotalDiscount  = order.OrderItems.Sum(y => y.UnitDiscount ?? 0),
                    NetTotalPrices = order.OrderItems.Sum(y => y.Total ?? 0),
                    Status         = order.Status,
                    UseWallet      = order.UseWallet,
                    WalletAmount   = order.WalletAmount,
                    DaysToDeliver  = order.DaysToDeliver,
                    InvoicePath    = order.InvoicePath,

                    OrderItems = order.OrderItems.Select(y => new OrderItemModel
                    {
                        OrderItemID        = y.ID,
                        Quantity           = y.Quantity,
                        Name               = y.Item?.Name,
                        UnitNetPrice       = y.UnitNetPrice,
                        ThumbnailImagePath = y.Item?.ThumbnailImagePath,
                        Total              = y.Total
                    }).ToList()
                };
                var promoCodDisc = ((order.Discount.GetValueOrDefault() / 100.0) * order.OrderItems.Sum(y => y.Total ?? 0));
                data.TotalDiscount  += promoCodDisc;
                data.NetTotalPrices -= promoCodDisc;
                var json = new JsonResult(data);
                #region add test Notification
                INotificationContext AppContext = _context as INotificationContext;
                if (AppContext != null)
                {
                    Notification notification = new Notification()
                    {
                        Body                 = "There are also many informal uses of this kind of letter, though they may not necessarily be officially titled as a “notification”.",
                        ObjectID             = (_context as NavaraDbContext)?.Items.FirstOrDefault().ID,
                        Subject              = "New Item arived",
                        RelatedToEnum        = NavaraNotificationRelatedTo.Item,
                        NotificationTypeEnum = NavaraNotificationType.NewItem
                    };
                    notification.AddStatus(AppContext, NotifyStatus.Sent, user.Id);
                    AppContext.Notifications.Add(notification);
                    AppContext.SaveChanges();
                }
                #endregion

                return(json);
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }