Class: VoucherItem

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

Overview

== Schema Information

Table name: voucher_items
Database name: primary

id :integer not null, primary key
due_date :date
gross_amount :decimal(8, 2)
jde_line :integer
remark :string(255)
state :string(255)
tax_amount :decimal(8, 2)
tax_only :boolean
tax_rate_percentage :decimal(6, 4)
tax_type :string(255)
taxable_amount :decimal(8, 2)
created_at :datetime
updated_at :datetime
business_unit_id :integer
creator_id :integer
gl_offset_account_id :integer
payee_id :integer
tax_rate_id :integer
updater_id :integer
voucher_id :integer

Indexes

index_voucher_items_on_payee_id (payee_id)
voucher_id_state (voucher_id,state)

Constant Summary collapse

VOUCHER_TAX_TYPES =

Recognised voucher tax types.

['S', 'U', 'V', 'N/A'].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

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 Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#due_dateObject (readonly)



47
# File 'app/models/voucher_item.rb', line 47

validates :gross_amount, :due_date, :tax_type, presence: true

#gross_amountObject (readonly)



47
# File 'app/models/voucher_item.rb', line 47

validates :gross_amount, :due_date, :tax_type, presence: true

#remarkObject (readonly)



55
# File 'app/models/voucher_item.rb', line 55

validates :remark, length: { maximum: 255 }

#tax_amountObject (readonly)



49
# File 'app/models/voucher_item.rb', line 49

validates :taxable_amount, :tax_amount, :tax_rate_percentage, presence: { unless: :tax_not_applicable }

#tax_rate_idObject (readonly)



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

validates :tax_rate_id, presence: { if: :requires_tax_rate }

#tax_rate_percentageObject (readonly)



49
# File 'app/models/voucher_item.rb', line 49

validates :taxable_amount, :tax_amount, :tax_rate_percentage, presence: { unless: :tax_not_applicable }

#tax_typeObject (readonly)



47
# File 'app/models/voucher_item.rb', line 47

validates :gross_amount, :due_date, :tax_type, presence: true

#taxable_amountObject (readonly)



49
# File 'app/models/voucher_item.rb', line 49

validates :taxable_amount, :tax_amount, :tax_rate_percentage, presence: { unless: :tax_not_applicable }

Class Method Details

.approvedActiveRecord::Relation<VoucherItem>

A relation of VoucherItems that are approved. Active Record Scope

Returns:

See Also:



64
# File 'app/models/voucher_item.rb', line 64

scope :approved, -> { where(state: 'approved') }

.available_to_applyActiveRecord::Relation<VoucherItem>

A relation of VoucherItems that are available to apply. Active Record Scope

Returns:

See Also:



66
# File 'app/models/voucher_item.rb', line 66

scope :available_to_apply, -> { joins(:voucher).where(voucher: { state: 'unpaid' }).where(state: %w[approved partially_paid]) }

.for_company_idActiveRecord::Relation<VoucherItem>

A relation of VoucherItems that are for company id. Active Record Scope

Returns:

See Also:



65
# File 'app/models/voucher_item.rb', line 65

scope :for_company_id, ->(company_id) { joins(:voucher).where(vouchers: { company_id: }) }

Instance Method Details

#amount_paid_in_full?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'app/models/voucher_item.rb', line 131

def amount_paid_in_full?
  outgoing_payment_items.applied.sum(:amount) == gross_amount
end

#amount_partially_paid?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'app/models/voucher_item.rb', line 135

def amount_partially_paid?
  outgoing_payment_items.applied.sum(:amount) != 0
end

#available_statusesArray<String>

State values an AP user is allowed to pick from in the UI. Once
payments are applied, paid/partially_paid become legal options too.

Returns:

  • (Array<String>)


165
166
167
168
169
170
171
# File 'app/models/voucher_item.rb', line 165

def available_statuses
  if status_locked?
    %w[approved on_hold partially_paid fully_paid]
  else
    %w[approved on_hold]
  end
end

#balanceBigDecimal

Outstanding amount still owed to the vendor on this line.

Returns:

  • (BigDecimal)

    gross amount minus sum of applied payments



146
147
148
# File 'app/models/voucher_item.rb', line 146

def balance
  gross_amount - outgoing_payment_items.applied.sum(:amount)
end

#business_unitBusinessUnit



43
# File 'app/models/voucher_item.rb', line 43

belongs_to :business_unit, optional: true

#gl_offset_accountLedgerCompanyAccount



41
# File 'app/models/voucher_item.rb', line 41

belongs_to :gl_offset_account, class_name: 'LedgerCompanyAccount', optional: true

#gl_offset_account_refString?

Returns human-readable identifier of the GL offset
account (gl_offset_account.identifier), used in CSV/import flows.

Returns:

  • (String, nil)

    human-readable identifier of the GL offset
    account (gl_offset_account.identifier), used in CSV/import flows.



175
176
177
# File 'app/models/voucher_item.rb', line 175

def 
  &.identifier
end

#gl_offset_account_ref=(identifier) ⇒ Object



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

def (identifier)
  self. = LedgerCompanyAccount.get_by_identifier(identifier)
end

#no_payment_made?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'app/models/voucher_item.rb', line 139

def no_payment_made?
  outgoing_payment_items.applied.sum(:amount) == 0
end

#outgoing_payment_itemsActiveRecord::Relation<OutgoingPaymentItem>

Returns:

See Also:



45
# File 'app/models/voucher_item.rb', line 45

has_many :outgoing_payment_items

#payeeParty

Returns:

See Also:



42
# File 'app/models/voucher_item.rb', line 42

belongs_to :payee, class_name: 'Party', optional: true

#payee_nameString?

Display name of the line-level payee (an alternate Party that
supersedes the voucher-level supplier when the supplier delegates
payment, e.g. installer commissions).

Returns:

  • (String, nil)


122
123
124
# File 'app/models/voucher_item.rb', line 122

def payee_name
  payee.try(:full_name)
end

#payee_typeString?

Returns Polymorphic class name of the payee Party.

Returns:

  • (String, nil)

    Polymorphic class name of the payee Party



127
128
129
# File 'app/models/voucher_item.rb', line 127

def payee_type
  payee.try(:class).try(:to_s)
end

#spiff_enrollmentSpiffEnrollment?

SPIFF enrollment inherited from the Order that produced the voucher.
AP uses this to attribute commission/incentive payouts back to the
originating sale.

Returns:



111
112
113
114
115
# File 'app/models/voucher_item.rb', line 111

def spiff_enrollment
  return if voucher.order.blank?

  voucher.order.spiff_enrollment
end

#status_locked?Boolean

Returns true once any payment has been applied; AP locks
editable status transitions in that case.

Returns:

  • (Boolean)

    true once any payment has been applied; AP locks
    editable status transitions in that case.



157
158
159
# File 'app/models/voucher_item.rb', line 157

def status_locked?
  partially_paid? or fully_paid?
end

#tax_rateTaxRate

Returns:

See Also:



40
# File 'app/models/voucher_item.rb', line 40

belongs_to :tax_rate, optional: true

#tax_rate_nameString?

Returns short tax-rate label (for AP grids).

Returns:

  • (String, nil)

    short tax-rate label (for AP grids)



151
152
153
# File 'app/models/voucher_item.rb', line 151

def tax_rate_name
  tax_rate.try(:short_name)
end

#voucherVoucher

Returns:

See Also:



39
# File 'app/models/voucher_item.rb', line 39

belongs_to :voucher, inverse_of: :voucher_items, optional: true