mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-18 02:20:37 -05:00
Add discounts, unlocking paid invoices and editing payments
- Estimates and invoices take a discount, as a percent of the subtotal or an amount, saved as QuickBooks' discount line and taken off before sales tax. Duplicated estimates and invoices made from an estimate keep it. The box is hidden when discounts are off in QuickBooks. - A paid invoice can be unlocked for editing after a confirmation. The payments stay linked, and the total can't go below what's been paid. Unlocking needs the new "Edit partially paid invoices" or "Edit paid invoices" permission, checked again when saving. - Saved payments can be edited: date, method, reference, memo, amount and the invoices they pay. Each invoice keeps its amount unless the amount or the invoices change. Payments that also apply credit memos stay read-only. - Locked estimates and invoices are readable again (disabled boxes had faint or invisible text, especially in Safari). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
89f4797bbe
commit
1c71e3a4c9
20 files changed
+482
-74
No files matched your search
@@ -12,8 +12,13 @@ module RedmineQboBilling
|
||||
# Receives customer payments in QuickBooks: payment type and amount, applied to the customer's open
|
||||
# invoices oldest first (the way QuickBooks applies them on its own screen), or only to the invoices ticked
|
||||
# on the page, or saved unapplied as a deposit. Whatever doesn't fit stays unapplied as a credit.
|
||||
#
|
||||
# A saved payment can be edited the same way (date, method, reference, memo, amount and the invoices it
|
||||
# pays); its customer stays. Left as it was, it keeps the amounts it already applied to each invoice.
|
||||
module PaymentWriter
|
||||
Error = EstimateWriter::Error
|
||||
Conflict = EstimateWriter::Conflict
|
||||
INVOICE = 'Invoice'.freeze
|
||||
|
||||
METHODS_CACHE_KEY = 'redmine_qbo_billing:payment_methods'
|
||||
|
||||
@@ -39,17 +44,21 @@ module RedmineQboBilling
|
||||
[]
|
||||
end
|
||||
|
||||
# The customer's invoices with a balance, oldest first (by date, then number)
|
||||
def open_invoices(customer)
|
||||
# The customer's invoices with a balance, oldest first (by date, then number). For a payment being edited
|
||||
# (payment:, the QuickBooks payment), what it paid on each invoice counts as still open, so the invoices it
|
||||
# pays are listed even when it paid them off.
|
||||
def open_invoices(customer, payment: nil)
|
||||
invoices = with_service('Invoice') do |service|
|
||||
service.query("SELECT * FROM Invoice WHERE CustomerRef = '#{customer.id.to_i}' ORDERBY TxnDate", per_page: 1000).entries
|
||||
end
|
||||
paid_here = payment ? applied_amounts(payment) : {}
|
||||
|
||||
Array(invoices).select { |i| BigDecimal(i.balance.to_s.presence || '0') > 0 }
|
||||
.sort_by { |i| [i.txn_date || Date.new(1900), i.doc_number.to_s.rjust(12, '0')] }
|
||||
.map do |i|
|
||||
Array(invoices).map { |i| [i, BigDecimal(i.balance.to_s.presence || '0') + paid_here.fetch(i.id.to_s, 0)] }
|
||||
.select { |_, balance| balance > 0 }
|
||||
.sort_by { |i, _| [i.txn_date || Date.new(1900), i.doc_number.to_s.rjust(12, '0')] }
|
||||
.map do |i, balance|
|
||||
{ id: i.id.to_s, doc_number: i.doc_number.to_s, date: i.txn_date.to_s, due_date: i.due_date.to_s,
|
||||
total: i.total.to_s, balance: BigDecimal(i.balance.to_s).to_s('F') }
|
||||
total: i.total.to_s, balance: balance.to_s('F') }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -67,45 +76,132 @@ module RedmineQboBilling
|
||||
[applied, left]
|
||||
end
|
||||
|
||||
# Creates the payment in QuickBooks and returns it. Open invoices are read again here, so the payment
|
||||
# goes on what's open right now.
|
||||
# Creates the payment in QuickBooks (or updates the one being edited) and returns it. Open invoices are
|
||||
# read again here, so the payment goes on what's open right now.
|
||||
def save(form)
|
||||
return update(form) if form.persisted?
|
||||
|
||||
customer = Customer.find_by(id: form.customer_id)
|
||||
raise Error, I18n.t(:error_estimate_needs_customer) unless customer
|
||||
|
||||
amount = form.amount_value.round(2)
|
||||
applied =
|
||||
if form.deposit?
|
||||
[]
|
||||
else
|
||||
invoices = open_invoices(customer)
|
||||
invoices = invoices.select { |i| form.invoice_ids.include?(i[:id]) } if form.invoice_ids.any?
|
||||
allocate(amount, invoices).first
|
||||
end
|
||||
applied = allocation(form, customer, amount)
|
||||
|
||||
payment = Quickbooks::Model::Payment.new
|
||||
payment.customer_id = customer.id
|
||||
payment.txn_date = form.txn_date_value
|
||||
payment.payment_method_ref = Quickbooks::Model::BaseReference.new(form.payment_method_id)
|
||||
payment.payment_ref_number = form.reference if form.reference.present?
|
||||
payment.private_note = form.memo if form.memo.present?
|
||||
payment.total = amount
|
||||
payment.line_items = applied.map do |invoice, take|
|
||||
line = Quickbooks::Model::Line.new
|
||||
line.amount = take
|
||||
line.invoice_id = invoice[:id]
|
||||
line
|
||||
end
|
||||
fill(payment, form, amount, applied)
|
||||
payment.payment_ref_number = nil if form.reference.blank?
|
||||
payment.private_note = nil if form.memo.blank?
|
||||
|
||||
saved = with_service('Payment') { |service| service.create(payment) }
|
||||
raise Error, I18n.t(:error_payment_not_saved) unless saved
|
||||
|
||||
# Invoice balances changed: update the local invoices, and cache the payment for reports
|
||||
applied.each { |invoice, _| sync_invoice(invoice[:id]) }
|
||||
applied.each { |invoice_id, _| sync_invoice(invoice_id) }
|
||||
sync_payment(saved.id)
|
||||
saved
|
||||
end
|
||||
|
||||
# A saved payment on the Edit payment page
|
||||
def load_form(id)
|
||||
payment = load(id)
|
||||
applied = applied_amounts(payment)
|
||||
PaymentForm.new(
|
||||
id: payment.id.to_s,
|
||||
sync_token: payment.sync_token.to_s,
|
||||
customer_id: payment.customer_ref&.value.to_s,
|
||||
customer_name: payment.customer_ref&.name.presence || Customer.find_by(id: payment.customer_ref&.value)&.name,
|
||||
txn_date: payment.txn_date&.iso8601,
|
||||
payment_method_id: payment.payment_method_ref&.value.to_s,
|
||||
reference: payment.payment_ref_number.to_s,
|
||||
amount: BigDecimal(payment.total.to_s).round(2).to_s('F'),
|
||||
deposit: applied.empty? ? '1' : '0',
|
||||
memo: payment.private_note.to_s,
|
||||
invoice_ids: applied.keys,
|
||||
locked: edit_blocked(payment)
|
||||
)
|
||||
end
|
||||
|
||||
# Saves the changes to a payment. Its customer stays. If the amount and the ticked invoices are the
|
||||
# same as before, each invoice keeps the amount it had; otherwise the amount is spread again, oldest first.
|
||||
def update(form)
|
||||
applied = nil
|
||||
previously = []
|
||||
saved = with_service('Payment') do |service|
|
||||
remote = service.fetch_by_id(form.id)
|
||||
raise Error, I18n.t(:notice_payment_not_found) unless remote
|
||||
raise Conflict, I18n.t(:error_payment_changed_in_quickbooks) if remote.sync_token.to_s != form.sync_token.to_s
|
||||
blocked = edit_blocked(remote)
|
||||
raise Error, blocked if blocked
|
||||
|
||||
customer = Customer.find_by(id: remote.customer_ref&.value)
|
||||
raise Error, I18n.t(:error_estimate_needs_customer) unless customer
|
||||
|
||||
amount = form.amount_value.round(2)
|
||||
before = applied_amounts(remote)
|
||||
applied =
|
||||
if !form.deposit? && before.any? && amount == BigDecimal(remote.total.to_s) && form.invoice_ids.sort == before.keys.sort
|
||||
before.to_a
|
||||
else
|
||||
allocation(form, customer, amount, payment: remote)
|
||||
end
|
||||
previously = before.keys
|
||||
|
||||
payment = Quickbooks::Model::Payment.new
|
||||
payment.id = remote.id
|
||||
payment.sync_token = remote.sync_token
|
||||
payment.customer_ref = remote.customer_ref
|
||||
fill(payment, form, amount, applied)
|
||||
service.update(payment, sparse: true)
|
||||
end
|
||||
raise Error, I18n.t(:error_payment_not_saved) unless saved
|
||||
|
||||
# Balances changed on the invoices it paid before and the ones it pays now
|
||||
(previously + applied.map(&:first)).uniq.each { |invoice_id| sync_invoice(invoice_id) }
|
||||
sync_payment(saved.id)
|
||||
saved
|
||||
end
|
||||
|
||||
# Where the payment goes: [[invoice_id, amount], ...] (nothing for a deposit)
|
||||
def allocation(form, customer, amount, payment: nil)
|
||||
return [] if form.deposit?
|
||||
|
||||
invoices = open_invoices(customer, payment: payment)
|
||||
invoices = invoices.select { |i| form.invoice_ids.include?(i[:id]) } if form.invoice_ids.any?
|
||||
allocate(amount, invoices).first.map { |invoice, take| [invoice[:id], take] }
|
||||
end
|
||||
|
||||
# The form's details on a QuickBooks payment, applied as given ([[invoice_id, amount], ...])
|
||||
def fill(payment, form, amount, applied)
|
||||
payment.txn_date = form.txn_date_value
|
||||
payment.payment_method_ref = Quickbooks::Model::BaseReference.new(form.payment_method_id)
|
||||
payment.payment_ref_number = form.reference.to_s
|
||||
payment.private_note = form.memo.to_s
|
||||
payment.total = amount
|
||||
payment.line_items = applied.map do |invoice_id, take|
|
||||
line = Quickbooks::Model::Line.new
|
||||
line.amount = take
|
||||
line.invoice_id = invoice_id
|
||||
line
|
||||
end
|
||||
payment
|
||||
end
|
||||
|
||||
# Why a payment can't be edited here, or nil: it also applies credit memos or other transactions, which
|
||||
# this page doesn't show (saving would drop them)
|
||||
def edit_blocked(payment)
|
||||
others = Array(payment.line_items).flat_map { |line| Array(line.linked_transactions) }
|
||||
.reject { |t| t.txn_type.to_s == INVOICE }
|
||||
others.any? ? I18n.t(:error_payment_not_editable, types: others.map { |t| t.txn_type.to_s }.uniq.join(', ')) : nil
|
||||
end
|
||||
|
||||
# What the payment pays on each invoice: { invoice_id => amount }
|
||||
def applied_amounts(payment)
|
||||
applied_invoices(payment).each_with_object(Hash.new(BigDecimal('0'))) do |(invoice_id, _, amount), sums|
|
||||
sums[invoice_id] += BigDecimal(amount.to_s.presence || '0')
|
||||
end.to_h
|
||||
end
|
||||
|
||||
# Every payment the customer has made, newest first (by date, then QuickBooks id), read from QuickBooks
|
||||
def customer_payments(customer)
|
||||
payments = with_service('Payment') do |service|
|
||||
|
||||
Reference in new issue
Block a user