mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-17 18:20:36 -05:00
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:
4 files changed
+35
-13
No files matched your search
@@ -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
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
<% if invoice && form.locked == :paid %>
|
||||
<% 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>
|
||||
<% 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>
|
||||
<% end %>
|
||||
<% else %>
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user