Class: ReceiptDetail
Overview
== Schema Information
Table name: receipt_details
Database name: primary
id :integer not null, primary key
amount :decimal(8, 2)
category :string(255)
discount :decimal(8, 2) default(0.0)
discount_code :string(255)
gl_date :date
remark :string
state :string(255)
tax_document_reference :string
taxjar_state :string
voided :boolean
write_off :decimal(8, 2) default(0.0)
write_off_code :string(255)
created_at :datetime
updated_at :datetime
business_unit_id :integer
creator_id :integer
credit_memo_id :integer
deleter_id :integer
invoice_id :integer
ledger_company_account_id :integer
ledger_detail_project_id :integer
receipt_id :integer
updater_id :integer
Indexes
by_iid_rid (invoice_id,receipt_id)
index_receipt_details_on_credit_memo_id (credit_memo_id)
receipt_id_category (receipt_id,category)
taxjar_state_created_at (taxjar_state,created_at)
Constant Summary
collapse
- WRITE_OFF_CODE_DETAILS =
{
"Bad Debt" => { account: BAD_DEBT_ACCOUNT, business_unit: "accounting" },
"Rebate" => { account: REBATES_ACCOUNT, business_unit: "sales" },
"Minor Write Off" => { account: MINOR_WRITE_OFF_ACCOUNT, business_unit: "accounting" }
}.freeze
- WRITE_OFF_CODES =
WRITE_OFF_CODE_DETAILS.keys.sort
- CATEGORIES =
["Invoice", "Credit Memo", "GL Account"].freeze
Models::Auditable::ALWAYS_IGNORED
Constants included
from Schedulable
Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary collapse
#creator, #updater
Class Method Summary
collapse
Instance Method Summary
collapse
#customer_sync_instance, #delete_from_taxjar, #evaluate_taxjar_submission, #record_already_exists_on_taxjar?, #resubmit_to_taxjar, #should_be_submitted_to_taxjar?, #should_sync_customer_with_taxjar?, #submit_to_taxjar, #sync_customer_with_taxjar, #taxjar_customer_id, #taxjar_submission_instance
#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
config
#after_commit
#publish_event
Instance Attribute Details
#amount ⇒ Object
99
|
# File 'app/models/receipt_detail.rb', line 99
validates :amount, :gl_date, :category, presence: true
|
#category ⇒ Object
99
|
# File 'app/models/receipt_detail.rb', line 99
validates :amount, :gl_date, :category, presence: true
|
#credit_memo_id ⇒ Object
105
|
# File 'app/models/receipt_detail.rb', line 105
validates :credit_memo_id, presence: { if: proc { |rd| rd.category == "Credit Memo" } }
|
#discount ⇒ Object
104
|
# File 'app/models/receipt_detail.rb', line 104
validates :invoice_id, :write_off, :discount, presence: { if: proc { |rd| rd.category == "Invoice" } }
|
#gl_date ⇒ Object
99
|
# File 'app/models/receipt_detail.rb', line 99
validates :amount, :gl_date, :category, presence: true
|
#invoice_id ⇒ Object
104
|
# File 'app/models/receipt_detail.rb', line 104
validates :invoice_id, :write_off, :discount, presence: { if: proc { |rd| rd.category == "Invoice" } }
|
#is_a_new_record ⇒ Object
Returns the value of attribute is_a_new_record.
151
152
153
|
# File 'app/models/receipt_detail.rb', line 151
def is_a_new_record
@is_a_new_record
end
|
#ledger_company_account_id ⇒ Object
110
|
# File 'app/models/receipt_detail.rb', line 110
validates :ledger_company_account_id, presence: { if: proc { |rd| rd.category == "GL Account" } }
|
#receipt_id ⇒ Object
100
|
# File 'app/models/receipt_detail.rb', line 100
validates :receipt_id, presence: { if: proc { |rd| rd.receipt.nil? } }
|
#tax_document_reference ⇒ Object
111
|
# File 'app/models/receipt_detail.rb', line 111
validates :tax_document_reference, presence: { if: proc { |rd| rd.category == "GL Account" and rd.should_be_submitted_to_taxjar? }, message: '(CM or INV) linked to this line must be specified if posting to tax GL account' }
|
#write_off ⇒ Object
104
|
# File 'app/models/receipt_detail.rb', line 104
validates :invoice_id, :write_off, :discount, presence: { if: proc { |rd| rd.category == "Invoice" } }
|
#write_off_code ⇒ Object
107
|
# File 'app/models/receipt_detail.rb', line 107
validates :write_off_code, presence: { if: proc { |rd| rd.category == "Invoice" and rd.write_off != 0 } }
|
Class Method Details
.gl_accounts ⇒ ActiveRecord::Relation<ReceiptDetail>
A relation of ReceiptDetails that are gl accounts. Active Record Scope
130
|
# File 'app/models/receipt_detail.rb', line 130
scope :gl_accounts, -> { where(category: "GL Account") }
|
.invoices ⇒ ActiveRecord::Relation<ReceiptDetail>
A relation of ReceiptDetails that are invoices. Active Record Scope
129
|
# File 'app/models/receipt_detail.rb', line 129
scope :invoices, -> { where(category: "Invoice") }
|
.non_voided ⇒ ActiveRecord::Relation<ReceiptDetail>
A relation of ReceiptDetails that are non voided. Active Record Scope
128
|
# File 'app/models/receipt_detail.rb', line 128
scope :non_voided, -> { where("state != 'voided'") }
|
Instance Method Details
#applied_amount ⇒ BigDecimal
Total cash equivalent applied to the Invoice: amount actually
collected, plus any early-payment discount granted, plus any write-off
taken. Sums to the invoice balance reduction.
158
159
160
|
# File 'app/models/receipt_detail.rb', line 158
def applied_amount
amount + discount + write_off
end
|
97
|
# File 'app/models/receipt_detail.rb', line 97
belongs_to :business_unit, optional: true
|
#company_account_ref ⇒ Object
193
194
195
|
# File 'app/models/receipt_detail.rb', line 193
def company_account_ref
ledger_company_account&.identifier
end
|
94
|
# File 'app/models/receipt_detail.rb', line 94
belongs_to :credit_memo, inverse_of: :receipt_details, optional: true
|
#credit_memo_ref ⇒ Object
166
167
168
|
# File 'app/models/receipt_detail.rb', line 166
def credit_memo_ref
credit_memo&.reference_number
end
|
#customer_name ⇒ Object
174
175
176
177
|
# File 'app/models/receipt_detail.rb', line 174
def customer_name
resource = invoice || credit_memo
resource&.customer&.full_name
end
|
#description(with_order = false) ⇒ String?
Human label for this line: invoice or credit-memo reference, with the
originating Order reference appended in parentheses when requested.
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'app/models/receipt_detail.rb', line 206
def description(with_order = false)
case category
when "Invoice"
desc = invoice.reference_number
desc += " (#{invoice.order.reference_number})" if with_order && invoice.order.present?
desc
when "Credit Memo"
credit_memo.reference_number
when "GL Account"
nil
end
end
|
92
|
# File 'app/models/receipt_detail.rb', line 92
belongs_to :invoice, inverse_of: :receipt_details, optional: true
|
#invoice_balance ⇒ Object
184
185
186
187
|
# File 'app/models/receipt_detail.rb', line 184
def invoice_balance
resource = invoice || credit_memo
resource&.balance
end
|
#invoice_ref ⇒ Object
162
163
164
|
# File 'app/models/receipt_detail.rb', line 162
def invoice_ref
invoice&.reference_number
end
|
#invoice_total ⇒ Object
179
180
181
182
|
# File 'app/models/receipt_detail.rb', line 179
def invoice_total
resource = invoice || credit_memo
resource&.total
end
|
95
|
# File 'app/models/receipt_detail.rb', line 95
belongs_to :ledger_company_account, optional: true
|
96
|
# File 'app/models/receipt_detail.rb', line 96
belongs_to :ledger_detail_project, optional: true
|
#order_ref ⇒ Object
170
171
172
|
# File 'app/models/receipt_detail.rb', line 170
def order_ref
invoice.nil? ? nil : invoice.order.try(:reference_number)
end
|
#pos_amount ⇒ Object
189
190
191
|
# File 'app/models/receipt_detail.rb', line 189
def pos_amount
amount.abs
end
|
#project_ref ⇒ Object
197
198
199
|
# File 'app/models/receipt_detail.rb', line 197
def project_ref
ledger_detail_project&.project_number
end
|
93
|
# File 'app/models/receipt_detail.rb', line 93
belongs_to :receipt, inverse_of: :receipt_details, optional: true
|
#shipping_address ⇒ Address?
Ship-to address used for sales-tax sourcing. Falls back through
Invoice, CreditMemo, then the linked tax document.
234
235
236
237
238
239
240
241
242
|
# File 'app/models/receipt_detail.rb', line 234
def shipping_address
if invoice
invoice.shipping_address
elsif credit_memo
credit_memo.shipping_address
elsif tax_document
tax_document.shipping_address
end
end
|
#tax_document ⇒ Invoice, ...
Resolve the Invoice or CreditMemo pointed at by
tax_document_reference for GL-Account category lines that need
TaxJar attribution.
224
225
226
227
228
|
# File 'app/models/receipt_detail.rb', line 224
def tax_document
return nil if tax_document_reference.nil?
Invoice.find_by(reference_number: tax_document_reference) || CreditMemo.find_by(reference_number: tax_document_reference)
end
|