diff --git a/app/models/qbo_item.rb b/app/models/qbo_item.rb index d21c128..d4263bc 100644 --- a/app/models/qbo_item.rb +++ b/app/models/qbo_item.rb @@ -10,8 +10,9 @@ class QboItem < ActiveRecord::Base unloadable - attr_accessible :name - validates_presence_of :id, :name + has_many :issues + attr_accessible :name + validates_presence_of :id, :name def self.update_all qbo = Qbo.first diff --git a/app/views/qbo/index.html.erb b/app/views/qbo/index.html.erb index 38ff0f4..40925d8 100644 --- a/app/views/qbo/index.html.erb +++ b/app/views/qbo/index.html.erb @@ -34,8 +34,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
<%= f.select :qbo_employee_id, QboEmployee.all.pluck(:name, :id), :selected => @selected_employee, include_blank: true %> - - <%= f.submit %> <% end %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index e8de451..54259ba 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -12,3 +12,4 @@ en: # my_label: "My label" field_qbo_customer: "Customer" + field_qbo_item: "Item" diff --git a/db/migrate/003_update_issues.rb b/db/migrate/003_update_issues.rb index 1367e5f..05cdd54 100644 --- a/db/migrate/003_update_issues.rb +++ b/db/migrate/003_update_issues.rb @@ -11,5 +11,6 @@ class UpdateIssues < ActiveRecord::Migration def change add_reference :issues, :qbo_customer, index: true + add_reference :issues, :qbo_item, index: true end end diff --git a/init.rb b/init.rb index 9e75e6b..39c6db3 100644 --- a/init.rb +++ b/init.rb @@ -22,6 +22,7 @@ Redmine::Plugin.register :redmine_qbo do # Add qbo_customer to the safe Issue Attributes list Issue.safe_attributes 'qbo_customer_id' + Issue.safe_attributes 'qbo_item_id' # We are playing in the sandbox Quickbooks.sandbox_mode = true @@ -29,4 +30,4 @@ Redmine::Plugin.register :redmine_qbo do # Register QBO top menu item menu :top_menu, :qbo, { :controller => 'qbo', :action => 'index' }, :caption => 'Quickbooks' -end +end \ No newline at end of file diff --git a/lib/qbo_hook_listener.rb b/lib/qbo_hook_listener.rb index 36bc4bc..09989cf 100644 --- a/lib/qbo_hook_listener.rb +++ b/lib/qbo_hook_listener.rb @@ -17,12 +17,16 @@ class QboHookListener < Redmine::Hook::ViewListener # Check to see if there is a quickbooks user attached to the issue if not context[:issue].qbo_customer_id.nil? then - selected = context[:issue].qbo_customer_id + selected_customer = context[:issue].qbo_customer_id + selected_item = context[:issue].qbo_item_id end # 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 - return "

#{select}

" + select_customer = context[:form].select :qbo_customer_id, QboCustomers.all.pluck(:name, :id), :selected => selected_customer, include_blank: true + + # Generate the drop down list of quickbooks contacts + select_item = context[:form].select :qbo_item_id, QboItem.all.pluck(:name, :id), :selected => selected_item, include_blank: true + return "

#{select_customer}

#{select_item}

" end # View Issue @@ -34,9 +38,18 @@ class QboHookListener < Redmine::Hook::ViewListener if not context[:issue].qbo_customer_id.nil? then value = QboCustomers.find_by_id(context[:issue].qbo_customer_id).name end - + + output = content_tag(:div, content_tag(:div, content_tag(:div, content_tag(:span,"Customer") + ":", class:"label") + content_tag(:div, value, class:"value") , class:"qbo_customer_id attribute"), class:"attributes") + + # Check to see if there is a quickbooks user attached to the issue + if not context[:issue].qbo_customer_id.nil? then + value = QboItem.find_by_id(context[:issue].qbo_item_id).name + end + + output << content_tag(:div, content_tag(:div, content_tag(:div, content_tag(:span,"Item") + ":", class:"label") + content_tag(:div, value, class:"value") , class:"qbo_item_id attribute"), class:"attributes") + # Display the Customers name in the Issue attributes - return content_tag(:div, content_tag(:div, content_tag(:div, content_tag(:span,"Customer") + ":", class:"label") + content_tag(:div, value, class:"value") , class:"qbo_customer_id attribute"), class:"attributes") + return output end # New Issue Saved @@ -49,7 +62,9 @@ class QboHookListener < Redmine::Hook::ViewListener # 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 + item = item_service.fetch_by_id issue.qbo_item_id # Convert float spent time to hours and minutes hours = issue.spent_hours.to_i @@ -58,7 +73,7 @@ class QboHookListener < Redmine::Hook::ViewListener # 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? then + 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 @@ -67,8 +82,8 @@ class QboHookListener < Redmine::Hook::ViewListener 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.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)