From e5439901d658d557817a67dd73e5d102b0970571 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 19 Jan 2021 16:47:51 +0100 Subject: [PATCH] fix generate payment schedule pdf + send by mail TODO: total amount does not match with sum of all lines - generate invoices for each PS-item - interface to access the payment schedules --- app/mailers/notifications_mailer.rb | 7 +++++++ app/models/notification_type.rb | 1 + app/models/payment_schedule.rb | 4 ++++ app/pdfs/pdf/payment_schedule.rb | 18 +++++++++-------- ..._when_payment_schedule_ready.json.jbuilder | 9 +++++++++ ...ify_member_payment_schedule_ready.html.erb | 20 +++++++++++++++++++ app/workers/payment_schedule_worker.rb | 4 ++-- config/locales/en.yml | 2 ++ config/locales/fr.yml | 2 ++ config/locales/mails.en.yml | 6 ++++++ config/locales/mails.fr.yml | 6 ++++++ 11 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 app/views/api/notifications/_notify_user_when_payment_schedule_ready.json.jbuilder create mode 100644 app/views/notifications_mailer/notify_member_payment_schedule_ready.html.erb diff --git a/app/mailers/notifications_mailer.rb b/app/mailers/notifications_mailer.rb index 837a91395..6e49d9d85 100644 --- a/app/mailers/notifications_mailer.rb +++ b/app/mailers/notifications_mailer.rb @@ -43,4 +43,11 @@ class NotificationsMailer < NotifyWith::NotificationsMailer subject: t('notifications_mailer.notify_member_avoir_ready.subject'), template_name: 'notify_member_avoir_ready') end + + def notify_user_when_payment_schedule_ready + attachments[@attached_object.filename] = File.read(@attached_object.file) + mail(to: @recipient.email, + subject: t('notifications_mailer.notify_member_payment_schedule_ready.subject'), + template_name: 'notify_member_payment_schedule_ready') + end end diff --git a/app/models/notification_type.rb b/app/models/notification_type.rb index 619c51a47..1478f02dd 100644 --- a/app/models/notification_type.rb +++ b/app/models/notification_type.rb @@ -54,6 +54,7 @@ class NotificationType notify_admins_role_update notify_user_role_update notify_admin_members_stripe_sync + notify_user_when_payment_schedule_ready ] # deprecated: # - notify_member_subscribed_plan_is_changed diff --git a/app/models/payment_schedule.rb b/app/models/payment_schedule.rb index f68ed5aa6..5e9744421 100644 --- a/app/models/payment_schedule.rb +++ b/app/models/payment_schedule.rb @@ -45,6 +45,10 @@ class PaymentSchedule < PaymentDocument payment_schedule_items.order(due_date: :asc) end + def user + invoicing_profile.user + end + def check_footprint payment_schedule_items.map(&:check_footprint).all? && footprint == compute_footprint end diff --git a/app/pdfs/pdf/payment_schedule.rb b/app/pdfs/pdf/payment_schedule.rb index 6ffd5d5ed..1c4f4bb18 100644 --- a/app/pdfs/pdf/payment_schedule.rb +++ b/app/pdfs/pdf/payment_schedule.rb @@ -79,19 +79,21 @@ class PDF::PaymentSchedule < Prawn::Document payment_schedule.payment_schedule_items.each do |item| price = item.amount.to_i / 100.00 - date = item.due_date + date = I18n.l(item.due_date.to_date) data += [[date, number_to_currency(price)]] end data += [[I18n.t('payment_schedules.total_amount'), number_to_currency(payment_schedule.total / 100.0)]] # display table - table(data, header: true, column_widths: [400, 72], cell_style: { inline_format: true }) do - row(0).font_style = :bold - column(1).style align: :right - row(-1).style align: :right - row(-1).background_color = 'E4E4E4' - row(-1).font_style = :bold + font_size(8) do + table(data, header: true, column_widths: [400, 72], cell_style: { inline_format: true }) do + row(0).font_style = :bold + column(1).style align: :right + row(-1).style align: :right + row(-1).background_color = 'E4E4E4' + row(-1).font_style = :bold + end end # payment method @@ -117,7 +119,7 @@ class PDF::PaymentSchedule < Prawn::Document end # factice watermark - return unless %w[staging test development].include?(invoice.environment) + return unless %w[staging test development].include?(payment_schedule.environment) transparent(0.1) do rotate(45, origin: [0, 0]) do diff --git a/app/views/api/notifications/_notify_user_when_payment_schedule_ready.json.jbuilder b/app/views/api/notifications/_notify_user_when_payment_schedule_ready.json.jbuilder new file mode 100644 index 000000000..c1852f38a --- /dev/null +++ b/app/views/api/notifications/_notify_user_when_payment_schedule_ready.json.jbuilder @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +json.title notification.notification_type +amount = notification.attached_object.total / 100.0 +json.description t('.your_schedule_is_ready_html', + REFERENCE: notification.attached_object.reference, + AMOUNT: number_to_currency(amount), + SCHEDULE_ID: notification.attached_object.id) + diff --git a/app/views/notifications_mailer/notify_member_payment_schedule_ready.html.erb b/app/views/notifications_mailer/notify_member_payment_schedule_ready.html.erb new file mode 100644 index 000000000..a3f8cd132 --- /dev/null +++ b/app/views/notifications_mailer/notify_member_payment_schedule_ready.html.erb @@ -0,0 +1,20 @@ +<%= render 'notifications_mailer/shared/hello', recipient: @recipient %> + +

