public async Task <ActionResult <double> > CalculatePrice(CustomOrderDTO customOrderDTO)
        {
            var volume = await _modelVolumeRepository.getModelVolume(customOrderDTO.modelId);

            var email = HttpContext.User?.Claims?.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value;
            var user  = await _userManager.FindByEmailAsync(email);

            if (volume == -1)
            {
                string folder   = Path.Combine(_webHostEnvironment.WebRootPath, "customusermodels", user.Id);
                string fileName = customOrderDTO.modelId;
                string filePath = Path.Combine(folder, fileName);
                volume = await _priceCalculatorService.getModelVolume(filePath);

                await _modelVolumeRepository.addModelVolume(new ModelVolumes
                {
                    Id     = customOrderDTO.modelId,
                    Volume = volume
                });
            }


            var price = await _priceCalculatorService.
                        calculatePrice(volume, customOrderDTO.FilamentId, customOrderDTO.fillingPercent);

            Console.WriteLine("OTO CENA:");
            Console.WriteLine(price);
            return(price);
        }
Example #2
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int delvieryMethod, string basketId, OrderAddress shippingAdress)
        {
            Order toAdd = new Order
            {
                BuyerEmail     = buyerEmail,
                ShipToAddress  = shippingAdress,
                DeliveryMethod = _unitOfWork._deliveryMethods.GetById(delvieryMethod)
            };
            var              basket        = _basketRepo.GetBasketAsync(basketId).Result;
            var              bCustomOrders = _basketRepo.GetBasketAsync(basketId).Result.CustomOrderItems;
            decimal          price         = 0;
            List <OrderItem> oItems        = new List <OrderItem>();

            foreach (var item in basket.BasketItems)
            {
                var prodItemOrdered = new ProductItemOrdered();
                prodItemOrdered.PictureUrl    = item.PictureUrl;
                prodItemOrdered.ProductItemId = item.Id;
                prodItemOrdered.ProductName   = item.ProductName;
                var orderItem = new OrderItem();
                orderItem.ItemOrdered = prodItemOrdered;
                orderItem.Price       = _unitOfWork._products.GetById(item.Id).Price;
                orderItem.Quantity    = item.Quantity;
                price += orderItem.Price * orderItem.Quantity;
                oItems.Add(orderItem);
            }
            List <CustomOrderItem> bcustomItems = new List <CustomOrderItem>();

            foreach (var item in basket.CustomOrderItems)
            {
                var volume = await _modelVolumeRepository.getModelVolume(item.ModelName);

                var modelPrice = await _priceCalculatorService.calculatePrice(volume, item.FilamentId, (double)item.FilingPercent / 100);

                Console.WriteLine("CENA MODELU PRZED ZAOKRAGLENIEM");
                Console.WriteLine(modelPrice);
                modelPrice = Math.Round(modelPrice, 2);
                Console.WriteLine("CENA MODELU PO:");
                Console.WriteLine(modelPrice);
                CustomOrderItem newCustomItem = new CustomOrderItem
                {
                    UsersId       = item.UsersId,
                    ModelName     = item.ModelName,
                    Quantity      = item.Quantity,
                    FilamentId    = item.FilamentId,
                    FilingPercent = item.FilingPercent,
                    ColorId       = item.ColorId,
                    Price         = Convert.ToDecimal(modelPrice)
                };
                bcustomItems.Add(newCustomItem);
                price += Convert.ToDecimal(modelPrice * item.Quantity);
            }
            toAdd.CustomOrderItems = bcustomItems;
            toAdd.OrderItems       = oItems;
            toAdd.Subtotal         = price;
            _unitOfWork._orders.Add(toAdd);
            var result = await _unitOfWork.Complete();

            if (result < 1)
            {
                return(null);
            }

            return(toAdd);
        }