Class: ProductTaxCode

Inherits:
ApplicationRecord show all
Defined in:
app/models/product_tax_code.rb

Overview

== Schema Information

Table name: product_tax_codes
Database name: primary

id :integer not null, primary key
description :string
name :string not null
product_tax_code :string not null
tax_class :string not null

Constant Summary

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.default_for_serviceProductTaxCode?

Returns:



18
19
20
# File 'app/models/product_tax_code.rb', line 18

def self.default_for_service
  find_by(product_tax_code: '19000')
end

.import_from_taxjarInteger

This method pulls down the tax categories from taxjar and update
our tables.

Returns:

  • (Integer)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/product_tax_code.rb', line 25

def self.import_from_taxjar
  counter = 0
  Api::Taxjar.client.categories.each do |tj_cat|
    counter += 1
    tax_code = tj_cat.product_tax_code.presence.chomp
    ptc = where(product_tax_code: tax_code).first_or_initialize
    ptc.name = tj_cat.name.chomp
    ptc.description = tj_cat.description.chomp

    # Taking a guess at mapping to our oversimplified tax classification, these are books, tangibles, as far as i can tell
    ptc.tax_class = if ptc.new_record? && tax_code.first(2).in?(%w[20 51 40 81 41])
                      'g'
                    else
                      'svc'
                    end
    ptc.save!
  end
  counter
end

.options_for_selectArray<Array(String, Integer)>

Returns:

  • (Array<Array(String, Integer)>)


46
47
48
# File 'app/models/product_tax_code.rb', line 46

def self.options_for_select
  order(:name).map { |ptc| ["#{ptc.name} (#{ptc.product_tax_code})", ptc.id] }
end

Instance Method Details

#itemsActiveRecord::Relation<Item>

Returns:

  • (ActiveRecord::Relation<Item>)


15
# File 'app/models/product_tax_code.rb', line 15

has_many :items, inverse_of: :product_tax_code

#to_sString

Returns:

  • (String)


51
52
53
# File 'app/models/product_tax_code.rb', line 51

def to_s
  "#{name} (#{product_tax_code})"
end