Files
redmine_qbo_commission/lib/redmine_qbo_commission/txn.rb
T
rickyandClaude Opus 5.5 e76bd12b7f Receipts and credit memos open QuickBooks' PDF through Redmine
Sales receipts, refund receipts and credit memos on the commission
page link to their QuickBooks PDF, fetched by Redmine (approvers only),
so nothing on the page opens QuickBooks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 07:07:24 -05:00

68 lines
3.2 KiB
Ruby

#The MIT License (MIT)
#
#Copyright (c) 2026 rick barrette
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#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.
module RedmineQboCommission
# A QuickBooks transaction that counts toward a month: a paid sale (an invoice paid in full that month, or a
# sales receipt) or a deduction (a refund receipt or credit memo). Amounts are without sales tax.
class Txn < Struct.new(:kind, :txn_id, :doc_number, :customer_name, :txn_date, :total, :tax, keyword_init: true)
SALES = %w[invoice sales_receipt].freeze
DEDUCTIONS = %w[refund_receipt credit_memo].freeze
KINDS = (SALES + DEDUCTIONS).freeze
# From a cached hash (Rails.cache keeps plain values)
def self.from_h(hash)
hash = hash.to_h.symbolize_keys
new(kind: hash[:kind].to_s, txn_id: hash[:txn_id].to_s, doc_number: hash[:doc_number].to_s,
customer_name: hash[:customer_name].to_s, txn_date: hash[:txn_date] && Date.parse(hash[:txn_date].to_s),
total: BigDecimal(hash[:total].to_s), tax: BigDecimal(hash[:tax].to_s))
end
def to_cache
to_h.merge(txn_date: txn_date&.iso8601, total: total.to_d.to_s('F'), tax: tax.to_d.to_s('F'))
end
# "credit_memo:123": how a deduction is picked on the month page
def key
"#{kind}:#{txn_id}"
end
def sale?
SALES.include?(kind)
end
def deduction?
DEDUCTIONS.include?(kind)
end
# The total without sales tax
def net
total.to_d - tax.to_d
end
def kind_name
I18n.t("label_sales_commission_kind_#{kind}")
end
# The QuickBooks services that print the transactions Redmine has no page for
PDF_SERVICES = { 'sales_receipt' => 'SalesReceipt', 'refund_receipt' => 'RefundReceipt',
'credit_memo' => 'CreditMemo' }.freeze
# QuickBooks' PDF of a sales receipt, refund receipt or credit memo (an invoice opens in Redmine instead)
def self.pdf(kind, txn_id)
entity = PDF_SERVICES.fetch(kind.to_s)
qbo = QboConnectionService.current!
qbo.perform_authenticated_request do |access_token|
service = "Quickbooks::Service::#{entity}".constantize.new(company_id: qbo.realm_id, access_token: access_token)
service.pdf(Struct.new(:id).new(txn_id.to_s))
end
end
end
end