public async Task <bool> Create(OrderServiceModel orderService)
        {
            //IsProductAlreadyOrderedResult to increase quantity
            var IsProductAlreadyOrderedResult = await IsProductAlreadyOrdered(orderService);

            if (IsProductAlreadyOrderedResult)
            {
                return(true);
            }

            Order order = orderService.To <Order>();

            var issuedOn = DateTime.UtcNow;

            order.Quantity = orderQuantity;
            order.IssuedOn = DateTime.UtcNow;

            order.OrderStatus = await this.context.OrderStatuses
                                .SingleOrDefaultAsync(os => os.Name == ServicesGlobalConstants.OrderStatusActive);

            await this.context.Orders.AddAsync(order);

            var result = await this.context.SaveChangesAsync();

            return(result > num);
        }
        public async Task <OrderServiceModel> CreateAsync(OrderServiceModel orderServiceModel, string username, decimal deliveryPrice)
        {
            var shoppingCartProducts = this.shoppingCartService
                                       .GetAllShoppingCartProducts(username)
                                       .ToList();

            if (shoppingCartProducts.Count == 0)
            {
                throw new ArgumentNullException(nameof(shoppingCartProducts));
            }

            var user     = this.userService.GetUserByUsername(username);
            var supplier = this.supplierService.GetSupplierById(orderServiceModel.SupplierId);

            Order order = orderServiceModel.To <Order>();

            foreach (var shoppingCartProduct in shoppingCartProducts)
            {
                order.OrderProducts.Add(new OrderProduct
                {
                    Order     = order,
                    ProductId = shoppingCartProduct.ProductId,
                    Price     = shoppingCartProduct.Product.Price,
                    Quantity  = shoppingCartProduct.Quantity
                });

                await this.productService.DecreaseQuantityInStock(shoppingCartProduct.ProductId,
                                                                  shoppingCartProduct.Quantity);
            }

            order.OrderProducts = shoppingCartProducts
                                  .Select(scp => new OrderProduct
            {
                Order     = order,
                ProductId = scp.ProductId,
                Price     = scp.Product.Price,
                Quantity  = scp.Quantity
            })
                                  .ToList();

            await this.shoppingCartService.RemoveAllProductFromShoppingCart(username);

            OrderStatus   orderStatus   = this.context.OrderStatuses.FirstOrDefault(x => x.Name == GlobalConstants.Unprocessed) ?? throw new ArgumentNullException(nameof(orderStatus));
            PaymentStatus paymentStatus = this.context.PaymentStatuses.FirstOrDefault(x => x.Name == GlobalConstants.Unpaid) ?? throw new ArgumentNullException(nameof(paymentStatus));

            order.DeliveryPrice        = deliveryPrice;
            order.OrderDate            = DateTime.UtcNow;
            order.UserId               = user.Id;
            order.OrderStatusId        = orderStatus.Id;
            order.PaymentStatusId      = paymentStatus.Id;
            order.TotalPrice           = order.OrderProducts.Sum(product => product.Price * product.Quantity);
            order.ExpectedDeliveryDate = DateTime.UtcNow.AddDays(supplier.DeliveryTimeInDays);

            this.context.Orders.Add(order);
            this.context.SaveChanges();

            return(order.To <OrderServiceModel>());
        }
示例#3
0
        public async Task <bool> CreateOrder(OrderServiceModel orderServiceModel)
        {
            Order order = orderServiceModel.To <Order>();

            order.OrderStatus = await this.context.OrderStatuses.SingleOrDefaultAsync(orderStatus => orderStatus.Name == GlobalConstants.OrderStatusActive);

            order.CreatedOn = DateTime.UtcNow;

            this.context.Orders.Add(order);
            int result = await this.context.SaveChangesAsync();

            return(result > 0);
        }
