class TaxProfile < ApplicationRecord
  has_one :lookup, -> { where(lookup_type: 'tax_profiles') }, foreign_key: "value", primary_key: "group_type"

  default_scope { order('order_by asc') }
  # validations
  validates_presence_of :name, :rate, :group_type

  scope :filter_by_group_type, -> (group_type) { where group_type: group_type }

  def self.calculate_tax(group_type)
    divided_value =0.0
    exclusive =0.0
    tax_profiles = TaxProfile.where(group_type: group_type, tax_type: 'Percentage')
    if !tax_profiles.empty?
      tax_profiles.each do |tax|
        #include or execulive
        if tax.inclusive
          rate = tax.rate.to_f
          divided_value += (100 + rate)/rate
        else
          exclusive += tax.rate.to_f / 100
        end
      end
    end
    return divided_value,exclusive
  end
end
