diff --git a/app/assets/javascripts/controllers/admin/invoices.js.erb b/app/assets/javascripts/controllers/admin/invoices.js.erb index 43fee9304..692596a0b 100644 --- a/app/assets/javascripts/controllers/admin/invoices.js.erb +++ b/app/assets/javascripts/controllers/admin/invoices.js.erb @@ -55,9 +55,10 @@ Application.Controllers.controller('InvoicesController', ['$scope', '$state', 'I // Invoice PDF filename settings (and example) $scope.file = { - prefix: 'Fablab_demo-facture', + prefix: settings.invoice_prefix, nextId: 40, - date: moment().format('DDMMYYYY') + date: moment().format('DDMMYYYY'), + templateUrl: 'editPrefix.html' } // Invoices parameters @@ -484,6 +485,38 @@ Application.Controllers.controller('InvoicesController', ['$scope', '$state', 'I }); }; + /** + * Open a modal dialog allowing the user to edit the prefix of the invoice file name + */ + $scope.openEditPrefix = function () { + const modalInstance = $uibModal.open({ + animation: true, + templateUrl: $scope.file.templateUrl, + size: 'lg', + resolve: { + model () { return $scope.file.prefix;} + }, + controller: ['$scope', '$uibModalInstance', 'model', function ($scope, $uibModalInstance, model) { + $scope.model = model; + $scope.ok = function () { $uibModalInstance.close($scope.model); }; + $scope.cancel = function () { $uibModalInstance.dismiss('cancel'); }; + }] + }); + + return modalInstance.result.then(function (model) { + Setting.update({ name: 'invoice_prefix' }, { value: model }, function (data) { + $scope.file.prefix = model; + return growl.success(_t('app.admin.invoices.prefix_successfully_saved')); + } + , function (error) { + if (error.status === 304) return; + + growl.error(_t('app.admin.invoices.an_error_occurred_while_saving_the_prefix')); + console.error(error); + }); + }); + }; + /** * Callback to save the value of the text zone when editing is done */ diff --git a/app/assets/javascripts/router.js.erb b/app/assets/javascripts/router.js.erb index c959dc9b7..678c35ab5 100644 --- a/app/assets/javascripts/router.js.erb +++ b/app/assets/javascripts/router.js.erb @@ -839,7 +839,7 @@ angular.module('application.router', ['ui.router']) 'accounting_VAT_code', 'accounting_VAT_label', 'accounting_subscription_code', 'accounting_subscription_label', \ 'accounting_Machine_code', 'accounting_Machine_label', 'accounting_Training_code', 'accounting_Training_label', \ 'accounting_Event_code', 'accounting_Event_label', 'accounting_Space_code', 'accounting_Space_label', \ - 'feature_tour_display', 'online_payment_module', 'stripe_public_key', 'stripe_currency']` }).$promise; + 'feature_tour_display', 'online_payment_module', 'stripe_public_key', 'stripe_currency', 'invoice_prefix']` }).$promise; }], stripeSecretKey: ['Setting', function (Setting) { return Setting.isPresent({ name: 'stripe_secret_key' }).$promise; }], onlinePaymentStatus: ['Payment', function (Payment) { return Payment.onlinePaymentStatus().$promise; }], diff --git a/app/assets/templates/admin/invoices/settings.html.erb b/app/assets/templates/admin/invoices/settings.html.erb index 034498e11..84910a372 100644 --- a/app/assets/templates/admin/invoices/settings.html.erb +++ b/app/assets/templates/admin/invoices/settings.html.erb @@ -2,10 +2,6 @@ {{ 'app.admin.invoices.warning_invoices_disabled' }} -
- - {{file.prefix}}-{{file.nextId}}_{{file.date}}.pdf -
+
+

{{ 'app.admin.invoices.filename' }}

