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] 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 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) %>'); 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() } + }); }); }); 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 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}
"