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 =
{
  "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"}
}
WRITE_OFF_CODES =
WRITE_OFF_CODE_DETAILS.keys.sort
CATEGORIES =
["Invoice", "Credit Memo", "GL Account"]

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

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 Models::EventPublishable

#publish_event

Instance Attribute Details

#company_account_refObject

Returns the value of attribute company_account_ref.



143
144
145
# File 'app/models/receipt_detail.rb', line 143

def 
  @company_account_ref
end

#credit_memo_refObject

Returns the value of attribute credit_memo_ref.



143
144
145
# File 'app/models/receipt_detail.rb', line 143

def credit_memo_ref
  @credit_memo_ref
end

#customer_nameObject

Returns the value of attribute customer_name.



143
144
145
# File 'app/models/receipt_detail.rb', line 143

def customer_name
  @customer_name
end

#invoice_balanceObject

Returns the value of attribute invoice_balance.



143
144
145
# File 'app/models/receipt_detail.rb', line 143

def invoice_balance
  @invoice_balance
end

#invoice_refObject

Returns the value of attribute invoice_ref.



143
144
145
# File 'app/models/receipt_detail.rb', line 143

def invoice_ref
  @invoice_ref
end

#invoice_totalObject

Returns the value of attribute invoice_total.



143
144
145
# File 'app/models/receipt_detail.rb', line 143

def invoice_total
  @invoice_total
end

#is_a_new_recordObject

Returns the value of attribute is_a_new_record.



143
144
145
# File 'app/models/receipt_detail.rb', line 143

def is_a_new_record
  @is_a_new_record
end

#order_refObject

Returns the value of attribute order_ref.



143
144
145
# File 'app/models/receipt_detail.rb', line 143

def order_ref
  @order_ref
end

#pos_amountObject

Returns the value of attribute pos_amount.



143
144
145
# File 'app/models/receipt_detail.rb', line 143

def pos_amount
  @pos_amount
end

#project_refObject

Returns the value of attribute project_ref.



143
144
145
# File 'app/models/receipt_detail.rb', line 143

def project_ref
  @project_ref
end

Class Method Details

.gl_accountsActiveRecord::Relation<ReceiptDetail>

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

Returns:

See Also:



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

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

.invoicesActiveRecord::Relation<ReceiptDetail>

A relation of ReceiptDetails that are invoices. Active Record Scope

Returns:

See Also:



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

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

.non_voidedActiveRecord::Relation<ReceiptDetail>

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

Returns:

See Also:



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

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

Instance Method Details

#applied_amountObject



145
146
147
# File 'app/models/receipt_detail.rb', line 145

def applied_amount
  self.amount + self.discount + self.write_off
end

#business_unitBusinessUnit



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

belongs_to :business_unit, optional: true

#credit_memoCreditMemo

Returns:

See Also:



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

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

#description(with_order = false) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
# File 'app/models/receipt_detail.rb', line 188

def description(with_order = false)
  case category
  when "Invoice"
    desc = invoice.reference_number
    desc += " (#{invoice.order.reference_number})" if with_order and invoice.order.present?
    desc
  when "Credit Memo"
    credit_memo.reference_number
  when "GL Account"
    nil
  end
end

#invoiceInvoice

Returns:

See Also:



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

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

#ledger_company_accountLedgerCompanyAccount



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

belongs_to :ledger_company_account, optional: true

#ledger_detail_projectLedgerDetailProject



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

belongs_to :ledger_detail_project, optional: true

#receiptReceipt

Returns:

See Also:



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

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

#shipping_addressObject



206
207
208
209
210
211
212
213
214
215
216
# File 'app/models/receipt_detail.rb', line 206

def shipping_address
  if invoice
    invoice.shipping_address
  elsif credit_memo
    credit_memo.shipping_address
  elsif tax_document
    tax_document.shipping_address
  else
    nil
  end
end

#tax_documentObject



201
202
203
204
# File 'app/models/receipt_detail.rb', line 201

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