diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
index 5248cd3..90fb791 100644
--- a/app/controllers/items_controller.rb
+++ b/app/controllers/items_controller.rb
@@ -23,7 +23,7 @@ class ItemsController < ApplicationController
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)
+ .merge(Item.sellable)
.order(:description)
.limit(20)
diff --git a/app/controllers/qbo_estimates_controller.rb b/app/controllers/qbo_estimates_controller.rb
index 92d5c88..fe8e17e 100644
--- a/app/controllers/qbo_estimates_controller.rb
+++ b/app/controllers/qbo_estimates_controller.rb
@@ -125,7 +125,7 @@ class QboEstimatesController < ApplicationController
def items
term = "%#{ActiveRecord::Base.sanitize_sql_like(params[:q].to_s.strip)}%"
- items = Item.where(active: true)
+ items = Item.sellable
.where("name LIKE :t OR sku LIKE :t OR description LIKE :t", t: term)
.order(:name)
.limit(25)
diff --git a/app/models/item.rb b/app/models/item.rb
index d9a5f87..6beed84 100644
--- a/app/models/item.rb
+++ b/app/models/item.rb
@@ -11,8 +11,16 @@
class Item < QboBaseModel
belongs_to :account, optional: true
- validates_presence_of :id, :description
+ # QuickBooks only requires a name: services such as Labor often have no description or rate
+ validates_presence_of :id, :name
validates :unit_price, numericality: { greater_than_or_equal_to: 0 }
+
+ # QuickBooks item types that can't go on an estimate or invoice line themselves
+ UNSELLABLE_TYPES = %w[Category Group].freeze
+
+ # Active products/services that can go on a line (services, non-inventory and inventory items), for the
+ # pickers and the labor products/services setting
+ scope :sellable, -> { where(active: true).where.not(type: UNSELLABLE_TYPES).or(where(active: true, type: nil)) }
self.primary_key = :id
self.inheritance_column = :_type_disabled
qbo_sync push: true
diff --git a/app/services/item_sync_service.rb b/app/services/item_sync_service.rb
index a963e52..17eb883 100644
--- a/app/services/item_sync_service.rb
+++ b/app/services/item_sync_service.rb
@@ -30,6 +30,10 @@ class ItemSyncService < SyncServiceBase
account_id = remote.income_account_ref&.value
account_id.present? ? Account.find_by(id: account_id.to_i) : nil
end
- map_attributes :description, :id, :name, :sku, :type, :unit_price
+ map_attributes :id, :name, :sku, :type
+ # Services such as Labor often have no description or rate in QuickBooks; they sync as blank and 0
+ # instead of failing validation and being skipped
+ map_attribute(:description) { |remote| remote.description.to_s }
+ map_attribute(:unit_price) { |remote| remote.unit_price || 0 }
end
\ No newline at end of file
diff --git a/app/views/qbo/_billing_settings.html.erb b/app/views/qbo/_billing_settings.html.erb
index 0cfe8cb..b507963 100644
--- a/app/views/qbo/_billing_settings.html.erb
+++ b/app/views/qbo/_billing_settings.html.erb
@@ -11,7 +11,7 @@
<%= hidden_field_tag 'settings[labor_item_ids][]', '', id: nil %>
<%= select_tag 'settings[labor_item_ids][]',
- options_from_collection_for_select(Item.where(active: true).order(:name), :id, :name,
+ options_from_collection_for_select(Item.sellable.order(:name), :id, :name,
RedmineQboBilling::LaborItems.item_ids),
multiple: true, size: 8, id: 'settings_labor_item_ids' %>
<%= l(:text_labor_items) %>
diff --git a/lib/redmine_qbo_billing/labor_items.rb b/lib/redmine_qbo_billing/labor_items.rb
index f481ff7..d71b411 100644
--- a/lib/redmine_qbo_billing/labor_items.rb
+++ b/lib/redmine_qbo_billing/labor_items.rb
@@ -55,7 +55,7 @@ module RedmineQboBilling
return chosen if chosen.any?
list = codes
- Item.where(active: true).pluck(:id, :name).select { |_, name| name_matches?(name, list) }.map { |id, _| id.to_s }
+ Item.sellable.pluck(:id, :name).select { |_, name| name_matches?(name, list) }.map { |id, _| id.to_s }
rescue StandardError
[]
end