Report when an estimate can't be marked Accepted; show every status on the estimate page

- Accepting sends Accepted by (the customer) with the accepted date, as QuickBooks' own Accept does, and
  checks what QuickBooks returns. If it keeps the old status or refuses, the Create issues page shows a
  warning with QuickBooks' message instead of only logging it.
- The estimate page's header shows the status badge for every status, Pending included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
Claude committed 2026-10-31 12:43:17 -04:00
1 parent 338b4db237
commit 6ac0e27cb2
4 files changed
+35 -13

No files matched your search

+2 -1
View File
@@ -99,7 +99,8 @@ class QboEstimatesController < ApplicationController
# The work is going ahead: a pending estimate becomes Accepted in QuickBooks # The work is going ahead: a pending estimate becomes Accepted in QuickBooks
accepted = RedmineQboBilling::EstimateWriter.accept(@estimate.id, user: User.current) accepted = RedmineQboBilling::EstimateWriter.accept(@estimate.id, user: User.current)
flash[:notice] = l(:notice_estimate_issues_created, count: created.size) 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 redirect_to @return_path
else else
# Rebuild the fields so the form keeps what was entered # Rebuild the fields so the form keeps what was entered
+1 -1
View File
@@ -18,7 +18,7 @@
<% if invoice && form.locked == :paid %> <% if invoice && form.locked == :paid %>
<% fully_paid = form.balance.to_d.zero? %> <% fully_paid = form.balance.to_d.zero? %>
<span class="qbo-estimate-status qbo-estimate-status-<%= fully_paid ? 'closed' : 'accepted' %>"><%= l(fully_paid ? :label_invoice_paid : :label_invoice_partially_paid) %></span> <span class="qbo-estimate-status qbo-estimate-status-<%= fully_paid ? 'closed' : 'accepted' %>"><%= l(fully_paid ? :label_invoice_paid : :label_invoice_partially_paid) %></span>
<% elsif !invoice && form.txn_status.present? && form.txn_status != 'Pending' %> <% elsif !invoice && form.txn_status.present? %>
<span class="qbo-estimate-status qbo-estimate-status-<%= form.txn_status.downcase %>"><%= form.txn_status %></span> <span class="qbo-estimate-status qbo-estimate-status-<%= form.txn_status.downcase %>"><%= form.txn_status %></span>
<% end %> <% end %>
<% else %> <% else %>
+2
View File
@@ -270,6 +270,8 @@ en:
label_issue_template: "Template" label_issue_template: "Template"
label_no_issue_template: "(no template)" label_no_issue_template: "(no template)"
notice_estimate_accepted: "Estimate #%{number} is now Accepted in QuickBooks." 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: notice_estimate_issues_created:
one: "1 issue created from the estimate" one: "1 issue created from the estimate"
other: "%{count} issues created from the estimate" other: "%{count} issues created from the estimate"
+30 -11
View File
@@ -169,13 +169,20 @@ module RedmineQboBilling
# Statuses an estimate moves from to Accepted when the work starts (issues are created from it) # Statuses an estimate moves from to Accepted when the work starts (issues are created from it)
ACCEPTABLE_STATUSES = ['', 'Pending'].freeze ACCEPTABLE_STATUSES = ['', 'Pending'].freeze
# Marks the estimate Accepted in QuickBooks, dated today, when issues are created from it (the customer AcceptResult = Struct.new(:outcome, :message) do
# went ahead with the work). Only a pending estimate changes; Accepted, Closed, Converted or Rejected ones def accepted?; outcome == :accepted; end
# are left as they are. Returns true when it changed. A failure is logged, not raised: the issues stand. 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) 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) 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 = Quickbooks::Model::Estimate.new
update.id = estimate.id update.id = estimate.id
@@ -183,14 +190,26 @@ module RedmineQboBilling
update.customer_ref = estimate.customer_ref update.customer_ref = estimate.customer_ref
update.txn_status = 'Accepted' update.txn_status = 'Accepted'
update.accepted_date = user.today update.accepted_date = user.today
service.update(update, sparse: true) update.accepted_by = estimate.customer_ref&.name.presence ||
true 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 end
sync_local(Struct.new(:id).new(estimate_id)) if accepted
accepted
rescue StandardError => e rescue StandardError => e
Rails.logger.warn "[RedmineQboBilling] Could not mark estimate #{estimate_id} accepted: #{e.message}" message = e.respond_to?(:detail) && e.detail.present? ? "#{e.message}: #{e.detail}" : e.message
false Rails.logger.warn "[RedmineQboBilling] Could not mark estimate #{estimate_id} accepted: #{message}"
AcceptResult.new(:failed, message)
end end
# Emails the estimate; QuickBooks uses its own estimate email template # Emails the estimate; QuickBooks uses its own estimate email template