diff --git a/app/models/accounting_period.rb b/app/models/accounting_period.rb index ee0c20176..b50e106bc 100644 --- a/app/models/accounting_period.rb +++ b/app/models/accounting_period.rb @@ -36,7 +36,7 @@ class AccountingPeriod < ActiveRecord::Base end def archive_folder - dir = 'accounting' + dir = "accounting/#{id}" # create directory if it doesn't exists (accounting) FileUtils.mkdir_p dir @@ -48,7 +48,7 @@ class AccountingPeriod < ActiveRecord::Base end def archive_json_file - "#{archive_folder}/#{start_at.iso8601}_#{end_at.iso8601}.json" + "#{start_at.iso8601}_#{end_at.iso8601}.json" end def check_footprint diff --git a/app/models/notification_type.rb b/app/models/notification_type.rb index 54503aa56..200cbf65e 100644 --- a/app/models/notification_type.rb +++ b/app/models/notification_type.rb @@ -43,6 +43,7 @@ class NotificationType notify_member_about_coupon notify_member_reservation_reminder notify_admin_free_disk_space + notify_admin_close_period_reminder ] # deprecated: # - notify_member_subscribed_plan_is_changed diff --git a/app/views/api/notifications/_notify_admin_close_period_reminder.json.jbuilder b/app/views/api/notifications/_notify_admin_close_period_reminder.json.jbuilder new file mode 100644 index 000000000..c64979228 --- /dev/null +++ b/app/views/api/notifications/_notify_admin_close_period_reminder.json.jbuilder @@ -0,0 +1,7 @@ +json.title notification.notification_type +if notification.attached_object.class.name == AccountingPeriod.name + json.description t('warning_last_closed_period_over_1_year', LAST_END: notification.attached_object.end_at) +else + json.description t('warning_no_closed_periods', FIRST_DATE: notification.attached_object.created_at.to_date) +end +json.url notification_url(notification, format: :json) diff --git a/app/views/notifications_mailer/notify_admin_close_period_reminder.html.erb b/app/views/notifications_mailer/notify_admin_close_period_reminder.html.erb new file mode 100644 index 000000000..1e8cb20e0 --- /dev/null +++ b/app/views/notifications_mailer/notify_admin_close_period_reminder.html.erb @@ -0,0 +1,8 @@ +<%= render 'notifications_mailer/shared/hello', recipient: @recipient %> + +<% if @attached_object.class.name == AccountingPeriod.name %> +
<%= t('.body.warning_last_closed_period_over_1_year', LAST_END: @attached_object.end_at) %>
+<% else %> +<%= t('.body.warning_no_closed_periods', FIRST_DATE: @attached_object.created_at.to_date) %>
+<% end %> + diff --git a/app/workers/close_period_reminder_worker.rb b/app/workers/close_period_reminder_worker.rb new file mode 100644 index 000000000..d1131e329 --- /dev/null +++ b/app/workers/close_period_reminder_worker.rb @@ -0,0 +1,12 @@ +class ClosePeriodReminderWorker + include Sidekiq::Worker + + def perform + last_period = AccountingPeriod.order(closed_at: :desc).limit(1).last + return if Invoice.count == 0 || (last_period && last_period.end_at > (Time.current - 1.year)) + + NotificationCenter.call type: 'notify_admin_close_period_reminder', + receiver: User.admins, + attached_object: last_period || Invoice.order(:created_at).first + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index b6fdb2374..5b9e3390a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -311,6 +311,9 @@ en: enjoy_a_discount_of_AMOUNT_with_code_CODE: "Enjoy a discount of %{AMOUNT} with code %{CODE}" notify_admin_free_disk_space: warning_disk_space_under_threshold: "Warning: the server's available disk space is now %{AVAILABLE} MiB" + notify_admin_close_period_reminder: + warning_last_closed_period_over_1_year: "Please remind to periodically close your accounting periods. Last closed period ended at %{LAST_END}" + warning_no_closed_periods: "Please remind to periodically close your accounting periods. You have to close periods from %{FIRST_DATE}" statistics: # statistics tools for admins diff --git a/config/locales/es.yml b/config/locales/es.yml index e1c9ae2b6..0e0dcbe36 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -311,6 +311,9 @@ es: enjoy_a_discount_of_AMOUNT_with_code_CODE: "Disfruta de un descuento de %{AMOUNT} con el código %{CODE}" notify_admin_free_disk_space: warning_disk_space_under_threshold: "Warning: the server's available disk space is now %{AVAILABLE} MiB" # missing translation + notify_admin_close_period_reminder: + warning_last_closed_period_over_1_year: "Please remind to periodically close your accounting periods. Last closed period ended at %{LAST_END}" # missing translation + warning_no_closed_periods: "Please remind to periodically close your accounting periods. You have to close periods from %{FIRST_DATE}" # missing translation statistics: # statistics tools for admins diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 2fea6427b..497704060 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -311,6 +311,9 @@ fr: enjoy_a_discount_of_AMOUNT_with_code_CODE: "Bénéficiez d'une remise de %{AMOUNT} avec le code %{CODE}" notify_admin_free_disk_space: warning_disk_space_under_threshold: "Attention: l'espace disque disponible sur le serveur est désormais de %{AVAILABLE} MiO" + notify_admin_close_period_reminder: + warning_last_closed_period_over_1_year: "Pensez à clôturer régulièrement vos périodes comptables. Les comptes sont actuellement clôturés jusqu'au %{LAST_END}" + warning_no_closed_periods: "Pensez à clôturer régulièrement vos périodes comptables. Vous devez clôturer des périodes depuis le %{FIRST_DATE}" statistics: # outil de statistiques pour les administrateurs diff --git a/config/locales/mails.en.yml b/config/locales/mails.en.yml index 10c3d7fc3..580c74eab 100644 --- a/config/locales/mails.en.yml +++ b/config/locales/mails.en.yml @@ -280,5 +280,11 @@ en: subject: "Low disk space" body: "Warning: available disk space on the server hosting fab-manager is less than %{THRESHOLD} MiB. This can affect its operation and prevent saving some data. Currently, %{AVAILABLE} MiB of free disk space remains available on the mount point." + notify_admin_close_period_reminder: + subject: "Remind to close your accounting periods" + body: + warning_last_closed_period_over_1_year: "Please remind to periodically close your accounting periods. Last closed period ended at %{LAST_END}." + warning_no_closed_periods: "Please remind to periodically close your accounting periods. You have to close periods from %{FIRST_DATE}." + shared: hello: "Hello %{user_name}" diff --git a/config/locales/mails.es.yml b/config/locales/mails.es.yml index 842fcf77b..5605007a1 100644 --- a/config/locales/mails.es.yml +++ b/config/locales/mails.es.yml @@ -279,5 +279,11 @@ es: subject: "Low disk space" body: "Warning: available disk space on the server hosting fab-manager is less than %{THRESHOLD} MiB. This can affect its operation and prevent saving some data. Currently, %{AVAILABLE} MiB of free disk space remains available on the mount point." + notify_admin_close_period_reminder: #translation_missing + subject: "Remind to close your accounting periods" + body: + warning_last_closed_period_over_1_year: "Please remind to periodically close your accounting periods. Last closed period ended at %{LAST_END}." + warning_no_closed_periods: "Please remind to periodically close your accounting periods. You have to close periods from %{FIRST_DATE}." + shared: hello: "¡Hola %{user_name}!" diff --git a/config/locales/mails.fr.yml b/config/locales/mails.fr.yml index aca7d6b80..25a85599b 100644 --- a/config/locales/mails.fr.yml +++ b/config/locales/mails.fr.yml @@ -280,6 +280,11 @@ fr: subject: "Espace disque faible" body: "Attention : l'espace disque disponible sur le serveur hébergeant fab-manager est inférieur à %{THRESHOLD} MiO. Cela peut nuire à son bon fonctionnement et empêcher la sauvegarde de certaines données. Actuellement, il reste %{AVAILABLE} MiO d'espace libre sur le point de montage." + notify_admin_close_period_reminder: + subject: "Pensez à clôturer vos périodes comptables" + body: + warning_last_closed_period_over_1_year: "Pensez à clôturer régulièrement vos périodes comptables. Les comptes sont actuellement clôturés jusqu'au %{LAST_END}." + warning_no_closed_periods: "Pensez à clôturer régulièrement vos périodes comptables. Vous devez clôturer des périodes depuis le %{FIRST_DATE}." shared: hello: "Bonjour %{user_name}" diff --git a/config/locales/mails.pt.yml b/config/locales/mails.pt.yml index 06becefe3..26df865fa 100755 --- a/config/locales/mails.pt.yml +++ b/config/locales/mails.pt.yml @@ -280,5 +280,11 @@ pt: subject: "Low disk space" body: "Warning: available disk space on the server hosting fab-manager is less than %{THRESHOLD} MiB. This can affect its operation and prevent saving some data. Currently, %{AVAILABLE} MiB of free disk space remains available on the mount point." + notify_admin_close_period_reminder: #translation_missing + subject: "Remind to close your accounting periods" + body: + warning_last_closed_period_over_1_year: "Please remind to periodically close your accounting periods. Last closed period ended at %{LAST_END}." + warning_no_closed_periods: "Please remind to periodically close your accounting periods. You have to close periods from %{FIRST_DATE}." + shared: hello: "Olá %{user_name}" diff --git a/config/locales/pt.yml b/config/locales/pt.yml index 7a1b0111d..cffb4270e 100755 --- a/config/locales/pt.yml +++ b/config/locales/pt.yml @@ -311,6 +311,9 @@ pt: enjoy_a_discount_of_AMOUNT_with_code_CODE: "Desfrute de um desconto de %{AMOUNT} com o código %{CODE}" notify_admin_free_disk_space: warning_disk_space_under_threshold: "Warning: the server's available disk space is now %{AVAILABLE} MiB" # missing translation + notify_admin_close_period_reminder: + warning_last_closed_period_over_1_year: "Please remind to periodically close your accounting periods. Last closed period ended at %{LAST_END}" # missing translation + warning_no_closed_periods: "Please remind to periodically close your accounting periods. You have to close periods from %{FIRST_DATE}" # missing translation statistics: # statistics tools for admins diff --git a/config/schedule.yml b/config/schedule.yml index aa9e54101..f2a8e114e 100644 --- a/config/schedule.yml +++ b/config/schedule.yml @@ -24,6 +24,11 @@ reservation_reminder: class: "ReservationReminderWorker" queue: default +close_period_reminder_worker: + cron: "0 12 * * 1" # every monday at 12pm + class: "ClosePeriodReminderWorker" + queue: default + free_disk_space: cron: "0 5 * * 0" # every sunday at 5am class: "FreeDiskSpaceWorker" diff --git a/test/integration/accounting_period_test.rb b/test/integration/accounting_period_test.rb index 3adc82f65..1c0948673 100644 --- a/test/integration/accounting_period_test.rb +++ b/test/integration/accounting_period_test.rb @@ -35,7 +35,7 @@ class AccountingPeriodTest < ActionDispatch::IntegrationTest # Extract archive require 'tmpdir' require 'fileutils' - dest = "#{Dir.tmpdir}/#{accounting_period.archive_file[0..-5]}" + dest = "#{Dir.tmpdir}/accounting/#{accounting_period.id}" FileUtils.mkdir_p "#{dest}/accounting" Zip::File.open(accounting_period.archive_file) do |zip_file| # Handle entries one by one @@ -63,8 +63,9 @@ class AccountingPeriodTest < ActionDispatch::IntegrationTest require 'version' assert_equal Version.current, archive_json['software']['version'] - # we clean up the extraction dir before quitting + # we clean up the files before quitting FileUtils.rm_rf(dest) + FileUtils.rm_rf(accounting_period.archive_folder) end end