mirror of
https://github.com/rickbarrette/redmine_qbo_lineitems.git
synced 2026-04-02 07:01:59 -04:00
Added support for item name & sku. Also added manual sync link
This commit is contained in:
@@ -12,11 +12,19 @@ class ItemsController < ApplicationController
|
|||||||
before_action :require_login
|
before_action :require_login
|
||||||
|
|
||||||
def autocomplete
|
def autocomplete
|
||||||
term = params[:q].to_s
|
term = ActiveRecord::Base.sanitize_sql_like(params[:q].to_s)
|
||||||
items = Item.where("description LIKE ?", "%#{ActiveRecord::Base.sanitize_sql_like(term)}%").where(active: true).order(:description).limit(20)
|
|
||||||
|
|
||||||
|
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|
|
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
|
end
|
||||||
|
|
||||||
|
def sync
|
||||||
|
Item.sync
|
||||||
|
redirect_to :home, flash: { notice: I18n.t(:label_syncing) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
@@ -29,6 +29,8 @@ class ItemSyncService < SyncServiceBase
|
|||||||
local.description = remote.description
|
local.description = remote.description
|
||||||
local.unit_price = remote.unit_price
|
local.unit_price = remote.unit_price
|
||||||
local.active = remote.active?
|
local.active = remote.active?
|
||||||
|
local.name = remote.name
|
||||||
|
local.sku = remote.sku
|
||||||
end
|
end
|
||||||
|
|
||||||
def log(msg)
|
def log(msg)
|
||||||
|
|||||||
@@ -3,3 +3,4 @@
|
|||||||
<br/>
|
<br/>
|
||||||
<%=t(:label_last_sync)%> </b> <%= Qbo.last_sync if Qbo.exists? %>
|
<%=t(:label_last_sync)%> </b> <%= Qbo.last_sync if Qbo.exists? %>
|
||||||
</div>
|
</div>
|
||||||
|
<%= link_to t(:label_sync_now), items_sync_path%>
|
||||||
@@ -2,35 +2,45 @@
|
|||||||
|
|
||||||
window.initLineItemAutocomplete = function(context) {
|
window.initLineItemAutocomplete = function(context) {
|
||||||
let scope = context || document;
|
let scope = context || document;
|
||||||
|
|
||||||
$(scope).find(".line-item-description").each(function() {
|
$(scope).find(".line-item-description").each(function() {
|
||||||
if ($(this).data("autocomplete-initialized")) return;
|
if ($(this).data("autocomplete-initialized")) return;
|
||||||
$(this).data("autocomplete-initialized", true);
|
$(this).data("autocomplete-initialized", true);
|
||||||
|
|
||||||
$(this).autocomplete({
|
let ac = $(this).autocomplete({
|
||||||
appendTo: "body",
|
appendTo: "body",
|
||||||
minLength: 2,
|
minLength: 2,
|
||||||
|
|
||||||
source: function(request, response) {
|
source: function(request, response) {
|
||||||
$.getJSON("/items/autocomplete", { q: request.term })
|
$.getJSON("/items/autocomplete", { q: request.term })
|
||||||
.done(function(data) {
|
.done(function(data) {
|
||||||
|
|
||||||
response(data.map(function(item) {
|
response(data.map(function(item) {
|
||||||
return {
|
return {
|
||||||
label: item.text,
|
label: item.description,
|
||||||
value: item.text,
|
value: item.description,
|
||||||
id: item.id,
|
id: item.id,
|
||||||
|
name: item.name,
|
||||||
|
sku: item.sku,
|
||||||
|
description: item.description,
|
||||||
price: item.price || 0
|
price: item.price || 0
|
||||||
};
|
};
|
||||||
}));
|
}));
|
||||||
|
|
||||||
})
|
})
|
||||||
.fail(function(err){
|
.fail(function(err){
|
||||||
console.error("Autocomplete error:", err);
|
console.error("Autocomplete error:", err);
|
||||||
response([]);
|
response([]);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
select: function(event, ui) {
|
select: function(event, ui) {
|
||||||
let $input = $(this);
|
let $input = $(this);
|
||||||
let row = $input.closest(".line-item");
|
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);
|
row.find(".item-id-field").val(ui.item.id);
|
||||||
|
|
||||||
if (ui.item.price !== undefined && row.find(".price-field").length) {
|
if (ui.item.price !== undefined && row.find(".price-field").length) {
|
||||||
@@ -39,15 +49,31 @@
|
|||||||
|
|
||||||
updateLineItemTotals();
|
updateLineItemTotals();
|
||||||
|
|
||||||
return false; // still prevent default to avoid double entry
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
change: function(event, ui) {
|
change: function(event, ui) {
|
||||||
if (!ui.item) {
|
if (!ui.item) {
|
||||||
let row = $(this).closest(".line-item");
|
let row = $(this).closest(".line-item");
|
||||||
row.find(".item-id-field").val("");
|
row.find(".item-id-field").val("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Custom rendering of autocomplete suggestions
|
||||||
|
ac.autocomplete("instance")._renderItem = function(ul, item) {
|
||||||
|
return $("<li>")
|
||||||
|
.append(
|
||||||
|
"<div class='autocomplete-item'>" +
|
||||||
|
"<div class='item-name'>" + item.name + "</div>" +
|
||||||
|
"<div class='item-sku'>" + item.sku + "</div>" +
|
||||||
|
"<div class='item-description'>" + item.description + "</div>" +
|
||||||
|
"</div>"
|
||||||
|
)
|
||||||
|
.appendTo(ul);
|
||||||
|
};
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,24 @@
|
|||||||
max-width: 120px;
|
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 */
|
/* MOBILE MODE */
|
||||||
@media screen and (max-width: 700px) {
|
@media screen and (max-width: 700px) {
|
||||||
|
|
||||||
|
|||||||
@@ -9,3 +9,4 @@
|
|||||||
#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.
|
#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'
|
get 'items/autocomplete', to: 'items#autocomplete'
|
||||||
|
get 'items/sync', to: 'items#sync'
|
||||||
16
db/migrate/003_update_items.rb
Normal file
16
db/migrate/003_update_items.rb
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user