mirror of
https://github.com/rickbarrette/redmine_qbo_lineitems.git
synced 2026-04-02 07:01:59 -04:00
Compare commits
3 Commits
93ae77d9f6
...
89e3048c29
| Author | SHA1 | Date | |
|---|---|---|---|
| 89e3048c29 | |||
| a7899fb6b3 | |||
| ef7570fb3b |
@@ -21,7 +21,7 @@ This plugin allows **billable line items** to be attached to a Redmine issue. Wh
|
|||||||
|
|
||||||
| Plugin Version | Redmine Version | Ruby Version |
|
| Plugin Version | Redmine Version | Ruby Version |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| 2026.3.5+ | 6.1.x | 3.2+ |
|
| 2026.3.6+ | 6.1.x | 3.2+ |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -8,29 +8,13 @@
|
|||||||
#
|
#
|
||||||
#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.
|
||||||
|
|
||||||
class Item < ApplicationRecord
|
class Item < QboBaseModel
|
||||||
belongs_to :issue
|
belongs_to :issue
|
||||||
|
|
||||||
validates_presence_of :id, :description
|
validates_presence_of :id, :description
|
||||||
validates :unit_price, numericality: { greater_than_or_equal_to: 0 }
|
validates :unit_price, numericality: { greater_than_or_equal_to: 0 }
|
||||||
self.primary_key = :id
|
self.primary_key = :id
|
||||||
|
|
||||||
# Returns the details of the item. If the details have already been fetched, it returns the cached version. Otherwise, it fetches the details from QuickBooks Online and caches them for future use. This method is used to access the item's information in a way that minimizes unnecessary API calls to QBO, improving performance and reducing latency.
|
|
||||||
def details
|
|
||||||
@details ||= begin
|
|
||||||
xml = Rails.cache.fetch(details_cache_key, expires_in: 10.minutes) do
|
|
||||||
fetch_details.to_xml_ns
|
|
||||||
end
|
|
||||||
Quickbooks::Model::Item.from_xml(xml)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Generates a unique cache key for storing this customer's QBO details.
|
|
||||||
def details_cache_key
|
|
||||||
"item:#{id}:qbo_details:#{updated_at.to_i}"
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
# Updates Both local & remote DB description
|
# Updates Both local & remote DB description
|
||||||
def description=(s)
|
def description=(s)
|
||||||
details
|
details
|
||||||
@@ -38,23 +22,6 @@ class Item < ApplicationRecord
|
|||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
# 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))
|
|
||||||
end
|
|
||||||
|
|
||||||
# Magic Method
|
|
||||||
# Maps Get/Set methods to QBO item object
|
|
||||||
def method_missing(method_name, *args, &block)
|
|
||||||
if Quickbooks::Model::Item.method_defined?(method_name)
|
|
||||||
details
|
|
||||||
@details.public_send(method_name, *args, &block)
|
|
||||||
else
|
|
||||||
super
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Updates Both local & remote DB name
|
# Updates Both local & remote DB name
|
||||||
def name=(s)
|
def name=(s)
|
||||||
details
|
details
|
||||||
@@ -69,28 +36,6 @@ class Item < ApplicationRecord
|
|||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sync all items, typically triggered by a scheduled task or manual sync request
|
|
||||||
def self.sync
|
|
||||||
ItemSyncJob.perform_later(full_sync: true)
|
|
||||||
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)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Push the updates
|
|
||||||
def save_with_push
|
|
||||||
log "Starting push for item ##{self.id}..."
|
|
||||||
qbo = QboConnectionService.current!
|
|
||||||
ItemService.new(qbo: qbo, item: self).push()
|
|
||||||
Rails.cache.delete(details_cache_key)
|
|
||||||
save_without_push
|
|
||||||
end
|
|
||||||
|
|
||||||
alias_method :save_without_push, :save
|
|
||||||
alias_method :save, :save_with_push
|
|
||||||
|
|
||||||
# Updates Both local & remote DB price
|
# Updates Both local & remote DB price
|
||||||
def unit_price=(s)
|
def unit_price=(s)
|
||||||
details
|
details
|
||||||
@@ -98,18 +43,4 @@ class Item < ApplicationRecord
|
|||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def log(msg)
|
|
||||||
Rails.logger.info "[Item] #{msg}"
|
|
||||||
end
|
|
||||||
|
|
||||||
# Fetches the item's details from QuickBooks Online.
|
|
||||||
def fetch_details
|
|
||||||
log "Fetching details for item ##{id} from QBO..."
|
|
||||||
qbo = QboConnectionService.current!
|
|
||||||
ItemService.new(qbo: qbo, item: self).pull()
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -8,17 +8,11 @@
|
|||||||
#
|
#
|
||||||
#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.
|
||||||
|
|
||||||
class ItemService
|
class ItemService < ServiceBase
|
||||||
|
|
||||||
# Initializes the service with a QBO client and an optional item record. The QBO client is used to communicate with QuickBooks Online, while the item record contains the data that needs to be pushed to QBO. If no item is provided, the service will not perform any operations.
|
private
|
||||||
def initialize(qbo:, item: nil)
|
|
||||||
raise "No QBO configuration found" unless qbo
|
|
||||||
raise "Item record is required for push operation" unless item
|
|
||||||
@qbo = qbo
|
|
||||||
@item = item
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_qbo_item
|
def build_qbo_remote
|
||||||
log "Building new QBO Item"
|
log "Building new QBO Item"
|
||||||
account = default_income_account
|
account = default_income_account
|
||||||
log "Account: #{account.id} - #{account.name}"
|
log "Account: #{account.id} - #{account.name}"
|
||||||
@@ -44,50 +38,4 @@ class ItemService
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Pulls the Item data from QuickBooks Online.
|
|
||||||
def pull
|
|
||||||
return Quickbooks::Model::Item.new unless @item.present?
|
|
||||||
return build_qbo_item unless @item.id
|
|
||||||
log "Fetching details for item ##{@item.id} from QBO..."
|
|
||||||
qbo = QboConnectionService.current!
|
|
||||||
qbo.perform_authenticated_request do |access_token|
|
|
||||||
service = Quickbooks::Service::Item.new(
|
|
||||||
company_id: qbo.realm_id,
|
|
||||||
access_token: access_token
|
|
||||||
)
|
|
||||||
service.fetch_by_id(@item.id)
|
|
||||||
end
|
|
||||||
rescue => e
|
|
||||||
log "Fetch failed for #{@item.id}: #{e.message}"
|
|
||||||
build_qbo_item
|
|
||||||
end
|
|
||||||
|
|
||||||
# Pushes the Item data to QuickBooks Online. This method handles the communication with QBO, including authentication and error handling. It uses the QBO client to send the item data and logs the process for monitoring and debugging purposes. If the push is successful, it returns the item record; otherwise, it logs the error and returns false.
|
|
||||||
def push
|
|
||||||
log "Pushing item ##{@item.id} to QBO..."
|
|
||||||
|
|
||||||
item = @qbo.perform_authenticated_request do |access_token|
|
|
||||||
service = Quickbooks::Service::Item.new(
|
|
||||||
company_id: @qbo.realm_id,
|
|
||||||
access_token: access_token
|
|
||||||
)
|
|
||||||
if @item.id.present?
|
|
||||||
service.update(@item.details)
|
|
||||||
else
|
|
||||||
service.create(@item.details)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@item.id = item.id unless @item.persisted?
|
|
||||||
log "Push for item ##{@item.id} completed."
|
|
||||||
return @item
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
# Log messages with the entity type for better traceability
|
|
||||||
def log(msg)
|
|
||||||
Rails.logger.info "[ItemService] #{msg}"
|
|
||||||
end
|
|
||||||
|
|
||||||
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'
|
name 'Redmine QBO Line Items plugin'
|
||||||
author 'Rick Barrette'
|
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'
|
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.6'
|
version '2026.3.7'
|
||||||
url 'https://github.com/rickbarrette/redmine_qbo_lineitems'
|
url 'https://github.com/rickbarrette/redmine_qbo_lineitems'
|
||||||
author_url 'https://barrettefabrication.com'
|
author_url 'https://barrettefabrication.com'
|
||||||
requires_redmine version_or_higher: '6.1.0'
|
requires_redmine version_or_higher: '6.1.0'
|
||||||
@@ -22,7 +22,7 @@ Redmine::Plugin.register :redmine_qbo_lineitems do
|
|||||||
|
|
||||||
# Ensure redmine_qbo is installed
|
# Ensure redmine_qbo is installed
|
||||||
begin
|
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
|
rescue Redmine::PluginNotFound
|
||||||
raise 'Please install the redmine_qbo plugin (https://github.com/rickbarrette/redmine_qbo)'
|
raise 'Please install the redmine_qbo plugin (https://github.com/rickbarrette/redmine_qbo)'
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user