Class: TaxExemption

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/tax_exemption.rb

Overview

== Schema Information

Table name: tax_exemptions
Database name: primary

id :integer not null, primary key
attachment_name :string
attachment_uid :string
certificate_number :string(255)
expiration_date :date
expiration_notified :boolean default(FALSE)
migrated :boolean default(FALSE)
state_code :string(20)
tax_type :string(255)
valid_from_date :date
created_at :datetime
updated_at :datetime
creator_id :integer
customer_id :integer
updater_id :integer
upload_id :integer

Indexes

by_vfd_expd_cid (valid_from_date,expiration_date,customer_id)
c_id_sc_ed_vfd (customer_id,state_code,expiration_date,valid_from_date)
cust_id_valid_date (customer_id,valid_from_date)
index_tax_exemptions_on_upload_id (upload_id)

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#attachment_fileObject

Virtual attribute: assign an uploaded file (ActionDispatch::Http::UploadedFile
or any IO-like object). Saved to Upload (secure bucket) in after_save.



42
43
44
# File 'app/models/tax_exemption.rb', line 42

def attachment_file
  @attachment_file
end

#certificate_numberObject (readonly)



48
# File 'app/models/tax_exemption.rb', line 48

validates :customer_id, :certificate_number, :expiration_date, :valid_from_date, :state_code, :tax_type, presence: true

#customer_idObject (readonly)



48
# File 'app/models/tax_exemption.rb', line 48

validates :customer_id, :certificate_number, :expiration_date, :valid_from_date, :state_code, :tax_type, presence: true

#expiration_dateObject (readonly)



48
# File 'app/models/tax_exemption.rb', line 48

validates :customer_id, :certificate_number, :expiration_date, :valid_from_date, :state_code, :tax_type, presence: true

#state_codeObject (readonly)



48
# File 'app/models/tax_exemption.rb', line 48

validates :customer_id, :certificate_number, :expiration_date, :valid_from_date, :state_code, :tax_type, presence: true

#tax_typeObject (readonly)



48
# File 'app/models/tax_exemption.rb', line 48

validates :customer_id, :certificate_number, :expiration_date, :valid_from_date, :state_code, :tax_type, presence: true

#valid_from_dateObject (readonly)



48
# File 'app/models/tax_exemption.rb', line 48

validates :customer_id, :certificate_number, :expiration_date, :valid_from_date, :state_code, :tax_type, presence: true

Class Method Details

.effective_nowActiveRecord::Relation<TaxExemption>

A relation of TaxExemptions that are effective now. Active Record Scope

Returns:

See Also:



59
# File 'app/models/tax_exemption.rb', line 59

scope :effective_now, -> { effective_on }

.effective_onActiveRecord::Relation<TaxExemption>

A relation of TaxExemptions that are effective on. Active Record Scope

Returns:

See Also:



58
# File 'app/models/tax_exemption.rb', line 58

scope :effective_on, ->(date = Date.current) { where(':date BETWEEN valid_from_date AND expiration_date', date:) }

Instance Method Details

#customerCustomer

Returns:

See Also:



35
# File 'app/models/tax_exemption.rb', line 35

belongs_to :customer, optional: true

#descriptionObject



72
73
74
# File 'app/models/tax_exemption.rb', line 72

def description
  "#{state_code} (#{certificate_number}), expires #{expiration_date}"
end

#has_valid_date_rangeObject



76
77
78
79
80
81
82
# File 'app/models/tax_exemption.rb', line 76

def has_valid_date_range
  # Check dates are in order
  return unless valid_from_date && expiration_date
  return unless valid_from_date > expiration_date

  errors.add(:base, 'date range is invalid')
end

#is_valid?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/models/tax_exemption.rb', line 68

def is_valid?
  period === Date.current
end

#notify_creationObject



112
113
114
# File 'app/models/tax_exemption.rb', line 112

def notify_creation
  InternalMailer.tax_exemption_uploaded(self).deliver_later
end

#notify_emailsObject

who will get notifications of tax exemption expiration and uploads activities



104
105
106
107
108
109
110
# File 'app/models/tax_exemption.rb', line 104

def notify_emails
  if !customer.is_e_commerce_misc? && (sales_rep_email = customer.primary_sales_rep.try(:email))
    sales_rep_email
  else
    'ar@warmlyyours.com'
  end
end

#notify_expirationObject



116
117
118
119
# File 'app/models/tax_exemption.rb', line 116

def notify_expiration
  InternalMailer.tax_exemption_expired(self).deliver_later
  update(expiration_notified: true)
end

#ordersActiveRecord::Relation<Order>

Returns:

  • (ActiveRecord::Relation<Order>)

See Also:



38
# File 'app/models/tax_exemption.rb', line 38

has_many :orders

#periodObject



64
65
66
# File 'app/models/tax_exemption.rb', line 64

def period
  valid_from_date..expiration_date
end

#stateState

Returns:

See Also:



36
# File 'app/models/tax_exemption.rb', line 36

belongs_to :state, foreign_key: :state_code, primary_key: :code, optional: true

#tax_exemption_overlapObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/tax_exemption.rb', line 84

def tax_exemption_overlap
  return unless customer && valid_from_date.present? && expiration_date.present? && state_code.present? && tax_type.present?

  # Check we don't have overlapping dates
  sql = '(valid_from_date, expiration_date) OVERLAPS (:valid_from_date, :expiration_date)'
  te = customer.tax_exemptions.where(state_code:, tax_type:)
               .where.not(id: id.to_i)
               .where(sql, valid_from_date:, expiration_date:).first
  return unless te # all valid no overlapping tax exemption detected

  errors.add(:base, "date overlap with another tax exemption (id: #{te.id}).")
end

#to_sObject



53
54
55
# File 'app/models/tax_exemption.rb', line 53

def to_s
  "Tax Exemption (#{[state_code, certificate_number].compact.join(' - ')})"
end

#update_open_ordersObject



97
98
99
100
101
# File 'app/models/tax_exemption.rb', line 97

def update_open_orders
  logger.info "Updating open order for tax exemption id #{id} customer #{customer.id} - #{customer.name}"
  customer.orders.open_for_change.not_cancelled.each(&:refresh_tax_rate) # light touch here is to ignore cancelled order but perhaps we should remove it from OPEN_FOR_CHANGE_STATES in order since it is simultaneously in LOCKED_STATES which is a contradiction and you can have all kind of junk in cancelled orders, plus 100s of these. Also quote's open_quotes excludes cancelled state as well.
  customer.quotes.open_quotes.each(&:refresh_tax_rate)
end

#uploadUpload

Returns:

See Also:



34
# File 'app/models/tax_exemption.rb', line 34

belongs_to :upload, optional: true