Files
redmine_qbo/lib/redmine_qbo_billing.rb
T
rickyandClaude Opus 5.5 ae0ffc7c1c Leave other plugins' features to hooks
Refund emails and PDFs take the shop name and mail settings from
:qbo_mail_settings instead of reading redmine_qbo_reminders' settings; the
customer page's ticked boxes use generic data-ticked-field/data-ticked-params
instead of knowing redmine_qbo_calendar's Add appointment link; and Create
issues carries the params plugins name in :qbo_billing_issue_carry_params
instead of appointment_id.

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

144 lines
7.3 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.
# Billing in Redmine: writing, editing and emailing QuickBooks estimates and invoices, receiving payments,
# the QuickBooks product/service list and accounts, and creating issues from estimates. Called from
# RedmineQbo.setup. (It began as the separate redmine_qbo_billing plugin, hence the name; other plugins use
# its hooks and RedmineQboBilling::EstimateWriter.create_estimate.)
module RedmineQboBilling
def self.setup
# Invoice lines that bill time activities need a LinkedTxn
RedmineQboBilling::Patches::InvoiceLineItemLinkedTxn.apply!
# Hooks
RedmineQboBilling::Hooks::QboHookListener
# Administration menu: the QuickBooks product/service list (accounts are reached from it)
Redmine::MenuManager.map :admin_menu do |menu|
menu.push :redmine_qbo_items, { controller: 'items', action: 'index' }, icon: 'list', caption: :label_items,
html: { class: 'icon icon-list' } unless menu.exists?(:redmine_qbo_items)
end
end
# Suggestions for the editor's custom field boxes for a customer, from plugins answering
# :qbo_billing_field_options, { customer: } => { field_id => [{ value:, label: }, ...] }
# (e.g. redmine_qbo_vehicles: the customer's vehicles for its VIN field). Returns { "2" => [...] }.
# The shop's name and how customer emails (refund summaries) are sent, from other plugins (e.g.
# redmine_qbo_reminders: its shop name and SMTP server) through :qbo_mail_settings. Each returns any of
# shop_name:, from:, reply_to:, delivery: (options for ActionMailer's mail(), e.g. an SMTP server); blank
# ones fall back to Redmine's own settings.
def self.mail_settings
Redmine::Hook.call_hook(:qbo_mail_settings, {}).select { |result| result.is_a?(Hash) }
.reduce({}) { |merged, result| merged.merge(result.symbolize_keys.compact_blank) }
rescue StandardError => e
Rails.logger.warn "[RedmineQboBilling] Could not read mail settings from plugins: #{e.message}"
{}
end
def self.field_options(customer)
return {} unless customer
Redmine::Hook.call_hook(:qbo_billing_field_options, { customer: customer }).each_with_object({}) do |result, merged|
next unless result.is_a?(Hash)
result.each do |field, options|
id = (field.respond_to?(:id) ? field.id : field).to_s
list = Array(options).filter_map do |option|
option = option.to_h.symbolize_keys
value = option[:value].to_s.strip
{ value: value, label: option[:label].to_s.presence || value } unless value.empty?
end
(merged[id] ||= []).concat(list).uniq! { |o| o[:value].upcase }
end
end
rescue StandardError => e
Rails.logger.warn "[RedmineQboBilling] Custom field suggestions failed: #{e.message}"
{}
end
# Custom field values for a document's work orders, from plugins answering
# :qbo_billing_field_defaults, { customer:, issues: } => { field_id => value }
# (e.g. redmine_qbo_vehicles: the work orders' VIN). Used for an invoice made from an estimate, for the
# fields the estimate leaves blank. New estimates and invoices start blank. Returns { "2" => "..." }.
def self.field_defaults(customer, issues: [])
return {} unless customer
Redmine::Hook.call_hook(:qbo_billing_field_defaults, { customer: customer, issues: Array(issues) })
.each_with_object({}) do |result, merged|
next unless result.is_a?(Hash)
EstimateForm.custom_field_hash(result).each { |id, value| merged[id] ||= value if value.present? }
end
rescue StandardError => e
Rails.logger.warn "[RedmineQboBilling] Custom field defaults failed: #{e.message}"
{}
end
# True when the user may write estimates
def self.writer?(user = User.current)
user.allowed_to?(:write_estimates, nil, global: true)
end
# True when the user may email estimates to customers
def self.sender?(user = User.current)
user.allowed_to?(:send_estimates, nil, global: true)
end
# True when the user may receive customer payments
def self.payment_receiver?(user = User.current)
user.allowed_to?(:receive_payments, nil, global: true)
end
# True when the user may refund customers' credit (SOP-OPS-014)
def self.refunder?(user = User.current)
user.allowed_to?(:issue_refunds, nil, global: true)
end
# True when the user may see customer payments (receiving them includes seeing them)
def self.payment_viewer?(user = User.current)
user.allowed_to?(:view_payments, nil, global: true) || payment_receiver?(user)
end
# True when the user may unlock and change an estimate linked to an issue (e.g. a customer asks for changes to
# an estimate already on the diagnostic work order): "Edit estimates linked to issues" on top of "Write estimates"
def self.linked_estimate_editor?(user = User.current)
writer?(user) && user.allowed_to?(:edit_linked_estimates, nil, global: true)
end
# True when the user may open any estimate's PDF: View customers or Write estimates. Others open only the
# estimates attached to issues they can see (see AuthHelper#estimate_pdf_allowed?).
def self.estimate_viewer?(user = User.current)
user.allowed_to?(:view_customers, nil, global: true) || writer?(user)
end
# True when the user may open any invoice's PDF: View customers, Write invoices, View or Receive payments, or
# Issue refunds (those pages link to invoices). Others open only the invoices attached to issues they can see.
def self.invoice_viewer?(user = User.current)
user.allowed_to?(:view_customers, nil, global: true) || invoice_writer?(user) || payment_viewer?(user) ||
refunder?(user)
end
# True when the user may write invoices
def self.invoice_writer?(user = User.current)
user.allowed_to?(:write_invoices, nil, global: true)
end
# True when the user may unlock and change an invoice with a payment applied: a paid-off one needs
# "Edit paid invoices", a partly paid one "Edit partially paid invoices" (both on top of "Write invoices")
def self.paid_invoice_editor?(fully_paid, user = User.current)
invoice_writer?(user) &&
user.allowed_to?(fully_paid ? :edit_paid_invoices : :edit_partially_paid_invoices, nil, global: true)
end
# True when the user may email invoices to customers
def self.invoice_sender?(user = User.current)
user.allowed_to?(:send_invoices, nil, global: true)
end
end