/// <summary> /// Converts a given value in a given currency per area to another currency per area /// </summary> /// <param name="valueToConvert">Value to convert</param> /// <param name="fromCurrency">Currency to convert from</param> /// <param name="toCurrency">Currency to convert to</param> /// <param name="fromArea">Area to convert from</param> /// <param name="toArea">Area to convert to</param> /// <returns>Converted value to requested currency per area</returns> public async Task <double> convertCurrencyPerArea(double valueToConvert, string fromCurrency, string toCurrency, string fromArea, string toArea) { HttpClient client = clientFactory.CreateClient("CurrencyConversion"); String getRequest = String.Format(client.BaseAddress.OriginalString + "/currency?from={0}&to={1}", fromCurrency, toCurrency); HttpResponseMessage response = await client.GetAsync(getRequest); if (response.IsSuccessStatusCode) { CurrencyConversionInfoDTO currencyConversionInfoDTO = JsonConvert.DeserializeObject <CurrencyConversionInfoDTO>(await response.Content.ReadAsStringAsync()); double minimumMeasurementUnitValue = MeasurementUnitService.convertFromUnit (UNIT, new String(fromArea.Where(c => Char.IsLetter(c)).ToArray())); double areaConversionFactor = Math.Pow( MeasurementUnitService.convertToUnit( minimumMeasurementUnitValue, new String(toArea.Where(c => Char.IsLetter(c)).ToArray()) ), SQUARE); double convertedValue = currencyConversionInfoDTO.rate * valueToConvert * 1 / areaConversionFactor; return(Math.Round(convertedValue, DECIMAL_PLACES)); } else { throw new HttpRequestException(FAILED_TO_CONVERT); } }
/// <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); }