From 0b74f2cf52c5e97850b80a80d81021848d6c531b Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Sun, 24 Jan 2016 18:54:34 -0500 Subject: [PATCH] Added Invoice Support --- app/controllers/qbo_controller.rb | 10 +++++ app/models/qbo_invoice.rb | 38 +++++++++++++++++++ config/locales/en.yml | 1 + config/routes.rb | 3 +- db/migrate/011_create_qbo_invoices.rb | 18 +++++++++ db/migrate/012_update_issues_with_invoices.rb | 15 ++++++++ init.rb | 1 + lib/issue_patch.rb | 1 + lib/issues_form_hook_listener.rb | 22 ++++++----- lib/issues_show_hook_listener.rb | 16 +++++++- 10 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 app/models/qbo_invoice.rb create mode 100644 db/migrate/011_create_qbo_invoices.rb create mode 100644 db/migrate/012_update_issues_with_invoices.rb diff --git a/app/controllers/qbo_controller.rb b/app/controllers/qbo_controller.rb index 71525a0..e849f81 100644 --- a/app/controllers/qbo_controller.rb +++ b/app/controllers/qbo_controller.rb @@ -70,6 +70,7 @@ class QboController < ApplicationController QboItem.update_all QboEmployee.update_all QboEstimate.update_all + QboInvoice.update_all end redirect_to qbo_path(:redmine_qbo), :flash => { :notice => "Successfully synced to Quickbooks" } @@ -84,4 +85,13 @@ class QboController < ApplicationController send_data @pdf, filename: "estimate.pdf", :disposition => 'inline', :type => "application/pdf" end + # + # Downloads and forwards the invoice pdf + # + def invoice_pdf + base = QboInvoice.get_base.service + @pdf = base.pdf(base.fetch_by_id(params[:id])) + send_data @pdf, filename: "invoice.pdf", :disposition => 'inline', :type => "application/pdf" + end + end diff --git a/app/models/qbo_invoice.rb b/app/models/qbo_invoice.rb new file mode 100644 index 0000000..2f1af1f --- /dev/null +++ b/app/models/qbo_invoice.rb @@ -0,0 +1,38 @@ +#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 QboInvoice < ActiveRecord::Base + unloadable + has_many :issues + attr_accessible :doc_number + validates_presence_of :id, :doc_number + + def self.get_base + Qbo.get_base(:invoice) + end + + def self.update_all + # Update the item table + get_base.service.all.each { |invoice| + qbo_invoice = find_or_create_by(id: invoice.id) + qbo_invoice.doc_number = invoice.doc_number + qbo_invoice.id = invoice.id + qbo_invoice.save! + } + end + + def self.update(id) + # Update the item table + invoice = get_base.service.fetch_by_id(id) + qbo_invoice = find_or_create_by(id: id) + qbo_invoice.doc_number = invoice.doc_number + qbo_invoice.save! + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 221864f..39135e9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -14,3 +14,4 @@ en: field_qbo_customer: "Customer" field_qbo_item: "Item" field_qbo_employee: "Employee" + field_qbo_invoice: "Invoice" \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index a2983d7..18c2b64 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,4 +15,5 @@ get 'qbo', :to=> 'qbo#index' get 'qbo/authenticate', :to => 'qbo#authenticate' get 'qbo/oauth_callback', :to => 'qbo#oauth_callback' get 'qbo/sync', :to => 'qbo#sync' -get 'qbo/estimate/:id', :to => 'qbo#estimate_pdf', :as => :qbo_estimate_pdf \ No newline at end of file +get 'qbo/estimate/:id', :to => 'qbo#estimate_pdf', :as => :qbo_estimate_pdf +get 'qbo/invoice/:id', :to => 'qbo#invoice_pdf', :as => :qbo_invoice_pdf \ No newline at end of file diff --git a/db/migrate/011_create_qbo_invoices.rb b/db/migrate/011_create_qbo_invoices.rb new file mode 100644 index 0000000..2fe4051 --- /dev/null +++ b/db/migrate/011_create_qbo_invoices.rb @@ -0,0 +1,18 @@ +#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 CreateQboInvoices < ActiveRecord::Migration + def change + create_table :qbo_invoices, id: false do |t| + t.integer :id, :options => 'PRIMARY KEY' + t.string :doc_number + end + end +end diff --git a/db/migrate/012_update_issues_with_invoices.rb b/db/migrate/012_update_issues_with_invoices.rb new file mode 100644 index 0000000..af4e3dc --- /dev/null +++ b/db/migrate/012_update_issues_with_invoices.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 UpdateIssuesWithInvoices< ActiveRecord::Migration + def change + add_reference :issues, :qbo_invoice, index: true + end +end diff --git a/init.rb b/init.rb index 3918858..0ef35a2 100644 --- a/init.rb +++ b/init.rb @@ -32,6 +32,7 @@ Redmine::Plugin.register :redmine_qbo do Issue.safe_attributes 'qbo_customer_id' Issue.safe_attributes 'qbo_item_id' Issue.safe_attributes 'qbo_estimate_id' + Issue.safe_attributes 'qbo_invoice_id' User.safe_attributes 'qbo_employee_id' TimeEntry.safe_attributes 'qbo_billed' diff --git a/lib/issue_patch.rb b/lib/issue_patch.rb index 250f7c8..c99f586 100644 --- a/lib/issue_patch.rb +++ b/lib/issue_patch.rb @@ -24,6 +24,7 @@ module IssuePatch belongs_to :qbo_customer, primary_key: :id belongs_to :qbo_item, primary_key: :id belongs_to :qbo_estimate, primary_key: :id + belongs_to :qbo_invoice, primary_key: :id end end diff --git a/lib/issues_form_hook_listener.rb b/lib/issues_form_hook_listener.rb index bea5310..090a0ff 100644 --- a/lib/issues_form_hook_listener.rb +++ b/lib/issues_form_hook_listener.rb @@ -16,18 +16,22 @@ class IssuesFormHookListener < Redmine::Hook::ViewListener # Update the customer and item database QboCustomer.update_all QboItem.update_all + QboInvoice.update_all # Check to see if there is a quickbooks user attached to the issue - if context[:issue].qbo_customer && context[:issue].qbo_item - @selected_customer = context[:issue].qbo_customer.id - @selected_item = context[:issue].qbo_item.id - end - - # Generate the drop down list of quickbooks contacts + @selected_customer = context[:issue].qbo_customer.id if context[:issue].qbo_customer + @selected_item = context[:issue].qbo_item.id if context[:issue].qbo_item + @selected_invoice = context[:issue].qbo_invoice.id if context[:issue].qbo_invoice + + # Generate the drop down list of quickbooks customers @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 - return "

