mirror of
https://github.com/rickbarrette/redmine_qbo_lineitems.git
synced 2026-04-02 07:01:59 -04:00
Create estimate on issue close.
This commit is contained in:
70
app/jobs/bill_line_items_job.rb
Normal file
70
app/jobs/bill_line_items_job.rb
Normal file
@@ -0,0 +1,70 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2016 - 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 BillLineItemsJob < ActiveJob::Base
|
||||
queue_as :default
|
||||
retry_on StandardError, wait: 5.minutes, attempts: 5
|
||||
|
||||
# Perform billing of unbilled time entries for a given issue by creating corresponding TimeActivity records in QuickBooks Online, and then marking those entries as billed in Redmine. This job is typically triggered after an invoice is created or updated to ensure all relevant time is captured for billing.
|
||||
def perform(issue)
|
||||
|
||||
log "Starting billing for issue ##{issue.id}"
|
||||
issue.with_lock do
|
||||
unbilled_entries = issue.line_items.where(billed: [false, nil]).lock
|
||||
return if unbilled_entries.blank?
|
||||
|
||||
qbo = QboConnectionService.current!
|
||||
qbo.perform_authenticated_request do |access_token|
|
||||
create_estimate(issue, unbilled_entries, access_token, qbo)
|
||||
end
|
||||
|
||||
# Only mark billed AFTER successful QBO creation
|
||||
unbilled_entries.update_all(billed: true)
|
||||
end
|
||||
|
||||
log "Completed billing of for issue ##{issue.id}"
|
||||
Qbo.update_time_stamp
|
||||
rescue => e
|
||||
log "Billing failed for issue ##{issue.id} - #{e.message}"
|
||||
raise e
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Create TimeActivity records in QBO for each activity type with the appropriate hours and link them to the issue's assigned employee and customer
|
||||
def create_estimate(issue, unbilled_entries, access_token, qbo)
|
||||
log "Creating Estimate records in QBO for #{issue.customer.name} from issue ##{issue.id}"
|
||||
|
||||
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)
|
||||
|
||||
unbilled_entries.each do |item|
|
||||
log "Creating Line Item for #{item.description}"
|
||||
line = Quickbooks::Model::InvoiceLineItem.new
|
||||
line.amount = item.line_total
|
||||
line.description = item.description
|
||||
line.sales_item! do |detail|
|
||||
detail.unit_price = item.unit_price
|
||||
detail.quantity = item.quantity
|
||||
end
|
||||
|
||||
estimate.line_items << line
|
||||
end
|
||||
|
||||
e = estimate_service.create(estimate)
|
||||
log "Created estimate ##{e.doc_number}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def log(msg)
|
||||
Rails.logger.info "[BillLineItemsJob] #{msg}"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user