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

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

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#due_dateObject (readonly)



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

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

#gross_amountObject (readonly)



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

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

#remarkObject (readonly)



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

validates :remark, length: { maximum: 255 }

#tax_amountObject (readonly)



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

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

#tax_rate_idObject (readonly)



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

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

#tax_rate_percentageObject (readonly)



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

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

#tax_typeObject (readonly)



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

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

#taxable_amountObject (readonly)



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

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:



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

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:



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

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:



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

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

Instance Method Details

#amount_paid_in_full?Boolean

Returns:

  • (Boolean)


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

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

#amount_partially_paid?Boolean

Returns:

  • (Boolean)


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

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>)


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

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



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

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

#business_unitBusinessUnit



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

belongs_to :business_unit, optional: true

#gl_offset_accountLedgerCompanyAccount



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

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.



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

def 
  &.identifier
end

#gl_offset_account_ref=(identifier) ⇒ Object



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

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

#no_payment_made?Boolean

Returns:

  • (Boolean)


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

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

#outgoing_payment_itemsActiveRecord::Relation<OutgoingPaymentItem>

Returns:

See Also:



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

has_many :outgoing_payment_items

#payeeParty

Returns:

See Also:



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

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)


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

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



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

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:



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

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.



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

def status_locked?
  partially_paid? or fully_paid?
end

#tax_rateTaxRate

Returns:

See Also:



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

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)



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

def tax_rate_name
  tax_rate.try(:short_name)
end

#voucherVoucher

Returns:

See Also:



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

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