+ + {{file.prefix}}-{{file.nextId}}_{{file.date}}.pdf +
+ + + diff --git a/app/models/invoice.rb b/app/models/invoice.rb index d7813db3e..49374aa77 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -40,7 +40,13 @@ class Invoice < ApplicationRecord end def filename - "#{ENV['INVOICE_PREFIX']}-#{id}_#{created_at.strftime('%d%m%Y')}.pdf" + prefix = Setting.find_by(name: 'invoice_prefix').history_values.order(created_at: :desc).where('created_at <= ?', created_at).limit(1).first + prefix ||= if created_at < Setting.find_by(name: 'invoice_prefix').history_values.order(created_at: :asc).limit(1).first.created_at + Setting.find_by(name: 'invoice_prefix').history_values.order(created_at: :asc).limit(1).first + else + Setting.find_by(name: 'invoice_prefix')..history_values.order(created_at: :desc).limit(1).first + end + "#{prefix.value}-#{id}_#{created_at.strftime('%d%m%Y')}.pdf" end def user diff --git a/app/models/setting.rb b/app/models/setting.rb index a8e8fe604..0f6f4944c 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -99,7 +99,8 @@ class Setting < ApplicationRecord online_payment_module stripe_public_key stripe_secret_key - stripe_currency] } + stripe_currency + invoice_prefix] } # WARNING: when adding a new key, you may also want to add it in app/policies/setting_policy.rb#public_whitelist def value diff --git a/config/locales/app.admin.en.yml b/config/locales/app.admin.en.yml index b0cb70ce8..542a9e2f8 100644 --- a/config/locales/app.admin.en.yml +++ b/config/locales/app.admin.en.yml @@ -522,6 +522,11 @@ en: an_error_occurred_while_saving_the_address_and_the_legal_information: "An error occurred while saving the address and the legal information." logo_successfully_saved: "Logo successfully saved." an_error_occurred_while_saving_the_logo: "An error occurred while saving the logo." + filename: "File name" + prefix_info: "The invoices will be generated as PDF files, named with the following prefix." + prefix: "Prefix" + prefix_successfully_saved: "File prefix successfully saved" + an_error_occurred_while_saving_the_prefix: "An error occurred while saving the file prefix" online_payment: "Online payment" close_accounting_period: "Close an accounting period" close_from_date: "Close from" @@ -677,7 +682,7 @@ en: administrator_successfully_deleted: "Administrator successfully deleted." unable_to_delete_the_administrator: "Unable to delete the administrator." changes_successfully_saved: "Changes successfully saved." - an_error_occurred_while_saving_changes: "An error occurred when saving changes." + an__occurr_occurred_while_saving_changes: "An error occurred when saving changes." export_is_running_you_ll_be_notified_when_its_ready: "Export is running. You'll be notified when it's ready." tag_form: tags: "Tags" diff --git a/config/locales/app.admin.fr.yml b/config/locales/app.admin.fr.yml index 4abdeb9bd..526be66cb 100644 --- a/config/locales/app.admin.fr.yml +++ b/config/locales/app.admin.fr.yml @@ -522,6 +522,11 @@ fr: an_error_occurred_while_saving_the_address_and_the_legal_information: "Une erreur est survenue lors de l'enregistrement de l'adresse et des informations légales." logo_successfully_saved: "Le logo bien été enregistré." an_error_occurred_while_saving_the_logo: "Une erreur est survenue lors de l'enregistrement du logo." + filename: "Nom de fichier" + prefix_info: "Les factures seront générées sous la forme de fichiers PDF, nommés avec le préfixe suivant." + prefix: "Préfixe" + prefix_successfully_saved: "Le préfixe de fichier a bien été enregistré" + an_error_occurred_while_saving_the_prefix: "Une erreur est survenue lors de l'enregistrement du préfixe de fichier" online_payment: "Paiement en ligne" close_accounting_period: "Clôturer une période comptable" close_from_date: "Clôturer depuis" diff --git a/db/seeds.rb b/db/seeds.rb index d44cc9e1e..cb65e1108 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -882,6 +882,8 @@ end Setting.set('stripe_currency', 'EUR') unless Setting.find_by(name: 'stripe_currency').try(:value) +Setting.set('invoice_prefix', 'FabManager_invoice') unless Setting.find_by(name: 'invoice_prefix').try(:value) + if StatisticCustomAggregation.count.zero? # available reservations hours for machines machine_hours = StatisticType.find_by(key: 'hour', statistic_index_id: 2) diff --git a/doc/environment.md b/doc/environment.md index 058404285..180bfaabe 100644 --- a/doc/environment.md +++ b/doc/environment.md @@ -51,18 +51,6 @@ When using docker-compose, you should provide the name of the service in your [d Used by the authentication system to generate random tokens, eg. for resetting passwords. Used by Rails to verify the integrity of signed cookies. You can generate such a random key by running `rails secret`. - - - INVOICE_PREFIX - -When payments are done on the platform, an invoice will be generated as a PDF file. -The PDF file name will be of the form "(INVOICE_PREFIX) - (invoice ID) _ (invoice date) .pdf". - - - FABLAB_WITHOUT_ONLINE_PAYMENT - -If set to 'true', the online payment won't be available and the you'll be only able to process reservations when logged as admin. -Valid stripe API keys are still required, even if you don't require online payments. FABLAB_WITHOUT_WALLET @@ -132,7 +120,7 @@ The check will run every weeks and if the threshold is exceeded, an alert will b ADMIN_EMAIL, ADMIN_PASSWORD Credentials for the first admin user created when seeding the project. -By default, theses variables are not present in application.yml because they are only used once, when running the database seed with the command `rails db:seed`. +By default, these variables are not present in the env file, because they are only used once, when running the database seed with the command `rails db:seed`. SUPERADMIN_EMAIL diff --git a/env.example b/env.example index 1d2f3f05a..1575b2a82 100644 --- a/env.example +++ b/env.example @@ -8,12 +8,7 @@ ELASTICSEARCH_HOST=fabmanager-elastic SECRET_KEY_BASE=83daf5e7b80d990f037407bab78dff9904aaf3c195a50f84fa8695a22287e707dfbd9524b403b1dcf116ae1d8c06844c3d7ed942564e5b46be6ae3ead93a9d30 -# Invoices -INVOICE_PREFIX=Demo-FabLab_facture - -# FabLab optional modules -FABLAB_WITHOUT_ONLINE_PAYMENT=false -PHONE_REQUIRED=true +# FabLab optional modules= FABLAB_WITHOUT_WALLET=false USER_CONFIRMATION_NEEDED_TO_SIGN_IN=false diff --git a/lib/tasks/fablab/setup.rake b/lib/tasks/fablab/setup.rake index a4e819804..14da2647c 100644 --- a/lib/tasks/fablab/setup.rake +++ b/lib/tasks/fablab/setup.rake @@ -129,7 +129,8 @@ namespace :fablab do %w[! FABLAB_WITHOUT_ONLINE_PAYMENT online_payment_module false], %w[_ STRIPE_PUBLISHABLE_KEY stripe_public_key], %w[_ STRIPE_API_KEY stripe_secret_key], - %w[_ STRIPE_CURRENCY stripe_currency] + %w[_ STRIPE_CURRENCY stripe_currency], + %w[_ INVOICE_PREFIX invoice_prefix FabManager_invoice] ] mapping.each do |m| diff --git a/setup/env.example b/setup/env.example index 251c7a218..63b53b433 100644 --- a/setup/env.example +++ b/setup/env.example @@ -4,9 +4,6 @@ ELASTICSEARCH_HOST=elasticsearch SECRET_KEY_BASE= -INVOICE_PREFIX=Demo-FabLab_facture -FABLAB_WITHOUT_ONLINE_PAYMENT=false -PHONE_REQUIRED=false FABLAB_WITHOUT_WALLET=false SLOT_DURATION=60 diff --git a/setup/setup.sh b/setup/setup.sh index ef4133bc0..18af901e1 100755 --- a/setup/setup.sh +++ b/setup/setup.sh @@ -234,7 +234,7 @@ configure_env_file() local doc variables secret doc=$(\curl -sSL https://raw.githubusercontent.com/sleede/fab-manager/master/doc/environment.md) - variables=(INVOICE_PREFIX FABLAB_WITHOUT_ONLINE_PAYMENT FABLAB_WITHOUT_WALLET \ + variables=(FABLAB_WITHOUT_WALLET \ USER_CONFIRMATION_NEEDED_TO_SIGN_IN DEFAULT_HOST DEFAULT_PROTOCOL DELIVERY_METHOD SMTP_ADDRESS SMTP_PORT SMTP_USER_NAME SMTP_PASSWORD SMTP_AUTHENTICATION \ SMTP_ENABLE_STARTTLS_AUTO SMTP_OPENSSL_VERIFY_MODE SMTP_TLS \ LOG_LEVEL MAX_IMAGE_SIZE MAX_CAO_SIZE MAX_IMPORT_SIZE DISK_SPACE_MB_ALERT \ diff --git a/test/fixtures/history_values.yml b/test/fixtures/history_values.yml index 5ad2c2d28..8bcaa850c 100644 --- a/test/fixtures/history_values.yml +++ b/test/fixtures/history_values.yml @@ -661,3 +661,11 @@ history_value_69: value: usd created_at: 2020-06-08 17:12:16.846525000 Z updated_at: 2020-06-08 17:12:16.846525000 Z + +history_value_70: + id: 70 + setting_id: 70 + value: 1 + name: FabManager_invoice + created_at: 2020-06-15 10:04:06.216541000 Z + updated_at: 2020-06-15 10:04:06.216541000 Z diff --git a/test/fixtures/settings.yml b/test/fixtures/settings.yml index f886f618b..7dd77a6ce 100644 --- a/test/fixtures/settings.yml +++ b/test/fixtures/settings.yml @@ -406,3 +406,9 @@ setting_69: name: stripe_currency created_at: 2020-06-08 17:12:16.846525000 Z updated_at: 2020-06-08 17:12:16.846525000 Z + +setting_70: + id: 70 + name: invoice_prefix + created_at: 2020-06-15 10:04:06.216541000 Z + updated_at: 2020-06-15 10:04:06.216541000 Z