示例#4
0
        public async Task <IActionResult> Order(string id)
        //TODO: add quantity in details view
        {
            var inputModel = new  OrderServiceModel();
            OrderServiceModel orderServiceModel = inputModel.To <OrderServiceModel>();

            orderServiceModel.IssuerId = await Task.Run(() => this.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            orderServiceModel.ProductId = id;
            await this.ordersService.Create(orderServiceModel);

            return(this.RedirectToAction("Cart", "Orders"));
        }
示例#5
0
        public async Task <bool> CreateOrderAsync(OrderServiceModel orderServiceModel)
        {
            Order order = orderServiceModel.To <Order>();

            order.Status = await finalWebProjectDbContext.OrderStatuses
                           .SingleOrDefaultAsync(orderStatus => orderStatus.Name == "Active");

            order.IssuedOn = DateTime.UtcNow;

            this.finalWebProjectDbContext.Orders.Add(order);
            int result = await this.finalWebProjectDbContext.SaveChangesAsync();

            return(result > 0);
        }
示例#6
0
        public async Task <bool> CreateOrder(OrderServiceModel orderServiceModel)
        {
            var order = orderServiceModel.To <Order>();

            order.Id     = Guid.NewGuid().ToString();
            order.Status = await this.context.OrderStatuses.FirstOrDefaultAsync(status => status.Name == "Active");

            order.IssuedOn = DateTime.UtcNow;

            await this.context.Orders.AddAsync(order);

            var result = await this.context.SaveChangesAsync();

            return(result > 0);
        }
示例#7
0
        public OrderConfirmViewModel Create(OrderServiceModel orderServiceModel, string userId)
        {
            var user = this.context.Users.FirstOrDefault(x => x.Id == userId);

            Order order          = new Order();
            Order orderFromInput = orderServiceModel.To <Order>();

            order.ClientName      = orderFromInput.ClientName;
            order.DeliveryAddress = orderFromInput.DeliveryAddress;
            order.Comment         = orderFromInput.Comment;
            order.ClientId        = user.Id;
            order.Client          = user;
            order.DateOfCreation  = DateTime.UtcNow;
            order.OrderStage      = Models.Enums.OrderStage.Active;

            HashSet <ShoppingBagTyre> bagTyres = new HashSet <ShoppingBagTyre>();

            var sum = 0m;

            foreach (var tyre in this.context.ShoppingBagTyres.Where(x => x.UserId == user.Id))
            {
                bagTyres.Add(tyre);
            }

            foreach (var tyre in bagTyres)
            {
                var orderTyre = new OrderTyre()
                {
                    OrderId  = order.Id,
                    UserId   = user.Id,
                    TyreId   = tyre.TyreId,
                    Model    = tyre.Model,
                    Brand    = tyre.Brand,
                    Price    = tyre.Price,
                    Quantity = tyre.Quantity
                };

                sum += orderTyre.Price * orderTyre.Quantity;

                order.Tyres.Add(orderTyre);
                this.context.OrderTyres.Add(orderTyre);
            }

            foreach (var wheelRim in this.context.ShoppingBagWheelRims.Where(x => x.UserId == user.Id))
            {
                var orderWheelRim = new OrderWheelRim()
                {
                    OrderId    = order.Id,
                    UserId     = user.Id,
                    WheelRimId = wheelRim.WheelRimId,
                    Model      = wheelRim.Model,
                    Brand      = wheelRim.Brand,
                    Price      = wheelRim.Price,
                    Quantity   = wheelRim.Quantity
                };

                sum += orderWheelRim.Price * orderWheelRim.Quantity;

                order.WheelRims.Add(orderWheelRim);
                this.context.OrderWheelRims.Add(orderWheelRim);
            }

            foreach (var motorOil in this.context.ShoppingBagMotorOils.Where(x => x.UserId == user.Id))
            {
                var orderMotorOil = new OrderMotorOil()
                {
                    OrderId    = order.Id,
                    UserId     = user.Id,
                    MotorOilId = motorOil.MotorOilId,
                    Model      = motorOil.Model,
                    Brand      = motorOil.Brand,
                    Price      = motorOil.Price,
                    Quantity   = motorOil.Quantity
                };

                sum += orderMotorOil.Price * orderMotorOil.Quantity;

                order.MotorOils.Add(orderMotorOil);
                this.context.OrderMotorOils.Add(orderMotorOil);
            }

            order.Sum = sum;

            user.Orders.Add(order);

            this.context.Orders.Add(order);
            this.context.SaveChanges();

            var orderConfirmViewModel = new OrderConfirmViewModel()
            {
                Id              = order.Id,
                ClientName      = order.ClientName,
                DeliveryAddress = order.DeliveryAddress,
                Comment         = order.Comment,
                ClientId        = order.ClientId,
                Client          = order.Client,
                DateOfCreation  = order.DateOfCreation
            };

            orderConfirmViewModel.Tyres     = order.Tyres;
            orderConfirmViewModel.WheelRims = order.WheelRims;

            return(orderConfirmViewModel);
        }