diff --git a/db/migrate/007_update_time_entries.rb b/db/migrate/007_update_time_entries.rb new file mode 100644 index 0000000..5da7bd9 --- /dev/null +++ b/db/migrate/007_update_time_entries.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 UpdateTimeEntries < ActiveRecord::Migration + def change + add_column :time_entries, :qbo_billed, :boolean, :default => false + end +end diff --git a/init.rb b/init.rb index 7c0852c..91b3ac4 100644 --- a/init.rb +++ b/init.rb @@ -27,7 +27,8 @@ Redmine::Plugin.register :redmine_qbo do Issue.safe_attributes 'qbo_customer_id' Issue.safe_attributes 'qbo_item_id' User.safe_attributes 'qbo_employee_id' - + TimeEntry.safe_attributes 'qbo_billed' + # 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 44519ee..dc83500 100644 --- a/lib/issues_save_hook_listener.rb +++ b/lib/issues_save_hook_listener.rb @@ -24,11 +24,21 @@ class IssuesSaveHookListener < Redmine::Hook::ViewListener time_entry = Quickbooks::Model::TimeActivity.new item = item_service.fetch_by_id issue.qbo_item_id + # 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 - hours = issue.spent_hours.to_i - minutesDecimal = (( issue.spent_hours - hours) * 60) + hours = spent_hours.to_i + minutesDecimal = (( spent_hours - hours) * 60) minutes = minutesDecimal.to_i + # update time entries billed status + spent_time.each do |entry| + entry.qbo_billed = true + 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