class BillingLine < InvoicingRecord
  belongs_to :billing, optional: true
  belongs_to :right_owner, optional: true
  belongs_to :product_type, optional: true
  belongs_to :image, optional: true

  validates :product_type_id, :quantity, :price, :vat, :total_ht, :discount, :total_ttc, presence: true

  delegate :discount_type, to: :billing, allow_nil: true

  def default_photographer_rate?
    return false if photographer_rate.nil?
    photographer_rate == right_owner.try("#{country_type}_rate")
  end

  def country_type
    billing&.billing_company&.country_type || :foreign
  end

  def vat_amount
    total_ttc - total_ht
  end
end
