public async Task <ActionResult> fetchPrice(long id, [FromQuery] string currency, [FromQuery] string area) { try { FetchCustomizedProductPriceModelView fetchPrice = new FetchCustomizedProductPriceModelView(); fetchPrice.id = id; fetchPrice.currency = currency; fetchPrice.area = area; CustomizedProductFinalPriceModelView customizedProductPrice = await new core.application.CustomizedProductController().calculateCustomizedProductPrice(fetchPrice, clientFactory); return(Ok(customizedProductPrice)); } catch (ResourceNotFoundException e) { return(NotFound(new SimpleJSONMessageService(e.Message))); } catch (ArgumentException e) { return(BadRequest(new SimpleJSONMessageService(e.Message))); } catch (Exception) { return(StatusCode(500, new SimpleJSONMessageService(UNEXPECTED_ERROR))); } }
/// <summary> /// Calculates the price /// </summary> /// <param name="fetchCustomizedProductPrice">FetchCustomizedProductModelView with the necessary information to fetch the customized product's price</param> /// <returns>CustomizedProductPriceModelView with the price of the customized product</returns> public static async Task <CustomizedProductFinalPriceModelView> calculatePrice(FetchCustomizedProductPriceModelView fetchCustomizedProductPrice, IHttpClientFactory clientFactory) { if (fetchCustomizedProductPrice.currency != null) { CurrenciesService.checkCurrencySupport(fetchCustomizedProductPrice.currency); } if (fetchCustomizedProductPrice.area != null) { AreasService.checkAreaSupport(fetchCustomizedProductPrice.area); } CustomizedProduct customizedProduct = PersistenceContext.repositories().createCustomizedProductRepository().find(fetchCustomizedProductPrice.id); if (customizedProduct == null) { throw new ResourceNotFoundException ( string.Format(CUSTOMIZED_PRODUCT_NOT_FOUND, fetchCustomizedProductPrice.id) ); } if (customizedProduct.status == CustomizationStatus.PENDING) { throw new ArgumentException ( CUSTOMIZED_PRODUCT_NOT_FINISHED ); } //TODO Should the fetching of prices of customized products that aren't base products be allowed? MaterialPriceTableRepository materialPriceTableRepository = PersistenceContext.repositories().createMaterialPriceTableRepository(); FinishPriceTableRepository finishPriceTableRepository = PersistenceContext.repositories().createFinishPriceTableRepository(); CustomizedProductFinalPriceModelView customizedProductTotalPrice = new CustomizedProductFinalPriceModelView(); List <CustomizedProductPriceModelView> customizedProductPriceList = new List <CustomizedProductPriceModelView>(); MaterialPriceTableEntry materialPriceTableEntry = getCurrentMaterialPrice(materialPriceTableRepository, customizedProduct.customizedMaterial.material.Id); FinishPriceTableEntry finishPriceTableEntry = null; if (customizedProduct.customizedMaterial.finish != null) { finishPriceTableEntry = getCurrentFinishPrice(finishPriceTableRepository, customizedProduct.customizedMaterial.material.Finishes.Where(f => f.Equals(customizedProduct.customizedMaterial.finish)).SingleOrDefault().Id); } //TODO What should we do about doors? //TODO Should we consider that every component has a box geometry? //!For now the surface area of every product is being calculated as if it the product was a SIX FACED RIGHT RECTANGULAR PRISM CustomizedProductPriceModelView parentCustomizedProductModelView = await buildCustomizedProductPriceModelView(customizedProduct, fetchCustomizedProductPrice, materialPriceTableEntry, finishPriceTableEntry, clientFactory); customizedProductPriceList.Add(parentCustomizedProductModelView); if (customizedProduct.hasCustomizedProducts()) { List <CustomizedProductPriceModelView> childCustomizedProducts = new List <CustomizedProductPriceModelView>(); customizedProductPriceList.AddRange(await calculatePricesOfChildCustomizedProducts(childCustomizedProducts, customizedProduct, fetchCustomizedProductPrice, materialPriceTableRepository, finishPriceTableRepository, clientFactory)); } customizedProductTotalPrice.customizedProducts = customizedProductPriceList; customizedProductTotalPrice.finalPrice = calculateFinalPriceOfCustomizedProduct(customizedProductTotalPrice, fetchCustomizedProductPrice); return(customizedProductTotalPrice); }
/// <summary> /// Builds a CustomizedProductPriceModelView out of a Customized Product /// </summary> /// <param name="customizedProduct">Customized Product to build the model view out of</param> /// <param name="fetchCustomizedProductPriceModelView">ModelView to know if currency/area conversion is needed</param> /// <returns>CustomizedProductPriceModelView</returns> private static async Task <CustomizedProductPriceModelView> buildCustomizedProductPriceModelView(CustomizedProduct customizedProduct, FetchCustomizedProductPriceModelView fetchCustomizedProductPriceModelView, MaterialPriceTableEntry materialPriceTableEntry, FinishPriceTableEntry finishPriceTableEntry, IHttpClientFactory clientFactory) { CustomizedProductPriceModelView customizedProductPriceModelView = new CustomizedProductPriceModelView(); string defaultCurrency = CurrencyPerAreaConversionService.getBaseCurrency(); string defaultArea = CurrencyPerAreaConversionService.getBaseArea(); bool convertCurrencyPerArea = fetchCustomizedProductPriceModelView.currency != null && fetchCustomizedProductPriceModelView.area != null; customizedProductPriceModelView.customizedProductId = customizedProduct.Id; customizedProductPriceModelView.reference = customizedProduct.reference; customizedProductPriceModelView.productId = customizedProduct.product.Id; string requestedMeasurementUnit = null; if (convertCurrencyPerArea) { requestedMeasurementUnit = new String(fetchCustomizedProductPriceModelView.area.Where(c => Char.IsLetter(c)).ToArray()); } else { requestedMeasurementUnit = new String(CurrencyPerAreaConversionService.getBaseArea().Where(c => Char.IsLetter(c)).ToArray()); } customizedProductPriceModelView.customizedDimensions = new GetCustomizedDimensionsModelView(); customizedProductPriceModelView.customizedDimensions.unit = requestedMeasurementUnit; customizedProductPriceModelView.customizedDimensions.width = MeasurementUnitService.convertToUnit(customizedProduct.customizedDimensions.width, requestedMeasurementUnit); customizedProductPriceModelView.customizedDimensions.height = MeasurementUnitService.convertToUnit(customizedProduct.customizedDimensions.height, requestedMeasurementUnit); customizedProductPriceModelView.customizedDimensions.depth = MeasurementUnitService.convertToUnit(customizedProduct.customizedDimensions.depth, requestedMeasurementUnit); customizedProductPriceModelView.customizedMaterial = new CustomizedMaterialPriceModelView(); customizedProductPriceModelView.customizedMaterial.customizedMaterialId = customizedProduct.customizedMaterial.Id; customizedProductPriceModelView.customizedMaterial.materialId = customizedProduct.customizedMaterial.material.Id; if (customizedProduct.customizedMaterial.finish != null) { customizedProductPriceModelView.customizedMaterial.finish = new FinishPriceModelView(); customizedProductPriceModelView.customizedMaterial.finish.finishId = customizedProduct.customizedMaterial.finish.Id; customizedProductPriceModelView.customizedMaterial.finish.description = customizedProduct.customizedMaterial.finish.description; customizedProductPriceModelView.customizedMaterial.finish.shininess = customizedProduct.customizedMaterial.finish.shininess; customizedProductPriceModelView.customizedMaterial.finish.price = new PriceModelView(); if (convertCurrencyPerArea) { customizedProductPriceModelView.customizedMaterial.finish.price.currency = fetchCustomizedProductPriceModelView.currency; customizedProductPriceModelView.customizedMaterial.finish.price.area = fetchCustomizedProductPriceModelView.area; customizedProductPriceModelView.customizedMaterial.finish.price.value = await convertPriceValue( finishPriceTableEntry.price.value, fetchCustomizedProductPriceModelView.currency, fetchCustomizedProductPriceModelView.area, clientFactory ); } else { customizedProductPriceModelView.customizedMaterial.finish.price.currency = defaultCurrency; customizedProductPriceModelView.customizedMaterial.finish.price.area = defaultArea; customizedProductPriceModelView.customizedMaterial.finish.price.value = finishPriceTableEntry.price.value; } } if (customizedProduct.customizedMaterial.color != null) { customizedProductPriceModelView.customizedMaterial.color = ColorModelViewService.fromEntity(customizedProduct.customizedMaterial.color); } customizedProductPriceModelView.customizedMaterial.price = new PriceModelView(); if (convertCurrencyPerArea) { customizedProductPriceModelView.customizedMaterial.price.currency = fetchCustomizedProductPriceModelView.currency; customizedProductPriceModelView.customizedMaterial.price.area = fetchCustomizedProductPriceModelView.area; customizedProductPriceModelView.customizedMaterial.price.value = await convertPriceValue( materialPriceTableEntry.price.value, fetchCustomizedProductPriceModelView.currency, fetchCustomizedProductPriceModelView.area, clientFactory ); } else { customizedProductPriceModelView.customizedMaterial.price.currency = defaultCurrency; customizedProductPriceModelView.customizedMaterial.price.area = defaultArea; customizedProductPriceModelView.customizedMaterial.price.value = materialPriceTableEntry.price.value; } customizedProductPriceModelView.price = new PriceModelView(); customizedProductPriceModelView.totalArea = new AreaModelView(); if (convertCurrencyPerArea) { customizedProductPriceModelView.price.currency = fetchCustomizedProductPriceModelView.currency; customizedProductPriceModelView.totalArea.area = fetchCustomizedProductPriceModelView.area; } else { customizedProductPriceModelView.price.currency = defaultCurrency; customizedProductPriceModelView.totalArea.area = defaultArea; } calculateTotalAreaAndPriceOfCustomizedMaterial(customizedProductPriceModelView, customizedProduct); return(customizedProductPriceModelView); }
/// <summary> /// Calculates the price of all child customized products of a given customized product /// </summary> /// <param name="customizedProduct">Parent Customized Product</param> /// <param name="fetchCustomizedProductPrice">Information about currency/area conversion</param> /// <param name="materialPriceTableRepository">MaterialPriceTableRepository instance to fetch current material prices</param> /// <param name="finishPriceTableRepository">FinishPriceTableRepository instance to fetch current finish prices</param> /// <param name="clientFactory">Injected HTTP Client Factory</param> /// <returns>IEnumerable containing the prices of all child customized products</returns> private static async Task <IEnumerable <CustomizedProductPriceModelView> > calculatePricesOfChildCustomizedProducts(List <CustomizedProductPriceModelView> childCustomizedProductPrices, CustomizedProduct customizedProduct, FetchCustomizedProductPriceModelView fetchCustomizedProductPrice, MaterialPriceTableRepository materialPriceTableRepository, FinishPriceTableRepository finishPriceTableRepository, IHttpClientFactory clientFactory) { foreach (Slot slot in customizedProduct.slots) { foreach (CustomizedProduct childCustomizedProduct in slot.customizedProducts) { MaterialPriceTableEntry materialPriceTableEntry = getCurrentMaterialPrice(materialPriceTableRepository, childCustomizedProduct.customizedMaterial.material.Id); FinishPriceTableEntry finishPriceTableEntry = null; if (childCustomizedProduct.customizedMaterial.finish != null) { finishPriceTableEntry = getCurrentFinishPrice(finishPriceTableRepository, childCustomizedProduct.customizedMaterial.material.Finishes.Where(f => f.Equals(childCustomizedProduct.customizedMaterial.finish)).SingleOrDefault().Id); } CustomizedProductPriceModelView childCustomizedProductPriceModelView = await buildCustomizedProductPriceModelView(childCustomizedProduct, fetchCustomizedProductPrice, materialPriceTableEntry, finishPriceTableEntry, clientFactory); childCustomizedProductPrices.Add(childCustomizedProductPriceModelView); if (childCustomizedProduct.hasCustomizedProducts()) { await calculatePricesOfChildCustomizedProducts(childCustomizedProductPrices, childCustomizedProduct, fetchCustomizedProductPrice, materialPriceTableRepository, finishPriceTableRepository, clientFactory); } } } return(childCustomizedProductPrices); }
/// <summary> /// Calculates the final price of a customized product /// </summary> /// <param name="customizedProductTotalPriceModelView">ModelView containing the detailed info about the customized product's price</param> /// <param name="fetchCustomizedProductPriceModelView">ModelView containing information about currency/area conversion</param> /// <returns>PriceModelView containing the final price of a customized product</returns> private static PriceModelView calculateFinalPriceOfCustomizedProduct(CustomizedProductFinalPriceModelView customizedProductTotalPriceModelView, FetchCustomizedProductPriceModelView fetchCustomizedProductPriceModelView) { PriceModelView finalPriceModelView = new PriceModelView(); double finalPrice = 0; foreach (CustomizedProductPriceModelView customizedProductPrice in customizedProductTotalPriceModelView.customizedProducts) { finalPrice += customizedProductPrice.price.value; } finalPriceModelView.value = finalPrice; if (fetchCustomizedProductPriceModelView.currency != null && fetchCustomizedProductPriceModelView.area != null) { finalPriceModelView.currency = fetchCustomizedProductPriceModelView.currency; } else { finalPriceModelView.currency = CurrencyPerAreaConversionService.getBaseCurrency(); } return(finalPriceModelView); }
/// <summary> /// Calculates the price of a customized product /// </summary> /// <param name="fetchCustomizedProductPrice">FetchCustomizedProductPriceModelView with necessary information to fetch the customized product's price</param> /// <returns>CustomizedProductPriceModelView with the customized product's price</returns> public async Task <CustomizedProductFinalPriceModelView> calculateCustomizedProductPrice(FetchCustomizedProductPriceModelView fetchCustomizedProductPrice, IHttpClientFactory clientFactory) { return(await CustomizedProductPriceService.calculatePrice(fetchCustomizedProductPrice, clientFactory)); }