mirror of
https://github.com/rickbarrette/redmine_qbo_lineitems.git
synced 2026-04-02 15:11:58 -04:00
Added QBO Item support with autocomplete
This commit is contained in:
22
app/controllers/items_controller.rb
Normal file
22
app/controllers/items_controller.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
#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 ItemsController < ApplicationController
|
||||
before_action :require_login
|
||||
|
||||
def autocomplete
|
||||
term = params[:q].to_s
|
||||
items = Item.where("description LIKE ?", "%#{term}%").order(:description).limit(20)
|
||||
|
||||
render json: items.map { |i|
|
||||
{ id: i.id, text: i.description, price: i.unit_price }
|
||||
}
|
||||
end
|
||||
end
|
||||
36
app/jobs/item_sync_job.rb
Normal file
36
app/jobs/item_sync_job.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
#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 ItemSyncJob < ApplicationJob
|
||||
queue_as :default
|
||||
retry_on StandardError, wait: 5.minutes, attempts: 5
|
||||
|
||||
# Performs a sync of items from QuickBooks Online.
|
||||
def perform(full_sync: false, id: nil)
|
||||
qbo = QboConnectionService.current!
|
||||
raise "No QBO configuration found" unless qbo
|
||||
|
||||
log "Starting #{full_sync ? 'full' : 'incremental'} sync for item ##{id || 'all'}..."
|
||||
|
||||
service = ItemSyncService.new(qbo: qbo)
|
||||
|
||||
if id.present?
|
||||
service.sync_by_id(id)
|
||||
else
|
||||
service.sync(full_sync: full_sync)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def log(msg)
|
||||
Rails.logger.info "[ItemSyncJob] #{msg}"
|
||||
end
|
||||
end
|
||||
33
app/models/item.rb
Normal file
33
app/models/item.rb
Normal file
@@ -0,0 +1,33 @@
|
||||
#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 Item < ApplicationRecord
|
||||
belongs_to :issue
|
||||
|
||||
validates :description, presence: true
|
||||
validates :unit_price, numericality: { greater_than_or_equal_to: 0 }
|
||||
|
||||
# Sync all employees, typically triggered by a scheduled task or manual sync request
|
||||
def self.sync
|
||||
ItemSyncJob.perform_later(full_sync: true)
|
||||
end
|
||||
|
||||
# Sync a single employee by ID, typically triggered by a webhook notification or manual sync request
|
||||
def self.sync_by_id(id)
|
||||
ItemSyncJob.perform_later(id: id)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def log(msg)
|
||||
Rails.logger.info "[LineItem] #{msg}"
|
||||
end
|
||||
|
||||
end
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
class LineItem < ApplicationRecord
|
||||
belongs_to :issue
|
||||
belongs_to :item, optional: true
|
||||
|
||||
validates :description, presence: true
|
||||
validates :quantity, numericality: { greater_than: 0 }
|
||||
|
||||
32
app/services/item_sync_service.rb
Normal file
32
app/services/item_sync_service.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
#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 ItemSyncService < SyncServiceBase
|
||||
|
||||
private
|
||||
|
||||
# Specify the local model this service syncs
|
||||
def self.model_class
|
||||
Item
|
||||
end
|
||||
|
||||
# Determine if the remote entity should be deleted locally (e.g. if it's marked inactive in QBO)
|
||||
def destroy_remote?(remote)
|
||||
!remote.active?
|
||||
end
|
||||
|
||||
# Map relevant attributes from the QBO Employee to the local Employee model
|
||||
def process_attributes(local, remote)
|
||||
local.qbo_id = remote.id
|
||||
local.description = remote.description
|
||||
local.unit_price = remote.unit_price
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,10 +1,13 @@
|
||||
<tr class="line-item">
|
||||
<%= f.hidden_field :id %>
|
||||
<%= f.hidden_field :_destroy %>
|
||||
<%= f.hidden_field :item_id, class: "item-id-field" %>
|
||||
|
||||
<td data-label="<%= t :label_description %>">
|
||||
<%= f.text_field :description,
|
||||
placeholder: l(:label_description),
|
||||
class: "line-item-description",
|
||||
autocomplete: "off",
|
||||
no_label: true,
|
||||
disabled: readonly %>
|
||||
</td>
|
||||
|
||||
Reference in New Issue
Block a user