Added QBO Item support with autocomplete

This commit is contained in:
2026-03-08 14:24:30 -04:00
parent d3d039f191
commit 60fb4e197c
12 changed files with 281 additions and 2 deletions

View File

@@ -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

36
app/jobs/item_sync_job.rb Normal file
View File

@@ -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

33
app/models/item.rb Normal file
View File

@@ -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

View File

@@ -10,6 +10,7 @@
class LineItem < ApplicationRecord class LineItem < ApplicationRecord
belongs_to :issue belongs_to :issue
belongs_to :item, optional: true
validates :description, presence: true validates :description, presence: true
validates :quantity, numericality: { greater_than: 0 } validates :quantity, numericality: { greater_than: 0 }

View File

@@ -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

View File

@@ -1,10 +1,13 @@
<tr class="line-item"> <tr class="line-item">
<%= f.hidden_field :id %> <%= f.hidden_field :id %>
<%= f.hidden_field :_destroy %> <%= f.hidden_field :_destroy %>
<%= f.hidden_field :item_id, class: "item-id-field" %>
<td data-label="<%= t :label_description %>"> <td data-label="<%= t :label_description %>">
<%= f.text_field :description, <%= f.text_field :description,
placeholder: l(:label_description), placeholder: l(:label_description),
class: "line-item-description",
autocomplete: "off",
no_label: true, no_label: true,
disabled: readonly %> disabled: readonly %>
</td> </td>

View File

@@ -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);
})();

View File

@@ -22,7 +22,11 @@
Date.now().toString() Date.now().toString()
); );
//container.insertAdjacentHTML("beforeend", content);
container.insertAdjacentHTML("beforeend", content); container.insertAdjacentHTML("beforeend", content);
// initialize autocomplete on the new row
initLineItemAutocomplete(container.lastElementChild);
} }
// REMOVE // REMOVE
@@ -50,4 +54,11 @@
// Works for Turbo navigation // Works for Turbo navigation
document.addEventListener("turbo:load", initNestedForms); document.addEventListener("turbo:load", initNestedForms);
})(); })();
$(document).on("input", ".line-item-description", function(){
let row = $(this).closest(".line-item");
row.find(".item-id-field").val("");
});

11
config/routes.rb Normal file
View File

@@ -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'

View File

@@ -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

View File

@@ -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

View File

@@ -16,8 +16,9 @@ module RedmineQboLineItems
# Load the javascript to support the autocomplete forms # Load the javascript to support the autocomplete forms
def view_layouts_base_html_head(context = {}) def view_layouts_base_html_head(context = {})
safe_join([ 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("line_items", plugin: :redmine_qbo_lineitems),
javascript_include_tag("autocomplete", plugin: :redmine_qbo_lineitems),
stylesheet_link_tag("line_items", plugin: :redmine_qbo_lineitems) stylesheet_link_tag("line_items", plugin: :redmine_qbo_lineitems)
]) ])
end end