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:
rickyandClaude Opus 5.5 committed 2026-11-16 08:42:16 -05:00
commit 3e29f14f76
41 files changed
+2194

No files matched your search

+110
View File
@@ -0,0 +1,110 @@
#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.
require File.expand_path('../test_helper', __dir__)
class LabelAgentApiTest < Redmine::IntegrationTest
include QboLabelsTestHelper
fixtures :users
def setup
@printer, @token = create_printer
end
def headers(token = @token)
{ 'Authorization' => "Bearer #{token}", 'CONTENT_TYPE' => 'application/json' }
end
def test_unknown_token_is_refused
post '/label_agent/poll', params: '{}', headers: headers('qbl_wrong')
assert_response :unauthorized
post '/label_agent/poll', params: '{}'
assert_response :unauthorized
end
def test_poll_without_labels
post '/label_agent/poll', params: '{}', headers: headers
assert_response :success
json = response.parsed_body
assert_nil json['job']
assert_equal 2, json['poll_seconds']
assert @printer.reload.online?
end
def test_poll_hands_out_the_next_label
job = create_job(@printer, copies: 2)
post '/label_agent/poll', params: '{}', headers: headers
assert_response :success
json = response.parsed_body['job']
assert_equal job.id, json['id']
assert_equal %w[SMITH E1234], json['lines']
assert_equal 2, json['copies']
assert_equal 'printing', job.reload.status
post '/label_agent/poll', params: '{}', headers: headers
assert_nil response.parsed_body['job']
end
def test_poll_records_what_the_agent_says
post '/label_agent/poll', headers: headers,
params: { agent: { version: '2026.11.0', printer_model: 'QL-820NWB', printer_address: '192.168.0.100:9100',
printer_status: 'Ready, 62mm continuous', other: 'x' } }.to_json
assert_response :success
@printer.reload
assert_equal '2026.11.0', @printer.agent_version
assert_equal 'QL-820NWB', @printer.printer_model
assert_equal 'Ready, 62mm continuous', @printer.printer_status
end
def test_result_marks_the_label_printed_or_failed
job = create_job(@printer)
LabelJob.claim_next(@printer)
post "/label_agent/jobs/#{job.id}/result", params: { status: 'printed' }.to_json, headers: headers
assert_response :success
assert_equal 'printed', job.reload.status
# A repeated report changes nothing
post "/label_agent/jobs/#{job.id}/result", params: { status: 'failed', error: 'x' }.to_json, headers: headers
assert_response :success
assert_equal 'printed', job.reload.status
job = create_job(@printer)
post "/label_agent/jobs/#{job.id}/result", params: { status: 'failed', error: 'Error: No media' }.to_json,
headers: headers
assert_equal 'failed', job.reload.status
assert_equal 'Error: No media', job.error
end
def test_an_agent_only_reports_on_its_own_printers_labels
other, = create_printer(name: 'Shop')
job = create_job(other)
post "/label_agent/jobs/#{job.id}/result", params: { status: 'printed' }.to_json, headers: headers
assert_response :not_found
assert_equal 'queued', job.reload.status
end
def test_polls_stay_out_of_the_log_unless_turned_on
io = StringIO.new
logger = ActiveSupport::Logger.new(io)
previous = Rails.logger
Rails.logger = ActiveSupport::BroadcastLogger.new(logger)
begin
post '/label_agent/poll', params: '{}', headers: headers
assert_not_includes io.string, '/label_agent/poll'
with_label_settings('log_requests' => '1') do
post '/label_agent/poll', params: '{}', headers: headers
end
assert_includes io.string, 'Parts room agent'
ensure
Rails.logger = previous
end
end
end
+92
View File
@@ -0,0 +1,92 @@
#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.
require File.expand_path('../test_helper', __dir__)
class LabelJobsTest < Redmine::IntegrationTest
include QboLabelsTestHelper
fixtures :projects, :users, :email_addresses, :members, :member_roles, :roles, :issues, :trackers,
:projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :workflows
def setup
@printer, = create_printer
Role.find(1).add_permission!(:print_labels)
end
def test_print_dialog_for_an_issue
log_user('jsmith', 'jsmith')
get '/label_jobs/new', params: { source_type: 'issue', source_id: 1 }, xhr: true
assert_response :success
assert_includes response.body, 'W1'
assert_includes response.body, 'qbo-label-preview'
end
def test_printing_needs_the_permission
log_user('dlopper', 'foo') # Developer: no Print labels
get '/label_jobs/new', params: { source_type: 'issue', source_id: 1 }, xhr: true
assert_response :forbidden
end
def test_print_dialog_for_something_unseen
log_user('jsmith', 'jsmith')
get '/label_jobs/new', params: { source_type: 'issue', source_id: 999 }, xhr: true
assert_response :not_found
end
def test_print_queues_the_label
log_user('jsmith', 'jsmith')
assert_difference 'LabelJob.count', 1 do
post '/label_jobs', params: { source_type: 'issue', source_id: 1, lines: ['Smith', 'w1'], copies: '2' }, xhr: true
end
assert_response :success
job = LabelJob.last
assert_equal %w[SMITH W1], job.lines
assert_equal 2, job.copies
assert_equal @printer, job.label_printer
assert_equal 'issue', job.source_type
assert_equal '1', job.source_id
assert_includes response.body, "QboLabels.follow"
get "/label_jobs/#{job.id}", headers: { "Accept" => "application/json" }
assert_response :success
assert_equal 'queued', response.parsed_body['status']
assert_equal false, response.parsed_body['finished']
end
def test_an_empty_label_shows_the_dialog_again
log_user('jsmith', 'jsmith')
assert_no_difference 'LabelJob.count' do
post '/label_jobs', params: { source_type: 'issue', source_id: 1, lines: ['', ' '] }, xhr: true
end
assert_response :success
assert_includes response.body, 'errorExplanation'
end
def test_only_your_own_labels_can_be_followed
job = create_job(@printer, user: User.find(3))
log_user('jsmith', 'jsmith')
get "/label_jobs/#{job.id}", headers: { "Accept" => "application/json" }
assert_response :not_found
end
def test_issue_page_has_the_print_label_link
log_user('jsmith', 'jsmith')
get '/issues/1'
assert_response :success
assert_select 'template#qbo-label-issue-link a.icon-qbo-label-print[data-remote=true]'
end
def test_no_link_without_an_active_printer
@printer.update!(active: false)
log_user('jsmith', 'jsmith')
get '/issues/1'
assert_select 'template#qbo-label-issue-link', 0
end
end
+48
View File
@@ -0,0 +1,48 @@
#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.
require File.expand_path('../test_helper', __dir__)
class LabelPrintersTest < Redmine::IntegrationTest
include QboLabelsTestHelper
fixtures :users, :email_addresses
def test_admins_add_a_printer_and_see_its_token_once
log_user('admin', 'admin')
get '/label_printers'
assert_response :success
assert_difference 'LabelPrinter.count', 1 do
post '/label_printers', params: { label_printer: { name: 'Parts room', active: '1' } }
end
assert_response :success
token = css_select('pre.qbo-label-token').first.text.strip
assert LabelPrinter.authenticate(token)
assert_includes response.body, 'AGENT_TOKEN'
assert_equal 'no-store', response.headers['Cache-Control']
get '/label_printers'
assert_not_includes response.body, token
end
def test_new_token
printer, token = create_printer
log_user('admin', 'admin')
post "/label_printers/#{printer.id}/regenerate_token"
assert_response :success
assert_nil LabelPrinter.authenticate(token)
end
def test_admins_only
log_user('jsmith', 'jsmith')
get '/label_printers'
assert_response :forbidden
end
end