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
|
def self.get_oauth_consumer
|
||||||
return $qb_oauth_consumer
|
return $qb_oauth_consumer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.get_realm_id
|
||||||
|
return first.realmId
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -13,20 +13,33 @@ class IssuesSaveHookListener < Redmine::Hook::ViewListener
|
|||||||
# New Issue Saved
|
# New Issue Saved
|
||||||
def controller_issues_edit_after_save(context={})
|
def controller_issues_edit_after_save(context={})
|
||||||
issue = context[:issue]
|
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
|
# Check to see if we have registered with QBO
|
||||||
if not qbo.nil? then
|
unless Qbo.first.nil? then
|
||||||
|
|
||||||
|
# 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
|
# Prepare to create a new Time Activity
|
||||||
time_service = Quickbooks::Service::TimeActivity.new(:company_id => qbo.realmId, :access_token => Qbo.get_auth_token)
|
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.realmId, :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
|
time_entry = Quickbooks::Model::TimeActivity.new
|
||||||
|
|
||||||
# Get unbilled time entries
|
|
||||||
spent_time = issue.time_entries.where(qbo_billed: [false, nil])
|
|
||||||
spent_hours ||= spent_time.sum(:hours) || 0
|
|
||||||
|
|
||||||
# Convert float spent time to hours and minutes
|
# Convert float spent time to hours and minutes
|
||||||
hours = spent_hours.to_i
|
hours = spent_hours.to_i
|
||||||
minutesDecimal = (( spent_hours - hours) * 60)
|
minutesDecimal = (( spent_hours - hours) * 60)
|
||||||
@@ -38,26 +51,20 @@ class IssuesSaveHookListener < Redmine::Hook::ViewListener
|
|||||||
entry.save
|
entry.save
|
||||||
end
|
end
|
||||||
|
|
||||||
employee_id = User.find_by_id(issue.assigned_to_id).qbo_employee_id
|
item = item_service.fetch_by_id issue.qbo_item_id
|
||||||
|
time_entry.description = "#{issue.tracker} ##{issue.id}: #{issue.subject}"
|
||||||
# If the issue is closed, then create a new billable time activty for the customer
|
time_entry.employee_id = employee_id
|
||||||
# TODO Add configuration settings for employee_id, hourly_rate, item_id
|
time_entry.customer_id = issue.qbo_customer_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
|
time_entry.billable_status = "Billable"
|
||||||
item = item_service.fetch_by_id issue.qbo_item_id
|
time_entry.hours = hours
|
||||||
time_entry.description = "#{issue.tracker} ##{issue.id}: #{issue.subject}"
|
time_entry.minutes = minutes
|
||||||
time_entry.employee_id = employee_id
|
time_entry.name_of = "Employee"
|
||||||
time_entry.customer_id = issue.qbo_customer_id
|
time_entry.txn_date = Date.today
|
||||||
time_entry.billable_status = "Billable"
|
time_entry.hourly_rate = item.unit_price
|
||||||
time_entry.hours = hours
|
time_entry.item_id = issue.qbo_item_id
|
||||||
time_entry.minutes = minutes
|
time_entry.start_time = issue.start_date
|
||||||
time_entry.name_of = "Employee"
|
time_entry.end_time = Time.now
|
||||||
time_entry.txn_date = Date.today
|
time_service.create(time_entry)
|
||||||
time_entry.hourly_rate = item.unit_price
|
|
||||||
time_entry.item_id = issue.qbo_item_id
|
|
||||||
time_entry.start_time = issue.start_date
|
|
||||||
time_entry.end_time = Time.now
|
|
||||||
time_service.create(time_entry)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user