mirror of
https://github.com/rickbarrette/redmine_qbo_lineitems.git
synced 2026-11-17 19:20:36 -05:00
Permissions - Item catalog and accounts pages, and their syncs, are admin-only (they push to / pull from QuickBooks); shown in the Administration layout - Item autocomplete is allowed with Add or Edit line items - Permissions now map to real actions (they pointed at a non-existent line_items controller); line_items_attributes is only accepted from users with a line item permission - Line item changes are checked on the server: adding, editing and deleting each need their permission, and billed lines (already on a QBO estimate) can't be changed or removed Bugs - Billing on close now runs from an Issue after_commit, so closing via bulk edit or the context menu bills too (replaces the save hook); the job takes an issue id - Full sync runs Accounts before Items, and item sync no longer fails when an item has no income account or its account hasn't synced - New QBO items default to Non-inventory in the default income account (the account ref was being assigned to the item type) - Choosing a blank account on an item no longer raises; set_default handles no selection - Item autocomplete escapes item names, SKUs and descriptions (they were inserted into the page as HTML) - Items list showed "translation missing" for Yes/No Polish - Fractional quantities allowed (0.5 hrs, 2.5 qts) - Billed lines show a Billed badge and are read-only; remove button only where allowed; add button only with Add line items - Removed dead code (Item belongs_to :issue, Item#ref, no-op in new, swallowed errors); validation failures return 422 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
93 lines
2.8 KiB
JavaScript
93 lines
2.8 KiB
JavaScript
(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);
|
|
|
|
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.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");
|
|
|
|
// 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) {
|
|
row.find(".price-field").val(ui.item.price);
|
|
}
|
|
|
|
updateLineItemTotals();
|
|
|
|
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. Values are set with .text() so item names,
|
|
// SKUs and descriptions from QuickBooks can never be interpreted as HTML.
|
|
ac.autocomplete("instance")._renderItem = function(ul, item) {
|
|
let $box = $("<div>").addClass("autocomplete-item");
|
|
$("<div>").addClass("item-name").text(item.name || "").appendTo($box);
|
|
if (item.sku) {
|
|
$("<div>").addClass("item-sku").text(item.sku).appendTo($box);
|
|
}
|
|
$("<div>").addClass("item-description").text(item.description || "").appendTo($box);
|
|
|
|
return $("<li>").append($box).appendTo(ul);
|
|
};
|
|
|
|
});
|
|
};
|
|
|
|
// 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);
|
|
|
|
})(); |