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,111 @@
|
||||
#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 LabelJobTest < ActiveSupport::TestCase
|
||||
include QboLabelsTestHelper
|
||||
fixtures :users
|
||||
|
||||
def setup
|
||||
@printer, = create_printer
|
||||
end
|
||||
|
||||
def test_lines_are_tidied_and_capitalized
|
||||
job = create_job(@printer, lines: [' Smith ', '', 'e1234'])
|
||||
assert_equal %w[SMITH E1234], job.lines
|
||||
assert_equal %w[SMITH E1234], job.reload.lines
|
||||
end
|
||||
|
||||
def test_capitals_can_be_turned_off
|
||||
with_label_settings('uppercase' => '0') do
|
||||
assert_equal %w[Smith E1234], create_job(@printer, lines: %w[Smith E1234]).lines
|
||||
end
|
||||
end
|
||||
|
||||
def test_a_label_needs_text_that_fits
|
||||
assert_not LabelJob.new(label_printer: @printer, lines: ['', ' ']).valid?
|
||||
assert_not LabelJob.new(label_printer: @printer, lines: ['X' * 41]).valid?
|
||||
assert_not LabelJob.new(label_printer: @printer, lines: %w[A B C D E]).valid?
|
||||
assert_not LabelJob.new(label_printer: @printer, lines: %w[A], copies: 11).valid?
|
||||
assert LabelJob.new(label_printer: @printer, lines: %w[A], copies: 10).valid?
|
||||
end
|
||||
|
||||
def test_claim_next_takes_the_oldest_queued_label_of_the_printer
|
||||
other, = create_printer(name: 'Shop')
|
||||
first = create_job(@printer)
|
||||
second = create_job(@printer)
|
||||
create_job(other)
|
||||
|
||||
assert_equal first, LabelJob.claim_next(@printer)
|
||||
assert_equal 'printing', first.reload.status
|
||||
assert_not_nil first.claimed_at
|
||||
assert_equal second, LabelJob.claim_next(@printer)
|
||||
assert_nil LabelJob.claim_next(@printer)
|
||||
end
|
||||
|
||||
def test_labels_not_picked_up_in_time_expire
|
||||
job = create_job(@printer)
|
||||
travel 11.minutes do
|
||||
assert_nil LabelJob.claim_next(@printer)
|
||||
job.reload
|
||||
assert_equal 'expired', job.status
|
||||
assert job.finished?
|
||||
assert_not_nil job.error
|
||||
end
|
||||
end
|
||||
|
||||
def test_expiry_follows_the_setting
|
||||
job = create_job(@printer)
|
||||
with_label_settings('job_expiry_minutes' => '30') do
|
||||
travel 11.minutes do
|
||||
assert_equal job, LabelJob.claim_next(@printer)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_labels_the_agent_never_reports_on_fail
|
||||
job = create_job(@printer)
|
||||
LabelJob.claim_next(@printer)
|
||||
travel 3.minutes do
|
||||
LabelJob.expire_lapsed
|
||||
assert_equal 'failed', job.reload.status
|
||||
end
|
||||
end
|
||||
|
||||
def test_old_labels_are_removed
|
||||
job = create_job(@printer, status: 'printed')
|
||||
travel 31.days do
|
||||
LabelJob.expire_lapsed
|
||||
assert_not LabelJob.exists?(job.id)
|
||||
end
|
||||
end
|
||||
|
||||
def test_finish
|
||||
job = create_job(@printer)
|
||||
job.finish!(printed: true)
|
||||
assert_equal 'printed', job.reload.status
|
||||
assert_nil job.error
|
||||
|
||||
job = create_job(@printer)
|
||||
job.finish!(printed: false, error: 'Error: No media')
|
||||
assert_equal 'failed', job.reload.status
|
||||
assert_equal 'Error: No media', job.error
|
||||
end
|
||||
|
||||
def test_agent_json_has_the_label_and_its_lengths
|
||||
job = create_job(@printer, copies: 2)
|
||||
json = job.to_agent_json
|
||||
assert_equal %w[SMITH E1234], json[:lines]
|
||||
assert_equal 2, json[:copies]
|
||||
assert_equal 101.6, json[:max_length_mm]
|
||||
assert_equal 25.4, json[:min_length_mm]
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,74 @@
|
||||
#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 LabelPrinterTest < ActiveSupport::TestCase
|
||||
include QboLabelsTestHelper
|
||||
|
||||
def test_token_signs_the_agent_in_and_only_its_digest_is_kept
|
||||
printer, token = create_printer
|
||||
assert token.start_with?('qbl_')
|
||||
assert_not_equal token, printer.reload.token_digest
|
||||
assert_equal printer, LabelPrinter.authenticate(token)
|
||||
assert_nil LabelPrinter.authenticate('qbl_wrong')
|
||||
assert_nil LabelPrinter.authenticate('')
|
||||
end
|
||||
|
||||
def test_inactive_printers_cannot_sign_in
|
||||
printer, token = create_printer(active: false)
|
||||
assert_nil LabelPrinter.authenticate(token)
|
||||
printer.update!(active: true)
|
||||
assert_equal printer, LabelPrinter.authenticate(token)
|
||||
end
|
||||
|
||||
def test_new_token_replaces_the_old_one
|
||||
printer, token = create_printer
|
||||
new_token = printer.generate_token
|
||||
printer.save!
|
||||
assert_nil LabelPrinter.authenticate(token)
|
||||
assert_equal printer, LabelPrinter.authenticate(new_token)
|
||||
end
|
||||
|
||||
def test_seen_records_the_agent_and_its_printer
|
||||
printer, = create_printer
|
||||
assert_not printer.online?
|
||||
printer.seen!(ip: '10.0.0.5', agent: { 'version' => '2026.11.0', 'printer_model' => 'QL-820NWB',
|
||||
'printer_address' => '192.168.0.100:9100',
|
||||
'printer_status' => 'Ready, 62mm continuous' })
|
||||
printer.reload
|
||||
assert printer.online?
|
||||
assert_equal '10.0.0.5', printer.last_ip
|
||||
assert_equal 'QL-820NWB', printer.printer_model
|
||||
assert_equal '192.168.0.100:9100', printer.printer_address
|
||||
assert_equal 'Ready, 62mm continuous', printer.printer_status
|
||||
end
|
||||
|
||||
def test_seen_writes_last_seen_only_every_few_seconds
|
||||
printer, = create_printer
|
||||
printer.seen!(ip: '10.0.0.5')
|
||||
first = printer.reload.last_seen_at
|
||||
travel 5.seconds do
|
||||
printer.seen!(ip: '10.0.0.5')
|
||||
assert_equal first, printer.reload.last_seen_at
|
||||
end
|
||||
travel 20.seconds do
|
||||
printer.seen!(ip: '10.0.0.5')
|
||||
assert_operator printer.reload.last_seen_at, :>, first
|
||||
end
|
||||
end
|
||||
|
||||
def test_names_are_unique
|
||||
create_printer(name: 'Parts room')
|
||||
printer = LabelPrinter.new(name: 'parts ROOM')
|
||||
printer.generate_token
|
||||
assert_not printer.valid?
|
||||
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.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
class LabelSourceTest < ActiveSupport::TestCase
|
||||
include QboLabelsTestHelper
|
||||
fixtures :projects, :users, :members, :member_roles, :roles, :issues, :trackers, :projects_trackers,
|
||||
:issue_statuses, :enabled_modules, :enumerations
|
||||
|
||||
Details = Struct.new(:family_name, :company_name)
|
||||
FakeCustomer = Struct.new(:id, :name, :details)
|
||||
Document = Struct.new(:id, :doc_number, :customer)
|
||||
|
||||
def customer(name, family_name: nil, company_name: nil)
|
||||
FakeCustomer.new(1, name, Details.new(family_name, company_name))
|
||||
end
|
||||
|
||||
def test_po_numbers_follow_the_sales_sop
|
||||
assert_equal 'E1234', RedmineQboLabels::LabelSource.new('estimate', Document.new(99, '1234', nil)).po_number
|
||||
assert_equal 'I1234', RedmineQboLabels::LabelSource.new('invoice', Document.new(98, '1234', nil)).po_number
|
||||
assert_equal 'W3', RedmineQboLabels::LabelSource.new('issue', Issue.find(3)).po_number
|
||||
end
|
||||
|
||||
def test_last_name_is_the_quickbooks_family_name
|
||||
assert_equal 'Smith', RedmineQboLabels::LabelSource.last_name(customer('Jane A. Smith-Jones', family_name: 'Smith'))
|
||||
end
|
||||
|
||||
def test_last_name_falls_back_to_the_last_word_of_the_name
|
||||
assert_equal 'Smith', RedmineQboLabels::LabelSource.last_name(customer('Jane Smith'))
|
||||
assert_equal 'Smith', RedmineQboLabels::LabelSource.last_name(customer('John Smith Jr.'))
|
||||
assert_equal 'Smith', RedmineQboLabels::LabelSource.last_name(customer('John Smith III'))
|
||||
assert_equal 'Smith', RedmineQboLabels::LabelSource.last_name(customer('Smith'))
|
||||
end
|
||||
|
||||
def test_a_business_uses_its_whole_name
|
||||
assert_equal 'Acme Offroad LLC',
|
||||
RedmineQboLabels::LabelSource.last_name(customer('Acme Offroad LLC', company_name: 'Acme Offroad LLC'))
|
||||
end
|
||||
|
||||
def test_last_name_without_quickbooks
|
||||
broken = FakeCustomer.new(1, 'Jane Smith', nil)
|
||||
def broken.details = raise('QuickBooks is down')
|
||||
assert_equal 'Smith', RedmineQboLabels::LabelSource.last_name(broken)
|
||||
assert_equal '', RedmineQboLabels::LabelSource.last_name(nil)
|
||||
end
|
||||
|
||||
def test_issues_must_be_visible
|
||||
User.current = User.find(2)
|
||||
assert RedmineQboLabels::LabelSource.find('issue', 1)
|
||||
User.current = User.anonymous
|
||||
assert_nil RedmineQboLabels::LabelSource.find('issue', 4) # private project
|
||||
assert_nil RedmineQboLabels::LabelSource.find('project', 1)
|
||||
ensure
|
||||
User.current = nil
|
||||
end
|
||||
|
||||
def test_estimates_and_invoices_need_redmine_qbos_permissions
|
||||
User.current = User.find(7) # no global permissions
|
||||
assert_nil RedmineQboLabels::LabelSource.find('estimate', 1)
|
||||
assert_nil RedmineQboLabels::LabelSource.find('invoice', 1)
|
||||
ensure
|
||||
User.current = nil
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user