DocSpace-client/products/ASC.CRM/Server/Api/CurrencyRatesController.cs

337 lines
12 KiB
C#
Raw Normal View History

2020-04-16 19:41:37 +00:00
/*
*
* (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.
*
*/
2021-03-19 16:56:26 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2021-03-10 15:38:56 +00:00
using ASC.Api.CRM;
2020-04-18 17:49:09 +00:00
using ASC.Core.Common.Settings;
2021-03-09 15:37:16 +00:00
using ASC.CRM.ApiModels;
2020-04-16 19:41:37 +00:00
using ASC.CRM.Core;
2021-03-10 15:38:56 +00:00
using ASC.CRM.Core.Dao;
2020-04-16 19:41:37 +00:00
using ASC.CRM.Resources;
using ASC.MessagingSystem;
using ASC.Web.Api.Routing;
using ASC.Web.CRM.Classes;
2021-03-02 16:29:07 +00:00
2021-03-19 16:56:26 +00:00
using AutoMapper;
2020-04-16 19:41:37 +00:00
2021-03-24 15:55:47 +00:00
using Microsoft.AspNetCore.Mvc;
2021-03-10 15:38:56 +00:00
namespace ASC.CRM.Api
2020-04-16 19:41:37 +00:00
{
2021-03-10 15:38:56 +00:00
public class CurrencyRatesController : BaseApiController
2020-04-16 19:41:37 +00:00
{
2021-03-23 15:41:56 +00:00
private readonly CurrencyProvider _currencyProvider;
private readonly SettingsManager _settingsManager;
private readonly Global _global;
private readonly MessageService _messageService;
2021-05-05 14:09:05 +00:00
public CurrencyRatesController(CrmSecurity crmSecurity,
2021-03-10 15:38:56 +00:00
DaoFactory daoFactory,
MessageService messageService,
SettingsManager settingsManager,
Global global,
2021-03-19 16:56:26 +00:00
CurrencyProvider currencyProvider,
IMapper mapper)
: base(daoFactory, crmSecurity, mapper)
2021-03-10 15:38:56 +00:00
{
2021-03-23 15:41:56 +00:00
_messageService = messageService;
_settingsManager = settingsManager;
_global = global;
_currencyProvider = currencyProvider;
2021-03-10 15:38:56 +00:00
}
2020-04-16 19:41:37 +00:00
//TABLE `crm_currency_rate` column `rate` DECIMAL(10,2) NOT NULL
2021-03-02 16:29:07 +00:00
public const decimal MaxRateValue = (decimal)99999999.99;
2020-04-16 19:41:37 +00:00
/// <summary>
/// Get the list of currency rates
/// </summary>
/// <short>Get currency rates list</short>
/// <category>Common</category>
/// <returns>
/// List of currency rates
/// </returns>
[Read(@"currency/rates")]
2021-03-09 15:37:16 +00:00
public IEnumerable<CurrencyRateDto> GetCurrencyRates()
2020-04-16 19:41:37 +00:00
{
2021-03-19 16:56:26 +00:00
return _daoFactory.GetCurrencyRateDao().GetAll().ConvertAll(x => _mapper.Map<CurrencyRateDto>(x));
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Get currency rate by id
/// </summary>
/// <short>Get currency rate</short>
/// <category>Common</category>
/// <returns>
/// Currency rate
/// </returns>
/// <exception cref="ArgumentException"></exception>
[Read(@"currency/rates/{id:int}")]
2021-03-09 15:37:16 +00:00
public CurrencyRateDto GetCurrencyRate(int id)
2020-04-16 19:41:37 +00:00
{
if (id <= 0) throw new ArgumentException();
2021-03-19 16:56:26 +00:00
var currencyRate = _daoFactory.GetCurrencyRateDao().GetByID(id);
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
return _mapper.Map<CurrencyRateDto>(currencyRate);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Get currency rate by currencies
/// </summary>
/// <short>Get currency rate</short>
/// <category>Common</category>
/// <returns>
/// Currency rate
/// </returns>
/// <exception cref="ArgumentException"></exception>
[Read(@"currency/rates/{fromCurrency}/{toCurrency}")]
2021-03-09 15:37:16 +00:00
public CurrencyRateDto GetCurrencyRate(string fromCurrency, string toCurrency)
2020-04-16 19:41:37 +00:00
{
if (string.IsNullOrEmpty(fromCurrency) || string.IsNullOrEmpty(toCurrency))
throw new ArgumentException();
2021-03-19 16:56:26 +00:00
var currencyRate = _daoFactory.GetCurrencyRateDao().GetByCurrencies(fromCurrency, toCurrency);
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
return _mapper.Map<CurrencyRateDto>(currencyRate);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Create new currency rate object
/// </summary>
/// <short></short>
/// <category>Common</category>
/// <returns></returns>
[Create(@"currency/rates")]
2021-03-24 15:55:47 +00:00
public CurrencyRateDto CreateCurrencyRate(
[FromBody] CreateCurrencyRateRequestDto inDto)
2020-04-16 19:41:37 +00:00
{
var rate = inDto.Rate;
var fromCurrency = inDto.FromCurrency;
var toCurrency = inDto.ToCurrency;
2020-04-16 19:41:37 +00:00
ValidateRate(rate);
2021-03-02 16:29:07 +00:00
ValidateCurrencies(new[] { fromCurrency, toCurrency });
2020-04-16 19:41:37 +00:00
var currencyRate = new CurrencyRate
2021-03-02 16:29:07 +00:00
{
FromCurrency = fromCurrency,
ToCurrency = toCurrency,
Rate = rate
};
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
currencyRate.ID = _daoFactory.GetCurrencyRateDao().SaveOrUpdate(currencyRate);
2021-03-23 15:41:56 +00:00
_messageService.Send(MessageAction.CurrencyRateUpdated, fromCurrency, toCurrency);
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
return _mapper.Map<CurrencyRateDto>(currencyRate);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Update currency rate object
/// </summary>
/// <short></short>
/// <category>Common</category>
/// <returns></returns>
[Update(@"currency/rates/{id:int}")]
2021-03-09 15:37:16 +00:00
public CurrencyRateDto UpdateCurrencyRate(int id, string fromCurrency, string toCurrency, decimal rate)
2020-04-16 19:41:37 +00:00
{
if (id <= 0)
throw new ArgumentException();
ValidateRate(rate);
2021-03-02 16:29:07 +00:00
ValidateCurrencies(new[] { fromCurrency, toCurrency });
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
var currencyRate = _daoFactory.GetCurrencyRateDao().GetByID(id);
2020-04-16 19:41:37 +00:00
if (currencyRate == null)
throw new ArgumentException();
currencyRate.FromCurrency = fromCurrency;
currencyRate.ToCurrency = toCurrency;
currencyRate.Rate = rate;
2021-03-19 16:56:26 +00:00
currencyRate.ID = _daoFactory.GetCurrencyRateDao().SaveOrUpdate(currencyRate);
2021-03-23 15:41:56 +00:00
_messageService.Send(MessageAction.CurrencyRateUpdated, fromCurrency, toCurrency);
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
return _mapper.Map<CurrencyRateDto>(currencyRate);
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Set currency rates
/// </summary>
/// <short></short>
/// <category>Common</category>
/// <returns></returns>
[Create(@"currency/setrates")]
2021-03-24 15:55:47 +00:00
public List<CurrencyRateDto> SetCurrencyRates(
SetCurrencyRatesRequestDto inDto)
2020-04-16 19:41:37 +00:00
{
var currency = inDto.Currency;
var rates = inDto.Rates;
2021-03-19 16:56:26 +00:00
if (!_crmSecurity.IsAdmin)
throw _crmSecurity.CreateSecurityException();
2020-04-16 19:41:37 +00:00
if (string.IsNullOrEmpty(currency))
throw new ArgumentException();
ValidateCurrencyRates(rates);
currency = currency.ToUpper();
2021-03-02 16:29:07 +00:00
2021-05-05 14:09:05 +00:00
var crmSettings = _settingsManager.Load<CrmSettings>();
var defaultCurrency = _currencyProvider.Get(_settingsManager.Load<CrmSettings>().DefaultCurrency);
2021-03-23 15:41:56 +00:00
if (defaultCurrency.Abbreviation != currency)
2020-04-16 19:41:37 +00:00
{
2021-03-23 15:41:56 +00:00
var cur = _currencyProvider.Get(currency);
2020-04-16 19:41:37 +00:00
if (cur == null)
throw new ArgumentException();
2021-03-23 15:41:56 +00:00
_global.SaveDefaultCurrencySettings(cur);
2020-04-16 19:41:37 +00:00
2021-03-23 15:41:56 +00:00
_messageService.Send(MessageAction.CrmDefaultCurrencyUpdated);
2020-04-16 19:41:37 +00:00
}
2021-03-19 16:56:26 +00:00
rates = _daoFactory.GetCurrencyRateDao().SetCurrencyRates(rates);
2020-04-16 19:41:37 +00:00
foreach (var rate in rates)
{
2021-03-23 15:41:56 +00:00
_messageService.Send(MessageAction.CurrencyRateUpdated, rate.FromCurrency, rate.ToCurrency);
2020-04-16 19:41:37 +00:00
}
2021-03-19 16:56:26 +00:00
return rates.Select(x => _mapper.Map<CurrencyRateDto>(x)).ToList();
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Add currency rates
/// </summary>
/// <short></short>
/// <category>Common</category>
/// <returns></returns>
[Create(@"currency/addrates")]
public List<CurrencyRateDto> AddCurrencyRates([FromBody] List<CurrencyRate> rates)
2020-04-16 19:41:37 +00:00
{
2021-03-19 16:56:26 +00:00
if (!_crmSecurity.IsAdmin)
throw _crmSecurity.CreateSecurityException();
2020-04-16 19:41:37 +00:00
ValidateCurrencyRates(rates);
2021-03-19 16:56:26 +00:00
var existingRates = _daoFactory.GetCurrencyRateDao().GetAll();
2020-04-16 19:41:37 +00:00
foreach (var rate in rates)
{
var exist = false;
2021-03-02 16:29:07 +00:00
2020-04-16 19:41:37 +00:00
foreach (var existingRate in existingRates)
{
if (rate.FromCurrency != existingRate.FromCurrency || rate.ToCurrency != existingRate.ToCurrency)
continue;
existingRate.Rate = rate.Rate;
2021-03-19 16:56:26 +00:00
_daoFactory.GetCurrencyRateDao().SaveOrUpdate(existingRate);
2021-03-23 15:41:56 +00:00
_messageService.Send(MessageAction.CurrencyRateUpdated, rate.FromCurrency, rate.ToCurrency);
2020-04-16 19:41:37 +00:00
exist = true;
break;
}
if (exist) continue;
2021-03-19 16:56:26 +00:00
rate.ID = _daoFactory.GetCurrencyRateDao().SaveOrUpdate(rate);
2021-03-23 15:41:56 +00:00
_messageService.Send(MessageAction.CurrencyRateUpdated, rate.FromCurrency, rate.ToCurrency);
2020-04-16 19:41:37 +00:00
existingRates.Add(rate);
}
2021-03-19 16:56:26 +00:00
return existingRates.Select(x => _mapper.Map<CurrencyRateDto>(x)).ToList();
2020-04-16 19:41:37 +00:00
}
/// <summary>
/// Delete currency rate object
/// </summary>
/// <short></short>
/// <category>Common</category>
/// <returns></returns>
[Delete(@"currency/rates/{id:int}")]
2021-03-09 15:37:16 +00:00
public CurrencyRateDto DeleteCurrencyRate(int id)
2020-04-16 19:41:37 +00:00
{
if (id <= 0)
throw new ArgumentException();
2021-03-19 16:56:26 +00:00
var currencyRate = _daoFactory.GetCurrencyRateDao().GetByID(id);
2021-03-02 16:29:07 +00:00
2020-04-16 19:41:37 +00:00
if (currencyRate == null)
throw new ArgumentException();
2021-03-19 16:56:26 +00:00
_daoFactory.GetCurrencyRateDao().Delete(id);
2020-04-16 19:41:37 +00:00
2021-03-19 16:56:26 +00:00
return _mapper.Map<CurrencyRateDto>(currencyRate);
2020-04-16 19:41:37 +00:00
}
2020-04-18 17:49:09 +00:00
private void ValidateCurrencyRates(IEnumerable<CurrencyRate> rates)
2020-04-16 19:41:37 +00:00
{
var currencies = new List<string>();
foreach (var rate in rates)
{
ValidateRate(rate.Rate);
currencies.Add(rate.FromCurrency);
currencies.Add(rate.ToCurrency);
}
ValidateCurrencies(currencies.ToArray());
}
2021-03-02 16:29:07 +00:00
2020-04-18 17:49:09 +00:00
private void ValidateCurrencies(string[] currencies)
2020-04-16 19:41:37 +00:00
{
if (currencies.Any(string.IsNullOrEmpty))
throw new ArgumentException();
2021-03-23 15:41:56 +00:00
var available = _currencyProvider.GetAll().Select(x => x.Abbreviation);
2020-04-16 19:41:37 +00:00
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));
}
2021-03-02 16:29:07 +00:00
2020-04-16 19:41:37 +00:00
}
}