mirror of
https://github.com/rickbarrette/redmine_qbo_labels.git
synced 2026-11-17 17:50:36 -05:00
New plugin: print part labels on a Brother QL printer
Print label in the top-right actions of issues, estimates and invoices prints the customer's last name and the PO number (W/E/I + number) in big bold letters on a 62mm continuous roll, with a preview of the label and its status until it's printed. A print agent on the printer's network (redmine_qbo_label_agent) asks Redmine for labels every couple of seconds with its printer's token, so labels print from anywhere. Administration » Label printers: printers, agent tokens and status, recent labels. Settings: label lengths, capitals, poll interval, how long a label waits for an offline agent, and logging the agents' requests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
3e29f14f76
41 files changed
+2194
No files matched your search
@@ -0,0 +1,30 @@
|
||||
#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 RedmineQboLabels
|
||||
|
||||
def self.setup
|
||||
# Keep the print agent's 2-second polls (and the print dialog's status checks) out of the log unless
|
||||
# "Log label printer requests" is on
|
||||
Rails::Rack::Logger.prepend(RedmineQboLabels::RequestSilencer)
|
||||
|
||||
# Hooks
|
||||
RedmineQboLabels::Hooks::ViewHookListener
|
||||
|
||||
Rails.configuration.to_prepare do
|
||||
ApplicationController.prepend_view_path(File.expand_path('../app/views', __dir__))
|
||||
end
|
||||
end
|
||||
|
||||
# Whether the current user may print labels and there's a printer to print on
|
||||
def self.printing_available?
|
||||
User.current.allowed_to?(:print_labels, nil, global: true) && LabelPrinter.active.exists?
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,52 @@
|
||||
#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 RedmineQboLabels
|
||||
module Hooks
|
||||
# "Print label" in the top-right actions of issues, estimates and invoices
|
||||
class ViewHookListener < Redmine::Hook::ViewListener
|
||||
|
||||
# The print dialog's script and styles
|
||||
def view_layouts_base_html_head(context = {})
|
||||
return '' unless User.current.logged?
|
||||
|
||||
stylesheet_link_tag('qbo_labels', plugin: 'redmine_qbo_labels') +
|
||||
javascript_include_tag('qbo_labels', plugin: 'redmine_qbo_labels')
|
||||
end
|
||||
|
||||
# An estimate's page (redmine_qbo's :view_qbo_estimate_contextual)
|
||||
def view_qbo_estimate_contextual(context = {})
|
||||
print_link(context, 'estimate', context[:estimate_id])
|
||||
end
|
||||
|
||||
# An invoice's page (redmine_qbo's :view_qbo_invoice_contextual)
|
||||
def view_qbo_invoice_contextual(context = {})
|
||||
print_link(context, 'invoice', context[:invoice_id])
|
||||
end
|
||||
|
||||
# An issue's page: Redmine has no hook in the issue's actions, so the link is moved there by script
|
||||
def view_issues_show_details_bottom(context = {})
|
||||
issue = context[:issue]
|
||||
return '' unless issue&.persisted? && RedmineQboLabels.printing_available?
|
||||
|
||||
context[:hook_caller].render(partial: 'label_hooks/issue_print_link', locals: { issue: issue })
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def print_link(context, source_type, source_id)
|
||||
return '' if source_id.blank? || !RedmineQboLabels.printing_available?
|
||||
|
||||
context[:hook_caller].render(partial: 'label_hooks/print_link',
|
||||
locals: { source_type: source_type, source_id: source_id })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,98 @@
|
||||
#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 RedmineQboLabels
|
||||
# What goes on a part label for an issue, estimate or invoice: the customer's last name and the PO number
|
||||
# parts are ordered with (Sales SOP): E + estimate number, I + invoice number, W + work order (issue) number
|
||||
class LabelSource
|
||||
TYPES = %w[issue estimate invoice].freeze
|
||||
PREFIXES = { 'issue' => 'W', 'estimate' => 'E', 'invoice' => 'I' }.freeze
|
||||
NAME_SUFFIXES = /\A(jr|sr|ii|iii|iv|v|esq)\.?\z/i
|
||||
|
||||
attr_reader :type, :record
|
||||
|
||||
# The issue, estimate or invoice the current user may see, or nil
|
||||
def self.find(type, id)
|
||||
type = type.to_s
|
||||
return nil unless TYPES.include?(type)
|
||||
|
||||
record = case type
|
||||
when 'issue' then Issue.visible.find_by(id: id)
|
||||
when 'estimate' then Estimate.find_by(id: id) if billing_visible?(:write_estimates)
|
||||
when 'invoice' then Invoice.find_by(id: id) if billing_visible?(:write_invoices)
|
||||
end
|
||||
record && new(type, record)
|
||||
end
|
||||
|
||||
# Estimates and invoices are seen by those who view customers or write them (redmine_qbo's permissions)
|
||||
def self.billing_visible?(write_permission, user = User.current)
|
||||
[:view_customers, write_permission].any? { |permission| user.allowed_to?(permission, nil, global: true) }
|
||||
end
|
||||
private_class_method :billing_visible?
|
||||
|
||||
def initialize(type, record)
|
||||
@type = type
|
||||
@record = record
|
||||
end
|
||||
|
||||
def id
|
||||
record.id
|
||||
end
|
||||
|
||||
def customer
|
||||
record.respond_to?(:customer) ? record.customer : nil
|
||||
end
|
||||
|
||||
# E1234, I1234 or W1234
|
||||
def po_number
|
||||
number = type == 'issue' ? record.id : record.doc_number
|
||||
"#{PREFIXES[type]}#{number}"
|
||||
end
|
||||
|
||||
def last_name
|
||||
self.class.last_name(customer)
|
||||
end
|
||||
|
||||
# The label's lines, as they are printed unless changed in the print dialog
|
||||
def lines
|
||||
[last_name, po_number]
|
||||
end
|
||||
|
||||
# Shown in the job list: "Estimate #1234"
|
||||
def title
|
||||
number = type == 'issue' ? record.id : record.doc_number
|
||||
"#{I18n.t("label_qbo_labels_source_#{type}")} ##{number}"
|
||||
end
|
||||
|
||||
# The customer's last name: their family name in QuickBooks, or for a business their full name, or else
|
||||
# the last word of their name (skipping Jr., III, ...)
|
||||
def self.last_name(customer)
|
||||
return '' unless customer
|
||||
|
||||
details = qbo_details(customer)
|
||||
family = details&.family_name.to_s.strip
|
||||
return family if family.present?
|
||||
return customer.name.to_s if details&.company_name.present?
|
||||
|
||||
words = customer.name.to_s.split
|
||||
words.pop while words.size > 1 && words.last.match?(NAME_SUFFIXES)
|
||||
words.last.to_s
|
||||
end
|
||||
|
||||
# QuickBooks' copy of the customer (cached by redmine_qbo); nil when QuickBooks can't be reached
|
||||
def self.qbo_details(customer)
|
||||
customer.details
|
||||
rescue StandardError => e
|
||||
Rails.logger.warn "[redmine_qbo_labels] QuickBooks details for customer #{customer.id}: #{e.message}"
|
||||
nil
|
||||
end
|
||||
private_class_method :qbo_details
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,33 @@
|
||||
#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 RedmineQboLabels
|
||||
# Prepended to Rails::Rack::Logger: the print agent asks for labels every couple of seconds, and the print
|
||||
# dialog checks on its label every second, which would fill the log. Those requests are only logged (and
|
||||
# their SQL) when "Log label printer requests" is on; warnings (e.g. an agent with a wrong token) and errors
|
||||
# are always logged.
|
||||
module RequestSilencer
|
||||
QUIET_PATHS = %r{\A/(label_agent/|label_jobs/\d+(\.json)?\z)}
|
||||
|
||||
def call(env)
|
||||
return super unless quiet?(env)
|
||||
|
||||
logger.silence(::Logger::WARN) { super }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def quiet?(env)
|
||||
env['PATH_INFO'].to_s.match?(QUIET_PATHS) && !RedmineQboLabels::Settings.log_requests?
|
||||
rescue StandardError
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,72 @@
|
||||
#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 RedmineQboLabels
|
||||
# The plugin's settings (Administration » Plugins » Redmine QBO Labels plugin » Configure)
|
||||
module Settings
|
||||
DEFAULTS = {
|
||||
# Labels run along a 62mm (2.4in) continuous roll; their length fits the text, between these (inches)
|
||||
'max_length_in' => '4',
|
||||
'min_length_in' => '1',
|
||||
'uppercase' => '1',
|
||||
# A label still waiting this long (the agent is offline) is dropped, so it doesn't print hours later
|
||||
'job_expiry_minutes' => '10',
|
||||
# How often the print agent asks for labels (seconds)
|
||||
'poll_seconds' => '2',
|
||||
# Write the agent's polls and the print dialog's status checks to the Redmine log (for debugging)
|
||||
'log_requests' => '0'
|
||||
}.freeze
|
||||
|
||||
MM_PER_INCH = 25.4
|
||||
|
||||
def self.values
|
||||
DEFAULTS.merge((Setting.plugin_redmine_qbo_labels.presence || {}).to_h.stringify_keys)
|
||||
end
|
||||
|
||||
def self.max_length_in
|
||||
clamp(values['max_length_in'], DEFAULTS['max_length_in'], 1.0, 12.0)
|
||||
end
|
||||
|
||||
def self.min_length_in
|
||||
[clamp(values['min_length_in'], DEFAULTS['min_length_in'], 0.5, 12.0), max_length_in].min
|
||||
end
|
||||
|
||||
def self.max_length_mm
|
||||
(max_length_in * MM_PER_INCH).round(1)
|
||||
end
|
||||
|
||||
def self.min_length_mm
|
||||
(min_length_in * MM_PER_INCH).round(1)
|
||||
end
|
||||
|
||||
def self.uppercase?
|
||||
values['uppercase'].to_s == '1'
|
||||
end
|
||||
|
||||
def self.job_expiry
|
||||
clamp(values['job_expiry_minutes'], DEFAULTS['job_expiry_minutes'], 1, 1440).minutes
|
||||
end
|
||||
|
||||
def self.poll_seconds
|
||||
clamp(values['poll_seconds'], DEFAULTS['poll_seconds'], 1, 60)
|
||||
end
|
||||
|
||||
def self.log_requests?
|
||||
values['log_requests'].to_s == '1'
|
||||
end
|
||||
|
||||
def self.clamp(value, default, min, max)
|
||||
number = Float(value.to_s, exception: false) || Float(default)
|
||||
number = number.clamp(min, max)
|
||||
min.is_a?(Integer) ? number.round : number
|
||||
end
|
||||
private_class_method :clamp
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user