mirror of
https://github.com/rickbarrette/redmine_qbo_lineitems.git
synced 2026-04-02 07:01:59 -04:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a7899fb6b3 | |||
| ef7570fb3b | |||
| 93ae77d9f6 | |||
| ac82d5cf90 | |||
| a52ff3b860 | |||
| 4f8e5d9b2c | |||
| bfdf6b3065 | |||
| 1febdc3e04 | |||
| d1f8f027c0 |
@@ -10,13 +10,70 @@
|
||||
|
||||
class ItemsController < ApplicationController
|
||||
before_action :require_login
|
||||
before_action :find_item, only: [:show, :edit, :update, :destroy]
|
||||
|
||||
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 create
|
||||
@item = Item.new(item_params)
|
||||
|
||||
if @item.save
|
||||
redirect_to item_path(@item), notice: l(:notice_successful_create)
|
||||
else
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@item.destroy
|
||||
redirect_to items_path, notice: l(:notice_successful_delete)
|
||||
end
|
||||
|
||||
def edit
|
||||
end
|
||||
|
||||
def index
|
||||
@items = Item.order(:name)
|
||||
end
|
||||
|
||||
def new
|
||||
@item = Item.new
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def sync
|
||||
Item.sync
|
||||
redirect_to :home, flash: { notice: I18n.t(:label_syncing) }
|
||||
end
|
||||
|
||||
def update
|
||||
if @item.update(item_params)
|
||||
redirect_to item_path(@item), notice: l(:notice_successful_update)
|
||||
else
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_item
|
||||
@item = Item.find(params[:id])
|
||||
end
|
||||
|
||||
def item_params
|
||||
params.require(:item).permit(:name, :description, :sku, :unit_price, :active)
|
||||
end
|
||||
end
|
||||
@@ -8,33 +8,39 @@
|
||||
#
|
||||
#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
|
||||
class Item < QboBaseModel
|
||||
belongs_to :issue
|
||||
|
||||
validates_presence_of :id, :description
|
||||
validates :unit_price, numericality: { greater_than_or_equal_to: 0 }
|
||||
self.primary_key = :id
|
||||
|
||||
# Returns the last sync time formatted for display. If no sync has occurred, returns a default message.
|
||||
def self.last_sync
|
||||
return I18n.t(:label_qbo_never_synced) unless maximum(:updated_at)
|
||||
format_time(maximum(:updated_at))
|
||||
# Updates Both local & remote DB description
|
||||
def description=(s)
|
||||
details
|
||||
@details.description = s
|
||||
super
|
||||
end
|
||||
|
||||
# Sync all items, typically triggered by a scheduled task or manual sync request
|
||||
def self.sync
|
||||
ItemSyncJob.perform_later(full_sync: true)
|
||||
# Updates Both local & remote DB name
|
||||
def name=(s)
|
||||
details
|
||||
@details.name = s
|
||||
super
|
||||
end
|
||||
|
||||
# Sync a single items by ID, typically triggered by a webhook notification or manual sync request
|
||||
def self.sync_by_id(id)
|
||||
ItemSyncJob.perform_later(id: id)
|
||||
# Updates Both local & remote DB sku
|
||||
def sku=(s)
|
||||
details
|
||||
@details.sku = s
|
||||
super
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def log(msg)
|
||||
Rails.logger.info "[LineItem] #{msg}"
|
||||
# Updates Both local & remote DB price
|
||||
def unit_price=(s)
|
||||
details
|
||||
@details.unit_price = s
|
||||
super
|
||||
end
|
||||
|
||||
end
|
||||
41
app/services/item_service.rb
Normal file
41
app/services/item_service.rb
Normal file
@@ -0,0 +1,41 @@
|
||||
#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 ItemService < ServiceBase
|
||||
|
||||
private
|
||||
|
||||
def build_qbo_remote
|
||||
log "Building new QBO Item"
|
||||
account = default_income_account
|
||||
log "Account: #{account.id} - #{account.name}"
|
||||
income = Quickbooks::Model::BaseReference.new
|
||||
income.value = account.id
|
||||
income.name = account.name
|
||||
|
||||
Quickbooks::Model::Item.new(
|
||||
type: Quickbooks::Model::Item::NON_INVENTORY_TYPE,
|
||||
income_account_ref: income
|
||||
)
|
||||
end
|
||||
|
||||
def default_income_account
|
||||
log "Looking up sales income account"
|
||||
qbo = QboConnectionService.current!
|
||||
qbo.perform_authenticated_request do |token|
|
||||
service = Quickbooks::Service::Account.new(
|
||||
company_id: qbo.realm_id,
|
||||
access_token: token
|
||||
)
|
||||
service.query("SELECT * FROM Account WHERE AccountType='Income' AND Name LIKE '%Sales%'").first
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -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)
|
||||
|
||||
43
app/views/items/_form.html.erb
Normal file
43
app/views/items/_form.html.erb
Normal file
@@ -0,0 +1,43 @@
|
||||
<%= form_with model: @item, local: true do |f| %>
|
||||
|
||||
<% if @item.errors.any? %>
|
||||
<div id="errorExplanation">
|
||||
<h2><%= pluralize(@item.errors.count, "error") %></h2>
|
||||
<ul>
|
||||
<% @item.errors.full_messages.each do |msg| %>
|
||||
<li><%= msg %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<p>
|
||||
<%= f.label :name %><br>
|
||||
<%= f.text_field :name, required: true %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= f.label :sku %><br>
|
||||
<%= f.text_field :sku %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= f.label :description %><br>
|
||||
<%= f.text_area :description, rows: 3 %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= f.label :unit_price %><br>
|
||||
<%= f.number_field :unit_price, step: 0.01 %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= f.label :active %>
|
||||
<%= f.check_box :active %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= f.submit %>
|
||||
</p>
|
||||
|
||||
<% end %>
|
||||
5
app/views/items/edit.html.erb
Normal file
5
app/views/items/edit.html.erb
Normal file
@@ -0,0 +1,5 @@
|
||||
<h2>Edit Item</h2>
|
||||
|
||||
<%= render "form" %>
|
||||
|
||||
<%= link_to "Back", items_path %>
|
||||
37
app/views/items/index.html.erb
Normal file
37
app/views/items/index.html.erb
Normal file
@@ -0,0 +1,37 @@
|
||||
<h2>Items</h2>
|
||||
|
||||
<div class="contextual">
|
||||
<%= link_to "New Item", new_item_path, class: "icon icon-add" %>
|
||||
</div>
|
||||
|
||||
<table class="list items">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>SKU</th>
|
||||
<th>Description</th>
|
||||
<th>Price</th>
|
||||
<th>Active</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<% @items.each do |item| %>
|
||||
<tr>
|
||||
<td><%= link_to item.name, item_path(item) %></td>
|
||||
<td><%= item.sku %></td>
|
||||
<td><%= item.description %></td>
|
||||
<td><%= number_to_currency(item.unit_price) %></td>
|
||||
<td><%= item.active ? "Yes" : "No" %></td>
|
||||
<td>
|
||||
<%= link_to "Edit", edit_item_path(item), class: "icon icon-edit" %>
|
||||
<%= link_to "Delete", item_path(item),
|
||||
method: :delete,
|
||||
data: { confirm: "Are you sure?" },
|
||||
class: "icon icon-del" %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
5
app/views/items/new.html.erb
Normal file
5
app/views/items/new.html.erb
Normal file
@@ -0,0 +1,5 @@
|
||||
<h2>New Item</h2>
|
||||
|
||||
<%= render "form" %>
|
||||
|
||||
<%= link_to "Back", items_path %>
|
||||
26
app/views/items/show.html.erb
Normal file
26
app/views/items/show.html.erb
Normal file
@@ -0,0 +1,26 @@
|
||||
<h2><%= @item.name %></h2>
|
||||
|
||||
<p>
|
||||
<strong>SKU:</strong>
|
||||
<%= @item.sku %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Description:</strong>
|
||||
<%= @item.description %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Unit Price:</strong>
|
||||
<%= number_to_currency(@item.unit_price) %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Active:</strong>
|
||||
<%= @item.active ? "Yes" : "No" %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= link_to "Edit", edit_item_path(@item), class: "icon icon-edit" %>
|
||||
<%= link_to "Back", items_path %>
|
||||
</p>
|
||||
@@ -23,9 +23,10 @@
|
||||
</td>
|
||||
|
||||
<td data-label="<%= t :label_price %>">
|
||||
<%= f.number_field :unit_price,
|
||||
step: 0.01,
|
||||
<%= f.text_field :unit_price,
|
||||
class: "price-field",
|
||||
inputmode: "decimal",
|
||||
autocomplete: "off",
|
||||
no_label: true,
|
||||
disabled: readonly %>
|
||||
</td>
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
<br/>
|
||||
<%=t(:label_last_sync)%> </b> <%= Qbo.last_sync if Qbo.exists? %>
|
||||
</div>
|
||||
<%= link_to t(:label_sync_now), sync_items_path %>
|
||||
@@ -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 $("<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);
|
||||
};
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
33
assets/javascripts/blur.js
Normal file
33
assets/javascripts/blur.js
Normal file
@@ -0,0 +1,33 @@
|
||||
function evaluateMathExpression(expr) {
|
||||
if (!expr) return null;
|
||||
|
||||
// allow only digits, decimal, operators, parentheses, spaces
|
||||
if (!/^[0-9+\-*/().\s]+$/.test(expr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return Function('"use strict"; return (' + expr + ')')();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("blur", function(e) {
|
||||
|
||||
if (!e.target.classList.contains("price-field")) return;
|
||||
|
||||
const field = e.target;
|
||||
const value = field.value.trim();
|
||||
|
||||
const result = evaluateMathExpression(value);
|
||||
|
||||
if (result !== null && !isNaN(result)) {
|
||||
field.value = Number(result).toFixed(2);
|
||||
}
|
||||
|
||||
if (typeof updateLineItemTotals === "function") {
|
||||
updateLineItemTotals();
|
||||
}
|
||||
|
||||
}, true);
|
||||
@@ -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) {
|
||||
|
||||
|
||||
@@ -8,4 +8,9 @@
|
||||
#
|
||||
#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'
|
||||
resources :items do
|
||||
collection do
|
||||
get :autocomplete
|
||||
get :sync
|
||||
end
|
||||
end
|
||||
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
|
||||
4
init.rb
4
init.rb
@@ -14,7 +14,7 @@ Redmine::Plugin.register :redmine_qbo_lineitems do
|
||||
name 'Redmine QBO Line Items plugin'
|
||||
author 'Rick Barrette'
|
||||
description 'A plugin for Redmine to extend the capabilitys of the Redmine QuickBooks Online plugin to attach billable line items to an isuue'
|
||||
version '2026.3.4'
|
||||
version '2026.3.7'
|
||||
url 'https://github.com/rickbarrette/redmine_qbo_lineitems'
|
||||
author_url 'https://barrettefabrication.com'
|
||||
requires_redmine version_or_higher: '6.1.0'
|
||||
@@ -22,7 +22,7 @@ Redmine::Plugin.register :redmine_qbo_lineitems do
|
||||
|
||||
# Ensure redmine_qbo is installed
|
||||
begin
|
||||
requires_redmine_plugin :redmine_qbo, version_or_higher: '2026.3.5'
|
||||
requires_redmine_plugin :redmine_qbo, version_or_higher: '2026.3.6'
|
||||
rescue Redmine::PluginNotFound
|
||||
raise 'Please install the redmine_qbo plugin (https://github.com/rickbarrette/redmine_qbo)'
|
||||
end
|
||||
|
||||
@@ -19,6 +19,7 @@ module RedmineQboLineItems
|
||||
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),
|
||||
javascript_include_tag("blur", plugin: :redmine_qbo_lineitems),
|
||||
stylesheet_link_tag("line_items", plugin: :redmine_qbo_lineitems)
|
||||
])
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user