implemented some basic CRUD for QBO Items

This commit is contained in:
2026-03-11 23:39:24 -04:00
parent bfdf6b3065
commit 4f8e5d9b2c
9 changed files with 340 additions and 3 deletions

View File

@@ -15,12 +15,60 @@ class Item < ApplicationRecord
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
# Updates Both local & remote DB description
def description=(s)
details
@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
super
end
# Updates Both local & remote DB sku
def sku=(s)
details
@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)
@@ -31,10 +79,37 @@ class Item < ApplicationRecord
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
super
end
private
def log(msg)
Rails.logger.info "[LineItem] #{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