mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-19 13:54:25 +01:00
WIP: creating payment schedule workers
see TODO in app/workers/payment_schedule*
This commit is contained in:
parent
65277800e2
commit
3e4892d3a5
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
24
app/workers/payment_schedule_item_worker.rb
Normal file
24
app/workers/payment_schedule_item_worker.rb
Normal file
@ -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
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user