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 :scheduled, polymorphic: true
|
|
|
|
belongs_to :wallet_transaction
|
|
|
|
belongs_to :coupon
|
|
|
|
belongs_to :invoicing_profile
|
|
|
|
belongs_to :operator_profile, foreign_key: :operator_profile_id, class_name: 'InvoicingProfile'
|
2020-11-12 16:44:55 +01:00
|
|
|
|
|
|
|
belongs_to :subscription, foreign_type: 'Subscription', foreign_key: 'scheduled_id'
|
|
|
|
belongs_to :reservation, foreign_type: 'Reservation', foreign_key: 'scheduled_id'
|
|
|
|
|
|
|
|
has_many :payment_schedule_items
|
|
|
|
|
2020-11-16 16:37:40 +01:00
|
|
|
before_create :add_environment
|
|
|
|
after_create :update_reference, :chain_record
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def check_footprint
|
|
|
|
payment_schedule_items.map(&:check_footprint).all? && footprint == compute_footprint
|
|
|
|
end
|
2020-10-27 11:32:20 +01:00
|
|
|
end
|