From bfdf6b30655c8efebf4211835660d66f34861e17 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Wed, 11 Mar 2026 19:36:27 -0400 Subject: [PATCH] Added support for item name & sku. Also added manual sync link --- app/controllers/items_controller.rb | 14 +++++++--- app/services/item_sync_service.rb | 2 ++ app/views/line_items/_settings.html.erb | 3 ++- assets/javascripts/autocomplete.js | 36 +++++++++++++++++++++---- assets/stylesheets/line_items.css | 18 +++++++++++++ config/routes.rb | 3 ++- db/migrate/003_update_items.rb | 16 +++++++++++ 7 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 db/migrate/003_update_items.rb diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index 5679250..82548bd 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -12,11 +12,19 @@ class ItemsController < ApplicationController before_action :require_login def autocomplete - term = params[:q].to_s - items = Item.where("description LIKE ?", "%#{ActiveRecord::Base.sanitize_sql_like(term)}%").where(active: true).order(:description).limit(20) + term = ActiveRecord::Base.sanitize_sql_like(params[:q].to_s) + items = Item.where("description LIKE :t OR name LIKE :t OR sku LIKE :t", t: "%#{term}%") + .where(active: true) + .order(:description) + .limit(20) render json: items.map { |i| - { id: i.id, text: i.description, price: i.unit_price } + { id: i.id, name: i.name, sku: i.sku, description: i.description, price: i.unit_price } } end + + def sync + Item.sync + redirect_to :home, flash: { notice: I18n.t(:label_syncing) } + end end \ No newline at end of file diff --git a/app/services/item_sync_service.rb b/app/services/item_sync_service.rb index da85c7d..23f894e 100644 --- a/app/services/item_sync_service.rb +++ b/app/services/item_sync_service.rb @@ -29,6 +29,8 @@ class ItemSyncService < SyncServiceBase local.description = remote.description local.unit_price = remote.unit_price local.active = remote.active? + local.name = remote.name + local.sku = remote.sku end def log(msg) diff --git a/app/views/line_items/_settings.html.erb b/app/views/line_items/_settings.html.erb index 645a755..7ec7e56 100644 --- a/app/views/line_items/_settings.html.erb +++ b/app/views/line_items/_settings.html.erb @@ -2,4 +2,5 @@ <%=t(:label_item_count)%> <%= Item.count %> @ <%= Item.last_sync %>
<%=t(:label_last_sync)%> <%= Qbo.last_sync if Qbo.exists? %> - \ No newline at end of file + +<%= link_to t(:label_sync_now), items_sync_path%> \ No newline at end of file diff --git a/assets/javascripts/autocomplete.js b/assets/javascripts/autocomplete.js index bb33f4d..d45a3b4 100644 --- a/assets/javascripts/autocomplete.js +++ b/assets/javascripts/autocomplete.js @@ -2,35 +2,45 @@ 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({ + let ac = $(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, + label: item.description, + value: item.description, id: item.id, + name: item.name, + sku: item.sku, + description: item.description, 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 + // set description into input + $input.val(ui.item.description); + row.find(".item-id-field").val(ui.item.id); if (ui.item.price !== undefined && row.find(".price-field").length) { @@ -39,15 +49,31 @@ updateLineItemTotals(); - return false; // still prevent default to avoid double entry + return false; }, + change: function(event, ui) { if (!ui.item) { let row = $(this).closest(".line-item"); row.find(".item-id-field").val(""); } } + }); + + // Custom rendering of autocomplete suggestions + ac.autocomplete("instance")._renderItem = function(ul, item) { + return $("
  • ") + .append( + "
    " + + "
    " + item.name + "
    " + + "
    " + item.sku + "
    " + + "
    " + item.description + "
    " + + "
    " + ) + .appendTo(ul); + }; + }); }; diff --git a/assets/stylesheets/line_items.css b/assets/stylesheets/line_items.css index 3977961..53d208d 100644 --- a/assets/stylesheets/line_items.css +++ b/assets/stylesheets/line_items.css @@ -15,6 +15,24 @@ max-width: 120px; } +.autocomplete-item { + line-height: 1.2; +} + +.item-name { + font-weight: 600; +} + +.item-sku { + font-size: 0.75em; + color: #666; +} + +.item-description { + font-size: 0.70em; + color: #999; +} + /* MOBILE MODE */ @media screen and (max-width: 700px) { diff --git a/config/routes.rb b/config/routes.rb index e0ff812..86fde68 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,4 +8,5 @@ # #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 +get 'items/autocomplete', to: 'items#autocomplete' +get 'items/sync', to: 'items#sync' \ No newline at end of file diff --git a/db/migrate/003_update_items.rb b/db/migrate/003_update_items.rb new file mode 100644 index 0000000..a749a90 --- /dev/null +++ b/db/migrate/003_update_items.rb @@ -0,0 +1,16 @@ +#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 UpdateItems < ActiveRecord::Migration[7.0] + def change + add_column :items, :name, :string, null: false + add_column :items, :sku, :string + end +end \ No newline at end of file