+ <%= _t('.body.please_find_attached_html', + { + DATE: I18n.l(@attached_object.created_at.to_date), + AMOUNT: number_to_currency(@attached_object.total / 100.0), + TYPE: @attached_object.scheduled_type + }) + # messageFormat + %> +

+ +

<%= t('.body.schedule_in_your_dashboard_html', + DASHBOARD: link_to( + t('.body.your_dashboard'), + "#{root_url}#!/dashboard/invoices" + ) + ) %> +

diff --git a/app/workers/payment_schedule_worker.rb b/app/workers/payment_schedule_worker.rb index 820013c1b..5b9f325bd 100644 --- a/app/workers/payment_schedule_worker.rb +++ b/app/workers/payment_schedule_worker.rb @@ -8,13 +8,13 @@ class PaymentScheduleWorker def perform(payment_schedule_id) # generate a payment schedule document ps = PaymentSchedule.find(payment_schedule_id) - pdf = ::PDF::PaymentSchedule.new(ps).render # TODO, create ::PDF::PaymentSchedule + pdf = ::PDF::PaymentSchedule.new(ps).render # save the file on the disk File.binwrite(ps.file, pdf) # notify user, send schedule document by email - NotificationCenter.call type: 'notify_user_when_invoice_ready', # TODO, create a more appropriate notification type + NotificationCenter.call type: 'notify_user_when_payment_schedule_ready', receiver: ps.user, attached_object: ps end diff --git a/config/locales/en.yml b/config/locales/en.yml index 1082d8503..c4aaad9ef 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -356,6 +356,8 @@ en: user_NAME_changed_ROLE_html: "User %{NAME} is now %{ROLE}." notify_admin_members_stripe_sync: all_members_sync: "All members were successfully synchronized on Stripe." + notify_user_when_payment_schedule_ready: + your_schedule_is_ready_html: "Your payment schedule #%{REFERENCE}, of %{AMOUNT}, is ready. Click here to download." #statistics tools for admins statistics: subscriptions: "Subscriptions" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index b137674fe..8c3c340d1 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -356,6 +356,8 @@ fr: user_NAME_changed_ROLE_html: "L'utilisateur %{NAME} est maintenant %{ROLE}." notify_admin_members_stripe_sync: all_members_sync: "Tous les membres ont été synchronisés avec succès sur Stripe." + notify_user_when_payment_schedule_ready: + your_schedule_is_ready_html: "Votre échéancier n°%{REFERENCE}, d'un montant de %{AMOUNT}, est prêt. Cliquez ici pour le télécharger." #statistics tools for admins statistics: subscriptions: "Abonnements" diff --git a/config/locales/mails.en.yml b/config/locales/mails.en.yml index 16ff39df9..1cc3fa30a 100644 --- a/config/locales/mails.en.yml +++ b/config/locales/mails.en.yml @@ -287,5 +287,11 @@ en: subject: "Stripe synchronization" body: members_sync: "All members were successfully synchronized on Stripe." + notify_member_payment_schedule_ready: + subject: "Your payment schedule" + body: + please_find_attached_html: "Please find attached your payment schedule, issued on {DATE}, with an amount of {AMOUNT} concerning your {TYPE, select, Reservation{reservation} other{subscription}}." #messageFormat interpolation + schedule_in_your_dashboard_html: "You can find this payment schedule at any time from %{DASHBOARD} on the Fab Lab's website." + your_dashboard: "your dashboard" shared: hello: "Hello %{user_name}" diff --git a/config/locales/mails.fr.yml b/config/locales/mails.fr.yml index 63a8bb578..34b636b92 100644 --- a/config/locales/mails.fr.yml +++ b/config/locales/mails.fr.yml @@ -287,5 +287,11 @@ fr: subject: "Synchronisation Stripe" body: members_sync: "Tous les membres ont été synchronisés avec succès sur Stripe." + notify_member_payment_schedule_ready: + subject: "Votre échéancier de paiement" + body: + please_find_attached_html: "Vous trouverez en pièce jointe votre échéancier de paiement du {DATE}, d'un montant de {AMOUNT} concernant votre {TYPE, select, Reservation{réservation} other{abonnement}}." #messageFormat interpolation + schedule_in_your_dashboard_html: "Vous pouvez à tout moment retrouver votre échéancier dans %{DASHBOARD} sur le site du Fab Lab." + your_dashboard: "votre tableau de bord" shared: hello: "Bonjour %{user_name}"