mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-04-02 16:21:58 -04:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9fd7140e4a | |||
| a6c8923ea9 | |||
| 6fc8a18e93 | |||
| 8abc95c21e | |||
| 2bcb1840a4 | |||
| c87e18810b | |||
| eb6954ddf1 | |||
| be1a69217f | |||
| 99669f7baa | |||
| 29530e2c95 | |||
| beb4a66a93 | |||
| 40f7a3335c |
@@ -19,6 +19,7 @@ class Customer < QboBaseModel
|
|||||||
validates_presence_of :name
|
validates_presence_of :name
|
||||||
before_validation :normalize_phone_numbers
|
before_validation :normalize_phone_numbers
|
||||||
self.primary_key = :id
|
self.primary_key = :id
|
||||||
|
qbo_sync push: true
|
||||||
|
|
||||||
acts_as_searchable columns: %w[name phone_number mobile_phone_number ],
|
acts_as_searchable columns: %w[name phone_number mobile_phone_number ],
|
||||||
scope: ->(_context) { left_joins(:project) },
|
scope: ->(_context) { left_joins(:project) },
|
||||||
|
|||||||
@@ -13,5 +13,6 @@ class Employee < QboBaseModel
|
|||||||
has_many :users
|
has_many :users
|
||||||
validates_presence_of :id, :name
|
validates_presence_of :id, :name
|
||||||
self.primary_key = :id
|
self.primary_key = :id
|
||||||
|
qbo_sync push: false
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -14,6 +14,7 @@ class Estimate < QboBaseModel
|
|||||||
belongs_to :customer
|
belongs_to :customer
|
||||||
validates_presence_of :doc_number, :id
|
validates_presence_of :doc_number, :id
|
||||||
self.primary_key = :id
|
self.primary_key = :id
|
||||||
|
qbo_sync push: false
|
||||||
|
|
||||||
# returns a human readable string
|
# returns a human readable string
|
||||||
def to_s
|
def to_s
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ class Invoice < QboBaseModel
|
|||||||
validates :id, presence: true, uniqueness: true
|
validates :id, presence: true, uniqueness: true
|
||||||
validates :doc_number, :txn_date, presence: true
|
validates :doc_number, :txn_date, presence: true
|
||||||
self.primary_key = :id
|
self.primary_key = :id
|
||||||
|
qbo_sync push: false
|
||||||
|
|
||||||
# Return the invoice's document number as its string representation
|
# Return the invoice's document number as its string representation
|
||||||
def to_s
|
def to_s
|
||||||
|
|||||||
@@ -27,8 +27,9 @@ class Qbo < ActiveRecord::Base
|
|||||||
# Returns the last sync time formatted for display. If no sync has occurred, returns a default message.
|
# Returns the last sync time formatted for display. If no sync has occurred, returns a default message.
|
||||||
def self.last_sync
|
def self.last_sync
|
||||||
qbo = QboConnectionService.current!
|
qbo = QboConnectionService.current!
|
||||||
return I18n.t(:label_qbo_never_synced) unless qbo&.last_sync
|
|
||||||
format_time(qbo.last_sync)
|
format_time(qbo.last_sync)
|
||||||
|
rescue
|
||||||
|
return I18n.t(:label_qbo_never_synced)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ class QboBaseModel < ActiveRecord::Base
|
|||||||
|
|
||||||
self.abstract_class = true
|
self.abstract_class = true
|
||||||
validates_presence_of :id
|
validates_presence_of :id
|
||||||
|
class_attribute :qbo_push_enabled, default: true
|
||||||
|
attr_accessor :skip_qbo_push
|
||||||
|
before_validation :push_to_qbo, on: :create, if: :push_to_qbo?
|
||||||
|
after_commit :push_to_qbo, on: :update, if: :push_to_qbo?
|
||||||
|
|
||||||
# Returns the details of the entity.
|
# Returns the details of the entity.
|
||||||
# If the details have already been fetched, it returns the cached version.
|
# If the details have already been fetched, it returns the cached version.
|
||||||
@@ -24,8 +28,7 @@ class QboBaseModel < ActiveRecord::Base
|
|||||||
xml = Rails.cache.fetch(details_cache_key, expires_in: 10.minutes) do
|
xml = Rails.cache.fetch(details_cache_key, expires_in: 10.minutes) do
|
||||||
fetch_details.to_xml_ns
|
fetch_details.to_xml_ns
|
||||||
end
|
end
|
||||||
model_class = "Quickbooks::Model::#{model_name.name}".constantize
|
qbo_model_class.from_xml(xml)
|
||||||
model_class.from_xml(xml)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -34,7 +37,6 @@ class QboBaseModel < ActiveRecord::Base
|
|||||||
"#{model_name.name}:#{id}:qbo_details:#{updated_at.to_i}"
|
"#{model_name.name}:#{id}:qbo_details:#{updated_at.to_i}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# Returns the last sync time formatted for display.
|
# Returns the last sync time formatted for display.
|
||||||
# If no sync has occurred, returns a default message.
|
# If no sync has occurred, returns a default message.
|
||||||
def self.last_sync
|
def self.last_sync
|
||||||
@@ -45,8 +47,7 @@ class QboBaseModel < ActiveRecord::Base
|
|||||||
# Magic Method
|
# Magic Method
|
||||||
# Maps Get/Set methods to QBO entity object
|
# Maps Get/Set methods to QBO entity object
|
||||||
def method_missing(method_name, *args, &block)
|
def method_missing(method_name, *args, &block)
|
||||||
model_class = "Quickbooks::Model::#{model_name.name}".constantize
|
if qbo_model_class.method_defined?(method_name)
|
||||||
if model_class.method_defined?(method_name)
|
|
||||||
details
|
details
|
||||||
@details.public_send(method_name, *args, &block)
|
@details.public_send(method_name, *args, &block)
|
||||||
else
|
else
|
||||||
@@ -54,11 +55,17 @@ class QboBaseModel < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Repsonds to missing methods by delegating to the QBO customer details object if the method is defined there.
|
def push_to_qbo?
|
||||||
|
log "qbo_push_enabled #{self.class.qbo_push_enabled}"
|
||||||
|
log "skip_qbo_push #{skip_qbo_push}"
|
||||||
|
|
||||||
|
self.class.qbo_push_enabled && skip_qbo_push != true
|
||||||
|
end
|
||||||
|
|
||||||
|
# Repsonds to missing methods by delegating to the QBO entity calss if the method is defined there.
|
||||||
# This allows for dynamic access to any attributes or methods of the QBO customer without having to explicitly define them in the Subclass model, providing flexibility and reducing boilerplate code.
|
# This allows for dynamic access to any attributes or methods of the QBO customer without having to explicitly define them in the Subclass model, providing flexibility and reducing boilerplate code.
|
||||||
def respond_to_missing?(method_name, include_private = false)
|
def respond_to_missing?(method_name, include_private = false)
|
||||||
model_class = "Quickbooks::Model::#{model_name.name}".constantize
|
qbo_model_class.method_defined?(method_name) || super
|
||||||
model_class.method_defined?(method_name) || super
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sync all entities, typically triggered by a scheduled task or manual sync request
|
# Sync all entities, typically triggered by a scheduled task or manual sync request
|
||||||
@@ -73,21 +80,19 @@ class QboBaseModel < ActiveRecord::Base
|
|||||||
job.perform_later(id: id)
|
job.perform_later(id: id)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Push the updates
|
# Flag used to update local without pushing to QBO.
|
||||||
def save_with_push
|
# This is used to prevent loops with the webhook
|
||||||
log "Starting push for #{model_name.name} ##{self.id}..."
|
def skip_qbo_push?
|
||||||
qbo = QboConnectionService.current!
|
!!skip_qbo_push
|
||||||
service = "#{model_name.name}Service".constantize
|
|
||||||
service.new(qbo: qbo, remote: self).push()
|
|
||||||
Rails.cache.delete(details_cache_key)
|
|
||||||
save_without_push
|
|
||||||
end
|
end
|
||||||
|
|
||||||
alias_method :save_without_push, :save
|
def self.qbo_sync(push: true)
|
||||||
alias_method :save, :save_with_push
|
self.qbo_push_enabled = push
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
# Log messages with a standarized prefix
|
||||||
def log(msg)
|
def log(msg)
|
||||||
Rails.logger.info "[#{model_name.name}] #{msg}"
|
Rails.logger.info "[#{model_name.name}] #{msg}"
|
||||||
end
|
end
|
||||||
@@ -96,8 +101,26 @@ class QboBaseModel < ActiveRecord::Base
|
|||||||
def fetch_details
|
def fetch_details
|
||||||
log "Fetching details for #{model_name.name} ##{id} from QBO..."
|
log "Fetching details for #{model_name.name} ##{id} from QBO..."
|
||||||
qbo = QboConnectionService.current!
|
qbo = QboConnectionService.current!
|
||||||
service_class = "#{model_name.name}Service".constantize
|
service_class.new(qbo: qbo, local: self).pull()
|
||||||
service_class.new(qbo: qbo, remote: self).pull()
|
end
|
||||||
|
|
||||||
|
# Pushs the entity's details from QuickBooks Online.
|
||||||
|
def push_to_qbo
|
||||||
|
log "Starting push for #{model_name.name} ##{id}..."
|
||||||
|
qbo = QboConnectionService.current!
|
||||||
|
reslut = service_class.new(qbo: qbo, local: self).push
|
||||||
|
Rails.cache.delete(details_cache_key)
|
||||||
|
return reslut
|
||||||
|
end
|
||||||
|
|
||||||
|
# Dynamically get the Quickbooks Model Class
|
||||||
|
def qbo_model_class
|
||||||
|
@qbo_model_class ||= "Quickbooks::Model::#{model_name.name}".constantize
|
||||||
|
end
|
||||||
|
|
||||||
|
# Dynamically get the Service Class
|
||||||
|
def service_class
|
||||||
|
@service_class ||= "#{model_name.name}Service".constantize
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -17,16 +17,13 @@ class CustomerSyncService < SyncServiceBase
|
|||||||
Customer
|
Customer
|
||||||
end
|
end
|
||||||
|
|
||||||
# Determine if the remote entity should be deleted locally (e.g. if it's marked inactive in QBO)
|
# Determine if the local entity should be deleted (e.g. if it's marked inactive in QBO)
|
||||||
def destroy_remote?(remote)
|
def destroy_local?(remote)
|
||||||
!remote.active?
|
!remote.active?
|
||||||
end
|
end
|
||||||
|
|
||||||
# Map relevant attributes from the QBO Customer to the local Customer model
|
map_attribute :name, :display_name
|
||||||
def process_attributes(local, remote)
|
map_phone :phone_number, :primary_phone
|
||||||
local.name = remote.display_name
|
map_phone :mobile_phone_number, :mobile_phone
|
||||||
local.phone_number = remote.primary_phone&.free_form_number&.gsub(/\D/, '')
|
|
||||||
local.mobile_phone_number = remote.mobile_phone&.free_form_number&.gsub(/\D/, '')
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -17,14 +17,11 @@ class EmployeeSyncService < SyncServiceBase
|
|||||||
Employee
|
Employee
|
||||||
end
|
end
|
||||||
|
|
||||||
# Determine if the remote entity should be deleted locally (e.g. if it's marked inactive in QBO)
|
# Determine if the local entity should be deleted (e.g. if it's marked inactive in QBO)
|
||||||
def destroy_remote?(remote)
|
def destroy_local?(remote)
|
||||||
!remote.active?
|
!remote.active?
|
||||||
end
|
end
|
||||||
|
|
||||||
# Map relevant attributes from the QBO Employee to the local Employee model
|
map_attribute :name, :display_name
|
||||||
def process_attributes(local, remote)
|
|
||||||
local.name = remote.display_name
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -17,11 +17,7 @@ class EstimateSyncService < SyncServiceBase
|
|||||||
Estimate
|
Estimate
|
||||||
end
|
end
|
||||||
|
|
||||||
# Map relevant attributes from the QBO Estimate to the local Estimate model
|
map_attributes :doc_number, :txn_date
|
||||||
def process_attributes(local, remote)
|
map_belongs_to :customer
|
||||||
local.doc_number = remote.doc_number
|
|
||||||
local.txn_date = remote.txn_date
|
|
||||||
local.customer = Customer.find_by(id: remote.customer_ref&.value)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -17,19 +17,14 @@ class InvoiceSyncService < SyncServiceBase
|
|||||||
Invoice
|
Invoice
|
||||||
end
|
end
|
||||||
|
|
||||||
# Map relevant attributes from the QBO Invoice to the local Invoice model
|
|
||||||
def process_attributes(local, remote)
|
|
||||||
local.doc_number = remote.doc_number
|
|
||||||
local.txn_date = remote.txn_date
|
|
||||||
local.due_date = remote.due_date
|
|
||||||
local.total_amount = remote.total
|
|
||||||
local.balance = remote.balance
|
|
||||||
local.qbo_updated_at = remote.meta_data&.last_updated_time
|
|
||||||
local.customer = Customer.find_by(id: remote.customer_ref&.value)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Attach QBO Invoices to the local Issues
|
# Attach QBO Invoices to the local Issues
|
||||||
def attach_documents(local, remote)
|
def attach_documents(local, remote)
|
||||||
InvoiceAttachmentService.new(local, remote).attach
|
InvoiceAttachmentService.new(local, remote).attach
|
||||||
end
|
end
|
||||||
|
|
||||||
|
map_attributes :balance, :doc_number, :due_date, :txn_date
|
||||||
|
map_attribute :total_amount, :total
|
||||||
|
map_attribute :qbo_updated_at, "meta_data.last_updated_time"
|
||||||
|
map_belongs_to :customer
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -10,22 +10,17 @@
|
|||||||
|
|
||||||
class ServiceBase
|
class ServiceBase
|
||||||
|
|
||||||
# Subclasses should Initializes the service with a QBO client and an optional remote record.
|
# Subclasses should Initializes the service with a QBO client and a local record.
|
||||||
# The QBO client is used to communicate with QuickBooks Online, while the local record contains the data that needs to be pushed to QBO.
|
# The QBO client is used to communicate with QuickBooks Online, while the local record contains the data that needs to be pushed to QBO.
|
||||||
# If no remote is provided, the service will not perform any operations.
|
# If no local is provided, the service will not perform any operations.
|
||||||
def initialize(qbo:, remote: nil)
|
def initialize(qbo:, local: nil)
|
||||||
|
@entity = local.class.name
|
||||||
raise "No QBO configuration found" unless qbo
|
raise "No QBO configuration found" unless qbo
|
||||||
raise "#{@entity} record is required for push operation" unless remote
|
raise "#{@entity} record is required for push operation" unless local
|
||||||
@qbo = qbo
|
@qbo = qbo
|
||||||
@entity = remote.class.name
|
@local = local
|
||||||
@remote = remote
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# # Subclasses must implement this to specify which local model they sync (e.g. Customer, Invoice)
|
|
||||||
# def self.model_class
|
|
||||||
# raise NotImplementedError
|
|
||||||
# end
|
|
||||||
|
|
||||||
# Subclasses must implement this to build a new QBO entity if a remote is not found
|
# Subclasses must implement this to build a new QBO entity if a remote is not found
|
||||||
def build_qbo_remote
|
def build_qbo_remote
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
@@ -33,50 +28,55 @@ class ServiceBase
|
|||||||
|
|
||||||
# Pulls the Item data from QuickBooks Online.
|
# Pulls the Item data from QuickBooks Online.
|
||||||
def pull
|
def pull
|
||||||
return build_qbo_remote unless @remote.present?
|
return build_qbo_remote unless @local.present?
|
||||||
return build_qbo_remote unless @remote.id
|
return build_qbo_remote unless @local.id
|
||||||
log "Fetching details for #{@entity} ##{@remote.id} from QBO..."
|
log "Fetching details for #{@entity} ##{@local.id} from QBO..."
|
||||||
qbo = QboConnectionService.current!
|
with_qbo_service do |service|
|
||||||
qbo.perform_authenticated_request do |access_token|
|
service.fetch_by_id(@local.id)
|
||||||
service_class = "Quickbooks::Service::#{@entity}".constantize
|
|
||||||
service = service_class.new(
|
|
||||||
company_id: qbo.realm_id,
|
|
||||||
access_token: access_token
|
|
||||||
)
|
|
||||||
service.fetch_by_id(@remote.id)
|
|
||||||
end
|
end
|
||||||
rescue => e
|
rescue => e
|
||||||
log "Fetch failed for #{@remote.id}: #{e.message}"
|
log "Fetch failed for #{@local.id}: #{e.message}"
|
||||||
build_qbo_remote
|
build_qbo_remote
|
||||||
end
|
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 remote data and logs the process for monitoring and debugging purposes. If the push is successful, it returns the remote record; otherwise, it logs the error and returns false.
|
# 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 remote data and logs the process for monitoring and debugging purposes.
|
||||||
|
# If the push is successful, it returns the remote record; otherwise, it logs the error and returns false.
|
||||||
def push
|
def push
|
||||||
log "Pushing #{@entity} ##{@remote.id} to QBO..."
|
log "Pushing #{@entity} ##{@local.id} to QBO..."
|
||||||
|
remote = with_qbo_service do |service|
|
||||||
remote = @qbo.perform_authenticated_request do |access_token|
|
if @local.id.present?
|
||||||
service_class = "Quickbooks::Service::#{@entity}".constantize
|
log "Updating #{@entity}"
|
||||||
service = service_class.new(
|
service.update(@local.details)
|
||||||
company_id: @qbo.realm_id,
|
|
||||||
access_token: access_token
|
|
||||||
)
|
|
||||||
if @remote.id.present?
|
|
||||||
service.update(@remote.details)
|
|
||||||
else
|
else
|
||||||
service.create(@remote.details)
|
log "Creating #{@entity}"
|
||||||
|
service.create(@local.details)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@local.id = remote.id unless @local.persisted?
|
||||||
@remote.id = remote.id unless @remote.persisted?
|
log "Push for remote ##{@local.id} completed."
|
||||||
log "Push for remote ##{@remote.id} completed."
|
@local
|
||||||
return @remote
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
# Performs authenticaed requests with QBO service
|
||||||
|
def with_qbo_service
|
||||||
|
@qbo.perform_authenticated_request do |access_token|
|
||||||
|
service = service_class.new( company_id: @qbo.realm_id, access_token: access_token )
|
||||||
|
yield service
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Log messages with the entity type for better traceability
|
# Log messages with the entity type for better traceability
|
||||||
def log(msg)
|
def log(msg)
|
||||||
Rails.logger.info "[#{@entity}Service] #{msg}"
|
Rails.logger.info "[#{@entity}Service] #{msg}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Dynamically get the Quickbooks Service Class
|
||||||
|
def service_class
|
||||||
|
@service_class ||= "Quickbooks::Service::#{@entity}".constantize
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
@@ -32,17 +32,11 @@ class SyncServiceBase
|
|||||||
# Sync all entities, or only those updated since the last sync
|
# Sync all entities, or only those updated since the last sync
|
||||||
def sync(full_sync: false)
|
def sync(full_sync: false)
|
||||||
log "Starting #{full_sync ? 'full' : 'incremental'} #{@entity.name} sync with page size of: #{@page_size}"
|
log "Starting #{full_sync ? 'full' : 'incremental'} #{@entity.name} sync with page size of: #{@page_size}"
|
||||||
|
with_qbo_service do |service|
|
||||||
@qbo.perform_authenticated_request do |access_token|
|
|
||||||
service_class = "Quickbooks::Service::#{@entity.name}".constantize
|
|
||||||
service = service_class.new(company_id: @qbo.realm_id, access_token: access_token)
|
|
||||||
|
|
||||||
query = build_query(full_sync)
|
query = build_query(full_sync)
|
||||||
|
|
||||||
service.query_in_batches(query, per_page: @page_size) do |batch|
|
service.query_in_batches(query, per_page: @page_size) do |batch|
|
||||||
entries = Array(batch)
|
entries = Array(batch)
|
||||||
log "Processing batch of #{entries.size} #{@entity.name}"
|
log "Processing batch of #{entries.size} #{@entity.name}"
|
||||||
|
|
||||||
entries.each do |remote|
|
entries.each do |remote|
|
||||||
persist(remote)
|
persist(remote)
|
||||||
end
|
end
|
||||||
@@ -55,11 +49,7 @@ class SyncServiceBase
|
|||||||
# Sync a single entity by its QBO ID (webhook usage)
|
# Sync a single entity by its QBO ID (webhook usage)
|
||||||
def sync_by_id(id)
|
def sync_by_id(id)
|
||||||
log "Syncing #{@entity.name} with ID #{id}"
|
log "Syncing #{@entity.name} with ID #{id}"
|
||||||
|
with_qbo_service do |service|
|
||||||
@qbo.perform_authenticated_request do |access_token|
|
|
||||||
service_class = "Quickbooks::Service::#{@entity.name}".constantize
|
|
||||||
service = service_class.new(company_id: @qbo.realm_id, access_token: access_token)
|
|
||||||
|
|
||||||
remote = service.fetch_by_id(id)
|
remote = service.fetch_by_id(id)
|
||||||
persist(remote)
|
persist(remote)
|
||||||
end
|
end
|
||||||
@@ -67,6 +57,10 @@ class SyncServiceBase
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def attach_documents(local, remote)
|
||||||
|
# Override in subclasses if the entity has attachments (e.g. Invoice)
|
||||||
|
end
|
||||||
|
|
||||||
# Builds a QBO query for retrieving entities
|
# Builds a QBO query for retrieving entities
|
||||||
def build_query(full_sync)
|
def build_query(full_sync)
|
||||||
if full_sync
|
if full_sync
|
||||||
@@ -82,13 +76,125 @@ class SyncServiceBase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def attach_documents(local, remote)
|
# Determine if a remote entity should be deleted locally (e.g. if it's marked inactive in QBO)
|
||||||
# Override in subclasses if the entity has attachments (e.g. Invoice)
|
def destroy_local?(remote)
|
||||||
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
# Determine if a remote entity should be deleted locally (e.g. if it's marked inactive in QBO)
|
def extract_value(remote, remote_attr)
|
||||||
def destroy_remote?(remote)
|
case remote_attr
|
||||||
false
|
when Proc
|
||||||
|
remote_attr.call(remote)
|
||||||
|
else
|
||||||
|
remote.public_send(remote_attr)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Attribute Mapping DSL
|
||||||
|
#
|
||||||
|
# This DSL defines how attributes from a QuickBooks Online (QBO) entity
|
||||||
|
# are mapped onto a local ActiveRecord model during synchronization.
|
||||||
|
#
|
||||||
|
# Each mapping registers a lambda in `attribute_map`. When a remote QBO
|
||||||
|
# object is processed, the lambda is executed to extract and transform
|
||||||
|
# the value that will be assigned to the local model attribute.
|
||||||
|
#
|
||||||
|
# The DSL supports several mapping patterns:
|
||||||
|
#
|
||||||
|
# 1. Direct attribute mapping (same name)
|
||||||
|
#
|
||||||
|
# map_attribute :doc_number
|
||||||
|
#
|
||||||
|
# Equivalent to:
|
||||||
|
#
|
||||||
|
# local.doc_number = remote.doc_number
|
||||||
|
#
|
||||||
|
# 2. Renamed attribute mapping
|
||||||
|
#
|
||||||
|
# map_attribute :total_amount, :total
|
||||||
|
#
|
||||||
|
# Equivalent to:
|
||||||
|
#
|
||||||
|
# local.total_amount = remote.total
|
||||||
|
#
|
||||||
|
# 3. Custom transformation logic
|
||||||
|
#
|
||||||
|
# map_attribute :qbo_updated_at do |remote|
|
||||||
|
# remote.meta_data&.last_updated_time
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# Useful for nested fields or computed values.
|
||||||
|
#
|
||||||
|
# 4. Bulk attribute mapping
|
||||||
|
#
|
||||||
|
# map_attributes :doc_number, :txn_date, :due_date
|
||||||
|
#
|
||||||
|
# Convenience helper that maps multiple attributes with identical names.
|
||||||
|
#
|
||||||
|
# 5. Foreign key / reference mapping
|
||||||
|
#
|
||||||
|
# map_belongs_to :customer
|
||||||
|
#
|
||||||
|
# Resolves a QBO reference object (e.g. `customer_ref.value`) and finds
|
||||||
|
# the associated local ActiveRecord model.
|
||||||
|
#
|
||||||
|
# 6. Specialized helpers
|
||||||
|
#
|
||||||
|
# map_phone :phone_number, :primary_phone
|
||||||
|
#
|
||||||
|
# Extracts and normalizes phone numbers by stripping non-digit characters.
|
||||||
|
#
|
||||||
|
# Internally, the mappings are stored in `attribute_map` and executed by the
|
||||||
|
# SyncService during `process_attributes`, which iterates through each mapping
|
||||||
|
# and assigns the computed value to the local record.
|
||||||
|
#
|
||||||
|
# This design keeps synchronization services declarative, readable, and easy
|
||||||
|
# to extend while centralizing transformation logic in a single DSL.
|
||||||
|
class << self
|
||||||
|
|
||||||
|
def map_attributes(*attrs)
|
||||||
|
attrs.each do |attr|
|
||||||
|
map_attribute(attr)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def map_attribute(local_attr, remote_attr = nil, &block)
|
||||||
|
attribute_map[local_attr] =
|
||||||
|
if block_given?
|
||||||
|
block
|
||||||
|
elsif remote_attr
|
||||||
|
->(remote) do
|
||||||
|
remote_attr.to_s.split('.').reduce(remote) do |obj, method|
|
||||||
|
obj&.public_send(method)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
->(remote) { remote.public_send(local_attr) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def attribute_map
|
||||||
|
@attribute_map ||= {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def map_belongs_to(local_attr, ref: nil, model: nil)
|
||||||
|
ref ||= "#{local_attr}_ref"
|
||||||
|
model ||= local_attr.to_s.classify.constantize
|
||||||
|
|
||||||
|
attribute_map[local_attr] = lambda do |remote|
|
||||||
|
ref_obj = remote.public_send(ref)
|
||||||
|
id = ref_obj&.value
|
||||||
|
id ? model.find_by(id: id) : nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def map_phone(local_attr, remote_attr)
|
||||||
|
attribute_map[local_attr] = lambda do |remote|
|
||||||
|
phone = remote.public_send(remote_attr)
|
||||||
|
phone&.free_form_number&.gsub(/\D/, '')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Log messages with the entity type for better traceability
|
# Log messages with the entity type for better traceability
|
||||||
@@ -100,7 +206,7 @@ class SyncServiceBase
|
|||||||
def persist(remote)
|
def persist(remote)
|
||||||
local = @entity.find_or_initialize_by(id: remote.id)
|
local = @entity.find_or_initialize_by(id: remote.id)
|
||||||
|
|
||||||
if destroy_remote?(remote)
|
if destroy_local?(remote)
|
||||||
if local.persisted?
|
if local.persisted?
|
||||||
local.destroy
|
local.destroy
|
||||||
log "Deleted #{@entity.name} #{remote.id}"
|
log "Deleted #{@entity.name} #{remote.id}"
|
||||||
@@ -110,9 +216,11 @@ class SyncServiceBase
|
|||||||
|
|
||||||
process_attributes(local, remote)
|
process_attributes(local, remote)
|
||||||
|
|
||||||
if local.changed?
|
if local.new_record? || local.changed?
|
||||||
|
was_new = local.new_record?
|
||||||
|
local.skip_qbo_push = true
|
||||||
local.save!
|
local.save!
|
||||||
log "Updated #{@entity.name} #{remote.id}"
|
log "#{was_new ? 'Created' : 'Updated'} #{@entity.name} #{remote.id}"
|
||||||
attach_documents(local, remote)
|
attach_documents(local, remote)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -120,8 +228,29 @@ class SyncServiceBase
|
|||||||
log "Failed to sync #{@entity.name} #{remote.id}: #{e.message}"
|
log "Failed to sync #{@entity.name} #{remote.id}: #{e.message}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# This method should be implemented in subclasses to map remote attributes to local model
|
# Maps remote attributes to local model
|
||||||
def process_attributes(local, remote)
|
def process_attributes(local, remote)
|
||||||
raise NotImplementedError, "Subclasses must implement process_attributes"
|
log "Processing #{@entity} ##{remote.id}"
|
||||||
|
|
||||||
|
self.class.attribute_map.each do |local_attr, mapper|
|
||||||
|
value = mapper.call(remote)
|
||||||
|
|
||||||
|
if local.public_send(local_attr) != value
|
||||||
|
local.public_send("#{local_attr}=", value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Dynamically get the Quickbooks Service Class
|
||||||
|
def service_class
|
||||||
|
@service_class ||= "Quickbooks::Service::#{@entity}".constantize
|
||||||
|
end
|
||||||
|
|
||||||
|
# Performs authenticaed requests with QBO service
|
||||||
|
def with_qbo_service
|
||||||
|
@qbo.perform_authenticated_request do |access_token|
|
||||||
|
service = service_class.new( company_id: @qbo.realm_id, access_token: access_token )
|
||||||
|
yield service
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
2
init.rb
2
init.rb
@@ -14,7 +14,7 @@ Redmine::Plugin.register :redmine_qbo do
|
|||||||
name 'Redmine QBO plugin'
|
name 'Redmine QBO plugin'
|
||||||
author 'Rick Barrette'
|
author 'Rick Barrette'
|
||||||
description 'A pluging for Redmine to connect with QuickBooks Online to create Time Activity Entries for billable hours logged when an Issue is closed'
|
description 'A pluging for Redmine to connect with QuickBooks Online to create Time Activity Entries for billable hours logged when an Issue is closed'
|
||||||
version '2026.3.6'
|
version '2026.3.8'
|
||||||
url 'https://github.com/rickbarrette/redmine_qbo'
|
url 'https://github.com/rickbarrette/redmine_qbo'
|
||||||
author_url 'https://barrettefabrication.com'
|
author_url 'https://barrettefabrication.com'
|
||||||
settings default: {empty: true}, partial: 'qbo/settings'
|
settings default: {empty: true}, partial: 'qbo/settings'
|
||||||
|
|||||||
Reference in New Issue
Block a user