Refactored to use QboBaseModel & ServiceBase from redmine_qbo

This commit is contained in:
2026-03-12 11:23:07 -04:00
parent 93ae77d9f6
commit ef7570fb3b
2 changed files with 4 additions and 125 deletions

View File

@@ -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}"
@@ -44,50 +38,4 @@ class ItemService
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