From 3e4892d3a57f861a2ea08a23e8a409ddb5dba324 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 23 Dec 2020 16:56:19 +0100 Subject: [PATCH] WIP: creating payment schedule workers see TODO in app/workers/payment_schedule* --- app/models/payment_schedule.rb | 13 +++++++++++ app/workers/invoice_worker.rb | 3 +++ app/workers/payment_schedule_item_worker.rb | 24 +++++++++++++++++++ app/workers/payment_schedule_worker.rb | 26 ++++++++++----------- 4 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 app/workers/payment_schedule_item_worker.rb diff --git a/app/models/payment_schedule.rb b/app/models/payment_schedule.rb index 95a19dca5..896418a2d 100644 --- a/app/models/payment_schedule.rb +++ b/app/models/payment_schedule.rb @@ -18,6 +18,7 @@ class PaymentSchedule < PaymentDocument before_create :add_environment after_create :update_reference, :chain_record + after_commit :generate_and_send_document, on: [:create], if: :persisted? def file dir = "payment_schedules/#{invoicing_profile.id}" @@ -47,4 +48,16 @@ class PaymentSchedule < PaymentDocument def check_footprint payment_schedule_items.map(&:check_footprint).all? && footprint == compute_footprint end + + private + + def generate_and_send_document + return unless Setting.get('invoicing_module') + + unless Rails.env.test? + puts "Creating an InvoiceWorker job to generate the following invoice: id(#{id}), invoiced_id(#{invoiced_id}), " \ + "invoiced_type(#{invoiced_type}), user_id(#{invoicing_profile.user_id})" + end + InvoiceWorker.perform_async(id, user&.subscription&.expired_at) + end end diff --git a/app/workers/invoice_worker.rb b/app/workers/invoice_worker.rb index 7bf6ac310..094ee9af2 100644 --- a/app/workers/invoice_worker.rb +++ b/app/workers/invoice_worker.rb @@ -1,3 +1,6 @@ +# frozen_string_literal: true + +# Generates the PDF Document associated with the provided invoice, and send it to the customer class InvoiceWorker include Sidekiq::Worker diff --git a/app/workers/payment_schedule_item_worker.rb b/app/workers/payment_schedule_item_worker.rb new file mode 100644 index 000000000..3cd82984b --- /dev/null +++ b/app/workers/payment_schedule_item_worker.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +# Periodically checks if a PaymentScheduleItem cames to its due date. +# If this is the case +class PaymentScheduleItemWorker + include Sidekiq::Worker + + def perform + PaymentScheduleItem.where(due_date: [DateTime.current.at_beginning_of_day, DateTime.current.end_of_day], state: 'new').each do |psi| + # the following depends on the payment method (stripe/check) + if psi.payment_schedule.payment_method == 'stripe' + # TODO, if stripe: + # - verify the payment was successful + # - if not, alert the admins + # - if succeeded, generate the invoice + else + # TODO, if check: + # - alert the admins and the user that it is time to bank the check + # - generate the invoice + end + # TODO, finally, in any cases, update the psi.state field according to the new status + end + end +end diff --git a/app/workers/payment_schedule_worker.rb b/app/workers/payment_schedule_worker.rb index 34b99250c..820013c1b 100644 --- a/app/workers/payment_schedule_worker.rb +++ b/app/workers/payment_schedule_worker.rb @@ -1,21 +1,21 @@ # frozen_string_literal: true -# Periodically checks if a PaymentScheduleItem cames to its due date. +# Generates the PDF Document associated with the provided payment schedule, and send it to the customer # If this is the case class PaymentScheduleWorker include Sidekiq::Worker - def perform - PaymentScheduleItem.where(due_date: [DateTime.current.at_beginning_of_day, DateTime.current.end_of_day], state: 'new').each do |psi| - # the following depends on the payment method (stripe/check) - # if stripe: - # - verify the payment was successful - # - if not, alert the admins - # - if succeeded, generate the invoice - # if check: - # - alert the admins and the user that it is time to bank the check - # - generate the invoice - # finally, in any cases, update the psi.state field according to the new status - end + 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 + + # 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 + receiver: ps.user, + attached_object: ps end end