From 2a7cc3aeb0acbaf3dda4c475d0a5d3aaed3f4ada Mon Sep 17 00:00:00 2001 From: yuri Date: Tue, 30 Oct 2018 11:57:22 +0200 Subject: [PATCH] fiscal year start --- application/Espo/Core/SelectManagers/Base.php | 43 +++++++++++++++++++ .../Espo/Resources/i18n/en_US/Global.json | 6 ++- .../Espo/Resources/i18n/en_US/Settings.json | 3 +- .../Resources/layouts/Settings/settings.json | 3 +- .../metadata/entityDefs/Settings.json | 5 +++ client/src/views/fields/date.js | 10 +++++ .../settings/fields/fiscal-year-shift.js | 43 +++++++++++++++++++ 7 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 client/src/views/settings/fields/fiscal-year-shift.js diff --git a/application/Espo/Core/SelectManagers/Base.php b/application/Espo/Core/SelectManagers/Base.php index 829d5099d1..5ef084b668 100644 --- a/application/Espo/Core/SelectManagers/Base.php +++ b/application/Espo/Core/SelectManagers/Base.php @@ -1243,6 +1243,49 @@ class Base ]; break; + case 'currentFiscalYear': + case 'lastFiscalYear': + $dtToday = new \DateTime(); + $dt = new \DateTime(); + $fiscalYearShift = $this->getConfig()->get('fiscalYearShift', 0); + $dt->modify('first day of January this year')->modify('+' . $fiscalYearShift . ' months'); + if (intval($dtToday->format('m')) < $fiscalYearShift + 1) { + $dt->modify('-1 year'); + } + if ($type === 'lastFiscalYear') { + $dt->modify('-1 year'); + } + $part['AND'] = [ + $attribute . '>=' => $dt->format('Y-m-d'), + $attribute . '<' => $dt->add(new \DateInterval('P1Y'))->format('Y-m-d') + ]; + break; + + case 'currentFiscalQuarter': + case 'lastFiscalQuarter': + $dtToday = new \DateTime(); + $dt = new \DateTime(); + $fiscalYearShift = $this->getConfig()->get('fiscalYearShift', 0); + $dt->modify('first day of January this year')->modify('+' . $fiscalYearShift . ' months'); + $month = intval($dtToday->format('m')); + $quarterShift = floor(($month - $fiscalYearShift - 1) / 3); + if ($quarterShift) { + if ($quarterShift >= 0) { + $dt->add(new \DateInterval('P'.($quarterShift * 3).'M')); + } else { + $quarterShift *= -1; + $dt->sub(new \DateInterval('P'.($quarterShift * 3).'M')); + } + } + if ($type === 'lastFiscalQuarter') { + $dt->modify('-3 months'); + } + $part['AND'] = [ + $attribute . '>=' => $dt->format('Y-m-d'), + $attribute . '<' => $dt->add(new \DateInterval('P3M'))->format('Y-m-d') + ]; + break; + case 'between': if (is_array($value)) { $part['AND'] = [ diff --git a/application/Espo/Resources/i18n/en_US/Global.json b/application/Espo/Resources/i18n/en_US/Global.json index f3659773e8..33481f8969 100644 --- a/application/Espo/Resources/i18n/en_US/Global.json +++ b/application/Espo/Resources/i18n/en_US/Global.json @@ -576,7 +576,11 @@ "ever": "Ever", "isEmpty": "Is Empty", "olderThanXDays": "Older Than X Days", - "afterXDays": "After X Days" + "afterXDays": "After X Days", + "currentFiscalYear": "Current Fiscal Year", + "lastFiscalYear": "Last Fiscal Year", + "currentFiscalQuarter": "Current Fiscal Quarter", + "lastFiscalQuarter": "Last Fiscal Quarter" }, "searchRanges": { "is": "Is", diff --git a/application/Espo/Resources/i18n/en_US/Settings.json b/application/Espo/Resources/i18n/en_US/Settings.json index b7680b5215..f63b1aa4ab 100644 --- a/application/Espo/Resources/i18n/en_US/Settings.json +++ b/application/Espo/Resources/i18n/en_US/Settings.json @@ -105,7 +105,8 @@ "emailAddressIsOptedOutByDefault": "Mark new email addresses as opted-out", "outboundEmailBccAddress": "BCC address for external clients", "cleanupDeletedRecords": "Clean up deleted records", - "addressCountryList": "Address Country Autocomplete List" + "addressCountryList": "Address Country Autocomplete List", + "fiscalYearShift": "Fiscal Year Start" }, "options": { "weekStart": { diff --git a/application/Espo/Resources/layouts/Settings/settings.json b/application/Espo/Resources/layouts/Settings/settings.json index d95ff1757c..5f6e077397 100644 --- a/application/Espo/Resources/layouts/Settings/settings.json +++ b/application/Espo/Resources/layouts/Settings/settings.json @@ -17,7 +17,8 @@ [{"name": "dateFormat"}, {"name": "weekStart"}], [{"name": "timeFormat"}, {"name": "thousandSeparator"}], [{"name": "addressFormat"}, {"name": "decimalMark"}], - [{"name": "addressPreview"}, {"name": "addressCountryList"}] + [{"name": "addressPreview"}, {"name": "addressCountryList"}], + [{"name": "fiscalYearShift"}, false] ] }, { diff --git a/application/Espo/Resources/metadata/entityDefs/Settings.json b/application/Espo/Resources/metadata/entityDefs/Settings.json index c5a921e610..5d3d990578 100644 --- a/application/Espo/Resources/metadata/entityDefs/Settings.json +++ b/application/Espo/Resources/metadata/entityDefs/Settings.json @@ -40,6 +40,11 @@ "options": [0, 1], "default": 0 }, + "fiscalYearShift": { + "type": "enumInt", + "default": 0, + "view": "views/settings/fields/fiscal-year-shift" + }, "thousandSeparator": { "type": "varchar", "default": ",", diff --git a/client/src/views/fields/date.js b/client/src/views/fields/date.js index 2d5fd25843..34f3583c4e 100644 --- a/client/src/views/fields/date.js +++ b/client/src/views/fields/date.js @@ -48,6 +48,16 @@ Espo.define('views/fields/date', 'views/fields/base', function (Dep) { setup: function () { Dep.prototype.setup.call(this); + + if (this.getConfig().get('fiscalYearShift')) { + this.searchTypeList = Espo.Utils.clone(this.searchTypeList); + if (this.getConfig().get('fiscalYearShift') % 3 != 0) { + this.searchTypeList.push('currentFiscalQuarter'); + this.searchTypeList.push('lastFiscalQuarter'); + } + this.searchTypeList.push('currentFiscalYear'); + this.searchTypeList.push('lastFiscalYear'); + } }, data: function () { diff --git a/client/src/views/settings/fields/fiscal-year-shift.js b/client/src/views/settings/fields/fiscal-year-shift.js new file mode 100644 index 0000000000..c765e3c6f0 --- /dev/null +++ b/client/src/views/settings/fields/fiscal-year-shift.js @@ -0,0 +1,43 @@ +/************************************************************************ + * This file is part of EspoCRM. + * + * EspoCRM - Open Source CRM application. + * Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko + * Website: http://www.espocrm.com + * + * EspoCRM is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EspoCRM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EspoCRM. If not, see http://www.gnu.org/licenses/. + * + * The interactive user interfaces in modified source and object code versions + * of this program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU General Public License version 3. + * + * In accordance with Section 7(b) of the GNU General Public License version 3, + * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. + ************************************************************************/ + +Espo.define('views/settings/fields/fiscal-year-shift', 'views/fields/enum-int', function (Dep) { + + return Dep.extend({ + + setupOptions: function () { + this.params.options = []; + this.translatedOptions = {}; + var monthNameList = this.getLanguage().get('Global', 'lists', 'monthNames') || []; + monthNameList.forEach(function (name, i) { + this.params.options.push(i); + this.translatedOptions[i] = name; + }, this); + } + }); +});