mirror of
https://github.com/rickbarrette/redmine_qbo_lineitems.git
synced 2026-04-02 15:11:58 -04:00
Compare commits
12 Commits
2026.3.6
...
a2cc7a97fd
| Author | SHA1 | Date | |
|---|---|---|---|
| a2cc7a97fd | |||
| 7088550184 | |||
| 2302679c3a | |||
| ec1ef72267 | |||
| 564158722d | |||
| 60a7f769ce | |||
| 1367937614 | |||
| 62adcb11cd | |||
| 896813983c | |||
| 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 |
|
||||
| --- | --- | --- |
|
||||
| 2026.3.5+ | 6.1.x | 3.2+ |
|
||||
| 2026.3.6+ | 6.1.x | 3.2+ |
|
||||
|
||||
---
|
||||
|
||||
@@ -114,4 +114,4 @@ Before using this plugin:
|
||||
>
|
||||
> 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.
|
||||
> 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.
|
||||
|
||||
BIN
Screenshots/Form.png
Normal file
BIN
Screenshots/Form.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 193 KiB |
BIN
Screenshots/Issue View.png
Normal file
BIN
Screenshots/Issue View.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 337 KiB |
@@ -12,6 +12,7 @@ class ItemsController < ApplicationController
|
||||
before_action :require_login
|
||||
before_action :find_item, only: [:show, :edit, :update, :destroy]
|
||||
|
||||
# Used for autocomplete form
|
||||
def autocomplete
|
||||
term = ActiveRecord::Base.sanitize_sql_like(params[:q].to_s)
|
||||
|
||||
@@ -43,7 +44,7 @@ class ItemsController < ApplicationController
|
||||
def edit
|
||||
end
|
||||
|
||||
def index
|
||||
def index
|
||||
@items = Item.order(:name)
|
||||
end
|
||||
|
||||
|
||||
@@ -46,7 +46,9 @@ class BillLineItemsJob < ActiveJob::Base
|
||||
|
||||
estimate = Quickbooks::Model::Estimate.new(customer_id: issue.customer.id)
|
||||
estimate_service = Quickbooks::Service::Estimate.new( company_id: qbo.realm_id, access_token: access_token)
|
||||
estimate.line_items << Quickbooks::Model::InvoiceLineItem.new(description: "#{I18n.t(:notice_added_from)}#{issue.id} #{issue.subject}", detail_type: 'DescriptionOnly' )
|
||||
memo = "Added from: #{issue.tracker} ##{issue.id}: #{issue.subject}"
|
||||
estimate.private_note = memo
|
||||
estimate.line_items << Quickbooks::Model::InvoiceLineItem.new(description: memo, detail_type: 'DescriptionOnly' )
|
||||
|
||||
unbilled_entries.each do |item|
|
||||
log "Creating Line Item for #{item.description}"
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
#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
|
||||
@@ -8,108 +8,36 @@
|
||||
#
|
||||
#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 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
|
||||
|
||||
qbo_sync push: true
|
||||
|
||||
# Updates Both local & remote DB description
|
||||
def description=(s)
|
||||
details
|
||||
@details.description = s
|
||||
details.description = s
|
||||
super
|
||||
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
|
||||
def name=(s)
|
||||
details
|
||||
@details.name = s
|
||||
details.name = s
|
||||
super
|
||||
end
|
||||
|
||||
# Updates Both local & remote DB sku
|
||||
def sku=(s)
|
||||
details
|
||||
@details.sku = s
|
||||
details.sku = 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)
|
||||
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
|
||||
def unit_price=(s)
|
||||
details
|
||||
@details.unit_price = s
|
||||
details.unit_price = s
|
||||
super
|
||||
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
|
||||
@@ -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.
|
||||
|
||||
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.
|
||||
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
|
||||
private
|
||||
|
||||
def build_qbo_item
|
||||
def build_qbo_remote
|
||||
log "Building new QBO Item"
|
||||
account = default_income_account
|
||||
log "Account: #{account.id} - #{account.name}"
|
||||
@@ -34,60 +28,9 @@ class ItemService
|
||||
|
||||
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
|
||||
)
|
||||
QboConnectionService.with_qbo_service(entity: Invoice) do |service|
|
||||
service.query("SELECT * FROM Account WHERE AccountType='Income' AND Name LIKE '%Sales%'").first
|
||||
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
|
||||
@@ -22,19 +22,7 @@ class ItemSyncService < SyncServiceBase
|
||||
20
|
||||
end
|
||||
|
||||
# Map relevant attributes from the QBO Employee to the local Employee model
|
||||
def process_attributes(local, remote)
|
||||
log "Processing Item ##{remote.id}"
|
||||
local.id = remote.id
|
||||
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)
|
||||
Rails.logger.info "[ItemSyncService] #{msg}"
|
||||
end
|
||||
|
||||
map_attribute :active, :active?
|
||||
map_attributes :description, :id, :name, :sku, :unit_price
|
||||
|
||||
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.6'
|
||||
version '2026.3.8'
|
||||
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.9'
|
||||
rescue Redmine::PluginNotFound
|
||||
raise 'Please install the redmine_qbo plugin (https://github.com/rickbarrette/redmine_qbo)'
|
||||
end
|
||||
|
||||
@@ -21,8 +21,8 @@ module RedmineQboLineItems
|
||||
|
||||
# Called by the QboSyncDispatcher
|
||||
def qbo_full_sync (context={})
|
||||
log "Adding ItemSyncJob to QBO sync dispatcher"
|
||||
return ItemSyncJob
|
||||
log "Adding Item to QBO sync dispatcher"
|
||||
return Item
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
Reference in New Issue
Block a user