Sha256: 1d9aac81ea403b9b8b26df0282a8ff002e0aa854a93b52583134a17bfd652ae3

Contents?: true

Size: 1.79 KB

Versions: 21

Compression:

Stored size: 1.79 KB

Contents

module Workarea
  module Tax
    class Rate
      include ApplicationDocument


      field :country_percentage, type: Float, default: 0
      field :region_percentage, type: Float, default: 0
      field :postal_code_percentage, type: Float, default: 0
      field :country, type: Country
      field :region, type: String
      field :postal_code, type: String
      field :charge_on_shipping, type: Boolean, default: true
      field :tier_min, type: Money
      field :tier_max, type: Money

      # This field is deprecated in favor of the more specific percentage fields
      # TODO: Remove in v3.6,
      field :percentage, type: Float, default: 0
      alias_method :percentage=, :postal_code_percentage=

      index({ category_id: 1, country: 1, region: 1, postal_code: 1 })
      index({ country: 1 })
      index({ region: 1 })
      index({ postal_code: 1 })

      belongs_to :category,
        class_name: 'Workarea::Tax::Category',
        inverse_of: :rates,
        index: true

      def self.search(query)
        return all unless query.present?

        regex = /^#{::Regexp.quote(query)}/i
        country = Country[query]

        clauses = [{ region: regex }, { postal_code: regex }]
        clauses << { country: country } if country.present?

        any_of(clauses)
      end

      def self.sorts
        [Sort.country, Sort.region, Sort.postal_code, Sort.newest, Sort.modified]
      end

      def valid?(*)
        super.tap do
          self.region = nil if region.blank?
          self.postal_code = nil if postal_code.blank?
          self.percentage ||= 0
        end
      end

      def percentage
        percentage_field = super
        return percentage_field unless percentage_field.zero?

        country_percentage + region_percentage + postal_code_percentage
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
workarea-core-3.5.19 app/models/workarea/tax/rate.rb
workarea-core-3.5.18 app/models/workarea/tax/rate.rb
workarea-core-3.5.17 app/models/workarea/tax/rate.rb
workarea-core-3.5.16 app/models/workarea/tax/rate.rb
workarea-core-3.5.15 app/models/workarea/tax/rate.rb
workarea-core-3.5.14 app/models/workarea/tax/rate.rb
workarea-core-3.5.13 app/models/workarea/tax/rate.rb
workarea-core-3.5.12 app/models/workarea/tax/rate.rb
workarea-core-3.5.11 app/models/workarea/tax/rate.rb
workarea-core-3.5.10 app/models/workarea/tax/rate.rb
workarea-core-3.5.9 app/models/workarea/tax/rate.rb
workarea-core-3.5.8 app/models/workarea/tax/rate.rb
workarea-core-3.5.7 app/models/workarea/tax/rate.rb
workarea-core-3.5.6 app/models/workarea/tax/rate.rb
workarea-core-3.5.5 app/models/workarea/tax/rate.rb
workarea-core-3.5.4 app/models/workarea/tax/rate.rb
workarea-core-3.5.3 app/models/workarea/tax/rate.rb
workarea-core-3.5.2 app/models/workarea/tax/rate.rb
workarea-core-3.5.1 app/models/workarea/tax/rate.rb
workarea-core-3.5.0 app/models/workarea/tax/rate.rb