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
|
2020-12-22 09:53:41 +01:00
|
|
|
class InvoiceItem < Footprintable
|
2016-03-23 18:39:41 +01:00
|
|
|
belongs_to :invoice
|
|
|
|
|
2022-10-26 17:35:02 +02:00
|
|
|
has_one :invoice_item, dependent: :destroy # associates invoice_items of an invoice to invoice_items of an Avoir
|
|
|
|
has_one :payment_gateway_object, as: :item, dependent: :destroy
|
2019-02-12 14:45:21 +01:00
|
|
|
|
2021-05-25 17:28:35 +02:00
|
|
|
belongs_to :object, polymorphic: true
|
|
|
|
|
2019-02-12 14:45:21 +01:00
|
|
|
after_create :chain_record
|
2019-09-11 12:22:14 +02:00
|
|
|
after_update :log_changes
|
2019-02-12 14:45:21 +01:00
|
|
|
|
2019-09-18 17:14:59 +02:00
|
|
|
def amount_after_coupon
|
|
|
|
# deduct coupon discount
|
|
|
|
coupon_service = CouponService.new
|
2022-10-26 17:35:02 +02:00
|
|
|
total_without_coupon = coupon_service.invoice_total_no_coupon(invoice)
|
|
|
|
coupon_service.ventilate(total_without_coupon, amount, invoice.coupon)
|
2019-09-18 17:14:59 +02:00
|
|
|
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)
|
2022-09-08 20:37:22 +02:00
|
|
|
Rational(amount_after_coupon / ((vat_rate / 100.00) + 1)).round.to_f
|
2019-09-18 17:14:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# return the VAT amount for this item
|
|
|
|
def vat
|
|
|
|
amount_after_coupon - net_amount
|
|
|
|
end
|
|
|
|
|
2022-09-09 16:35:49 +02:00
|
|
|
# return invoice item type (Machine/Training/Space/Event/Subscription/OrderItem) 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
|
2022-09-09 16:35:49 +02:00
|
|
|
elsif object_type == OrderItem.name
|
2022-09-08 20:37:22 +02:00
|
|
|
Product.name
|
2021-12-28 19:42:04 +01:00
|
|
|
else
|
|
|
|
''
|
2021-12-23 19:36:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-20 11:01:53 +01:00
|
|
|
private
|
2019-02-12 16:00:36 +01:00
|
|
|
|
2019-09-11 12:22:14 +02:00
|
|
|
def log_changes
|
2019-09-11 14:29:35 +02:00
|
|
|
return if Rails.env.test?
|
2019-09-11 12:22:14 +02:00
|
|
|
return unless changed?
|
|
|
|
|
2022-07-26 17:27:33 +02:00
|
|
|
Rails.logger.warn "InvoiceItem update triggered [ id: #{id}, invoice reference: #{invoice.reference} ]\n" \
|
|
|
|
"---------- changes ----------\n#{changes}\n---------------------------------"
|
2019-09-11 12:22:14 +02:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|