public async Task <IActionResult> Post([FromBody] EnvelopeModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } int organizationId = 0; var organizationIdVal = User.FindFirst(ClaimTypes.Country)?.Value; bool isValid = int.TryParse(organizationIdVal, out organizationId); if (organizationId == 0) { return(BadRequest("A bad attempt to access the envelope")); } decimal exchangeRate = 1; var dated = DateTime.Now; var rates = await exchangeRateService.GetCurrencyRatesForDate(dated); if (rates.Rates == null) { string apiKey = exchangeRateService.GetAPIKeyForOpenExchange(); rates = await exchangeRateHttpService.GetRatesAsync(apiKey); if (rates.Rates != null) { await exchangeRateService.SaveCurrencyRatesAsync(rates.Rates, DateTime.Now); exchangeRate = currencyService.GetExchangeRateForCurrency(model.Currency, rates.Rates); } } else { exchangeRate = currencyService.GetExchangeRateForCurrency(model.Currency, rates.Rates); } model.ExchangeRate = exchangeRate; var response = await envelopeService.AddAsync(model, organizationId); if (!response.Success) { return(BadRequest(response.Message)); } return(Ok(true)); }
public async Task <IActionResult> GetAverageCurrencyRateForDate([FromBody] ExRateFinderModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } ExchangeRatesView ratesView = null; int count = ratesService.GetAPIsCallsCount(); if (count >= 999) { return(Ok(null)); } if (model.Dated > DateTime.Now) { ratesView = await ratesService.GetCurrencyRatesForDate(DateTime.Now); } else { ratesView = await ratesService.GetCurrencyRatesForDate(model.Dated); } if (ratesView.Rates == null) { string apiKey = ratesService.GetAPIKeyForOpenExchange(); ratesView = await ratesHttpService.GetRatesAsync(apiKey); if (ratesView.Rates != null) { await ratesService.SaveCurrencyRatesAsync(ratesView.Rates, DateTime.Now); } } var rates = await ratesService.GetAverageCurrencyRatesForDate(model.Dated); return(Ok(rates)); }
public async Task <IActionResult> Get() { var defaultCurrencyObj = currencyService.GetDefaultCurrency(); if (defaultCurrencyObj == null) { return(BadRequest("Default currency is not set. Please contact administrator")); } string defaultCurrency = defaultCurrencyObj.Currency; decimal exchangeRate = 1; if (!string.IsNullOrEmpty(defaultCurrency)) { var dated = DateTime.Now; var rates = await ratesService.GetCurrencyRatesForDate(dated); if (rates.Rates == null) { string apiKey = ratesService.GetAPIKeyForOpenExchange(); rates = await ratesHttpService.GetRatesAsync(apiKey); if (rates.Rates != null) { await ratesService.SaveCurrencyRatesAsync(rates.Rates, DateTime.Now); exchangeRate = projectService.GetExchangeRateForCurrency(defaultCurrency, rates.Rates); } } else { exchangeRate = projectService.GetExchangeRateForCurrency(defaultCurrency, rates.Rates); } } var projects = projectService.GetAll(exchangeRate); return(Ok(projects)); }
public async Task <IActionResult> GetProjectsBudgetSummaryReport() { var defaultCurrencyObj = currencyService.GetDefaultCurrency(); if (defaultCurrencyObj == null) { return(BadRequest("Default currency is not set. Please contact administrator")); } string defaultCurrency = defaultCurrencyObj.Currency; decimal exchangeRate = 1; if (!string.IsNullOrEmpty(defaultCurrency)) { var dated = DateTime.Now; var rates = await ratesService.GetCurrencyRatesForDate(dated); if (rates.Rates == null) { string apiKey = ratesService.GetAPIKeyForOpenExchange(); rates = await ratesHttpService.GetRatesAsync(apiKey); if (rates.Rates != null) { await ratesService.SaveCurrencyRatesAsync(rates.Rates, DateTime.Now); exchangeRate = reportService.GetExchangeRateForCurrency(defaultCurrency, rates.Rates); } } else { exchangeRate = reportService.GetExchangeRateForCurrency(defaultCurrency, rates.Rates); } } var report = await reportService.GetProjectsBudgetReportSummary(clientUrl, defaultCurrency, exchangeRate); report.ReportSettings.DefaultCurrency = defaultCurrency; var response = excelService.GenerateProjectBudgetReportSummary(report); if (response.Success) { report.ReportSettings.ExcelReportName = response.Message; } return(Ok(report)); }