/* * * (c) Copyright Ascensio System Limited 2010-2018 * * This program is freeware. You can redistribute it and/or modify it under the terms of the GNU * General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html). * In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that * Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights. * * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR * FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html * * You can contact Ascensio System SIA by email at sales@onlyoffice.com * * The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display * Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3. * * Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains * relevant author attributions when distributing the software. If the display of the logo in its graphic * form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE" * in every copy of the program you distribute. * Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks. * */ using System; using System.Collections.Generic; using System.Linq; using ASC.Api.CRM; using ASC.Core.Common.Settings; using ASC.CRM.ApiModels; using ASC.CRM.Core; using ASC.CRM.Core.Dao; using ASC.CRM.Resources; using ASC.MessagingSystem; using ASC.Web.Api.Routing; using ASC.Web.CRM.Classes; using AutoMapper; using Microsoft.AspNetCore.Mvc; namespace ASC.CRM.Api { public class CurrencyRatesController : BaseApiController { private readonly CurrencyProvider _currencyProvider; private readonly SettingsManager _settingsManager; private readonly Global _global; private readonly MessageService _messageService; public CurrencyRatesController(CrmSecurity crmSecurity, DaoFactory daoFactory, MessageService messageService, SettingsManager settingsManager, Global global, CurrencyProvider currencyProvider, IMapper mapper) : base(daoFactory, crmSecurity, mapper) { _messageService = messageService; _settingsManager = settingsManager; _global = global; _currencyProvider = currencyProvider; } //TABLE `crm_currency_rate` column `rate` DECIMAL(10,2) NOT NULL public const decimal MaxRateValue = (decimal)99999999.99; /// /// Get the list of currency rates /// /// Get currency rates list /// Common /// /// List of currency rates /// [Read(@"currency/rates")] public IEnumerable GetCurrencyRates() { return _daoFactory.GetCurrencyRateDao().GetAll().ConvertAll(x => _mapper.Map(x)); } /// /// Get currency rate by id /// /// Get currency rate /// Common /// /// Currency rate /// /// [Read(@"currency/rates/{id:int}")] public CurrencyRateDto GetCurrencyRate(int id) { if (id <= 0) throw new ArgumentException(); var currencyRate = _daoFactory.GetCurrencyRateDao().GetByID(id); return _mapper.Map(currencyRate); } /// /// Get currency rate by currencies /// /// Get currency rate /// Common /// /// Currency rate /// /// [Read(@"currency/rates/{fromCurrency}/{toCurrency}")] public CurrencyRateDto GetCurrencyRate(string fromCurrency, string toCurrency) { if (string.IsNullOrEmpty(fromCurrency) || string.IsNullOrEmpty(toCurrency)) throw new ArgumentException(); var currencyRate = _daoFactory.GetCurrencyRateDao().GetByCurrencies(fromCurrency, toCurrency); return _mapper.Map(currencyRate); } /// /// Create new currency rate object /// /// /// Common /// [Create(@"currency/rates")] public CurrencyRateDto CreateCurrencyRate( [FromForm] string fromCurrency, [FromForm] string toCurrency, [FromForm] decimal rate) { ValidateRate(rate); ValidateCurrencies(new[] { fromCurrency, toCurrency }); var currencyRate = new CurrencyRate { FromCurrency = fromCurrency, ToCurrency = toCurrency, Rate = rate }; currencyRate.ID = _daoFactory.GetCurrencyRateDao().SaveOrUpdate(currencyRate); _messageService.Send(MessageAction.CurrencyRateUpdated, fromCurrency, toCurrency); return _mapper.Map(currencyRate); } /// /// Update currency rate object /// /// /// Common /// [Update(@"currency/rates/{id:int}")] public CurrencyRateDto UpdateCurrencyRate(int id, string fromCurrency, string toCurrency, decimal rate) { if (id <= 0) throw new ArgumentException(); ValidateRate(rate); ValidateCurrencies(new[] { fromCurrency, toCurrency }); var currencyRate = _daoFactory.GetCurrencyRateDao().GetByID(id); if (currencyRate == null) throw new ArgumentException(); currencyRate.FromCurrency = fromCurrency; currencyRate.ToCurrency = toCurrency; currencyRate.Rate = rate; currencyRate.ID = _daoFactory.GetCurrencyRateDao().SaveOrUpdate(currencyRate); _messageService.Send(MessageAction.CurrencyRateUpdated, fromCurrency, toCurrency); return _mapper.Map(currencyRate); } /// /// Set currency rates /// /// /// Common /// [Create(@"currency/setrates")] public List SetCurrencyRates( [FromForm] String currency, [FromForm] List rates) { if (!_crmSecurity.IsAdmin) throw _crmSecurity.CreateSecurityException(); if (string.IsNullOrEmpty(currency)) throw new ArgumentException(); ValidateCurrencyRates(rates); currency = currency.ToUpper(); var crmSettings = _settingsManager.Load(); var defaultCurrency = _currencyProvider.Get(_settingsManager.Load().DefaultCurrency); if (defaultCurrency.Abbreviation != currency) { var cur = _currencyProvider.Get(currency); if (cur == null) throw new ArgumentException(); _global.SaveDefaultCurrencySettings(cur); _messageService.Send(MessageAction.CrmDefaultCurrencyUpdated); } rates = _daoFactory.GetCurrencyRateDao().SetCurrencyRates(rates); foreach (var rate in rates) { _messageService.Send(MessageAction.CurrencyRateUpdated, rate.FromCurrency, rate.ToCurrency); } return rates.Select(x => _mapper.Map(x)).ToList(); } /// /// Add currency rates /// /// /// Common /// [Create(@"currency/addrates")] public List AddCurrencyRates([FromBody] List rates) { if (!_crmSecurity.IsAdmin) throw _crmSecurity.CreateSecurityException(); ValidateCurrencyRates(rates); var existingRates = _daoFactory.GetCurrencyRateDao().GetAll(); foreach (var rate in rates) { var exist = false; foreach (var existingRate in existingRates) { if (rate.FromCurrency != existingRate.FromCurrency || rate.ToCurrency != existingRate.ToCurrency) continue; existingRate.Rate = rate.Rate; _daoFactory.GetCurrencyRateDao().SaveOrUpdate(existingRate); _messageService.Send(MessageAction.CurrencyRateUpdated, rate.FromCurrency, rate.ToCurrency); exist = true; break; } if (exist) continue; rate.ID = _daoFactory.GetCurrencyRateDao().SaveOrUpdate(rate); _messageService.Send(MessageAction.CurrencyRateUpdated, rate.FromCurrency, rate.ToCurrency); existingRates.Add(rate); } return existingRates.Select(x => _mapper.Map(x)).ToList(); } /// /// Delete currency rate object /// /// /// Common /// [Delete(@"currency/rates/{id:int}")] public CurrencyRateDto DeleteCurrencyRate(int id) { if (id <= 0) throw new ArgumentException(); var currencyRate = _daoFactory.GetCurrencyRateDao().GetByID(id); if (currencyRate == null) throw new ArgumentException(); _daoFactory.GetCurrencyRateDao().Delete(id); return _mapper.Map(currencyRate); } private void ValidateCurrencyRates(IEnumerable rates) { var currencies = new List(); foreach (var rate in rates) { ValidateRate(rate.Rate); currencies.Add(rate.FromCurrency); currencies.Add(rate.ToCurrency); } ValidateCurrencies(currencies.ToArray()); } private void ValidateCurrencies(string[] currencies) { if (currencies.Any(string.IsNullOrEmpty)) throw new ArgumentException(); var available = _currencyProvider.GetAll().Select(x => x.Abbreviation); var unknown = currencies.Where(x => !available.Contains(x)).ToArray(); if (!unknown.Any()) return; throw new ArgumentException(string.Format(CRMErrorsResource.UnknownCurrency, string.Join(",", unknown))); } private static void ValidateRate(decimal rate) { if (rate < 0 || rate > MaxRateValue) throw new ArgumentException(string.Format(CRMErrorsResource.InvalidCurrencyRate, rate)); } } }