diff --git a/app/controllers/qbo_estimates_controller.rb b/app/controllers/qbo_estimates_controller.rb
index 4369006..2586872 100644
--- a/app/controllers/qbo_estimates_controller.rb
+++ b/app/controllers/qbo_estimates_controller.rb
@@ -99,7 +99,8 @@ class QboEstimatesController < ApplicationController
# The work is going ahead: a pending estimate becomes Accepted in QuickBooks
accepted = RedmineQboBilling::EstimateWriter.accept(@estimate.id, user: User.current)
flash[:notice] = l(:notice_estimate_issues_created, count: created.size)
- flash[:notice] += " #{l(:notice_estimate_accepted, number: @estimate.doc_number)}" if accepted
+ flash[:notice] += " #{l(:notice_estimate_accepted, number: @estimate.doc_number)}" if accepted.accepted?
+ flash[:warning] = l(:warning_estimate_not_accepted, number: @estimate.doc_number, message: accepted.message) if accepted.failed?
redirect_to @return_path
else
# Rebuild the fields so the form keeps what was entered
diff --git a/app/views/qbo_estimates/_header.html.erb b/app/views/qbo_estimates/_header.html.erb
index 78ce036..79e5d88 100644
--- a/app/views/qbo_estimates/_header.html.erb
+++ b/app/views/qbo_estimates/_header.html.erb
@@ -18,7 +18,7 @@
<% if invoice && form.locked == :paid %>
<% fully_paid = form.balance.to_d.zero? %>
<%= l(fully_paid ? :label_invoice_paid : :label_invoice_partially_paid) %>
- <% elsif !invoice && form.txn_status.present? && form.txn_status != 'Pending' %>
+ <% elsif !invoice && form.txn_status.present? %>
<%= form.txn_status %>
<% end %>
<% else %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 860cbc7..2c75c99 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -270,6 +270,8 @@ en:
label_issue_template: "Template"
label_no_issue_template: "(no template)"
notice_estimate_accepted: "Estimate #%{number} is now Accepted in QuickBooks."
+ warning_estimate_not_accepted: "The issues were created, but estimate #%{number} couldn't be marked Accepted in QuickBooks: %{message}"
+ error_estimate_not_accepted: "QuickBooks kept its status as %{status}"
notice_estimate_issues_created:
one: "1 issue created from the estimate"
other: "%{count} issues created from the estimate"
diff --git a/lib/redmine_qbo_billing/estimate_writer.rb b/lib/redmine_qbo_billing/estimate_writer.rb
index 46b97c0..e72ba91 100644
--- a/lib/redmine_qbo_billing/estimate_writer.rb
+++ b/lib/redmine_qbo_billing/estimate_writer.rb
@@ -169,13 +169,20 @@ module RedmineQboBilling
# Statuses an estimate moves from to Accepted when the work starts (issues are created from it)
ACCEPTABLE_STATUSES = ['', 'Pending'].freeze
- # Marks the estimate Accepted in QuickBooks, dated today, when issues are created from it (the customer
- # went ahead with the work). Only a pending estimate changes; Accepted, Closed, Converted or Rejected ones
- # are left as they are. Returns true when it changed. A failure is logged, not raised: the issues stand.
+ AcceptResult = Struct.new(:outcome, :message) do
+ def accepted?; outcome == :accepted; end
+ def failed?; outcome == :failed; end
+ end
+
+ # Marks the estimate Accepted in QuickBooks when issues are created from it (the customer went ahead with
+ # the work), accepted today by the customer, as QuickBooks' own Accept does. Only a pending estimate
+ # changes; Accepted, Closed, Converted or Rejected ones are left as they are. Returns an AcceptResult:
+ # :accepted, :unchanged, or :failed with QuickBooks' message (never raised: the issues stand).
def accept(estimate_id, user: User.current)
- accepted = QboEstimateService.with_service do |service|
+ outcome, status = QboEstimateService.with_service do |service|
estimate = service.fetch_by_id(estimate_id)
- next false if estimate.nil? || !ACCEPTABLE_STATUSES.include?(estimate.txn_status.to_s)
+ next [:failed, I18n.t(:notice_estimate_not_found)] if estimate.nil?
+ next [:unchanged, estimate.txn_status.to_s] unless ACCEPTABLE_STATUSES.include?(estimate.txn_status.to_s)
update = Quickbooks::Model::Estimate.new
update.id = estimate.id
@@ -183,14 +190,26 @@ module RedmineQboBilling
update.customer_ref = estimate.customer_ref
update.txn_status = 'Accepted'
update.accepted_date = user.today
- service.update(update, sparse: true)
- true
+ update.accepted_by = estimate.customer_ref&.name.presence ||
+ Customer.find_by(id: estimate.customer_ref&.value)&.name
+ saved = service.update(update, sparse: true)
+ saved&.txn_status.to_s == 'Accepted' ? [:accepted, nil] : [:failed, saved&.txn_status.to_s.presence || 'no answer']
+ end
+
+ if outcome == :accepted
+ sync_local(Struct.new(:id).new(estimate_id))
+ AcceptResult.new(:accepted, nil)
+ elsif outcome == :unchanged
+ AcceptResult.new(:unchanged, status)
+ else
+ message = I18n.t(:error_estimate_not_accepted, status: status)
+ Rails.logger.warn "[RedmineQboBilling] Estimate #{estimate_id}: #{message}"
+ AcceptResult.new(:failed, message)
end
- sync_local(Struct.new(:id).new(estimate_id)) if accepted
- accepted
rescue StandardError => e
- Rails.logger.warn "[RedmineQboBilling] Could not mark estimate #{estimate_id} accepted: #{e.message}"
- false
+ message = e.respond_to?(:detail) && e.detail.present? ? "#{e.message}: #{e.detail}" : e.message
+ Rails.logger.warn "[RedmineQboBilling] Could not mark estimate #{estimate_id} accepted: #{message}"
+ AcceptResult.new(:failed, message)
end
# Emails the estimate; QuickBooks uses its own estimate email template