diff --git a/README.md b/README.md index d3ba051..b313108 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ Estimates and invoices keep their number, date, customer, total and **man-hours* * **Edit** any estimate from the Edit link next to it on the customer page or the issue page. * **Duplicate** an estimate to start a similar one: same customer, lines, subtotals, VIN and messages, dated today, with the current user as sales person. * **Read-only once linked to an issue**: an estimate that an issue is linked to can be viewed, emailed and duplicated, but not changed, so the work doesn't drift from what was quoted. The list shows a View link for these instead of Edit. +* **Read-only once billed**: an estimate that's Converted or Closed in QuickBooks, or linked to an invoice, can't be changed here either (including redmine_qbo_lineitems' estimates once they're invoiced). Its page lists the invoices it's on; it can still be viewed, emailed and duplicated, and it shows View instead of Edit in lists. * **Create issues** from an estimate's labor lines (moved here from redmine_qbo_calendar): each line with one of the **labor products/services** (e.g. Labor) becomes an issue with the line's description as subject and its quantity as estimated hours. Pick the project, trackers, templates and shared custom fields first. Lines already turned into issues can't be created twice, and issues from the same estimate are related to each other. Opened from an appointment, the issues get the appointment's dates, technician and vehicle. * **Email** the estimate with QuickBooks' own estimate email, with **Save and send** or the Email link. * The page follows QuickBooks' estimate screen: customer, customer email, billing address, estimate date, expiration date, estimate number, then the product/service lines (#, product/service, description, qty, rate, amount, tax), the message displayed on the estimate, the memo, and the totals. diff --git a/app/models/estimate.rb b/app/models/estimate.rb index 298f854..a1e07dc 100644 --- a/app/models/estimate.rb +++ b/app/models/estimate.rb @@ -20,6 +20,12 @@ class Estimate < QboBaseModel to_s end + # True once QuickBooks has turned it into an invoice (status cached by the sync). The estimate page also + # checks for a linked invoice; either way a billed estimate can't be changed in Redmine. + def billed? + RedmineQboBilling::EstimateWriter::BILLED_STATUSES.include?(self[:txn_status].to_s) + end + # returns a human readable string def to_s return self[:doc_number] diff --git a/app/services/estimate_sync_service.rb b/app/services/estimate_sync_service.rb index defad49..6649a22 100644 --- a/app/services/estimate_sync_service.rb +++ b/app/services/estimate_sync_service.rb @@ -27,6 +27,7 @@ class EstimateSyncService < SyncServiceBase map_attributes :doc_number, :txn_date map_attribute :total_amount, :total + map_attribute :txn_status # Man-hours: quantities of the labor products/services (plugin settings) map_attribute(:hours) { |remote| RedmineQboBilling::ManHours.hours(remote) } map_belongs_to :customer diff --git a/app/views/qbo_estimates/_estimate_actions.html.erb b/app/views/qbo_estimates/_estimate_actions.html.erb index 01d76ec..d56a4a7 100644 --- a/app/views/qbo_estimates/_estimate_actions.html.erb +++ b/app/views/qbo_estimates/_estimate_actions.html.erb @@ -1,5 +1,5 @@ -<%# Estimates linked to an issue are read-only here, so they get a View link instead of Edit %> -<% if estimate.issues.exists? %> +<%# Billed estimates and estimates linked to an issue are read-only here, so they get a View link instead of Edit %> +<% if estimate.billed? || estimate.issues.exists? %> <%= link_to sprite_icon('zoom-in', l(:button_view_estimate)), edit_qbo_estimate_path(estimate.id), class: 'icon-only icon-zoom-in qbo-estimate-edit-link', title: l(:button_view_estimate) %> <% else %> diff --git a/app/views/qbo_estimates/_header.html.erb b/app/views/qbo_estimates/_header.html.erb index 01554d2..78ce036 100644 --- a/app/views/qbo_estimates/_header.html.erb +++ b/app/views/qbo_estimates/_header.html.erb @@ -35,16 +35,6 @@
<% end %> -<% if !invoice && Array(form.invoiced_on).any? %> -<%= 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