From 60fb4e197ccc902c9b8ed45346d0fb62090412bc Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Sun, 8 Mar 2026 14:24:30 -0400 Subject: [PATCH] Added QBO Item support with autocomplete --- app/controllers/items_controller.rb | 22 ++++++ app/jobs/item_sync_job.rb | 36 ++++++++++ app/models/item.rb | 33 +++++++++ app/models/line_item.rb | 1 + app/services/item_sync_service.rb | 32 +++++++++ .../line_items/_line_item_fields.html.erb | 3 + assets/javascripts/autocomplete.js | 67 +++++++++++++++++++ assets/javascripts/nested_form_controller.js | 13 +++- config/routes.rb | 11 +++ db/migrate/002_create_items.rb | 26 +++++++ .../hooks/qbo_hook_listener.rb | 36 ++++++++++ .../hooks/view_hook_listener.rb | 3 +- 12 files changed, 281 insertions(+), 2 deletions(-) create mode 100644 app/controllers/items_controller.rb create mode 100644 app/jobs/item_sync_job.rb create mode 100644 app/models/item.rb create mode 100644 app/services/item_sync_service.rb create mode 100644 assets/javascripts/autocomplete.js create mode 100644 config/routes.rb create mode 100644 db/migrate/002_create_items.rb create mode 100644 lib/redmine_qbo_line_items/hooks/qbo_hook_listener.rb diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb new file mode 100644 index 0000000..d2f73ba --- /dev/null +++ b/app/controllers/items_controller.rb @@ -0,0 +1,22 @@ +#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. + +class ItemsController < ApplicationController + before_action :require_login + + def autocomplete + term = params[:q].to_s + items = Item.where("description LIKE ?", "%#{term}%").order(:description).limit(20) + + render json: items.map { |i| + { id: i.id, text: i.description, price: i.unit_price } + } + end +end \ No newline at end of file diff --git a/app/jobs/item_sync_job.rb b/app/jobs/item_sync_job.rb new file mode 100644 index 0000000..fa1303a --- /dev/null +++ b/app/jobs/item_sync_job.rb @@ -0,0 +1,36 @@ +#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. + +class ItemSyncJob < ApplicationJob + queue_as :default + retry_on StandardError, wait: 5.minutes, attempts: 5 + + # Performs a sync of items from QuickBooks Online. + def perform(full_sync: false, id: nil) + qbo = QboConnectionService.current! + raise "No QBO configuration found" unless qbo + + log "Starting #{full_sync ? 'full' : 'incremental'} sync for item ##{id || 'all'}..." + + service = ItemSyncService.new(qbo: qbo) + + if id.present? + service.sync_by_id(id) + else + service.sync(full_sync: full_sync) + end + end + + private + + def log(msg) + Rails.logger.info "[ItemSyncJob] #{msg}" + end +end \ No newline at end of file diff --git a/app/models/item.rb b/app/models/item.rb new file mode 100644 index 0000000..419ccf9 --- /dev/null +++ b/app/models/item.rb @@ -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. + +class Item < ApplicationRecord + belongs_to :issue + + validates :description, presence: true + validates :unit_price, numericality: { greater_than_or_equal_to: 0 } + + # Sync all employees, typically triggered by a scheduled task or manual sync request + def self.sync + ItemSyncJob.perform_later(full_sync: true) + end + + # Sync a single employee by ID, typically triggered by a webhook notification or manual sync request + def self.sync_by_id(id) + ItemSyncJob.perform_later(id: id) + end + + private + + def log(msg) + Rails.logger.info "[LineItem] #{msg}" + end + +end \ No newline at end of file diff --git a/app/models/line_item.rb b/app/models/line_item.rb index 73e4fe2..0ef5af2 100644 --- a/app/models/line_item.rb +++ b/app/models/line_item.rb @@ -10,6 +10,7 @@ class LineItem < ApplicationRecord belongs_to :issue + belongs_to :item, optional: true validates :description, presence: true validates :quantity, numericality: { greater_than: 0 } diff --git a/app/services/item_sync_service.rb b/app/services/item_sync_service.rb new file mode 100644 index 0000000..7c1d2bf --- /dev/null +++ b/app/services/item_sync_service.rb @@ -0,0 +1,32 @@ +#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. + +class ItemSyncService < SyncServiceBase + + private + + # Specify the local model this service syncs + def self.model_class + Item + end + + # Determine if the remote entity should be deleted locally (e.g. if it's marked inactive in QBO) + def destroy_remote?(remote) + !remote.active? + end + + # Map relevant attributes from the QBO Employee to the local Employee model + def process_attributes(local, remote) + local.qbo_id = remote.id + local.description = remote.description + local.unit_price = remote.unit_price + end + +end \ No newline at end of file diff --git a/app/views/line_items/_line_item_fields.html.erb b/app/views/line_items/_line_item_fields.html.erb index 0981b28..8b8a0c8 100644 --- a/app/views/line_items/_line_item_fields.html.erb +++ b/app/views/line_items/_line_item_fields.html.erb @@ -1,10 +1,13 @@ <%= f.hidden_field :id %> <%= f.hidden_field :_destroy %> + <%= f.hidden_field :item_id, class: "item-id-field" %> <%= f.text_field :description, placeholder: l(:label_description), + class: "line-item-description", + autocomplete: "off", no_label: true, disabled: readonly %> diff --git a/assets/javascripts/autocomplete.js b/assets/javascripts/autocomplete.js new file mode 100644 index 0000000..bb33f4d --- /dev/null +++ b/assets/javascripts/autocomplete.js @@ -0,0 +1,67 @@ +(function () { + + window.initLineItemAutocomplete = function(context) { + let scope = context || document; + $(scope).find(".line-item-description").each(function() { + if ($(this).data("autocomplete-initialized")) return; + $(this).data("autocomplete-initialized", true); + + $(this).autocomplete({ + appendTo: "body", + minLength: 2, + source: function(request, response) { + $.getJSON("/items/autocomplete", { q: request.term }) + .done(function(data) { + response(data.map(function(item) { + return { + label: item.text, + value: item.text, + id: item.id, + price: item.price || 0 + }; + })); + }) + .fail(function(err){ + console.error("Autocomplete error:", err); + response([]); + }); + }, + select: function(event, ui) { + let $input = $(this); + let row = $input.closest(".line-item"); + + $input.val(ui.item.value); // <-- set description + row.find(".item-id-field").val(ui.item.id); + + if (ui.item.price !== undefined && row.find(".price-field").length) { + row.find(".price-field").val(ui.item.price); + } + + updateLineItemTotals(); + + return false; // still prevent default to avoid double entry + }, + change: function(event, ui) { + if (!ui.item) { + let row = $(this).closest(".line-item"); + row.find(".item-id-field").val(""); + } + } + }); + }); + }; + + // Clear item_id when user types manually + $(document).on("input", ".line-item-description", function(){ + let row = $(this).closest(".line-item"); + row.find(".item-id-field").val(""); + }); + + function initializeAutocomplete() { + window.initLineItemAutocomplete(document); + } + + $(document).ready(initializeAutocomplete); + document.addEventListener("turbo:load", initializeAutocomplete); + +})(); \ No newline at end of file diff --git a/assets/javascripts/nested_form_controller.js b/assets/javascripts/nested_form_controller.js index fdb3264..221473f 100644 --- a/assets/javascripts/nested_form_controller.js +++ b/assets/javascripts/nested_form_controller.js @@ -22,7 +22,11 @@ Date.now().toString() ); + //container.insertAdjacentHTML("beforeend", content); container.insertAdjacentHTML("beforeend", content); + + // initialize autocomplete on the new row + initLineItemAutocomplete(container.lastElementChild); } // REMOVE @@ -50,4 +54,11 @@ // Works for Turbo navigation document.addEventListener("turbo:load", initNestedForms); -})(); \ No newline at end of file +})(); + +$(document).on("input", ".line-item-description", function(){ + + let row = $(this).closest(".line-item"); + row.find(".item-id-field").val(""); + +}); \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb new file mode 100644 index 0000000..e0ff812 --- /dev/null +++ b/config/routes.rb @@ -0,0 +1,11 @@ +#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. + +get 'items/autocomplete', to: 'items#autocomplete' \ No newline at end of file diff --git a/db/migrate/002_create_items.rb b/db/migrate/002_create_items.rb new file mode 100644 index 0000000..378c53f --- /dev/null +++ b/db/migrate/002_create_items.rb @@ -0,0 +1,26 @@ +#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. + +class CreateItems < ActiveRecord::Migration[7.0] + def change + create_table :items do |t| + t.integer :qbo_id, null: false + t.text :description, null: false + t.decimal :unit_price, + precision: 15, + scale: 4, + null: false, + default: 0 + t.timestamps + end + + add_reference :line_items, :item, foreign_key: true + end +end \ No newline at end of file diff --git a/lib/redmine_qbo_line_items/hooks/qbo_hook_listener.rb b/lib/redmine_qbo_line_items/hooks/qbo_hook_listener.rb new file mode 100644 index 0000000..2a388d1 --- /dev/null +++ b/lib/redmine_qbo_line_items/hooks/qbo_hook_listener.rb @@ -0,0 +1,36 @@ +#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 RedmineQboLineItems + module Hooks + + class QboHookListener < Redmine::Hook::ViewListener + + # Called by WebhookProcessJob + def qbo_additional_entities(context={}) + log "Added QBO Item to allowed webook entities" + return "Item" + end + + # Called by the QboSyncDispatcher + def qbo_full_sync (context={}) + log "Adding ItemSyncJob to QBO sync dispatcher" + return ItemSyncJob + end + + private + + def log(msg) + Rails.logger.info "[QboHookListener] #{msg}" + end + + end + end +end \ No newline at end of file diff --git a/lib/redmine_qbo_line_items/hooks/view_hook_listener.rb b/lib/redmine_qbo_line_items/hooks/view_hook_listener.rb index 1bc2166..5a4b076 100644 --- a/lib/redmine_qbo_line_items/hooks/view_hook_listener.rb +++ b/lib/redmine_qbo_line_items/hooks/view_hook_listener.rb @@ -16,8 +16,9 @@ module RedmineQboLineItems # Load the javascript to support the autocomplete forms def view_layouts_base_html_head(context = {}) safe_join([ - javascript_include_tag( 'nested_form_controller.js', plugin: :redmine_qbo_lineitems), + javascript_include_tag( 'nested_form_controller', plugin: :redmine_qbo_lineitems), javascript_include_tag("line_items", plugin: :redmine_qbo_lineitems), + javascript_include_tag("autocomplete", plugin: :redmine_qbo_lineitems), stylesheet_link_tag("line_items", plugin: :redmine_qbo_lineitems) ]) end