1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/app/models/invoice_item.rb

64 lines
2.1 KiB
Ruby
Raw Normal View History

2019-03-13 16:49:03 +01:00
# frozen_string_literal: true
# A single line inside an invoice. Can be a subscription or a reservation
class InvoiceItem < Footprintable
2016-03-23 18:39:41 +01:00
belongs_to :invoice
has_one :invoice_item # associates invoice_items of an invoice to invoice_items of an Avoir
has_one :payment_gateway_object, as: :item
belongs_to :object, polymorphic: true
belongs_to :reservation, foreign_type: 'Reservation', foreign_key: 'object_id'
belongs_to :subscription, foreign_type: 'Subscription', foreign_key: 'object_id'
belongs_to :wallet_transaction, foreign_type: 'WalletTransaction', foreign_key: 'object_id'
belongs_to :offer_day, foreign_type: 'OfferDay', foreign_key: 'object_id'
2021-06-28 18:17:11 +02:00
belongs_to :statistic_profile_prepaid_pack, foreign_type: 'StatisticProfilePrepaidPack', foreign_key: 'object_id'
after_create :chain_record
after_update :log_changes
def amount_after_coupon
# deduct coupon discount
coupon_service = CouponService.new
coupon_service.ventilate(invoice.total, amount, invoice.coupon)
end
# return the item amount, coupon discount deducted, if any, and VAT excluded, if applicable
def net_amount
# deduct VAT
vat_service = VatHistoryService.new
2021-12-28 19:42:04 +01:00
vat_rate = vat_service.invoice_item_vat(self)
Rational(amount_after_coupon / (vat_rate / 100.00 + 1)).round.to_f
end
# return the VAT amount for this item
def vat
amount_after_coupon - net_amount
end
2021-12-28 19:42:04 +01:00
# return invoice item type (Machine/Training/Space/Event/Subscription) used to determine the VAT rate
2021-12-23 19:36:23 +01:00
def invoice_item_type
if object_type == Reservation.name
2021-12-28 19:42:04 +01:00
object.try(:reservable_type) || ''
2021-12-29 17:00:49 +01:00
elsif [Subscription.name, OfferDay.name].include? object_type
2021-12-28 19:42:04 +01:00
Subscription.name
2021-12-29 17:00:49 +01:00
elsif object_type == StatisticProfilePrepaidPack.name
object.prepaid_pack.priceable_type
2021-12-28 19:42:04 +01:00
else
''
2021-12-23 19:36:23 +01:00
end
end
private
2019-02-12 16:00:36 +01:00
def log_changes
return if Rails.env.test?
return unless changed?
puts "WARNING: InvoiceItem update triggered [ id: #{id}, invoice reference: #{invoice.reference} ]"
puts '---------- changes ----------'
puts changes
puts '---------------------------------'
end
2016-03-23 18:39:41 +01:00
end