mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2025-11-08 17:04:23 -05:00
Broke billing of time into a seprate method
This commit is contained in:
@@ -30,4 +30,8 @@ class Qbo < ActiveRecord::Base
|
||||
def self.get_oauth_consumer
|
||||
return $qb_oauth_consumer
|
||||
end
|
||||
|
||||
def self.get_realm_id
|
||||
return first.realmId
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,20 +13,33 @@ class IssuesSaveHookListener < Redmine::Hook::ViewListener
|
||||
# New Issue Saved
|
||||
def controller_issues_edit_after_save(context={})
|
||||
issue = context[:issue]
|
||||
qbo = Qbo.first
|
||||
employee_id = User.find_by_id(issue.assigned_to_id).qbo_employee_id
|
||||
|
||||
# Check to see if we have registered with QBO
|
||||
if not qbo.nil? then
|
||||
unless Qbo.first.nil? then
|
||||
|
||||
# Prepare to create a new Time Activity
|
||||
time_service = Quickbooks::Service::TimeActivity.new(:company_id => qbo.realmId, :access_token => Qbo.get_auth_token)
|
||||
item_service = Quickbooks::Service::Item.new(:company_id => qbo.realmId, :access_token => Qbo.get_auth_token)
|
||||
time_entry = Quickbooks::Model::TimeActivity.new
|
||||
# If the issue is closed, then create a new billable time activty for the customer
|
||||
# TODO Add configuration settings for employee_id, hourly_rate, item_id
|
||||
if issue.status.is_closed? and not issue.qbo_customer_id.nil? and not issue.qbo_item_id.nil? and not employee_id.nil? then
|
||||
bill_time(issue, employee_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Create billable time entries
|
||||
def bill_time(issue, employee_id)
|
||||
|
||||
# Get unbilled time entries
|
||||
spent_time = issue.time_entries.where(qbo_billed: [false, nil])
|
||||
spent_hours ||= spent_time.sum(:hours) || 0
|
||||
|
||||
if spent_hours > 0 then
|
||||
|
||||
# Prepare to create a new Time Activity
|
||||
time_service = Quickbooks::Service::TimeActivity.new(:company_id => Qbo.get_realm_id, :access_token => Qbo.get_auth_token)
|
||||
item_service = Quickbooks::Service::Item.new(:company_id => Qbo.get_realm_id, :access_token => Qbo.get_auth_token)
|
||||
time_entry = Quickbooks::Model::TimeActivity.new
|
||||
|
||||
# Convert float spent time to hours and minutes
|
||||
hours = spent_hours.to_i
|
||||
minutesDecimal = (( spent_hours - hours) * 60)
|
||||
@@ -38,11 +51,6 @@ class IssuesSaveHookListener < Redmine::Hook::ViewListener
|
||||
entry.save
|
||||
end
|
||||
|
||||
employee_id = User.find_by_id(issue.assigned_to_id).qbo_employee_id
|
||||
|
||||
# If the issue is closed, then create a new billable time activty for the customer
|
||||
# TODO Add configuration settings for employee_id, hourly_rate, item_id
|
||||
if issue.status.is_closed? and not issue.qbo_customer_id.nil? and not issue.qbo_item_id.nil? and not employee_id.nil? and spent_hours > 0 then
|
||||
item = item_service.fetch_by_id issue.qbo_item_id
|
||||
time_entry.description = "#{issue.tracker} ##{issue.id}: #{issue.subject}"
|
||||
time_entry.employee_id = employee_id
|
||||
@@ -60,4 +68,3 @@ class IssuesSaveHookListener < Redmine::Hook::ViewListener
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user