2020-10-27 11:32:20 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-10-27 16:02:41 +01:00
|
|
|
# PaymentSchedule is a way for members to pay something (especially a Subscription) with multiple payment,
|
2020-10-27 11:32:20 +01:00
|
|
|
# staged on a long period rather than with a single payment
|
2020-12-22 14:43:08 +01:00
|
|
|
class PaymentSchedule < PaymentDocument
|
|
|
|
require 'fileutils'
|
|
|
|
|
2020-10-27 11:32:20 +01:00
|
|
|
belongs_to :wallet_transaction
|
|
|
|
belongs_to :coupon
|
|
|
|
belongs_to :invoicing_profile
|
2021-01-25 14:37:07 +01:00
|
|
|
belongs_to :statistic_profile
|
2022-08-26 15:56:20 +02:00
|
|
|
belongs_to :operator_profile, class_name: 'InvoicingProfile'
|
2020-11-12 16:44:55 +01:00
|
|
|
|
2022-11-10 16:14:49 +01:00
|
|
|
has_many :payment_schedule_items, dependent: :destroy
|
|
|
|
has_many :payment_gateway_objects, as: :item, dependent: :destroy
|
|
|
|
has_many :payment_schedule_objects, dependent: :destroy
|
2020-11-12 16:44:55 +01:00
|
|
|
|
2020-11-16 16:37:40 +01:00
|
|
|
before_create :add_environment
|
|
|
|
after_create :update_reference, :chain_record
|
2020-12-23 16:56:19 +01:00
|
|
|
after_commit :generate_and_send_document, on: [:create], if: :persisted?
|
2021-01-25 14:37:07 +01:00
|
|
|
after_commit :generate_initial_invoice, on: [:create], if: :persisted?
|
2020-11-16 16:37:40 +01:00
|
|
|
|
2020-12-22 14:43:08 +01:00
|
|
|
def file
|
|
|
|
dir = "payment_schedules/#{invoicing_profile.id}"
|
2020-11-16 16:37:40 +01:00
|
|
|
|
2020-12-22 14:43:08 +01:00
|
|
|
# create directories if they doesn't exists (payment_schedules & invoicing_profile_id)
|
|
|
|
FileUtils.mkdir_p dir
|
|
|
|
"#{dir}/#{filename}"
|
2020-11-16 16:37:40 +01:00
|
|
|
end
|
|
|
|
|
2020-12-22 14:43:08 +01:00
|
|
|
def filename
|
2020-12-22 16:39:37 +01:00
|
|
|
prefix = Setting.find_by(name: 'payment_schedule_prefix').value_at(created_at)
|
|
|
|
prefix ||= if created_at < Setting.find_by(name: 'payment_schedule_prefix').first_update
|
|
|
|
Setting.find_by(name: 'payment_schedule_prefix').first_value
|
2020-12-22 14:43:08 +01:00
|
|
|
else
|
2020-12-22 16:39:37 +01:00
|
|
|
Setting.get('payment_schedule_prefix')
|
2020-12-22 14:43:08 +01:00
|
|
|
end
|
2020-12-22 16:39:37 +01:00
|
|
|
"#{prefix}-#{id}_#{created_at.strftime('%d%m%Y')}.pdf"
|
2020-11-16 16:37:40 +01:00
|
|
|
end
|
|
|
|
|
2023-03-21 15:11:08 +01:00
|
|
|
def order_number
|
|
|
|
ordered_items.first&.invoice&.order_number || PaymentDocumentService.generate_order_number(self)
|
|
|
|
end
|
|
|
|
|
2020-12-22 14:43:08 +01:00
|
|
|
##
|
|
|
|
# This is useful to check the first item because its amount may be different from the others
|
|
|
|
##
|
|
|
|
def ordered_items
|
|
|
|
payment_schedule_items.order(due_date: :asc)
|
2020-12-16 18:33:43 +01:00
|
|
|
end
|
|
|
|
|
2021-04-23 12:52:06 +02:00
|
|
|
def gateway_payment_mean
|
|
|
|
payment_gateway_objects.map(&:gateway_object).find(&:payment_mean?)
|
2021-04-21 17:38:06 +02:00
|
|
|
end
|
|
|
|
|
2021-04-23 17:54:59 +02:00
|
|
|
def gateway_subscription
|
2022-01-11 16:04:14 +01:00
|
|
|
payment_gateway_objects.includes(:payment_gateway_object).map(&:gateway_object).find(&:subscription?)
|
2021-06-04 18:26:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def gateway_order
|
|
|
|
payment_gateway_objects.map(&:gateway_object).find(&:order?)
|
2021-04-23 17:54:59 +02:00
|
|
|
end
|
|
|
|
|
2021-05-27 15:58:55 +02:00
|
|
|
def main_object
|
2021-06-04 18:26:20 +02:00
|
|
|
payment_schedule_objects.find_by(main: true)
|
2021-05-27 15:58:55 +02:00
|
|
|
end
|
|
|
|
|
2022-08-26 15:56:20 +02:00
|
|
|
delegate :user, to: :invoicing_profile
|
2021-01-19 16:47:51 +01:00
|
|
|
|
2021-02-10 11:40:23 +01:00
|
|
|
# for debug & used by rake task "fablab:maintenance:regenerate_schedules"
|
|
|
|
def regenerate_pdf
|
|
|
|
pdf = ::PDF::PaymentSchedule.new(self).render
|
|
|
|
File.binwrite(file, pdf)
|
|
|
|
end
|
|
|
|
|
2021-04-16 16:03:10 +02:00
|
|
|
def footprint_children
|
|
|
|
payment_schedule_items
|
2020-12-16 18:33:43 +01:00
|
|
|
end
|
2020-12-23 16:56:19 +01:00
|
|
|
|
2022-01-11 16:15:43 +01:00
|
|
|
def self.columns_out_of_footprint
|
|
|
|
%w[payment_method]
|
|
|
|
end
|
|
|
|
|
2021-10-15 17:31:01 +02:00
|
|
|
def post_save(*args)
|
2021-04-21 17:38:06 +02:00
|
|
|
return unless payment_method == 'card'
|
2020-12-29 18:55:00 +01:00
|
|
|
|
2021-10-15 17:31:01 +02:00
|
|
|
PaymentGatewayService.new.create_subscription(self, *args)
|
2020-12-29 18:55:00 +01:00
|
|
|
end
|
|
|
|
|
2021-05-28 17:34:20 +02:00
|
|
|
def render_resource
|
|
|
|
{ partial: 'api/payment_schedules/payment_schedule', locals: { payment_schedule: self } }
|
|
|
|
end
|
|
|
|
|
2021-06-04 18:26:20 +02:00
|
|
|
def to_cart
|
|
|
|
service = CartService.new(operator_profile.user)
|
|
|
|
service.from_payment_schedule(self)
|
|
|
|
end
|
|
|
|
|
2020-12-23 16:56:19 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def generate_and_send_document
|
|
|
|
return unless Setting.get('invoicing_module')
|
|
|
|
|
|
|
|
unless Rails.env.test?
|
2022-07-26 17:27:33 +02:00
|
|
|
Rails.logger.info "Creating an PaymentScheduleWorker job to generate the following payment schedule: id(#{id}), " \
|
|
|
|
"main_object.object_id(#{main_object.object_id}), " \
|
|
|
|
"main_object.object_type(#{main_object.object_type}), user_id(#{invoicing_profile.user_id})"
|
2020-12-23 16:56:19 +01:00
|
|
|
end
|
2020-12-28 18:30:03 +01:00
|
|
|
PaymentScheduleWorker.perform_async(id)
|
2020-12-23 16:56:19 +01:00
|
|
|
end
|
2021-01-25 14:37:07 +01:00
|
|
|
|
|
|
|
def generate_initial_invoice
|
|
|
|
PaymentScheduleItemWorker.perform_async
|
|
|
|
end
|
2020-10-27 11:32:20 +01:00
|
|
|
end
|