mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-17 18:20:36 -05:00
- 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>
108 lines
5.4 KiB
Ruby
108 lines
5.4 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" => [...] }.
|
|
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 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 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
|