Added TimeActivty Creation

A new Time Activity is now created when an issue is closed.
This commit is contained in:
2016-01-02 10:05:02 -05:00
parent 6ef89d3e45
commit f8ff28f955
3 changed files with 65 additions and 19 deletions

View File

@@ -3,3 +3,6 @@ source 'https://rubygems.org'
gem 'quickbooks-ruby'#, :git => 'https://github.com/ruckus/quickbooks-ruby.git' gem 'quickbooks-ruby'#, :git => 'https://github.com/ruckus/quickbooks-ruby.git'
gem 'oauth-plugin'#, '~> 0.5.1' gem 'oauth-plugin'#, '~> 0.5.1'
gem 'oauth'
gem 'roxml'
gem 'nokogiri'

View File

@@ -11,20 +11,24 @@
class QboController < ApplicationController class QboController < ApplicationController
unloadable unloadable
# Load OAuth Token
QB_KEY = Setting.plugin_redmine_qbo['settingsOAuthConsumerKey'] QB_KEY = Setting.plugin_redmine_qbo['settingsOAuthConsumerKey']
QB_SECRET = Setting.plugin_redmine_qbo['settingsOAuthConsumerSecret'] QB_SECRET = Setting.plugin_redmine_qbo['settingsOAuthConsumerSecret']
$qb_oauth_consumer = OAuth::Consumer.new(QB_KEY, QB_SECRET, { $qb_oauth_consumer = OAuth::Consumer.new(QB_KEY, QB_SECRET, {
:site => "https://oauth.intuit.com", :site => "https://oauth.intuit.com",
:request_token_path => "/oauth/v1/get_request_token", :request_token_path => "/oauth/v1/get_request_token",
:authorize_url => "https://appcenter.intuit.com/Connect/Begin", :authorize_url => "https://appcenter.intuit.com/Connect/Begin",
:access_token_path => "/oauth/v1/get_access_token" :access_token_path => "/oauth/v1/get_access_token"
}) })
def index def index
end end
#
# Called when the user requests that Redmine to connect to QBO
#
def authenticate def authenticate
callback = "https://rickbarrette.org/redmine/oauth_callback" callback = "https://rickbarrette.org/redmine/oauth_callback"
token = $qb_oauth_consumer.get_request_token(:oauth_callback => callback) token = $qb_oauth_consumer.get_request_token(:oauth_callback => callback)
@@ -32,6 +36,9 @@ class QboController < ApplicationController
redirect_to("https://appcenter.intuit.com/Connect/Begin?oauth_token=#{token.token}") and return redirect_to("https://appcenter.intuit.com/Connect/Begin?oauth_token=#{token.token}") and return
end end
#
# Called by QBO after authentication has been processed
#
def oauth_callback def oauth_callback
at = session[:qb_request_token].get_access_token(:oauth_verifier => params[:oauth_verifier]) at = session[:qb_request_token].get_access_token(:oauth_verifier => params[:oauth_verifier])
token = at.token token = at.token
@@ -41,6 +48,7 @@ class QboController < ApplicationController
#There can only be one... #There can only be one...
Qbo.destroy_all Qbo.destroy_all
# Save the authentication information
qbo = Qbo.new qbo = Qbo.new
qbo.token = token qbo.token = token
qbo.secret = secret qbo.secret = secret
@@ -55,6 +63,9 @@ class QboController < ApplicationController
end end
#
# Synchronizes the QboCustomer table with QBO
#
def sync def sync
if Qbo.exists? then if Qbo.exists? then
qbo = Qbo.first qbo = Qbo.first

View File

@@ -23,8 +23,6 @@ class QboHookListener < Redmine::Hook::ViewListener
# Generate the drop down list of quickbooks contacts # Generate the drop down list of quickbooks contacts
select = context[:form].select :qbo_customer_id, QboCustomers.all.pluck(:name, :id), :selected => selected, include_blank: true select = context[:form].select :qbo_customer_id, QboCustomers.all.pluck(:name, :id), :selected => selected, include_blank: true
return "<p>#{select}</p>" return "<p>#{select}</p>"
#TODO save selection to Issues.qbp_customer_id
end end
# View Issue # View Issue
@@ -42,14 +40,48 @@ class QboHookListener < Redmine::Hook::ViewListener
end end
# New Issue Saved # New Issue Saved
def controller_issues_new_after_save(context={}) def controller_issues_edit_after_save(context={})
issue = context[:issue] issue = context[:issue]
#TODO check if closed then create new quickbooks billable time entery qbo = Qbo.first
end
# Existing Issue updated qb_oauth_consumer = OAuth::Consumer.new(Setting.plugin_redmine_qbo['settingsOAuthConsumerKey'], Setting.plugin_redmine_qbo['settingsOAuthConsumerSecret'], {
def controller_issues_edit_after_save (context={}) :site => "https://oauth.intuit.com",
issue = context[:issue] :request_token_path => "/oauth/v1/get_request_token",
#TODO check if closed then create new quickbooks billable time entery :authorize_url => "https://appcenter.intuit.com/Connect/Begin",
:access_token_path => "/oauth/v1/get_access_token"
})
# Check to see if we have registered with QBO
if not qbo.nil? then
# Connect to QBO
oauth_client = OAuth::AccessToken.new(qb_oauth_consumer, qbo.token, qbo.secret)
# Prepare to create a new Time Activity
time_service = Quickbooks::Service::TimeActivity.new(:company_id => qbo.realmId, :access_token => oauth_client)
time_entry = Quickbooks::Model::TimeActivity.new
# Convert float spent time to hours and minutes
hours = issue.spent_hours.to_i
minutesDecimal = (( issue.spent_hours - hours) * 60)
minutes = minutesDecimal.to_i
# 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? then
time_entry.description = issue.subject
time_entry.employee_id = 59
time_entry.customer_id = issue.qbo_customer_id
time_entry.billable_status = "Billable"
time_entry.hours = hours
time_entry.minutes = minutes
time_entry.name_of = "Employee"
time_entry.txn_date = Date.today
time_entry.hourly_rate = 50
time_entry.item_id = 19
time_entry.start_time = issue.start_date
time_entry.end_time = Time.now
time_service.create(time_entry)
end
end
end end
end end