mirror of
https://github.com/rickbarrette/redmine_qbo_lineitems.git
synced 2026-04-02 07:01:59 -04:00
Refactored to use QboBaseModel & ServiceBase from redmine_qbo
This commit is contained in:
@@ -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.
|
||||
|
||||
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
|
||||
|
||||
|
||||
# Updates Both local & remote DB description
|
||||
def description=(s)
|
||||
details
|
||||
@@ -38,23 +22,6 @@ class Item < ApplicationRecord
|
||||
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
|
||||
@@ -69,28 +36,6 @@ class Item < ApplicationRecord
|
||||
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
|
||||
@@ -98,18 +43,4 @@ class Item < ApplicationRecord
|
||||
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
|
||||
Reference in New Issue
Block a user