From 3304fce92f5b94d58cf89e50da5e33426f6ad4c7 Mon Sep 17 00:00:00 2001
From: Claude
<%= l(:error_invoice_paid) %>
<% elsif form.locked == :bundles %><%= l(invoice ? :error_invoice_has_bundles : :error_estimate_has_bundles) %>
-<% elsif !invoice && %w[Converted Closed].include?(form.txn_status.to_s) && Array(form.invoiced_on).empty? %> -<%= l(:text_estimate_closed) %>
<% end %> <% if RedmineQboBilling::Company.settings.empty? %> diff --git a/app/views/qbo_estimates/edit.html.erb b/app/views/qbo_estimates/edit.html.erb index 1f8d4e8..37134b4 100644 --- a/app/views/qbo_estimates/edit.html.erb +++ b/app/views/qbo_estimates/edit.html.erb @@ -4,7 +4,7 @@ <%# Plugins add estimate actions here (e.g. redmine_qbo_calendar's Add appointment) %> <%= call_hook :view_qbo_estimate_contextual, { estimate_id: @form.id, customer_id: @form.customer_id, vin: @form.vin, doc_number: @form.doc_number } %> <%# An estimate is billed once: no Create invoice once it's on an invoice, closed or rejected %> - <% if RedmineQboBilling.invoice_writer? && Array(@form.invoiced_on).empty? && !%w[Converted Closed Rejected].include?(@form.txn_status.to_s) %> + <% if RedmineQboBilling.invoice_writer? && @form.locked != :billed && @form.txn_status.to_s != 'Rejected' %> <%= link_to sprite_icon('document', l(:button_create_invoice)), new_qbo_invoice_path(estimate_id: @form.id), class: 'icon icon-document' %> <% end %> <% if User.current.allowed_to?(:create_estimate_issues, nil, global: true) %> diff --git a/config/locales/en.yml b/config/locales/en.yml index e33cfa2..7fe397e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -134,6 +134,7 @@ en: error_estimate_changed_in_quickbooks: "This estimate was changed in QuickBooks after you opened it, so it wasn't saved." error_estimate_field_invalid: "%{field} is not valid" error_estimate_linked_to_issues: "This estimate is linked to these issues, so it can't be changed here. Duplicate it to make a new estimate:" + error_estimate_billed: "This estimate has been billed (turned into an invoice), so it can't be changed here. Duplicate it to make a new estimate." error_estimate_has_bundles: "This estimate has bundles, which can only be edited in QuickBooks." error_estimate_line_description: "Line %{line} needs a description, a product or service, or a rate" error_estimate_line_number: "Line %{line} has a quantity or rate that isn't a number" @@ -177,7 +178,6 @@ en: text_clear_all_lines_confirmation: "Clear all lines?" text_email_estimate_confirmation: "Email estimate #%{number} to %{email}?" text_estimate_duplicated_from: "Copied from estimate #%{number}. It's saved as a new estimate." - text_estimate_closed: "This estimate is closed (it was turned into an invoice). Changes here won't change the invoice." text_estimate_expiration_days: "New estimates get an expiration date this many days after the estimate date, e.g. 30. Leave blank for no expiration date." text_estimate_reload: "Reloading shows the latest version from QuickBooks; changes made on this page will be lost." text_estimate_send_needs_email: "Enter the customer's email to send it." @@ -231,7 +231,6 @@ en: error_estimate_already_invoiced: "Estimate #%{number} is already billed on invoice #%{invoice}." error_estimate_closed_billed: "Estimate #%{number} is already billed (%{status})." error_estimate_rejected: "Estimate #%{number} was rejected, so it can't be billed." - text_estimate_billed_on: "This estimate is billed on:" error_invoice_changed_in_quickbooks: "This invoice was changed in QuickBooks after you opened it, so it wasn't saved." error_invoice_has_bundles: "This invoice has bundles, which can only be edited in QuickBooks." error_invoice_not_saved: "QuickBooks didn't save the invoice" diff --git a/db/migrate/047_add_estimate_totals_and_hours.rb b/db/migrate/047_add_estimate_totals_and_hours.rb index 07628db..a1d0714 100644 --- a/db/migrate/047_add_estimate_totals_and_hours.rb +++ b/db/migrate/047_add_estimate_totals_and_hours.rb @@ -9,12 +9,14 @@ #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -# Cached report figures: each estimate's total, and the man-hours on estimates and invoices (quantities of the -# products/services set as "Replaced by imported time"). Filled in by the sync and the webhook. +# Cached from QuickBooks by the sync and the webhook: each estimate's total and status (Converted or Closed +# estimates are billed, so they're shown as View instead of Edit), and the man-hours on estimates and invoices +# (quantities of the labor products/services) for the business report. class AddEstimateTotalsAndHours < ActiveRecord::Migration[7.0] def up add_column :estimates, :total_amount, :decimal, precision: 15, scale: 2 unless column_exists?(:estimates, :total_amount) add_column :estimates, :hours, :float unless column_exists?(:estimates, :hours) + add_column :estimates, :txn_status, :string unless column_exists?(:estimates, :txn_status) add_column :invoices, :hours, :float unless column_exists?(:invoices, :hours) add_index :estimates, :txn_date unless index_exists?(:estimates, :txn_date) add_index :invoices, :txn_date unless index_exists?(:invoices, :txn_date) @@ -24,6 +26,7 @@ class AddEstimateTotalsAndHours < ActiveRecord::Migration[7.0] remove_index :invoices, :txn_date if index_exists?(:invoices, :txn_date) remove_index :estimates, :txn_date if index_exists?(:estimates, :txn_date) remove_column :invoices, :hours if column_exists?(:invoices, :hours) + remove_column :estimates, :txn_status if column_exists?(:estimates, :txn_status) remove_column :estimates, :hours if column_exists?(:estimates, :hours) remove_column :estimates, :total_amount if column_exists?(:estimates, :total_amount) end diff --git a/lib/redmine_qbo_billing/estimate_writer.rb b/lib/redmine_qbo_billing/estimate_writer.rb index 1da4505..4e85024 100644 --- a/lib/redmine_qbo_billing/estimate_writer.rb +++ b/lib/redmine_qbo_billing/estimate_writer.rb @@ -121,6 +121,7 @@ module RedmineQboBilling remote = service.fetch_by_id(form.id) raise Error, I18n.t(:notice_estimate_not_found) unless remote raise Conflict, I18n.t(:error_estimate_changed_in_quickbooks) if remote.sync_token.to_s != form.sync_token.to_s + raise Error, I18n.t(:error_estimate_billed) if billed?(remote) raise Error, I18n.t(:error_estimate_has_bundles) if group_lines?(remote) raise Error, I18n.t(:error_estimate_linked_to_issues) if linked_issues(form.id).any? @@ -150,6 +151,16 @@ module RedmineQboBilling # ---- QuickBooks estimate -> form ---- + # Statuses QuickBooks gives an estimate that has been turned into an invoice + BILLED_STATUSES = %w[Converted Closed].freeze + + # True once the estimate is billed: converted or closed, or linked to an invoice. Billed estimates (including + # redmine_qbo_lineitems' once they're invoiced) can't be changed here. + def billed?(remote) + BILLED_STATUSES.include?(remote.txn_status.to_s) || + Array(remote.linked_transactions).any? { |t| t.txn_type.to_s == 'Invoice' } + end + def form_from_remote(remote) EstimateForm.new( id: remote.id.to_s, @@ -175,8 +186,10 @@ module RedmineQboBilling invoiced_on: Array(remote.linked_transactions).select { |t| t.txn_type.to_s == 'Invoice' }.map { |t| [t.txn_id.to_s, InvoiceWriter.invoice_number(t.txn_id)] }.uniq, - # Read-only when it has bundles, or once an issue is linked to it (the work is under way) - locked: (:bundles if group_lines?(remote)) || (:linked if linked_issues(remote.id).any?) + # Read-only once it's billed (converted, closed or on an invoice), when it has bundles, or once an issue + # is linked to it (the work is under way) + locked: (:billed if billed?(remote)) || (:bundles if group_lines?(remote)) || + (:linked if linked_issues(remote.id).any?) ) end diff --git a/lib/redmine_qbo_billing/invoice_writer.rb b/lib/redmine_qbo_billing/invoice_writer.rb index ad8c8aa..e7c158e 100644 --- a/lib/redmine_qbo_billing/invoice_writer.rb +++ b/lib/redmine_qbo_billing/invoice_writer.rb @@ -29,7 +29,7 @@ module RedmineQboBilling # Estimates in these states can still be imported (Converted/Closed ones are already invoiced, Rejected ones lost) OPEN_ESTIMATE_STATUSES = ['', 'Pending', 'Accepted'].freeze # How QuickBooks marks an invoiced estimate: Converted when it made the invoice, Closed when closed by hand - BILLED_ESTIMATE_STATUSES = %w[Converted Closed].freeze + BILLED_ESTIMATE_STATUSES = EstimateWriter::BILLED_STATUSES # How far back to look for billable time if QuickBooks won't filter time activities by customer TIME_LOOKBACK = 400