Refunds of paid invoices: refund receipt with optional restocking fee

Completed work that's billed and paid in full can be refunded from its
invoice (Refund on the invoice, or Paid invoice on the customer's Refund
page). It makes a QuickBooks Refund Receipt: the invoice lines refunded
(all or part, by quantity or amount) with their products/services and
tax codes, the invoice discount's share, and the restocking fee as a
negative line when it's ticked: the fee rate of the parts refunded, so
labor-only work has none. Paid out of the refund check or card account,
with the check number.

Each refund records the lines it refunded, so a line can't be refunded
for more than it was billed across refunds. Optionally notes the refund
on the invoice's work orders and emails the customer. The refund page,
PDF and email show the lines, tax and fee. Migration 056 adds the
columns to qbo_refunds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-18 13:23:10 -05:00
1 parent 56859d1794
commit c851929e02
21 files changed
+1380 -22

No files matched your search

+52 -3
View File
@@ -8,10 +8,16 @@
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# A customer refund (SOP-OPS-014), as it was made: the restocking fee, the refund, and each step's outcome
# A customer refund (SOP-OPS-014), as it was made: the restocking fee, the refund, and each step's outcome.
# Two kinds: credit, a canceled order's deposit (RefundWriter), and invoice, completed work on a paid invoice
# (InvoiceRefundWriter).
class QboRefund < ApplicationRecord
METHODS = %w[check card].freeze
STATUSES = %w[done incomplete].freeze
KINDS = %w[credit invoice].freeze
# A refunded invoice line, as recorded: its key on the invoice (InvoiceRefundWriter.line_key)
RefundedLine = Struct.new(:key, :description, :quantity, :amount, :parts, keyword_init: true)
belongs_to :customer
belongs_to :user, optional: true
@@ -19,8 +25,15 @@ class QboRefund < ApplicationRecord
validates :customer_id, presence: true
validates :method, inclusion: { in: METHODS }
validates :status, inclusion: { in: STATUSES }
validates :kind, inclusion: { in: KINDS }
scope :recent, -> { order(created_at: :desc, id: :desc) }
# The invoice's refunds that made a refund receipt in QuickBooks
scope :of_invoice, ->(id) { where(kind: 'invoice', invoice_id: id).where.not(purchase_id: [nil, '']) }
def invoice?
kind == 'invoice'
end
def done?
status == 'done'
@@ -44,11 +57,47 @@ class QboRefund < ApplicationRecord
self[:canceled_issue_ids] = Array(ids).map(&:to_i).uniq.to_json
end
# The QuickBooks screen for the refund check or expense
def noted_issue_ids
Array(JSON.parse(self[:noted_issue_ids].presence || '[]')).map(&:to_i)
rescue JSON::ParserError
[]
end
def noted_issue_ids=(ids)
self[:noted_issue_ids] = Array(ids).map(&:to_i).uniq.to_json
end
def refunded_lines
Array(JSON.parse(self[:refunded_lines].presence || '[]')).map do |line|
RefundedLine.new(key: line['key'].to_s, description: line['description'].to_s, quantity: line['quantity'],
amount: BigDecimal(line['amount'].to_s.presence || '0'), parts: line['parts'] == true)
end
rescue JSON::ParserError
[]
end
def refunded_lines=(lines)
self[:refunded_lines] = Array(lines).map do |line|
{ key: line.key.to_s, description: line.description.to_s, quantity: line.quantity&.to_s,
amount: line.amount.to_d.to_s('F'), parts: line.parts ? true : false }
end.to_json
end
# What the order or invoice is called: "E1068", "Invoice #1234", or nil
def document_label
if invoice? then I18n.t(:label_refund_invoice_number, number: invoice_number)
elsif estimate_number.present? then "E#{estimate_number}"
end
end
# The QuickBooks screen for the refund receipt, check or expense
def purchase_url
return nil if purchase_id.blank?
page = check? ? 'check' : 'expense'
page = if invoice? then 'refundreceipt'
elsif check? then 'check'
else 'expense'
end
"https://#{RedmineQbo.sandbox_mode? ? 'sandbox' : 'app'}.qbo.intuit.com/app/#{page}?txnId=#{purchase_id}"
end
end