2020-12-23 16:56:19 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Periodically checks if a PaymentScheduleItem cames to its due date.
|
|
|
|
# If this is the case
|
|
|
|
class PaymentScheduleItemWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
|
2021-02-08 15:28:47 +01:00
|
|
|
def perform(record_id = nil)
|
|
|
|
if record_id
|
|
|
|
psi = PaymentScheduleItem.find(record_id)
|
|
|
|
check_item(psi)
|
|
|
|
else
|
|
|
|
PaymentScheduleItem.where.not(state: 'paid').where('due_date < ?', DateTime.current).each do |psi|
|
|
|
|
check_item(psi)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_item(psi)
|
2021-06-03 12:22:37 +02:00
|
|
|
# the following depends on the payment method (card/check)
|
2021-04-21 17:38:06 +02:00
|
|
|
if psi.payment_schedule.payment_method == 'card'
|
2021-06-03 12:22:37 +02:00
|
|
|
### Cards
|
|
|
|
PaymentGatewayService.new.process_payment_schedule_item(psi)
|
2021-03-03 09:09:17 +01:00
|
|
|
elsif psi.state == 'new'
|
|
|
|
### Check (only new deadlines, to prevent spamming)
|
2021-02-08 15:28:47 +01:00
|
|
|
NotificationCenter.call type: 'notify_admin_payment_schedule_check_deadline',
|
|
|
|
receiver: User.admins_and_managers,
|
|
|
|
attached_object: psi
|
|
|
|
psi.update_attributes(state: 'pending')
|
2020-12-23 16:56:19 +01:00
|
|
|
end
|
2021-02-22 11:27:29 +01:00
|
|
|
rescue StandardError
|
|
|
|
psi.update_attributes(state: 'error')
|
2020-12-23 16:56:19 +01:00
|
|
|
end
|
|
|
|
end
|