Class: ReceiptDetail

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable, Models::TaxjarSubmittable
Defined in:
app/models/receipt_detail.rb

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 =

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 codes.

WRITE_OFF_CODE_DETAILS.keys.sort
CATEGORIES =

Categories.

["Invoice", "Credit Memo", "GL Account"].freeze

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::TaxjarSubmittable

#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

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 Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#amountObject (readonly)



99
# File 'app/models/receipt_detail.rb', line 99

validates :amount, :gl_date, :category, presence: true

#categoryObject (readonly)



99
# File 'app/models/receipt_detail.rb', line 99

validates :amount, :gl_date, :category, presence: true

#credit_memo_idObject (readonly)



105
# File 'app/models/receipt_detail.rb', line 105

validates :credit_memo_id, presence: { if: proc { |rd| rd.category == "Credit Memo" } }

#discountObject (readonly)



104
# File 'app/models/receipt_detail.rb', line 104

validates :invoice_id, :write_off, :discount, presence: { if: proc { |rd| rd.category == "Invoice" } }

#gl_dateObject (readonly)



99
# File 'app/models/receipt_detail.rb', line 99

validates :amount, :gl_date, :category, presence: true

#invoice_idObject (readonly)



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_recordObject

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_idObject (readonly)



110
# File 'app/models/receipt_detail.rb', line 110

validates :ledger_company_account_id, presence: { if: proc { |rd| rd.category == "GL Account" } }

#receipt_idObject (readonly)



100
# File 'app/models/receipt_detail.rb', line 100

validates :receipt_id, presence: { if: proc { |rd| rd.receipt.nil? } }

#tax_document_referenceObject (readonly)



111
# File 'app/models/receipt_detail.rb', line 111

validates :tax_document_reference, presence: { if: proc { |rd| rd.category == "GL Account" and rd. }, message: '(CM or INV) linked to this line must be specified if posting to tax GL account' }

#write_offObject (readonly)



104
# File 'app/models/receipt_detail.rb', line 104

validates :invoice_id, :write_off, :discount, presence: { if: proc { |rd| rd.category == "Invoice" } }

#write_off_codeObject (readonly)



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_accountsActiveRecord::Relation<ReceiptDetail>

A relation of ReceiptDetails that are gl accounts. Active Record Scope

Returns:

See Also:



130
# File 'app/models/receipt_detail.rb', line 130

scope :gl_accounts, -> { where(category: "GL Account") }

.invoicesActiveRecord::Relation<ReceiptDetail>

A relation of ReceiptDetails that are invoices. Active Record Scope

Returns:

See Also:



129
# File 'app/models/receipt_detail.rb', line 129

scope :invoices, -> { where(category: "Invoice") }

.non_voidedActiveRecord::Relation<ReceiptDetail>

A relation of ReceiptDetails that are non voided. Active Record Scope

Returns:

See Also:



128
# File 'app/models/receipt_detail.rb', line 128

scope :non_voided, -> { where("state != 'voided'") }

Instance Method Details

#applied_amountBigDecimal

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.

Returns:

  • (BigDecimal)


158
159
160
# File 'app/models/receipt_detail.rb', line 158

def applied_amount
  amount + discount + write_off
end

#business_unitBusinessUnit



97
# File 'app/models/receipt_detail.rb', line 97

belongs_to :business_unit, optional: true

#company_account_refObject



193
194
195
# File 'app/models/receipt_detail.rb', line 193

def 
  &.identifier
end

#credit_memoCreditMemo

Returns:

See Also:



94
# File 'app/models/receipt_detail.rb', line 94

belongs_to :credit_memo, inverse_of: :receipt_details, optional: true

#credit_memo_refObject



166
167
168
# File 'app/models/receipt_detail.rb', line 166

def credit_memo_ref
  credit_memo&.reference_number
end

#customer_nameObject



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.

Parameters:

  • with_order (Boolean) (defaults to: false)

Returns:

  • (String, nil)


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

#invoiceInvoice

Returns:

See Also:



92
# File 'app/models/receipt_detail.rb', line 92

belongs_to :invoice, inverse_of: :receipt_details, optional: true

#invoice_balanceObject



184
185
186
187
# File 'app/models/receipt_detail.rb', line 184

def invoice_balance
  resource = invoice || credit_memo
  resource&.balance
end

#invoice_refObject



162
163
164
# File 'app/models/receipt_detail.rb', line 162

def invoice_ref
  invoice&.reference_number
end

#invoice_totalObject



179
180
181
182
# File 'app/models/receipt_detail.rb', line 179

def invoice_total
  resource = invoice || credit_memo
  resource&.total
end

#ledger_company_accountLedgerCompanyAccount



95
# File 'app/models/receipt_detail.rb', line 95

belongs_to :ledger_company_account, optional: true

#ledger_detail_projectLedgerDetailProject



96
# File 'app/models/receipt_detail.rb', line 96

belongs_to :ledger_detail_project, optional: true

#order_refObject



170
171
172
# File 'app/models/receipt_detail.rb', line 170

def order_ref
  invoice.nil? ? nil : invoice.order.try(:reference_number)
end

#pos_amountObject



189
190
191
# File 'app/models/receipt_detail.rb', line 189

def pos_amount
  amount.abs
end

#project_refObject



197
198
199
# File 'app/models/receipt_detail.rb', line 197

def project_ref
  ledger_detail_project&.project_number
end

#receiptReceipt

Returns:

See Also:



93
# File 'app/models/receipt_detail.rb', line 93

belongs_to :receipt, inverse_of: :receipt_details, optional: true

#shipping_addressAddress?

Ship-to address used for sales-tax sourcing. Falls back through
Invoice, CreditMemo, then the linked tax document.

Returns:



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_documentInvoice, ...

Resolve the Invoice or CreditMemo pointed at by
tax_document_reference for GL-Account category lines that need
TaxJar attribution.

Returns:



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