diff --git a/app/models/qbo_employee.rb b/app/models/qbo_employee.rb index 9eaef83..8ddfad2 100644 --- a/app/models/qbo_employee.rb +++ b/app/models/qbo_employee.rb @@ -10,8 +10,9 @@ class QboEmployee < ActiveRecord::Base unloadable - attr_accessible :name - validates_presence_of :id, :name + has_many :users + attr_accessible :name + validates_presence_of :id, :name def self.update_all qbo = Qbo.first @@ -25,5 +26,4 @@ class QboEmployee < ActiveRecord::Base qbo_employee.save! } end - -end +end \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 54259ba..221864f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -13,3 +13,4 @@ en: # my_label: "My label" field_qbo_customer: "Customer" field_qbo_item: "Item" + field_qbo_employee: "Employee" diff --git a/db/migrate/006_update_users.rb b/db/migrate/006_update_users.rb new file mode 100644 index 0000000..b428767 --- /dev/null +++ b/db/migrate/006_update_users.rb @@ -0,0 +1,15 @@ +#The MIT License (MIT) +# +#Copyright (c) 2016 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 UpdateUsers < ActiveRecord::Migration + def change + add_reference :users, :qbo_employee, index: true + end +end diff --git a/init.rb b/init.rb index 311a67c..7c0852c 100644 --- a/init.rb +++ b/init.rb @@ -13,6 +13,7 @@ Redmine::Plugin.register :redmine_qbo do require_dependency 'issues_form_hook_listener' require_dependency 'issues_save_hook_listener' require_dependency 'issues_show_hook_listener' + require_dependency 'users_show_hook_listener' name 'Redmine Quickbooks Online plugin' author 'Rick Barrette' @@ -22,9 +23,10 @@ Redmine::Plugin.register :redmine_qbo do author_url 'http://rickbarrette.org' settings :default => {'empty' => true}, :partial => 'qbo/settings' - # Add qbo_customer to the safe Issue Attributes list + # Add safe attributes Issue.safe_attributes 'qbo_customer_id' Issue.safe_attributes 'qbo_item_id' + User.safe_attributes 'qbo_employee_id' # We are playing in the sandbox Quickbooks.sandbox_mode = true diff --git a/lib/issues_save_hook_listener.rb b/lib/issues_save_hook_listener.rb index acd3ecb..44519ee 100644 --- a/lib/issues_save_hook_listener.rb +++ b/lib/issues_save_hook_listener.rb @@ -28,23 +28,25 @@ class IssuesSaveHookListener < Redmine::Hook::ViewListener hours = issue.spent_hours.to_i minutesDecimal = (( issue.spent_hours - hours) * 60) minutes = minutesDecimal.to_i + + 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? 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 = 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) + 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 + time_entry.description = "Ticket ##{issue.id}: #{issue.subject}" + time_entry.employee_id = employee_id + 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 = 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 diff --git a/lib/users_show_hook_listener.rb b/lib/users_show_hook_listener.rb new file mode 100644 index 0000000..43edb42 --- /dev/null +++ b/lib/users_show_hook_listener.rb @@ -0,0 +1,25 @@ +#The MIT License (MIT) +# +#Copyright (c) 2016 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 UsersShowHookListener < Redmine::Hook::ViewListener + + # View User + def view_users_form(context={}) + selected = "" + + # Check to see if there is a quickbooks user attached to the issue + if not context[:user].qbo_employee_id.nil? then + selected = context[:user].qbo_employee_id + end + + # Generate the drop down list of quickbooks contacts + return "
#{context[:form].select :qbo_employee_id, QboEmployee.all.pluck(:name, :id), :selected => selected, include_blank: true}
" + end +end \ No newline at end of file