/* * * (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 System.Security; using ASC.Api.CRM; using ASC.Common.Threading.Progress; using ASC.Core.Common.Settings; using ASC.CRM.ApiModels; using ASC.CRM.Core; using ASC.CRM.Core.Dao; using ASC.CRM.Core.Entities; using ASC.CRM.Core.Enums; using ASC.CRM.Resources; using ASC.MessagingSystem; using ASC.Web.Api.Routing; using ASC.Web.Core.Utility; using ASC.Web.CRM.Classes; using AutoMapper; namespace ASC.CRM.Api { public class UtilsController : BaseApiController { public UtilsController(CRMSecurity cRMSecurity, DaoFactory daoFactory, MessageService messageService, SettingsManager settingsManager, CurrencyProvider currencyProvider, CurrencyRateInfoDtoHelper currencyRateInfoDtoHelper, InvoiceSetting invoiceSetting, ImportFromCSVManager importFromCSVManager, OrganisationLogoManager organisationLogoManager, Global global, ImportFromCSV importFromCSV, ExportToCsv exportToCsv, IMapper mapper) : base(daoFactory, cRMSecurity, mapper) { MessageService = messageService; CurrencyProvider = currencyProvider; SettingsManager = settingsManager; CurrencyRateInfoDtoHelper = currencyRateInfoDtoHelper; InvoiceSetting = invoiceSetting; ImportFromCSVManager = importFromCSVManager; OrganisationLogoManager = organisationLogoManager; Global = global; ImportFromCSV = importFromCSV; ExportToCsv = exportToCsv; } public ExportToCsv ExportToCsv { get; } public ImportFromCSV ImportFromCSV { get; } public Global Global { get; } public OrganisationLogoManager OrganisationLogoManager { get; } public ImportFromCSVManager ImportFromCSVManager { get; } public InvoiceSetting InvoiceSetting { get; } public CurrencyRateInfoDtoHelper CurrencyRateInfoDtoHelper { get; } public SettingsManager SettingsManager { get; } public CurrencyProvider CurrencyProvider { get; } public MessageService MessageService { get; } /// /// Returns the list of all currencies currently available on the portal /// /// Get currency list /// Common /// /// List of available currencies /// [Read(@"settings/currency")] public IEnumerable GetAvaliableCurrency() { return CurrencyProvider.GetAll().ConvertAll(item => _mapper.Map(item)); } /// /// Returns the result of convertation from one currency to another /// /// Amount to convert /// Old currency key /// New currency key /// Get the result of convertation /// Common /// /// Decimal result of convertation /// [Read(@"settings/currency/convert")] public Decimal ConvertAmount(Decimal amount, String fromcurrency, String tocurrency) { return CurrencyProvider.MoneyConvert(amount, fromcurrency, tocurrency); } /// /// Returns the summary table with rates for selected currency /// /// Currency (Abbreviation) /// Get the summary table /// Common /// /// Dictionary of currencies and rates /// /// [Read(@"settings/currency/summarytable")] public IEnumerable GetSummaryTable(String currency) { var result = new List(); if (string.IsNullOrEmpty(currency)) { throw new ArgumentException(); } var cur = CurrencyProvider.Get(currency.ToUpper()); if (cur == null) throw new ArgumentException(); var table = CurrencyProvider.MoneyConvert(cur); table.ToList().ForEach(tableItem => result.Add(CurrencyRateInfoDtoHelper.Get(tableItem.Key, tableItem.Value))); return result; } /// /// /// /// Change contact status group auto /// /// Contacts /// /// ChangeContactStatusGroupAuto setting value (true, false or null) /// /// [Update(@"contact/status/settings")] public Boolean? UpdateCRMContactStatusSettings(Boolean? changeContactStatusGroupAuto) { var tenantSettings = SettingsManager.Load(); tenantSettings.ChangeContactStatusGroupAuto = changeContactStatusGroupAuto; SettingsManager.Save(tenantSettings); MessageService.Send(MessageAction.ContactTemperatureLevelSettingsUpdated); return changeContactStatusGroupAuto; } /// /// /// /// Write mail to history auto /// /// Contacts /// /// WriteMailToHistoryAuto setting value (true or false) /// /// [Update(@"contact/mailtohistory/settings")] public Boolean UpdateCRMWriteMailToHistorySettings(Boolean writeMailToHistoryAuto) { var tenantSettings = SettingsManager.Load(); tenantSettings.WriteMailToHistoryAuto = writeMailToHistoryAuto; SettingsManager.Save(tenantSettings); //MessageService.Send( MessageAction.ContactTemperatureLevelSettingsUpdated); return writeMailToHistoryAuto; } /// /// /// /// add tag to contact group auto /// /// Contacts /// /// AddTagToContactGroupAuto setting value (true, false or null) /// /// [Update(@"contact/tag/settings")] public Boolean? UpdateCRMContactTagSettings(Boolean? addTagToContactGroupAuto) { var tenantSettings = SettingsManager.Load(); tenantSettings.AddTagToContactGroupAuto = addTagToContactGroupAuto; SettingsManager.Save(tenantSettings); MessageService.Send(MessageAction.ContactsTagSettingsUpdated); return addTagToContactGroupAuto; } /// /// Set IsConfiguredPortal tenant setting and website contact form key specified in the request /// /// Set tenant settings /// Common /// /// IsConfiguredPortal setting value (true or false) /// [Update(@"settings")] public Boolean SetIsPortalConfigured(Boolean? configured, Guid? webFormKey) { if (!_crmSecurity.IsAdmin) throw _crmSecurity.CreateSecurityException(); var tenantSettings = SettingsManager.Load(); tenantSettings.IsConfiguredPortal = configured ?? true; tenantSettings.WebFormKey = webFormKey ?? Guid.NewGuid(); SettingsManager.Save(tenantSettings); return tenantSettings.IsConfiguredPortal; } /// /// Save organisation company name /// /// Organisation company name /// Save organisation company name /// Organisation /// Organisation company name /// [Update(@"settings/organisation/base")] public String UpdateOrganisationSettingsCompanyName(String companyName) { if (!_crmSecurity.IsAdmin) throw _crmSecurity.CreateSecurityException(); var tenantSettings = SettingsManager.Load(); if (tenantSettings.InvoiceSetting == null) { tenantSettings.InvoiceSetting = InvoiceSetting.DefaultSettings; } tenantSettings.InvoiceSetting.CompanyName = companyName; SettingsManager.Save(tenantSettings); MessageService.Send(MessageAction.OrganizationProfileUpdatedCompanyName, companyName); return companyName; } /// /// Save organisation company address /// /// Organisation company street/building/apartment address /// City /// State /// Zip /// Country /// Save organisation company address /// Organisation /// Returns a JSON object with the organization company address details /// [Update(@"settings/organisation/address")] public String UpdateOrganisationSettingsCompanyAddress(String street, String city, String state, String zip, String country) { if (!_crmSecurity.IsAdmin) throw _crmSecurity.CreateSecurityException(); var tenantSettings = SettingsManager.Load(); if (tenantSettings.InvoiceSetting == null) { tenantSettings.InvoiceSetting = InvoiceSetting.DefaultSettings; } var companyAddress = Newtonsoft.Json.JsonConvert.SerializeObject(new { type = AddressCategory.Billing.ToString(), street, city, state, zip, country }); tenantSettings.InvoiceSetting.CompanyAddress = companyAddress; SettingsManager.Save(tenantSettings); MessageService.Send(MessageAction.OrganizationProfileUpdatedAddress); return companyAddress; } /// /// Save organisation logo /// /// Reset organisation logo /// Save organisation logo /// Organisation /// Organisation logo ID /// /// [Update(@"settings/organisation/logo")] public Int32 UpdateOrganisationSettingsLogo(bool reset) { if (!_crmSecurity.IsAdmin) throw _crmSecurity.CreateSecurityException(); int companyLogoID; if (!reset) { companyLogoID = OrganisationLogoManager.TryUploadOrganisationLogoFromTmp(_daoFactory); if (companyLogoID == 0) { throw new Exception("Downloaded image not found"); } } else { companyLogoID = 0; } var tenantSettings = SettingsManager.Load(); if (tenantSettings.InvoiceSetting == null) { tenantSettings.InvoiceSetting = InvoiceSetting.DefaultSettings; } tenantSettings.InvoiceSetting.CompanyLogoID = companyLogoID; SettingsManager.Save(tenantSettings); MessageService.Send(MessageAction.OrganizationProfileUpdatedInvoiceLogo); return companyLogoID; } /// /// Get organisation logo in base64 format (if 'id' is 0 then take current logo) /// /// organisation logo id /// Get organisation logo /// Organisation /// Organisation logo content in base64 /// [Read(@"settings/organisation/logo")] public String GetOrganisationSettingsLogo(int id) { if (id != 0) { return OrganisationLogoManager.GetOrganisationLogoBase64(id); } else { var tenantSettings = SettingsManager.Load(); if (tenantSettings.InvoiceSetting == null) { return string.Empty; } return OrganisationLogoManager.GetOrganisationLogoBase64(tenantSettings.InvoiceSetting.CompanyLogoID); } } /// /// Change Website Contact Form key /// /// Change web form key /// Common /// Web form key /// [Update(@"settings/webformkey/change")] public string ChangeWebToLeadFormKey() { if (!_crmSecurity.IsAdmin) throw _crmSecurity.CreateSecurityException(); var tenantSettings = SettingsManager.Load(); tenantSettings.WebFormKey = Guid.NewGuid(); SettingsManager.Save(tenantSettings); MessageService.Send(MessageAction.WebsiteContactFormUpdatedKey); return tenantSettings.WebFormKey.ToString(); } /// /// Change default CRM currency /// /// Currency (Abbreviation) /// Change currency /// Common /// currency /// /// [Update(@"settings/currency")] public CurrencyInfoDto UpdateCRMCurrency(String currency) { if (!_crmSecurity.IsAdmin) throw _crmSecurity.CreateSecurityException(); if (string.IsNullOrEmpty(currency)) { throw new ArgumentException(); } currency = currency.ToUpper(); var cur = CurrencyProvider.Get(currency); if (cur == null) throw new ArgumentException(); Global.SaveDefaultCurrencySettings(cur); MessageService.Send(MessageAction.CrmDefaultCurrencyUpdated); return _mapper.Map(cur); } /// false [Create(@"{entityType:regex(contact|opportunity|case|task)}/import/start")] public string StartImportFromCSV(string entityType, string csvFileURI, string jsonSettings) { EntityType entityTypeObj; if (string.IsNullOrEmpty(entityType) || string.IsNullOrEmpty(csvFileURI) || string.IsNullOrEmpty(jsonSettings)) throw new ArgumentException(); switch (entityType.ToLower()) { case "contact": entityTypeObj = EntityType.Contact; break; case "opportunity": entityTypeObj = EntityType.Opportunity; break; case "case": entityTypeObj = EntityType.Case; break; case "task": entityTypeObj = EntityType.Task; break; default: throw new ArgumentException(); } ImportFromCSVManager.StartImport(entityTypeObj, csvFileURI, jsonSettings); return ""; } /// false [Read(@"{entityType:regex(contact|opportunity|case|task)}/import/status")] public IProgressItem GetImportFromCSVStatus(string entityType) { EntityType entityTypeObj; if (string.IsNullOrEmpty(entityType)) throw new ArgumentException(); switch (entityType.ToLower()) { case "contact": entityTypeObj = EntityType.Contact; break; case "opportunity": entityTypeObj = EntityType.Opportunity; break; case "case": entityTypeObj = EntityType.Case; break; case "task": entityTypeObj = EntityType.Task; break; default: throw new ArgumentException(); } return ImportFromCSV.GetStatus(entityTypeObj); } /// false [Read(@"import/samplerow")] public String GetImportFromCSVSampleRow(string csvFileURI, int indexRow, string jsonSettings) { if (String.IsNullOrEmpty(csvFileURI) || indexRow < 0) throw new ArgumentException(); if (!Global.GetStore().IsFile("temp", csvFileURI)) throw new ArgumentException(); var CSVFileStream = Global.GetStore().GetReadStream("temp", csvFileURI); return ImportFromCSV.GetRow(CSVFileStream, indexRow, jsonSettings); } /// false [Create(@"import/uploadfake")] public FileUploadResult ProcessUploadFake(string csvFileURI, string jsonSettings) { return ImportFromCSVManager.ProcessUploadFake(csvFileURI, jsonSettings); } /// false [Read(@"export/status")] public IProgressItem GetExportStatus() { if (!_crmSecurity.IsAdmin) throw _crmSecurity.CreateSecurityException(); return ExportToCsv.GetStatus(false); } /// false [Update(@"export/cancel")] public IProgressItem CancelExport() { if (!_crmSecurity.IsAdmin) throw _crmSecurity.CreateSecurityException(); ExportToCsv.Cancel(false); return ExportToCsv.GetStatus(false); } /// false [Create(@"export/start")] public IProgressItem StartExport() { if (!_crmSecurity.IsAdmin) throw _crmSecurity.CreateSecurityException(); MessageService.Send(MessageAction.CrmAllDataExported); return ExportToCsv.Start(null, CRMSettingResource.Export + ".zip"); } /// false [Read(@"export/partial/status")] public IProgressItem GetPartialExportStatus() { return ExportToCsv.GetStatus(true); } /// false [Update(@"export/partial/cancel")] public IProgressItem CancelPartialExport() { ExportToCsv.Cancel(true); return ExportToCsv.GetStatus(true); } /// false [Create(@"export/partial/{entityType:regex(contact|opportunity|case|task|invoiceitem)}/start")] public IProgressItem StartPartialExport(string entityType, string base64FilterString) { if (string.IsNullOrEmpty(base64FilterString)) throw new ArgumentException(); FilterObject filterObject; String fileName; switch (entityType.ToLower()) { case "contact": filterObject = new ContactFilterObject(base64FilterString); fileName = CRMContactResource.Contacts + ".csv"; MessageService.Send(MessageAction.ContactsExportedToCsv); break; case "opportunity": filterObject = new DealFilterObject(base64FilterString); fileName = CRMCommonResource.DealModuleName + ".csv"; MessageService.Send(MessageAction.OpportunitiesExportedToCsv); break; case "case": filterObject = new CasesFilterObject(base64FilterString); fileName = CRMCommonResource.CasesModuleName + ".csv"; MessageService.Send(MessageAction.CasesExportedToCsv); break; case "task": filterObject = new TaskFilterObject(base64FilterString); fileName = CRMCommonResource.TaskModuleName + ".csv"; MessageService.Send(MessageAction.CrmTasksExportedToCsv); break; case "invoiceitem": fileName = CRMCommonResource.ProductsAndServices + ".csv"; filterObject = new InvoiceItemFilterObject(base64FilterString); break; default: throw new ArgumentException(); } return ExportToCsv.Start(filterObject, fileName); } } }