Class: OutgoingPaymentItem

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

Overview

== Schema Information

Table name: outgoing_payment_items
Database name: primary

id :integer not null, primary key
amount :decimal(10, 2)
state :string(255)
created_at :datetime
updated_at :datetime
creator_id :integer
credit_memo_id :integer
outgoing_payment_id :integer
receipt_id :integer
updater_id :integer
voucher_item_id :integer

Indexes

credit_memo_id_outgoing_payment_id (credit_memo_id,outgoing_payment_id)
index_outgoing_payment_items_on_outgoing_payment_id (outgoing_payment_id)
index_outgoing_payment_items_on_receipt_id (receipt_id)
index_outgoing_payment_items_on_voucher_item_id (voucher_item_id)
state_credit_memo_id (state,credit_memo_id)
state_receipt_id (state,receipt_id)
state_voucher_item_id (state,voucher_item_id)

Constant Summary

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



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

validates :amount, :presence => true, :numericality => true

Class Method Details

.appliedActiveRecord::Relation<OutgoingPaymentItem>

A relation of OutgoingPaymentItems that are applied. Active Record Scope

Returns:

See Also:



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

scope :applied, -> { where(:state => "applied") }

.not_voidedActiveRecord::Relation<OutgoingPaymentItem>

A relation of OutgoingPaymentItems that are not voided. Active Record Scope

Returns:

See Also:



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

scope :not_voided, -> { where(:state => ["draft", "applied"]) }

Instance Method Details

#credit_memoCreditMemo

Returns:

See Also:



35
# File 'app/models/outgoing_payment_item.rb', line 35

belongs_to :credit_memo, optional: true

#gross_amountBigDecimal?

Pre-discount face value of the source document; the #amount on this
line may be smaller when discounts or partial payments apply.

Returns:

  • (BigDecimal, nil)


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

def gross_amount
  if credit_memo.present?
    credit_memo.total
  elsif voucher_item.present?
    voucher_item.gross_amount
  elsif receipt.present?
    receipt.amount
  end
end

#invoice_dateDate?

Document date of the source resource: vendor invoice date, credit memo
date, or receipt date.

Returns:

  • (Date, nil)


136
137
138
139
140
141
142
143
144
# File 'app/models/outgoing_payment_item.rb', line 136

def invoice_date
  if credit_memo.present?
    credit_memo.document_date
  elsif voucher_item.present?
    voucher_item.voucher.invoice_date
  elsif receipt.present?
    receipt.receipt_date
  end
end

#invoice_number(with_prefix = false) ⇒ String?

Reference number of the source document (voucher invoice, credit memo,
or receipt) this line settles.

Parameters:

  • with_prefix (Boolean) (defaults to: false)

    prepend a one-letter type prefix (DM/INV/R)

Returns:

  • (String, nil)


121
122
123
124
125
126
127
128
129
130
# File 'app/models/outgoing_payment_item.rb', line 121

def invoice_number(with_prefix=false)
  prefix, ref = if credit_memo.present?
    ["DM", credit_memo.reference_number]
  elsif voucher_item.present?
    ["INV", voucher_item.voucher.invoice_number]
  elsif receipt.present?
    ["R", receipt.id]
  end
  with_prefix ? "#{prefix} #{ref}" : ref
end

#outgoing_paymentOutgoingPayment



33
# File 'app/models/outgoing_payment_item.rb', line 33

belongs_to :outgoing_payment, :inverse_of => :outgoing_payment_items, optional: true

#payeeCustomer, ...

Party receiving (or netted against) this line. Vendor for a voucher
payment, customer for a credit memo offset or unapplied receipt.

Returns:



106
107
108
109
110
111
112
113
114
# File 'app/models/outgoing_payment_item.rb', line 106

def payee
  if credit_memo.present?
    credit_memo.billing_customer
  elsif voucher_item.present?
    voucher_item.payee || voucher_item.voucher.supplier
  elsif receipt.present?
    receipt.customer
  end
end

#receiptReceipt

Returns:

See Also:



36
# File 'app/models/outgoing_payment_item.rb', line 36

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

#remarkString?

Free-text remark from the source resource, used on check stubs and AP
reports.

Returns:

  • (String, nil)


150
151
152
153
154
155
156
157
158
# File 'app/models/outgoing_payment_item.rb', line 150

def remark
  if credit_memo.present?
    credit_memo.remark
  elsif voucher_item.present?
    voucher_item.remark
  elsif receipt.present?
    receipt.remark
  end
end

#spiff_enrollmentSpiffEnrollment?

SPIFF enrollment associated with the underlying voucher item, used to
link AP payouts back to commission/incentive programs.

Returns:



203
204
205
206
207
208
209
# File 'app/models/outgoing_payment_item.rb', line 203

def spiff_enrollment
  if voucher_item.present?
    voucher_item.spiff_enrollment
  else
    nil
  end
end

#update_resource_stateBoolean?

Cascade the applied state to the linked accounts-payable resource.
Marks the VoucherItem paid, offsets a CreditMemo (and resyncs its
parent RMA), or applies the Receipt when unapplied funds are being
used to pay a voucher.

Returns:

  • (Boolean, nil)

    true when a downstream resource was updated



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/outgoing_payment_item.rb', line 70

def update_resource_state
  if voucher_item.present?
    voucher_item.paid
    return true
  elsif credit_memo.present?
    credit_memo.offset!
    credit_memo.rma.sync_state if credit_memo.rma.present?
    return true
  elsif receipt.present?
    receipt.apply
  else
    return false
  end
end

#void_resourceObject

Reverse #update_resource_state when this line is voided so the
voucher/credit memo/receipt returns to its open balance.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/outgoing_payment_item.rb', line 87

def void_resource
  if voucher_item.present?
    voucher_item.state_event = "unpaid"
    voucher_item.save!
  elsif credit_memo.present?
    credit_memo.state_event = "unoffset"
    credit_memo.save!
  elsif receipt.present?
    receipt.state_event = "unapply"
    receipt.save!
  else
    return false
  end
end

#voucher_itemVoucherItem



34
# File 'app/models/outgoing_payment_item.rb', line 34

belongs_to :voucher_item, optional: true

#voucher_numberString?

Reference number of the parent Voucher (when this line settles a
voucher item).

Returns:

  • (String, nil)


178
179
180
181
182
183
184
# File 'app/models/outgoing_payment_item.rb', line 178

def voucher_number
  if voucher_item.present?
    voucher_item.voucher.reference_number
  else
    nil
  end
end

#voucher_uploadUpload?

First attached upload (scanned vendor invoice etc.) on the parent
Voucher; returns nil for credit-memo or receipt-backed lines or
when the voucher has no uploads.

Returns:



191
192
193
194
195
196
197
# File 'app/models/outgoing_payment_item.rb', line 191

def voucher_upload
  if voucher_item.present? and voucher_item.voucher.uploads.any?
    voucher_item.voucher.uploads.first
  else
    nil
  end
end