#{@select_customer}

#{@select_item}

" + # Generate the drop down list of quickbooks items + @select_item = context[:form].select :qbo_item_id, QboItem.all.pluck(:name, :id), :selected => @selected_item, include_blank: true + + # Generate the drop down list of quickbooks invoices + @select_invoice = context[:form].select :qbo_invoice_id, QboInvoice.all.pluck(:doc_number, :id).sort! {|x, y| y <=> x}, :selected => @selected_invoice, include_blank: true + + return "

#{@select_customer}

#{@select_item}

#{@select_invoice}

" end end diff --git a/lib/issues_show_hook_listener.rb b/lib/issues_show_hook_listener.rb index e1a4487..cc140a3 100644 --- a/lib/issues_show_hook_listener.rb +++ b/lib/issues_show_hook_listener.rb @@ -30,7 +30,14 @@ class IssuesShowHookListener < Redmine::Hook::ViewListener if issue.qbo_estimate QboEstimate.update(issue.qbo_estimate.id) @estimate = issue.qbo_estimate.doc_number - @link = link_to @estimate, qbo_estimate_pdf_path(issue.qbo_estimate.id) + @estimate_link = link_to @estimate, qbo_estimate_pdf_path(issue.qbo_estimate.id) + end + + # Invoice Number + if issue.qbo_invoice + QboInvoice.update(issue.qbo_invoice.id) + @invoice = issue.qbo_invoice.doc_number + @invoice_link = link_to @invoice, qbo_invoice_pdf_path(issue.qbo_invoice.id) end return "
@@ -46,7 +53,12 @@ class IssuesShowHookListener < Redmine::Hook::ViewListener
Estimate:
-
#{@link}
+
#{@estimate_link}
+
+ +
+
Invoice:
+
#{@invoice_link}
" end