From 135850b74d8959a481e4431de82b1836dea6d1da Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Tue, 12 Jan 2016 21:45:56 -0500 Subject: [PATCH] Estimates are now attached to issues and displayed on the issue --- app/controllers/qbo_controller.rb | 1 + app/models/qbo_estimate.rb | 4 ++-- lib/issues_save_hook_listener.rb | 24 ++++++++++++++---------- lib/issues_show_hook_listener.rb | 9 ++++++++- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/controllers/qbo_controller.rb b/app/controllers/qbo_controller.rb index af7406f..ee0350a 100644 --- a/app/controllers/qbo_controller.rb +++ b/app/controllers/qbo_controller.rb @@ -69,6 +69,7 @@ class QboController < ApplicationController QboCustomers.update_all QboItem.update_all QboEmployee.update_all + QboEstimate.update_all end redirect_to qbo_path(:redmine_qbo), :flash => { :notice => "Successfully synced to Quickbooks" } diff --git a/app/models/qbo_estimate.rb b/app/models/qbo_estimate.rb index 8da46cc..884dfff 100644 --- a/app/models/qbo_estimate.rb +++ b/app/models/qbo_estimate.rb @@ -21,8 +21,8 @@ class QboEstimate < ActiveRecord::Base def self.update_all # Update the item table get_base.service.all.each { |estimate| - qbo_estimate = QboItem.find_or_create_by(id: estimate.id) - qbo_estimate.name = estimate.doc_number + qbo_estimate = QboEstimate.find_or_create_by(id: estimate.id) + qbo_estimate.doc_number = estimate.doc_number qbo_estimate.id = estimate.id qbo_estimate.save! } diff --git a/lib/issues_save_hook_listener.rb b/lib/issues_save_hook_listener.rb index 3e148ae..21ed03d 100644 --- a/lib/issues_save_hook_listener.rb +++ b/lib/issues_save_hook_listener.rb @@ -19,15 +19,20 @@ class IssuesSaveHookListener < Redmine::Hook::ViewListener unless Qbo.first.nil? and issue.qbo_customer_id.nil? and issue.qbo_item_id.nil? and employee_id.nil? then # if this is a quote, lets create a new estimate based off estimated hours - if issue.tracker.name = "Quote" and issue.status.name = "New" and issue.custom_field_value(CustomField.find_by_name("Estimate").id).empty? then + if issue.tracker.name = "Quote" and issue.status.name = "New" and issue.qbo_estimate_id.nil? then + # Get QBO Services item_service = QboItem.get_base.service - - estimate = Quickbooks::Model::Estimate .new - estimate .customer_id = issue.qbo_customer_id - estimate .txn_date = Date.today + estimate_base = QboEstimate.get_base + + # Create the estimate + estimate = estimate_base.qr_model(:estimate) + estimate.customer_id = issue.qbo_customer_id + estimate.txn_date = Date.today + # Create the line item for labor item = item_service.fetch_by_id issue.qbo_item_id + line_item = Quickbooks::Model::InvoiceLineItem.new line_item.amount = item.unit_price * issue.estimated_hours line_item.description = issue.subject @@ -38,12 +43,11 @@ class IssuesSaveHookListener < Redmine::Hook::ViewListener detail.item_id = issue.qbo_item_id end - estimate .line_items << line_item + # Add the line items to the estimate + estimate.line_items << line_item - service = Quickbooks::Service::Estimate.new(:company_id => Qbo.get_realm_id, :access_token => Qbo.get_auth_token) - - created_estimate = service.create(estimate) - issue.custom_field_values = {CustomField.find_by_name("Estimate").id => created_estimate.doc_number} + # Save the etimate to the issue + issue.qbo_estimate_id = estimate_base.service.create(estimate).id issue.save! end end diff --git a/lib/issues_show_hook_listener.rb b/lib/issues_show_hook_listener.rb index f8df436..8098278 100644 --- a/lib/issues_show_hook_listener.rb +++ b/lib/issues_show_hook_listener.rb @@ -13,6 +13,7 @@ class IssuesShowHookListener < Redmine::Hook::ViewListener # View Issue # Display the quickbooks contact in the issue def view_issues_show_details_bottom(context={}) + issue = context[:issue] value = "" # Check to see if there is a quickbooks user attached to the issue @@ -25,13 +26,19 @@ class IssuesShowHookListener < Redmine::Hook::ViewListener value = "" # Check to see if there is a quickbooks item attached to the issue - if not context[:issue].qbo_customer_id.nil? then + if not issue.qbo_customer_id.nil? then if not QboItem.find_by_id(context[:issue].qbo_item_id).nil? then value = QboItem.find_by_id(context[:issue].qbo_item_id).name end 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") + + # Estimate Number + unless (issue.qbo_estimate_id.nil?) + QboEstimate.update_all + output << content_tag(:div, content_tag(:div, content_tag(:div, content_tag(:span,"Estimate") + ":", class:"label") + content_tag(:div, QboEstimate.find_by_id(issue.qbo_estimate_id).doc_number, class:"value") , class:"qbo_item_id attribute"), class:"attributes") + end # Display the Customers name in the Issue attributes return output