diff --git a/README.md b/README.md index 99ad42a..6b1df66 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,7 @@ Estimates and invoices keep their number, date, customer, total and **man-hours* * Time lines are linked to their time activities, so QuickBooks marks the time as billed. Estimates the invoice was made from, or imported into it, are linked and closed when it's saved. * **Custom fields from the work orders**: the issues behind an invoice are the ones its time lines and lineitems estimates name. Plugins can fill custom fields from them when they're imported, and block a save (e.g. redmine_qbo_vehicles fills the VIN and mileage and allows one vehicle per invoice). On its own, redmine_qbo leaves the boxes as typed. * **Sales person**: an invoice credits the sales person on the estimate it was made from (the first linked estimate that names one); if none does, the user who writes the invoice. +* Invoices show their status, worked out from the balance and due date like QuickBooks' invoice list: **Paid**, **Partially paid**, **Overdue**, **Open** or **Voided** (a zero total), on the customer page's invoice list and the invoice page. * Invoices with a payment applied open read-only. The due date is left to QuickBooks (from the customer's terms) unless one is picked. The "default message shown on invoices" fills in the message. ### Payments diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 28ace31..35e2d34 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -22,4 +22,11 @@ class Invoice < QboBaseModel doc_number end + # Paid, partially paid, overdue, open or voided, from the cached total, balance and due date + # (RedmineQboBilling::InvoiceStatus); nil when the balance isn't cached + def status(today: User.current.today) + RedmineQboBilling::InvoiceStatus.status(total: self[:total_amount], balance: self[:balance], + due_date: self[:due_date], today: today) + end + end \ No newline at end of file diff --git a/app/views/invoices/_list.html.erb b/app/views/invoices/_list.html.erb index 2bd439f..00ebfb7 100644 --- a/app/views/invoices/_list.html.erb +++ b/app/views/invoices/_list.html.erb @@ -1,4 +1,5 @@ <% unless invoices.empty? %> + <%= render partial: 'qbo_estimates/status_styles' %> <%= form_with(url: invoice_path, method: :get) do |form| %> @@ -10,13 +11,19 @@
<% end %> +
<% invoices.sort.reverse.each do |invoice| %>
<%= check_box_tag "invoice_ids[]", invoice.id, false, onchange: "updateLink()", data: { url: invoice_path(invoice), text: "Invoice ##{invoice.to_s}" }, class: "invoice-checkbox appointment" if invoices.count > 1 %> <%= link_to "##{invoice.doc_number}", invoice_path(invoice), target: :_blank %> <%= invoice.txn_date %> + <%# Paid / partially paid / overdue / open / voided, from the cached balance and due date %> + <% if (status = invoice.status) %> + <%= RedmineQboBilling::InvoiceStatus.label(status) %> + <% end %> <%= render partial: 'qbo_invoices/invoice_actions', locals: { invoice: invoice } if RedmineQboBilling.invoice_writer? %>
<% end %> +
<% if invoices.count > 1 %> <%= form.submit t(:button_bulk_pdf) %> diff --git a/app/views/qbo_estimates/_header.html.erb b/app/views/qbo_estimates/_header.html.erb index 79e5d88..825b0b8 100644 --- a/app/views/qbo_estimates/_header.html.erb +++ b/app/views/qbo_estimates/_header.html.erb @@ -15,9 +15,13 @@ <% if form.sent? %> <%= l(:label_estimate_sent) %> <% end %> - <% if invoice && form.locked == :paid %> - <% fully_paid = form.balance.to_d.zero? %> - <%= l(fully_paid ? :label_invoice_paid : :label_invoice_partially_paid) %> + <% if invoice %> + <% status = RedmineQboBilling::InvoiceStatus.status(total: form.total, balance: form.balance, + due_date: RedmineQboBilling::EstimateForm.date(form.due_date), + today: User.current.today) %> + <% if status %> + <%= RedmineQboBilling::InvoiceStatus.label(status) %> + <% end %> <% elsif !invoice && form.txn_status.present? %> <%= form.txn_status %> <% end %> diff --git a/app/views/qbo_estimates/_status_styles.html.erb b/app/views/qbo_estimates/_status_styles.html.erb index d4f20d4..98b76ee 100644 --- a/app/views/qbo_estimates/_status_styles.html.erb +++ b/app/views/qbo_estimates/_status_styles.html.erb @@ -7,4 +7,11 @@ .qbo-estimate-status-closed, .qbo-estimate-status-converted { background: #e8eefa; color: #0d47a1; } .qbo-estimate-status-rejected { background: #fdecea; color: #b71c1c; } .qbo-estimate-list .qbo-estimate-status { font-size: .8em; padding: 1px 7px; margin-left: 4px; } + /* Invoices (RedmineQboBilling::InvoiceStatus) */ + .qbo-invoice-status-paid { background: #e6f4e4; color: #108000; } + .qbo-invoice-status-partial { background: #fff4d6; color: #8a6100; } + .qbo-invoice-status-overdue { background: #fdecea; color: #b71c1c; } + .qbo-invoice-status-open { background: #e8eefa; color: #0d47a1; } + .qbo-invoice-status-voided { background: #eceef1; color: #6b6c72; text-decoration: line-through; } + .qbo-invoice-list .qbo-estimate-status { font-size: .8em; padding: 1px 7px; margin-left: 4px; } diff --git a/config/locales/en.yml b/config/locales/en.yml index 3b21291..4ecb3f0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -240,8 +240,11 @@ en: label_invoice_balance: "Balance due %{balance}" label_invoice_date: "Invoice date" label_invoice_no: "Invoice no." - label_invoice_paid: "Paid" - label_invoice_partially_paid: "Partially paid" + label_invoice_status_paid: "Paid" + label_invoice_status_partial: "Partially paid" + label_invoice_status_overdue: "Overdue" + label_invoice_status_open: "Open" + label_invoice_status_voided: "Voided" label_message_on_invoice: "Message displayed on invoice" label_new_invoice: "New invoice" label_open_estimates: "Open estimates" diff --git a/lib/redmine_qbo_billing/invoice_status.rb b/lib/redmine_qbo_billing/invoice_status.rb new file mode 100644 index 0000000..3d01a96 --- /dev/null +++ b/lib/redmine_qbo_billing/invoice_status.rb @@ -0,0 +1,47 @@ +#The MIT License (MIT) +# +#Copyright (c) 2026 rick barrette +# +#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +# +#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +# +#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. + + +module RedmineQboBilling + # An invoice's status, worked out from its total, balance and due date the way QuickBooks' invoice list + # shows it (QuickBooks keeps no status field on invoices): + # :voided - nothing to pay at all (a voided invoice has a zero total) + # :paid - nothing left owing + # :partial - some of it paid + # :overdue - money owing and past the due date + # :open - money owing, not yet due + # nil when the balance isn't known (e.g. an invoice synced before balances were kept). + module InvoiceStatus + STATUSES = %i[voided paid partial overdue open].freeze + + module_function + + def status(total:, balance:, due_date: nil, today: Date.current) + return nil if balance.nil? || balance.to_s.strip.empty? + + total = to_d(total) + balance = to_d(balance) + return :voided if total.zero? && balance.zero? + return :paid if balance <= 0 + return :overdue if due_date && due_date < today + balance < total ? :partial : :open + end + + def label(status) + status ? I18n.t("label_invoice_status_#{status}") : '' + end + + def to_d(value) + BigDecimal(value.to_s.presence || '0') + rescue ArgumentError + BigDecimal('0') + end + end +end