Save new estimates and invoices as drafts

A draft is kept in Redmine only, however incomplete, so nothing is in QuickBooks
until it's saved from the editor. QuickBooks can't apply a deposit to an invoice
that's still being written, which made it read-only. Saving to QuickBooks deletes
the draft.

Drafts are listed on the customer's page, on a Drafts page linked from Customers,
and on a new invoice or estimate for a customer who already has one. Anyone with
Write invoices (or Write estimates) can continue or delete invoice (or estimate)
drafts. Adds the qbo_drafts table (migration 052).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-04 10:33:03 -05:00
1 parent a1fb88e77c
commit c8e2d2d92e
17 files changed
+365 -7

No files matched your search

+5
View File
@@ -5,6 +5,11 @@
<% if User.current.allowed_to?(:add_customers, nil, global: true) %>
<%= link_to sprite_icon('add', l(:label_new_customer)), new_customer_path, class: 'icon icon-add' %>
<% end %>
<%# Estimate and invoice drafts, for users who may write either %>
<% if RedmineQboBilling.writer? || RedmineQboBilling.invoice_writer? %>
<% draft_count = QboDraft.visible.count %>
<%= link_to sprite_icon('document', "#{l(:label_qbo_drafts)} (#{draft_count})"), qbo_drafts_path, class: 'icon icon-document' %>
<% end %>
<% if User.current.admin? %>
<%= link_to sprite_icon('reload', l(:label_sync)), qbo_sync_path, class: 'icon icon-reload' %>
<% end %>
+7
View File
@@ -58,6 +58,13 @@
</div>
</div>
<%# Estimate and invoice drafts, kept in Redmine until they're saved to QuickBooks %>
<% customer_drafts = QboDraft.visible.where(customer_id: @customer.id).includes(:user).newest_first.to_a %>
<% if customer_drafts.any? %>
<h4><%= l(:label_qbo_drafts) %>:</h4>
<%= render partial: 'qbo_drafts/list', locals: { drafts: customer_drafts, show_customer: false } %>
<% end %>
</div>
<div class="splitcontentleft">
@@ -0,0 +1,18 @@
<%# On the new estimate/invoice editor: the draft being continued, or the customer's other drafts of this
document so a second one isn't started by accident %>
<% draft = local_assigns[:draft] %>
<% drafts = local_assigns[:drafts] || [] %>
<% if draft %>
<div class="flash notice qbo-draft-notice">
<%= l(:text_qbo_editing_draft, user: h(draft.user&.name || l(:label_user_anonymous)), age: time_tag(draft.updated_at)).html_safe %>
</div>
<% elsif drafts.any? %>
<div class="warning qbo-estimate-notice qbo-draft-notice">
<%= l(drafts.first.invoice? ? :text_qbo_customer_has_invoice_drafts : :text_qbo_customer_has_estimate_drafts, count: drafts.size) %>
<% drafts.each do |other| %>
<%= link_to l(:text_qbo_draft_link, user: other.user&.name || l(:label_user_anonymous),
subtotal: number_to_currency(other.subtotal), date: format_time(other.updated_at)),
other.invoice? ? new_qbo_invoice_path(draft_id: other.id) : new_qbo_estimate_path(draft_id: other.id) %>
<% end %>
</div>
<% end %>
+40
View File
@@ -0,0 +1,40 @@
<%# Estimate and invoice drafts (QboDraft) with Continue and Delete. show_customer: false on a customer's page. %>
<% show_customer = local_assigns.fetch(:show_customer, true) %>
<table class="list qbo-drafts">
<thead>
<tr>
<th><%= l(:label_qbo_draft_document) %></th>
<% if show_customer %><th><%= l(:field_customer) %></th><% end %>
<th class="qbo-draft-number"><%= l(:label_qbo_draft_lines) %></th>
<th class="qbo-draft-number"><%= l(:label_subtotal) %></th>
<th><%= l(:label_qbo_draft_saved_by) %></th>
<th></th>
</tr>
</thead>
<tbody>
<% drafts.each do |draft| %>
<tr>
<td><%= link_to l(draft.invoice? ? :label_qbo_invoice_draft : :label_qbo_estimate_draft),
draft.invoice? ? new_qbo_invoice_path(draft_id: draft.id) : new_qbo_estimate_path(draft_id: draft.id) %></td>
<% if show_customer %>
<td><%= draft.customer ? link_to(draft.customer.name, customer_path(draft.customer)) : (draft.customer_name || '–') %></td>
<% end %>
<td class="qbo-draft-number"><%= draft.line_count %></td>
<td class="qbo-draft-number"><%= number_to_currency(draft.subtotal) %></td>
<td><%= l(:text_qbo_draft_saved_by, user: h(draft.user&.name || l(:label_user_anonymous)), age: time_tag(draft.updated_at)).html_safe %></td>
<td class="buttons">
<%= link_to sprite_icon('edit', l(:button_qbo_continue_draft)),
draft.invoice? ? new_qbo_invoice_path(draft_id: draft.id) : new_qbo_estimate_path(draft_id: draft.id),
class: 'icon icon-edit' %>
<%= link_to sprite_icon('del', l(:button_delete)), qbo_draft_path(draft), method: :delete,
data: { confirm: l(:text_are_you_sure) }, class: 'icon icon-del' %>
</td>
</tr>
<% end %>
</tbody>
</table>
<style>
table.qbo-drafts td, table.qbo-drafts th { text-align: left; }
table.qbo-drafts .qbo-draft-number { text-align: right; white-space: nowrap; }
table.qbo-drafts td.buttons { white-space: nowrap; }
</style>
+11
View File
@@ -0,0 +1,11 @@
<% html_title l(:label_qbo_drafts) %>
<h2><%= link_to l(:label_customers), customers_path %> &#187; <%= l(:label_qbo_drafts) %></h2>
<p class="info"><%= l(:text_qbo_drafts_info) %></p>
<% if @drafts.any? %>
<%= render partial: 'qbo_drafts/list', locals: { drafts: @drafts } %>
<% else %>
<p class="nodata"><%= l(:label_no_data) %></p>
<% end %>
+12
View File
@@ -10,6 +10,8 @@
settings_read = company.settings.present?
# The same editor writes estimates and invoices
invoice = form.invoice?
# The draft being continued (QboDraft), if any; new documents can be saved as drafts
draft = local_assigns[:draft]
sender = invoice ? RedmineQboBilling.invoice_sender? : RedmineQboBilling.sender?
cancel_path = customer ? customer_path(customer) : home_path
form_url =
@@ -38,6 +40,7 @@
warn_unsaved: User.current.pref.warn_on_leaving_unsaved != '0'
}) do %>
<%= hidden_field_tag 'estimate[sync_token]', form.sync_token, id: nil %>
<%= hidden_field_tag 'draft_id', draft.id, id: nil if draft %>
<%= hidden_field_tag 'estimate[total_tax]', form.total_tax, id: nil %>
<%= hidden_field_tag 'estimate[discount]', form.discount, id: nil %>
<%= hidden_field_tag 'estimate[email_status]', form.email_status, id: nil %>
@@ -283,6 +286,15 @@
<%= link_to l(:button_cancel), cancel_path, class: 'qbo-button qbo-button-dark', id: 'qbo-estimate-cancel' %>
<% unless form.locked %>
<span class="qbo-estimate-actions-save">
<% unless form.persisted? %>
<%# Kept in Redmine only, however incomplete (QboDraftHandling) %>
<% if draft %>
<%= link_to l(:button_qbo_delete_draft), qbo_draft_path(draft), method: :delete,
data: { confirm: l(:text_are_you_sure) }, class: 'qbo-button qbo-button-dark', id: 'qbo-delete-draft' %>
<% end %>
<%= submit_tag l(:button_qbo_save_draft), name: 'save_draft', class: 'qbo-button qbo-button-dark',
id: 'qbo-save-draft', formnovalidate: true %>
<% end %>
<%= submit_tag l(:button_save), name: 'save', class: 'qbo-button qbo-button-dark' %>
<% if sender %>
<%= submit_tag l(:button_save_and_send), name: 'save_and_send', class: 'qbo-button qbo-button-primary',
+2 -1
View File
@@ -1,4 +1,5 @@
<% html_title l(:label_new_estimate) %>
<%= render partial: 'qbo_estimates/header', locals: { form: @form } %>
<%= render partial: 'qbo_estimates/form', locals: { form: @form } %>
<%= render partial: 'qbo_drafts/editor_notice', locals: { draft: @draft, drafts: @customer_drafts } %>
<%= render partial: 'qbo_estimates/form', locals: { form: @form, draft: @draft } %>
+2 -1
View File
@@ -1,5 +1,6 @@
<% html_title l(:label_new_invoice) %>
<%= render partial: 'qbo_estimates/header', locals: { form: @form } %>
<%= render partial: 'qbo_drafts/editor_notice', locals: { draft: @draft, drafts: @customer_drafts } %>
<%= render partial: 'qbo_invoices/open_issues', locals: { customer: (Customer.find_by(id: @form.customer_id) if @form.customer_id.present?) } %>
<%= render partial: 'qbo_estimates/form', locals: { form: @form } %>
<%= render partial: 'qbo_estimates/form', locals: { form: @form, draft: @draft } %>