Class: TaxExemption
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
Models::Auditable::ALWAYS_IGNORED
Instance Attribute Summary collapse
#creator, #updater
Class Method Summary
collapse
Instance Method Summary
collapse
#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
#publish_event
Instance Attribute Details
#attachment_file ⇒ Object
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_number ⇒ Object
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_id ⇒ Object
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_date ⇒ Object
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_code ⇒ Object
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_type ⇒ Object
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_date ⇒ Object
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_now ⇒ ActiveRecord::Relation<TaxExemption>
A relation of TaxExemptions that are effective now. Active Record Scope
59
|
# File 'app/models/tax_exemption.rb', line 59
scope :effective_now, -> { effective_on }
|
.effective_on ⇒ ActiveRecord::Relation<TaxExemption>
A relation of TaxExemptions that are effective on. Active Record Scope
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
35
|
# File 'app/models/tax_exemption.rb', line 35
belongs_to :customer, optional: true
|
#description ⇒ Object
72
73
74
|
# File 'app/models/tax_exemption.rb', line 72
def description
"#{state_code} (#{certificate_number}), expires #{expiration_date}"
end
|
#has_valid_date_range ⇒ Object
76
77
78
79
80
81
82
|
# File 'app/models/tax_exemption.rb', line 76
def has_valid_date_range
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
68
69
70
|
# File 'app/models/tax_exemption.rb', line 68
def is_valid?
period === Date.current
end
|
#notify_creation ⇒ Object
#notify_emails ⇒ Object
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_expiration ⇒ Object
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
|
#orders ⇒ ActiveRecord::Relation<Order>
38
|
# File 'app/models/tax_exemption.rb', line 38
has_many :orders
|
#period ⇒ Object
64
65
66
|
# File 'app/models/tax_exemption.rb', line 64
def period
valid_from_date..expiration_date
end
|
36
|
# File 'app/models/tax_exemption.rb', line 36
belongs_to :state, foreign_key: :state_code, primary_key: :code, optional: true
|
#tax_exemption_overlap ⇒ Object
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?
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
errors.add(:base, "date overlap with another tax exemption (id: #{te.id}).")
end
|
#to_s ⇒ Object
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_orders ⇒ Object
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) customer.quotes.open_quotes.each(&:refresh_tax_rate)
end
|
34
|
# File 'app/models/tax_exemption.rb', line 34
belongs_to :upload, optional: true
|