diff --git a/app/models/qbo_customers.rb b/app/models/qbo_customer.rb similarity index 93% rename from app/models/qbo_customers.rb rename to app/models/qbo_customer.rb index 6c0ba50..8b69ad5 100644 --- a/app/models/qbo_customers.rb +++ b/app/models/qbo_customer.rb @@ -8,7 +8,7 @@ # #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 QboCustomers < ActiveRecord::Base +class QboCustomer < ActiveRecord::Base unloadable has_many :issues attr_accessible :name @@ -25,7 +25,7 @@ class QboCustomers < ActiveRecord::Base def self.update_all # Update the customer table get_base.service.all.each { |customer| - qbo_customer = QboCustomers.find_or_create_by(id: customer.id) + qbo_customer = QboCustomer.find_or_create_by(id: customer.id) qbo_customer.id = customer.id qbo_customer.name = customer.display_name qbo_customer.save! diff --git a/app/models/qbo_estimate.rb b/app/models/qbo_estimate.rb index 884dfff..070a2b4 100644 --- a/app/models/qbo_estimate.rb +++ b/app/models/qbo_estimate.rb @@ -27,4 +27,12 @@ class QboEstimate < ActiveRecord::Base qbo_estimate.save! } end + + def self.update(id) + # Update the item table + estimate = get_base.service.fetch_by_id(id) + qbo_estimate = QboEstimate.find_or_create_by(id: id) + qbo_estimate.doc_number = estimate.doc_number + qbo_estimate.save! + end end \ No newline at end of file diff --git a/init.rb b/init.rb index 7f8f434..8caebd5 100644 --- a/init.rb +++ b/init.rb @@ -10,30 +10,34 @@ 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' + # View Hook Listeners + require_dependency 'issues_form_hook_listener' + require_dependency 'issues_save_hook_listener' + require_dependency 'issues_show_hook_listener' + require_dependency 'users_show_hook_listener' + + # Patches to the Redmine core. Will not work in development mode + require_dependency 'issue_patch' - name 'Redmine Quickbooks Online plugin' - author 'Rick Barrette' - description 'This is a plugin for Redmine to intergrate with Quickbooks Online to allow for seamless intergration CRM and invoicing of completed issues' - version '0.0.2' - url 'https://github.com/rickbarrette/redmine_qbo' - author_url 'http://rickbarrette.org' - settings :default => {'empty' => true}, :partial => 'qbo/settings' + name 'Redmine Quickbooks Online plugin' + author 'Rick Barrette' + description 'This is a plugin for Redmine to intergrate with Quickbooks Online to allow for seamless intergration CRM and invoicing of completed issues' + version '0.0.3' + url 'https://github.com/rickbarrette/redmine_qbo' + author_url 'http://rickbarrette.org' + settings :default => {'empty' => true}, :partial => 'qbo/settings' - # Add safe attributes - Issue.safe_attributes 'qbo_customer_id' - Issue.safe_attributes 'qbo_item_id' - Issue.safe_attributes 'qbo_estimate_id' - User.safe_attributes 'qbo_employee_id' - TimeEntry.safe_attributes 'qbo_billed' - - # We are playing in the sandbox - #Quickbooks.sandbox_mode = true + # Add safe attributes + Issue.safe_attributes 'qbo_customer_id' + Issue.safe_attributes 'qbo_item_id' + Issue.safe_attributes 'qbo_estimate_id' + User.safe_attributes 'qbo_employee_id' + TimeEntry.safe_attributes 'qbo_billed' + + # We are playing in the sandbox + #Quickbooks.sandbox_mode = true - # Register QBO top menu item - menu :top_menu, :qbo, { :controller => 'qbo', :action => 'index' }, :caption => 'Quickbooks' + # Register QBO top menu item + menu :top_menu, :qbo, { :controller => 'qbo', :action => 'index' }, :caption => 'Quickbooks' end \ No newline at end of file diff --git a/lib/issue_patch.rb b/lib/issue_patch.rb new file mode 100644 index 0000000..250f7c8 --- /dev/null +++ b/lib/issue_patch.rb @@ -0,0 +1,41 @@ +#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. + +require_dependency 'issue' + +# Patches Redmine's Issues dynamically. +# Adds a relationships +module IssuePatch + def self.included(base) # :nodoc: + base.extend(ClassMethods) + + base.send(:include, InstanceMethods) + + # Same as typing in the class + base.class_eval do + unloadable # Send unloadable so it will not be unloaded in development + belongs_to :qbo_customer, primary_key: :id + belongs_to :qbo_item, primary_key: :id + belongs_to :qbo_estimate, primary_key: :id + end + end + + module ClassMethods + + end + + module InstanceMethods + + end + +end + +# Add module to Issue +Issue.send(:include, IssuePatch) \ No newline at end of file diff --git a/lib/issues_form_hook_listener.rb b/lib/issues_form_hook_listener.rb index be13913..e578ddb 100644 --- a/lib/issues_form_hook_listener.rb +++ b/lib/issues_form_hook_listener.rb @@ -15,7 +15,7 @@ class IssuesFormHookListener < Redmine::Hook::ViewListener def view_issues_form_details_bottom(context={}) selected = "" - QboCustomers.update_all + QboCustomer.update_all QboItem.update_all # Check to see if there is a quickbooks user attached to the issue @@ -25,7 +25,7 @@ class IssuesFormHookListener < Redmine::Hook::ViewListener end # Generate the drop down list of quickbooks contacts - select_customer = context[:form].select :qbo_customer_id, QboCustomers.all.pluck(:name, :id), :selected => selected_customer, include_blank: true + select_customer = context[:form].select :qbo_customer_id, QboCustomer.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).reverse, :selected => selected_item, include_blank: true diff --git a/lib/issues_show_hook_listener.rb b/lib/issues_show_hook_listener.rb index d0a3594..1d677c9 100644 --- a/lib/issues_show_hook_listener.rb +++ b/lib/issues_show_hook_listener.rb @@ -21,21 +21,17 @@ class IssuesShowHookListener < Redmine::Hook::ViewListener issue = context[:issue] # Check to see if there is a quickbooks user attached to the issue - unless context[:issue].qbo_customer_id.nil? - @customer = QboCustomers.find_by_id(context[:issue].qbo_customer_id).name - end + #unless context[:issue].qbo_customer_id.nil? + @customer = issue.qbo_customer.name + #end # Check to see if there is a quickbooks item attached to the issue - unless issue.qbo_customer_id.nil? then - unless QboItem.find_by_id(context[:issue].qbo_item_id).nil? - @item = QboItem.find_by_id(context[:issue].qbo_item_id).name - end - end + @item = issue.qbo_item.name # Estimate Number - unless (issue.qbo_estimate_id.nil?) - QboEstimate.update_all - @estimate = QboEstimate.find_by_id(issue.qbo_estimate_id).doc_number + unless (issue.qbo_estimate.nil?) + QboEstimate.update(issue.qbo_estimate.id) + @estimate = issue.qbo_estimate.doc_number end return "