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)
|
2023-11-23 08:07:18 +01:00
|
|
|
if record_id
|
|
|
|
psi = PaymentScheduleItem.find(record_id)
|
|
|
|
check_item(psi)
|
|
|
|
else
|
|
|
|
PaymentScheduleItem.where.not(state: 'paid').where('due_date < ?', Time.current).each do |item|
|
|
|
|
check_item(item)
|
|
|
|
end
|
|
|
|
end
|
2021-02-08 15:28:47 +01:00
|
|
|
end
|
|
|
|
|
2023-01-05 12:09:16 +01:00
|
|
|
# @param psi [PaymentScheduleItem]
|
2021-02-08 15:28:47 +01:00
|
|
|
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'
|
2022-01-05 15:58:33 +01:00
|
|
|
### Check/Bank transfer (only new deadlines, to prevent spamming)
|
|
|
|
NotificationCenter.call type: "notify_admin_payment_schedule_#{psi.payment_schedule.payment_method}_deadline",
|
2021-02-08 15:28:47 +01:00
|
|
|
receiver: User.admins_and_managers,
|
|
|
|
attached_object: psi
|
2023-01-05 12:09:16 +01:00
|
|
|
psi.update(state: 'pending')
|
2020-12-23 16:56:19 +01:00
|
|
|
end
|
2023-01-05 12:09:16 +01:00
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.debug(e.backtrace)
|
|
|
|
psi.update(state: 'error')
|
2020-12-23 16:56:19 +01:00
|
|
|
end
|
|
|
|
end
|