From fd3c8e15e69f8ceb23d0a1a735804e1f92e8c69e Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Mon, 3 Apr 2017 21:49:57 -0400 Subject: [PATCH 1/6] Added filter methods for estimates & invoices --- app/controllers/customers_controller.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index e71a5b5..6eafafc 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -39,6 +39,14 @@ class CustomersController < ApplicationController @filtered_vehicles = Vehicle.all.where(customer_id: params[:selected_customer]) end + def filter_invoices_by_customer + @filtered_invoices = QboInvoice.all.where(customer_id: params[:selected_customer]) + end + + def filter_estimates_by_customer + @filtered_estimates = QboEstimate.all.where(customer_id: params[:selected_customer]) + end + # display a list of all customers def index if params[:search] From e8619529d43bd0b9b3480059293a76301928bd0d Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Mon, 3 Apr 2017 21:52:39 -0400 Subject: [PATCH 2/6] Added routes for filtering estimates & invoices --- config/routes.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 21aa526..8fce2bf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -34,14 +34,15 @@ resources :payments #webhook post 'qbo/webhook', :to => 'qbo#qbo_webhook' -#ajax +#java script routes get 'filter_vehicles_by_customer' => 'customers#filter_vehicles_by_customer' +get 'filter_estimates_by_customer' => 'customers#filter_estimates_by_customer' +get 'filter_invoices_by_customer' => 'customers#filter_invoices_by_customer' # Nest Vehicles under customers resources :customers do resources :vehicles get :autocomplete_customer_name, :on => :collection - get :autocomplete_customer_vehicles, :on => :collection end #allow for just vehicles too From bd8706deeef53bdcc5ce9830ff7798003286f111 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Mon, 3 Apr 2017 21:55:47 -0400 Subject: [PATCH 3/6] Create filter_estimates_by_customer.js.erb --- app/views/customers/filter_estimates_by_customer.js.erb | 1 + 1 file changed, 1 insertion(+) create mode 100644 app/views/customers/filter_estimates_by_customer.js.erb diff --git a/app/views/customers/filter_estimates_by_customer.js.erb b/app/views/customers/filter_estimates_by_customer.js.erb new file mode 100644 index 0000000..b705d98 --- /dev/null +++ b/app/views/customers/filter_estimates_by_customer.js.erb @@ -0,0 +1 @@ +$('select#issue_qbo_estimate_id').html('<%= j options_from_collection_for_select(@filtered_estimates, :id, :to_s) %>'); From 72bf10680fd496b1b55c5f860f4bc8aa2daaff1b Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Mon, 3 Apr 2017 21:57:56 -0400 Subject: [PATCH 4/6] Added ajax to update estimates --- assets/javascripts/application.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/assets/javascripts/application.js b/assets/javascripts/application.js index 09a670c..ef95764 100644 --- a/assets/javascripts/application.js +++ b/assets/javascripts/application.js @@ -5,5 +5,11 @@ $(function() { type: "GET", data: { selected_customer: $("input#issue_customer_id").val() } }); + + $.ajax({ + url: "/filter_estimates_by_customer", + type: "GET", + data: { selected_customer: $("input#issue_customer_id").val() } + }); }); }); From 35bf300f2d0cfd0f227c0a46fe44022d1aa8c88e Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Mon, 3 Apr 2017 22:07:06 -0400 Subject: [PATCH 5/6] Show only estimates attached to the customer --- lib/issues_form_hook_listener.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/issues_form_hook_listener.rb b/lib/issues_form_hook_listener.rb index 166d166..537bada 100644 --- a/lib/issues_form_hook_listener.rb +++ b/lib/issues_form_hook_listener.rb @@ -32,15 +32,17 @@ class IssuesFormHookListener < Redmine::Hook::ViewListener search_customer = f.autocomplete_field :customer, autocomplete_customer_name_customers_path, :selected => selected_customer, :update_elements => {:id => '#issue_customer_id', :value => '#issue_customer'} customer_id = f.hidden_field :customer_id, :id => "issue_customer_id" - # Generate the drop down list of quickbooks extimates - select_estimate = f.select :qbo_estimate_id, QboEstimate.all.pluck(:doc_number, :id).sort! {|x, y| y <=> x}, :selected => selected_estimate, include_blank: true - if context[:issue].customer vehicles = customer.vehicles.pluck(:name, :id).sort! + estimates = customer.qbo_estimates.pluck(:doc_number, :id).sort! {|x, y| y <=> x} else vehicles = [nil].compact + estimates = [nil].compact end + # Generate the drop down list of quickbooks extimates + select_estimate = f.select :qbo_estimate_id, estimates, :selected => selected_estimate, include_blank: true + vehicle = f.select :vehicles_id, vehicles, :selected => selected_vehicle, include_blank: true return "

#{search_customer} #{customer_id}

#{select_estimate}

#{vehicle}

" From 15ea3aeaa235d43e95b4487406729e4e30c4eacf Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Mon, 3 Apr 2017 22:24:04 -0400 Subject: [PATCH 6/6] Update qbo_estimate.rb --- app/models/qbo_estimate.rb | 60 ++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/app/models/qbo_estimate.rb b/app/models/qbo_estimate.rb index 1985a5b..d974e37 100644 --- a/app/models/qbo_estimate.rb +++ b/app/models/qbo_estimate.rb @@ -1,6 +1,6 @@ #The MIT License (MIT) # -#Copyright (c) 2016 rick barrette +#Copyright (c) 2017 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: # @@ -10,47 +10,49 @@ class QboEstimate < ActiveRecord::Base unloadable - has_many :issues - belongs_to :customer - attr_accessible :doc_number - validates_presence_of :id, :doc_number, :customer_id + has_and_belongs_to_many :issues + belongs_to :customer + attr_accessible :doc_number, :id + validates_presence_of :doc_number, :id + self.primary_key = :id + + # return the QBO Estimate service def self.get_base - Qbo.get_base(:estimate) + Qbo.get_base(:estimate).service end + # sync all estimates def self.sync - estimates = get_base.service.all - - # Update the item table - transaction do - estimates.each { |estimate| - qbo_estimate = QboEstimate.find_or_create_by(id: estimate.id) - qbo_estimate.doc_number = estimate.doc_number - qbo_estimate.customer_id = estimate.customer_ref.value - qbo_estimate.id = estimate.id - qbo_estimate.save! - } - end - + estimates = get_base.all + estimates.each { |estimate| + process_estimate(estimate) + } + #remove deleted estimates where.not(estimates.map(&:id)).destroy_all end + # sync only one estimate def self.sync_by_id(id) - estimate = get_base.service.fetch_by_id(id) - qbo_estimate = QboEstimate.find_or_create_by(id: estimate.id) + process_estimate(get_base.fetch_by_id(id)) + end + + # update an estimate + def self.update(id) + # Update the item table + estimate = get_base.fetch_by_id(id) + qbo_estimate = find_or_create_by(id: id) + qbo_estimate.doc_number = estimate.doc_number + qbo_estimate.save! + end + + # process an estimate into the database + def process_estimate(estimate) + qbo_estimate = find_or_create_by(id: estimate.id) qbo_estimate.doc_number = estimate.doc_number qbo_estimate.customer_id = estimate.customer_ref.value qbo_estimate.id = estimate.id 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