mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-19 10:40:36 -05:00
Compare commits
17
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
89f4797bbe | ||
|
|
3bbceacda4 | ||
|
|
d75dee2866 | ||
|
|
c8e2d2d92e | ||
|
|
a1fb88e77c | ||
|
|
0a7b2e34b8 | ||
|
|
895733f534 | ||
|
|
fa1b4eed06 | ||
|
|
95e76a41fd | ||
|
|
92b2a18eb6 | ||
|
|
084df36061 | ||
|
|
3b51625df4 | ||
|
|
7cfc8a370e | ||
|
|
d2cdd798cd | ||
|
|
159420fac1 | ||
|
|
dbee228cdf | ||
|
|
ec00684edd |
No files matched your search
@@ -0,0 +1,75 @@
|
||||
#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.
|
||||
|
||||
# Drafts on the new estimate and new invoice editors (see QboDraft). "Save draft" keeps what's filled in, even
|
||||
# if it isn't complete, and reopens it (new?draft_id=). Saving to QuickBooks deletes the draft.
|
||||
module QboDraftHandling
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
private
|
||||
|
||||
# The draft named by draft_id (in the URL, or the editor's hidden field), if the user may open it
|
||||
def find_draft(document)
|
||||
@draft = (QboDraft.visible.for_document(document).find_by(id: params[:draft_id]) if params[:draft_id].present?)
|
||||
end
|
||||
|
||||
# The editor form filled in from a draft, with the details the editor shows but doesn't post
|
||||
def form_from_draft(draft)
|
||||
form = draft.form
|
||||
customer = Customer.find_by(id: form.customer_id) if form.customer_id.present?
|
||||
if customer
|
||||
form.billing_address = RedmineQboBilling::EstimateWriter.customer_address(customer)
|
||||
form.customer_name = customer.name if form.customer_name.blank?
|
||||
end
|
||||
# Invoices keep the sales person shown on the editor (e.g. from an estimate); estimates are always the user's
|
||||
form.sales_person = (draft.invoice? && draft.fields['sales_person'].presence) || User.current.name
|
||||
form.lines = form.lines.reject(&:blank?)
|
||||
form.lines += Array.new([2 - form.lines.size, 1].max) { RedmineQboBilling::EstimateForm::Line.new(quantity: '', taxable: true) }
|
||||
form
|
||||
end
|
||||
|
||||
# Saves what's on the editor as a draft (no checks: a draft can be missing anything) and reopens it
|
||||
def save_draft(document, fields)
|
||||
draft = @draft || QboDraft.new(document: document)
|
||||
fields = fields.to_h.stringify_keys
|
||||
draft.fields = fields
|
||||
draft.customer_id = (Customer.where(id: fields['customer_id']).pick(:id) if fields['customer_id'].present?)
|
||||
draft.user = User.current
|
||||
draft.save!
|
||||
flash[:notice] = l(:notice_qbo_draft_saved)
|
||||
redirect_to draft_editor_path(draft)
|
||||
end
|
||||
|
||||
# Saved to QuickBooks: the draft isn't needed any more
|
||||
def discard_draft
|
||||
@draft&.destroy
|
||||
@draft = nil
|
||||
end
|
||||
|
||||
# A new document for a customer who already has drafts of it: the editor points them out
|
||||
def load_customer_drafts(document, customer_id)
|
||||
@customer_drafts =
|
||||
if @draft.nil? && customer_id.present?
|
||||
QboDraft.visible.for_document(document).where(customer_id: customer_id).newest_first.to_a
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
# A draft_id that's gone (e.g. finished or deleted by someone else): say so and list the drafts
|
||||
def draft_missing
|
||||
flash[:error] = l(:error_qbo_draft_not_found)
|
||||
redirect_to qbo_drafts_path
|
||||
end
|
||||
|
||||
def draft_editor_path(draft)
|
||||
draft.invoice? ? new_qbo_invoice_path(draft_id: draft.id) : new_qbo_estimate_path(draft_id: draft.id)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,31 @@
|
||||
#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.
|
||||
|
||||
# Estimate and invoice drafts (QboDraft): all of them, and deleting one. Drafts are continued on the new
|
||||
# estimate/invoice editor (new?draft_id=). Users see the drafts of the documents they may write.
|
||||
class QboDraftsController < ApplicationController
|
||||
|
||||
before_action :authorize_global
|
||||
|
||||
def index
|
||||
@drafts = QboDraft.visible.includes(:customer, :user).newest_first.to_a
|
||||
end
|
||||
|
||||
def destroy
|
||||
draft = QboDraft.visible.find_by(id: params[:id])
|
||||
if draft
|
||||
draft.destroy
|
||||
flash[:notice] = l(:notice_qbo_draft_deleted)
|
||||
else
|
||||
flash[:error] = l(:error_qbo_draft_not_found)
|
||||
end
|
||||
redirect_back_or_default qbo_drafts_path
|
||||
end
|
||||
end
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
# Writes, edits and emails QuickBooks estimates from Redmine, on a page laid out like QuickBooks' estimate screen.
|
||||
class QboEstimatesController < ApplicationController
|
||||
include QboDraftHandling
|
||||
|
||||
before_action :require_admin, only: :refresh_company
|
||||
before_action :authorize_global, except: :refresh_company
|
||||
@@ -25,26 +26,34 @@ class QboEstimatesController < ApplicationController
|
||||
# redmine_qbo raises plain errors when QuickBooks isn't connected or the token can't be refreshed
|
||||
RuntimeError].freeze
|
||||
|
||||
# A blank estimate, or a copy of another one (?duplicate=<estimate id>)
|
||||
# A blank estimate, a copy of another one (?duplicate=<estimate id>), or a draft (?draft_id=, see QboDraft)
|
||||
def new
|
||||
if params[:duplicate].present?
|
||||
if params[:draft_id].present?
|
||||
return draft_missing unless find_draft('estimate')
|
||||
@form = form_from_draft(@draft)
|
||||
elsif params[:duplicate].present?
|
||||
@form = RedmineQboBilling::EstimateWriter.duplicate_form(params[:duplicate], user: User.current)
|
||||
else
|
||||
customer = Customer.find_by(id: params[:customer_id]) if params[:customer_id].present?
|
||||
@form = RedmineQboBilling::EstimateWriter.new_form(customer: customer, user: User.current)
|
||||
end
|
||||
load_customer_drafts('estimate', @form.customer_id)
|
||||
rescue *QBO_ERRORS => e
|
||||
flash[:error] = qbo_message(e)
|
||||
redirect_back_or_default home_path
|
||||
end
|
||||
|
||||
def create
|
||||
find_draft('estimate')
|
||||
return save_draft('estimate', estimate_params) if params[:save_draft].present?
|
||||
|
||||
@form = RedmineQboBilling::EstimateForm.from_params(estimate_params)
|
||||
# Shown again if the save fails; the sales person is always the user creating the estimate
|
||||
@form.sales_person = User.current.name
|
||||
return show_invalid(:new) unless @form.valid?
|
||||
|
||||
saved = RedmineQboBilling::EstimateWriter.save(@form, user: User.current)
|
||||
discard_draft
|
||||
finish_save(saved, l(:notice_estimate_saved_in_quickbooks, number: saved.doc_number))
|
||||
rescue *QBO_ERRORS => e
|
||||
show_error(e, :new)
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
# Writes, edits and emails QuickBooks invoices from Redmine, with the same editor as estimates, plus importing
|
||||
# billable time and open estimates. The customer and product searches are QboEstimatesController's.
|
||||
class QboInvoicesController < ApplicationController
|
||||
include QboDraftHandling
|
||||
|
||||
before_action :authorize_global
|
||||
before_action :require_sender_for_save_and_send, only: [:create, :update]
|
||||
@@ -18,8 +19,15 @@ class QboInvoicesController < ApplicationController
|
||||
|
||||
QBO_ERRORS = QboEstimatesController::QBO_ERRORS
|
||||
|
||||
# A blank invoice, or one made from an estimate (?estimate_id=<estimate id>)
|
||||
# A blank invoice, one made from an estimate (?estimate_id=<estimate id>), or a draft (?draft_id=, see
|
||||
# QboDraft). A draft stays in Redmine, so QuickBooks can't apply a deposit to it while it's being written.
|
||||
def new
|
||||
if params[:draft_id].present?
|
||||
return draft_missing unless find_draft('invoice')
|
||||
@form = form_from_draft(@draft)
|
||||
return
|
||||
end
|
||||
|
||||
@form =
|
||||
if params[:estimate_id].present?
|
||||
RedmineQboBilling::InvoiceWriter.from_estimate(params[:estimate_id], user: User.current)
|
||||
@@ -27,18 +35,24 @@ class QboInvoicesController < ApplicationController
|
||||
customer = Customer.find_by(id: params[:customer_id]) if params[:customer_id].present?
|
||||
RedmineQboBilling::InvoiceWriter.new_form(customer: customer, user: User.current)
|
||||
end
|
||||
load_customer_drafts('invoice', @form.customer_id)
|
||||
rescue *QBO_ERRORS => e
|
||||
flash[:error] = qbo_message(e)
|
||||
redirect_back_or_default home_path
|
||||
end
|
||||
|
||||
def create
|
||||
find_draft('invoice')
|
||||
# The sales person shown on the editor is kept with the draft
|
||||
return save_draft('invoice', invoice_params.to_h.merge('sales_person' => params.dig(:estimate, :sales_person).to_s)) if params[:save_draft].present?
|
||||
|
||||
@form = RedmineQboBilling::EstimateForm.from_params(invoice_params, document: 'invoice')
|
||||
# Display only (e.g. the estimate's sales person); InvoiceWriter.save works out what's saved
|
||||
@form.sales_person = params.dig(:estimate, :sales_person).presence || User.current.name
|
||||
return show_invalid(:new) unless @form.valid?
|
||||
|
||||
saved = RedmineQboBilling::InvoiceWriter.save(@form, user: User.current)
|
||||
discard_draft
|
||||
finish_save(saved, l(:notice_invoice_saved_in_quickbooks, number: saved.doc_number))
|
||||
rescue *QBO_ERRORS => e
|
||||
show_error(e, :new)
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2016 - 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.
|
||||
|
||||
# Estimates, invoices and payments on Redmine's activity page. They come from QuickBooks and belong to no
|
||||
# project, so they show on the overall activity page only (not a project's, nor a user's), and each kind only to
|
||||
# users with one of its global permissions.
|
||||
module QboActivity
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
class_methods do
|
||||
# event_type: the activity type registered in init.rb (e.g. 'qbo_estimates'); icon: the event's CSS class
|
||||
# and icon in this plugin's sprite (assets/images/icons.svg); permissions: any one of these shows it
|
||||
def qbo_activity(event_type, icon:, permissions:, title:, description:, url:)
|
||||
# These models can load before Redmine adds acts_as_event to every model
|
||||
include Redmine::Acts::Event unless respond_to?(:acts_as_event)
|
||||
acts_as_event title: title, description: description, url: url, type: icon,
|
||||
datetime: :activity_datetime, author: nil
|
||||
class_attribute :qbo_activity_permissions, instance_writer: false, default: permissions
|
||||
class_attribute :qbo_activity_event_type, instance_writer: false, default: event_type
|
||||
end
|
||||
|
||||
def qbo_activity_visible_to?(user)
|
||||
qbo_activity_permissions.any? { |permission| user.allowed_to?(permission, nil, global: true) }
|
||||
end
|
||||
|
||||
# What Redmine::Activity::Fetcher reads for a project's activity page; these never show there
|
||||
def activity_provider_options
|
||||
{ qbo_activity_event_type => { permission: qbo_activity_permissions.first } }
|
||||
end
|
||||
|
||||
# Documents dated from..to (the page's days, to not included), newest first. From the QuickBooks date, so
|
||||
# a sync bringing in old documents doesn't pile them onto the day it ran.
|
||||
def find_events(event_type, user, from, to, options)
|
||||
return [] if options[:project] || options[:author] || !qbo_activity_visible_to?(user)
|
||||
|
||||
scope = includes(:customer).where.not(txn_date: nil)
|
||||
scope = scope.where('txn_date >= ?', from.to_date) if from
|
||||
scope = scope.where('txn_date < ?', to.to_date) if to
|
||||
scope = scope.order(txn_date: :desc, id: :desc).limit(options[:limit]) if options[:limit]
|
||||
scope.to_a
|
||||
end
|
||||
end
|
||||
|
||||
# When it happened: the time it reached Redmine when that was its QuickBooks date (created or changed in
|
||||
# QuickBooks and sent by the webhook moments later), else midday on its QuickBooks date (e.g. a full sync)
|
||||
def activity_datetime
|
||||
return created_at if created_at && created_at.getlocal.to_date == txn_date
|
||||
|
||||
Time.local(txn_date.year, txn_date.month, txn_date.day, 12)
|
||||
end
|
||||
|
||||
# Activity events have a project; these don't
|
||||
def project
|
||||
nil
|
||||
end
|
||||
|
||||
def activity_money(amount)
|
||||
ActiveSupport::NumberHelper.number_to_currency(amount) if amount
|
||||
end
|
||||
|
||||
def activity_customer
|
||||
customer&.name.presence || I18n.t(:label_none)
|
||||
end
|
||||
end
|
||||
@@ -17,6 +17,12 @@ class Estimate < QboBaseModel
|
||||
self.primary_key = :id
|
||||
qbo_sync push: false
|
||||
|
||||
include QboActivity
|
||||
qbo_activity 'qbo_estimates', icon: 'qbo-estimate', permissions: %i[view_customers write_estimates],
|
||||
title: ->(e) { I18n.t(:label_activity_estimate, number: e.doc_number, customer: e.activity_customer) },
|
||||
description: ->(e) { [e.activity_money(e[:total_amount]), e[:txn_status]].compact.join(' · ') },
|
||||
url: ->(e) { { controller: 'estimates', action: 'show', id: e.id } }
|
||||
|
||||
def name
|
||||
to_s
|
||||
end
|
||||
|
||||
@@ -17,11 +17,26 @@ class Invoice < QboBaseModel
|
||||
self.primary_key = :id
|
||||
qbo_sync push: false
|
||||
|
||||
include QboActivity
|
||||
qbo_activity 'qbo_invoices', icon: 'qbo-invoice', permissions: %i[view_customers write_invoices],
|
||||
title: ->(i) { I18n.t(:label_activity_invoice, number: i.doc_number, customer: i.activity_customer) },
|
||||
description: ->(i) { i.activity_description },
|
||||
url: ->(i) { { controller: 'invoices', action: 'show', id: i.id } }
|
||||
|
||||
# Return the invoice's document number as its string representation
|
||||
def to_s
|
||||
doc_number
|
||||
end
|
||||
|
||||
# Amount and where it stands, e.g. "$1,250.00 · Partially paid ($400.00 owed)"
|
||||
def activity_description
|
||||
state = status
|
||||
owed = activity_money(self[:balance]) if state && self[:balance].to_d.positive?
|
||||
[activity_money(self[:total_amount]),
|
||||
(I18n.t(:"label_invoice_status_#{state}", default: state.to_s.humanize) if state),
|
||||
(I18n.t(:text_activity_invoice_owed, amount: owed) if owed && state != :open && state != :overdue)].compact.join(' · ')
|
||||
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)
|
||||
|
||||
@@ -18,6 +18,19 @@ class Payment < QboBaseModel
|
||||
self.primary_key = :id
|
||||
qbo_sync push: false
|
||||
|
||||
include QboActivity
|
||||
qbo_activity 'qbo_payments', icon: 'qbo-payment', permissions: %i[view_payments receive_payments],
|
||||
title: ->(p) { I18n.t(:label_activity_payment, customer: p.activity_customer, amount: p.activity_money(p.total_amount)) },
|
||||
description: ->(p) { p.activity_description },
|
||||
url: ->(p) { { controller: 'qbo_payments', action: 'show', id: p.id } }
|
||||
|
||||
# Money not yet applied to an invoice, when there is some
|
||||
def activity_description
|
||||
return '' unless unapplied_amount.to_d.positive?
|
||||
|
||||
I18n.t(:text_activity_payment_unapplied, amount: activity_money(unapplied_amount))
|
||||
end
|
||||
|
||||
def to_s
|
||||
"#{txn_date} #{total_amount}"
|
||||
end
|
||||
|
||||
+3
-2
@@ -23,16 +23,17 @@ class Qbo < ActiveRecord::Base
|
||||
return I18n.t(:label_qbo_never_synced)
|
||||
end
|
||||
|
||||
# Token expiry times, or nil when QuickBooks isn't connected (the settings page shows N/A)
|
||||
def self.oauth2_access_token_expires_at
|
||||
QboConnectionService.current!.oauth2_access_token_expires_at
|
||||
rescue
|
||||
return I18n.t(:label_qbo_never_synced)
|
||||
nil
|
||||
end
|
||||
|
||||
def self.oauth2_refresh_token_expires_at
|
||||
QboConnectionService.current!.oauth2_refresh_token_expires_at
|
||||
rescue
|
||||
return I18n.t(:label_qbo_never_synced)
|
||||
nil
|
||||
end
|
||||
|
||||
# Updates last sync time stamp
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
#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.
|
||||
|
||||
# A new estimate or invoice saved part way, in Redmine only. Nothing is in QuickBooks until it's saved from
|
||||
# the editor, so QuickBooks can't apply a deposit to an invoice that's still being written. Drafts are shared:
|
||||
# anyone with Write estimates (or Write invoices) can continue or delete any estimate (or invoice) draft.
|
||||
class QboDraft < ApplicationRecord
|
||||
DOCUMENTS = %w[estimate invoice].freeze
|
||||
|
||||
belongs_to :customer, optional: true
|
||||
# Who saved it last
|
||||
belongs_to :user, optional: true
|
||||
|
||||
validates :document, inclusion: { in: DOCUMENTS }
|
||||
|
||||
scope :newest_first, -> { order(updated_at: :desc, id: :desc) }
|
||||
scope :for_document, ->(document) { where(document: document.to_s) }
|
||||
|
||||
# The drafts a user may open: estimate drafts need Write estimates, invoice drafts Write invoices
|
||||
scope :visible, lambda { |user = User.current|
|
||||
documents = []
|
||||
documents << 'estimate' if RedmineQboBilling.writer?(user)
|
||||
documents << 'invoice' if RedmineQboBilling.invoice_writer?(user)
|
||||
where(document: documents)
|
||||
}
|
||||
|
||||
def self.writable_document?(document, user = User.current)
|
||||
case document.to_s
|
||||
when 'estimate' then RedmineQboBilling.writer?(user)
|
||||
when 'invoice' then RedmineQboBilling.invoice_writer?(user)
|
||||
else false
|
||||
end
|
||||
end
|
||||
|
||||
# The editor's fields as posted (estimate[...])
|
||||
def fields
|
||||
parsed = JSON.parse(data.presence || '{}')
|
||||
parsed.is_a?(Hash) ? parsed : {}
|
||||
rescue JSON::ParserError
|
||||
{}
|
||||
end
|
||||
|
||||
def fields=(hash)
|
||||
self.data = hash.to_h.to_json
|
||||
end
|
||||
|
||||
# The editor form, filled in as it was saved
|
||||
def form
|
||||
RedmineQboBilling::EstimateForm.from_params(fields, document: document)
|
||||
end
|
||||
|
||||
def invoice?
|
||||
document == 'invoice'
|
||||
end
|
||||
|
||||
def customer_name
|
||||
customer&.name.presence || fields['customer_name'].presence
|
||||
end
|
||||
|
||||
def subtotal
|
||||
form.subtotal
|
||||
end
|
||||
|
||||
def line_count
|
||||
form.filled_lines.count { |line| !line.subtotal? }
|
||||
end
|
||||
end
|
||||
@@ -27,6 +27,7 @@ class InvoiceAttachmentService
|
||||
unless issue.invoices.exists?(@invoice.id)
|
||||
issue.invoices << @invoice
|
||||
issue.save! if issue.changed?
|
||||
journalize(issue)
|
||||
log "Attached invoice ##{@invoice.id} to issue ##{issue.id}"
|
||||
end
|
||||
end
|
||||
@@ -34,6 +35,18 @@ class InvoiceAttachmentService
|
||||
|
||||
private
|
||||
|
||||
# Records the new invoice in the issue's history (shown by RedmineQbo::Patches::IssuesHelperPatch).
|
||||
# The webhook has no Redmine user, so the entry is by Anonymous. Saving the journal also sends
|
||||
# Redmine's usual issue-updated emails.
|
||||
def journalize(issue)
|
||||
journal = Journal.new(journalized: issue, user: User.anonymous, notes: '')
|
||||
journal.details << JournalDetail.new(property: 'invoice', prop_key: @invoice.id.to_s, value: @invoice.to_s.presence || @invoice.id.to_s)
|
||||
journal.save
|
||||
rescue => e
|
||||
# The invoice is attached either way; a missing history entry shouldn't fail the sync
|
||||
log "Couldn't add invoice ##{@invoice.id} to the history of issue ##{issue.id}: #{e.message}"
|
||||
end
|
||||
|
||||
# Extract issue IDs from the invoice's private note and line descriptions
|
||||
def extract_issue_ids
|
||||
ids = []
|
||||
|
||||
@@ -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 %>
|
||||
|
||||
@@ -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">
|
||||
|
||||
@@ -16,6 +16,36 @@
|
||||
multiple: true, size: 8, id: 'settings_labor_item_ids' %>
|
||||
<em class="info"><%= l(:text_labor_items) %></em>
|
||||
</p>
|
||||
<%# QuickBooks custom fields to leave out of estimates and invoices (see RedmineQboBilling::Company). Hidden
|
||||
fields that aren't found right now (e.g. no recent estimate has them) are kept as hidden. %>
|
||||
<% company = RedmineQboBilling::Company %>
|
||||
<% qbo_fields = company.all_custom_fields %>
|
||||
<% hidden_ids = company.hidden_field_ids %>
|
||||
<% sales_field = company.sales_person_field %>
|
||||
<p class="qbo-custom-fields-setting">
|
||||
<label><%= l(:label_qbo_hidden_custom_fields) %></label>
|
||||
<%= hidden_field_tag 'settings[hidden_custom_field_ids][]', '', id: nil %>
|
||||
<% (hidden_ids - qbo_fields.map { |f| f.id.to_i }).each do |id| %>
|
||||
<%= hidden_field_tag 'settings[hidden_custom_field_ids][]', id, id: nil %>
|
||||
<% end %>
|
||||
<% if qbo_fields.empty? %>
|
||||
<%= l(:label_none) %>
|
||||
<% else %>
|
||||
<% qbo_fields.each do |field| %>
|
||||
<label class="block">
|
||||
<%= check_box_tag 'settings[hidden_custom_field_ids][]', field.id, hidden_ids.include?(field.id.to_i),
|
||||
id: "settings_hidden_custom_field_#{field.id}" %>
|
||||
<%= field.name %>
|
||||
<% if sales_field && sales_field.id.to_i == field.id.to_i %>
|
||||
<em>(<%= l(:text_qbo_sales_person_field) %>)</em>
|
||||
<% end %>
|
||||
</label>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<%= link_to sprite_icon('reload', l(:button_refresh_quickbooks_fields)), refresh_company_qbo_estimates_path,
|
||||
method: :post, class: 'icon icon-reload' %>
|
||||
<em class="info"><%= l(:text_qbo_hidden_custom_fields) %></em>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<% issue_builder = RedmineQboBilling::EstimateIssueBuilder %>
|
||||
|
||||
@@ -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 %>
|
||||
@@ -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>
|
||||
@@ -0,0 +1,11 @@
|
||||
<% html_title l(:label_qbo_drafts) %>
|
||||
|
||||
<h2><%= link_to l(:label_customers), customers_path %> » <%= 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 %>
|
||||
@@ -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',
|
||||
|
||||
@@ -415,10 +415,18 @@ $(function () {
|
||||
});
|
||||
|
||||
$('#qbo-estimate-clear-lines').on('click', function () {
|
||||
if (!window.confirm($(this).data('confirmText'))) return;
|
||||
rows().remove();
|
||||
addRow(); addRow();
|
||||
changed();
|
||||
function clearLines() {
|
||||
rows().remove();
|
||||
addRow(); addRow();
|
||||
changed();
|
||||
}
|
||||
// redmine_dark_mode asks in a dark dialog on dark pages; otherwise the browser's own box
|
||||
var message = $(this).data('confirmText');
|
||||
if (window.RedmineDarkMode && window.RedmineDarkMode.confirm) {
|
||||
window.RedmineDarkMode.confirm(message, clearLines);
|
||||
} else if (window.confirm(message)) {
|
||||
clearLines();
|
||||
}
|
||||
});
|
||||
|
||||
// Drag lines by the handle to reorder them; they're saved in table order
|
||||
|
||||
@@ -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 } %>
|
||||
@@ -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 } %>
|
||||
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0"?>
|
||||
<!-- Activity icons (Tabler outline icons, MIT), in the layout of Redmine's own sprite -->
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="icon--sprite">
|
||||
<defs>
|
||||
<symbol viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" id="icon--qbo-estimate">
|
||||
<path d="M9 5h-2a2 2 0 0 0 -2 2v12a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-12a2 2 0 0 0 -2 -2h-2"/>
|
||||
<path d="M9 5a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2a2 2 0 0 1 -2 2h-2a2 2 0 0 1 -2 -2z"/>
|
||||
<path d="M9 12h.01"/>
|
||||
<path d="M13 12h2"/>
|
||||
<path d="M9 16h.01"/>
|
||||
<path d="M13 16h2"/>
|
||||
</symbol>
|
||||
<symbol viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" id="icon--qbo-invoice">
|
||||
<path d="M14 3v4a1 1 0 0 0 1 1h4"/>
|
||||
<path d="M17 21h-10a2 2 0 0 1 -2 -2v-14a2 2 0 0 1 2 -2h7l5 5v11a2 2 0 0 1 -2 2z"/>
|
||||
<path d="M9 7h1"/>
|
||||
<path d="M9 13h6"/>
|
||||
<path d="M13 17h2"/>
|
||||
</symbol>
|
||||
<symbol viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" id="icon--qbo-payment">
|
||||
<path d="M7 11a2 2 0 0 1 2 -2h10a2 2 0 0 1 2 2v6a2 2 0 0 1 -2 2h-10a2 2 0 0 1 -2 -2z"/>
|
||||
<path d="M12 14a2 2 0 1 0 4 0a2 2 0 1 0 -4 0"/>
|
||||
<path d="M17 9v-2a2 2 0 0 0 -2 -2h-10a2 2 0 0 0 -2 2v6a2 2 0 0 0 2 2h2"/>
|
||||
</symbol>
|
||||
</defs>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 1.3 KiB |
@@ -12,3 +12,8 @@
|
||||
margin: -4px 0 10px;
|
||||
padding-left: 0;
|
||||
}
|
||||
|
||||
/* Estimates, invoices and payments on the activity page belong to no project: no " - " after an empty name */
|
||||
div#activity span.project:empty:after {
|
||||
content: none;
|
||||
}
|
||||
@@ -129,6 +129,33 @@ en:
|
||||
button_email_estimate: "Email"
|
||||
button_open_in_quickbooks: "Open in QuickBooks"
|
||||
button_refresh_quickbooks_fields: "Refresh QuickBooks fields"
|
||||
label_qbo_hidden_custom_fields: "Hide QuickBooks custom fields"
|
||||
|
||||
# Estimate and invoice drafts
|
||||
label_qbo_drafts: "Drafts"
|
||||
label_qbo_draft_document: "Draft"
|
||||
label_qbo_draft_lines: "Lines"
|
||||
label_qbo_draft_saved_by: "Last saved"
|
||||
label_qbo_estimate_draft: "Estimate draft"
|
||||
label_qbo_invoice_draft: "Invoice draft"
|
||||
button_qbo_save_draft: "Save draft"
|
||||
button_qbo_delete_draft: "Delete draft"
|
||||
button_qbo_continue_draft: "Continue"
|
||||
notice_qbo_draft_saved: "Draft saved. It's kept in Redmine only, until you save it to QuickBooks."
|
||||
notice_qbo_draft_deleted: "Draft deleted."
|
||||
error_qbo_draft_not_found: "That draft is gone. It may have been finished or deleted."
|
||||
text_qbo_draft_saved_by: "%{user}, %{age} ago"
|
||||
text_qbo_editing_draft: "Draft, not in QuickBooks yet. Last saved by %{user}, %{age} ago. Save it to send it to QuickBooks; that deletes the draft."
|
||||
text_qbo_customer_has_estimate_drafts:
|
||||
one: "This customer has an estimate draft:"
|
||||
other: "This customer has %{count} estimate drafts:"
|
||||
text_qbo_customer_has_invoice_drafts:
|
||||
one: "This customer has an invoice draft:"
|
||||
other: "This customer has %{count} invoice drafts:"
|
||||
text_qbo_draft_link: "%{subtotal}, saved by %{user} %{date}"
|
||||
text_qbo_drafts_info: "New estimates and invoices saved as drafts. They're kept in Redmine only, so nothing is in QuickBooks (and no deposit is applied to an invoice) until a draft is saved from the editor."
|
||||
text_qbo_sales_person_field: "filled in with the sales person"
|
||||
text_qbo_hidden_custom_fields: "Ticked fields are left off estimates and invoices. QuickBooks doesn't tell apps which custom fields are inactive, and a field stays on its old estimates after it's turned off, so tick ones you no longer use (e.g. a renamed \"Sales Rep old\"). The sales person goes in the first field shown whose name has \"sales\" or \"rep\" in it."
|
||||
button_reload_estimate: "Reload the estimate"
|
||||
button_save_and_send: "Save and send"
|
||||
|
||||
@@ -358,3 +385,13 @@ en:
|
||||
text_payment_pick_invoices: "Tick invoices to fill in the amount and pay just those. With none ticked, the payment goes on the oldest invoices first."
|
||||
text_payment_reference: "e.g. the check number"
|
||||
text_payment_unapplied_deposit: "Not applied to any invoice: it's a credit on the customer's account."
|
||||
|
||||
# Activity page
|
||||
label_qbo_estimate_plural: "Estimates"
|
||||
label_qbo_invoice_plural: "Invoices"
|
||||
label_qbo_payment_plural: "Payments"
|
||||
label_activity_estimate: "Estimate #%{number}: %{customer}"
|
||||
label_activity_invoice: "Invoice #%{number}: %{customer}"
|
||||
label_activity_payment: "Payment from %{customer}: %{amount}"
|
||||
text_activity_invoice_owed: "%{amount} owed"
|
||||
text_activity_payment_unapplied: "%{amount} not applied to an invoice"
|
||||
@@ -68,6 +68,9 @@ resources :qbo_invoices, only: [:new, :create, :edit, :update] do
|
||||
end
|
||||
end
|
||||
|
||||
# Estimate and invoice drafts, kept in Redmine until they're saved to QuickBooks (continued on the editors)
|
||||
resources :qbo_drafts, only: [:index, :destroy]
|
||||
|
||||
# Receive payment pages, and a customer's payments (/qbo_payments?customer_id=)
|
||||
resources :qbo_payments, only: [:index, :new, :create, :show] do
|
||||
member do
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#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.
|
||||
|
||||
# Estimate and invoice drafts: what's filled in on the editor, kept in Redmine until it's saved to QuickBooks
|
||||
class CreateQboDrafts < ActiveRecord::Migration[7.0]
|
||||
def up
|
||||
return if table_exists?(:qbo_drafts)
|
||||
|
||||
create_table :qbo_drafts do |t|
|
||||
t.string :document, null: false, limit: 20
|
||||
t.bigint :customer_id
|
||||
# Who saved it last
|
||||
t.integer :user_id
|
||||
# The editor's fields, as posted (JSON)
|
||||
t.text :data, size: :medium
|
||||
t.timestamps
|
||||
end
|
||||
add_index :qbo_drafts, [:document, :customer_id]
|
||||
end
|
||||
|
||||
def down
|
||||
drop_table :qbo_drafts if table_exists?(:qbo_drafts)
|
||||
end
|
||||
end
|
||||
@@ -14,7 +14,7 @@ Redmine::Plugin.register :redmine_qbo do
|
||||
name 'Redmine QBO plugin'
|
||||
author 'Rick Barrette'
|
||||
description 'A pluging for Redmine to connect with QuickBooks Online: customers, time activities for billable hours when an issue is closed, and estimates, invoices and payments written in Redmine'
|
||||
version '2026.11.8'
|
||||
version '2026.11.15'
|
||||
url 'https://github.com/rickbarrette/redmine_qbo'
|
||||
author_url 'https://barrettefabrication.com'
|
||||
settings default: {empty: true}, partial: 'qbo/settings'
|
||||
@@ -40,11 +40,13 @@ Redmine::Plugin.register :redmine_qbo do
|
||||
|
||||
# Billing (global): estimates, invoices, payments, issues from estimates
|
||||
# customer_info and items feed the customer details and product/service search on the estimate/invoice editor
|
||||
permission :write_estimates, { qbo_estimates: [:new, :create, :edit, :update, :customer_info, :items] }, global: true
|
||||
# Drafts (QboDraft): each user sees the drafts of the documents they may write
|
||||
permission :write_estimates, { qbo_estimates: [:new, :create, :edit, :update, :customer_info, :items],
|
||||
qbo_drafts: [:index, :destroy] }, global: true
|
||||
permission :send_estimates, { qbo_estimates: [:send_email] }, global: true
|
||||
permission :create_estimate_issues, { qbo_estimates: [:issues_preview, :create_issues] }, global: true
|
||||
permission :write_invoices, { qbo_invoices: [:new, :create, :edit, :update, :import_options],
|
||||
qbo_estimates: [:customer_info, :items] }, global: true
|
||||
qbo_estimates: [:customer_info, :items], qbo_drafts: [:index, :destroy] }, global: true
|
||||
permission :send_invoices, { qbo_invoices: [:send_email] }, global: true
|
||||
permission :view_payments, { qbo_payments: [:index, :show, :pdf] }, global: true
|
||||
permission :receive_payments, { qbo_payments: [:index, :new, :create, :show, :pdf, :open_invoices],
|
||||
@@ -52,6 +54,13 @@ Redmine::Plugin.register :redmine_qbo do
|
||||
permission :view_business_reports, { qbo_reports: [:index] }, global: true
|
||||
# The product/service list, accounts and their syncs are admin-only (see ItemsController, AccountsController)
|
||||
|
||||
# Estimates, invoices and payments on the activity page (QboActivity): the overall page only, for users with
|
||||
# View customers or Write estimates / invoices, and View or Receive payments
|
||||
# (plugin: picks their icons from this plugin's sprite, assets/images/icons.svg)
|
||||
activity_provider :qbo_estimates, class_name: 'Estimate', plugin: :redmine_qbo
|
||||
activity_provider :qbo_invoices, class_name: 'Invoice', plugin: :redmine_qbo
|
||||
activity_provider :qbo_payments, class_name: 'Payment', plugin: :redmine_qbo
|
||||
|
||||
# Register top menu items
|
||||
menu :top_menu, :customers, { controller: :customers, action: :index }, caption: :label_customers, if: Proc.new {User.current.allowed_to?(:view_customers, nil, global: true)}
|
||||
menu :top_menu, :qbo_reports, { controller: :qbo_reports, action: :index }, caption: :label_business_report,
|
||||
|
||||
@@ -18,11 +18,16 @@ module RedmineQbo
|
||||
IssueQuery.prepend RedmineQbo::Patches::QueryPatch
|
||||
TimeEntryQuery.prepend RedmineQbo::Patches::TimeEntryQueryPatch
|
||||
|
||||
# === Activity: estimates, invoices and payments, for users who may see them ===
|
||||
Redmine::Activity::Fetcher.prepend RedmineQbo::Patches::ActivityFetcherPatch
|
||||
|
||||
# === Controllers ===
|
||||
RedmineQbo::Patches::IssuesControllerPatch.apply
|
||||
RedmineQbo::Patches::AttachmentsControllerPatch.apply
|
||||
|
||||
# === Helpers / Exports ===
|
||||
IssuesHelper.prepend RedmineQbo::Patches::IssuesHelperPatch
|
||||
QueriesHelper.prepend RedmineQbo::Patches::QueriesHelperPatch
|
||||
Redmine::Export::PDF::IssuesPdfHelper.prepend RedmineQbo::Patches::PdfPatch
|
||||
|
||||
# === Hooks ===
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2016 - 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 RedmineQbo
|
||||
module Patches
|
||||
# The activity page lists every activity type for the whole site, and a project's page those its members may
|
||||
# see. QuickBooks estimates, invoices and payments (QboActivity) belong to no project: they're kept to the
|
||||
# overall page, and to users with one of their permissions, so nobody gets a checkbox for activity they can't see.
|
||||
module ActivityFetcherPatch
|
||||
def event_types
|
||||
return @event_types if @qbo_activity_checked
|
||||
|
||||
@qbo_activity_checked = true
|
||||
@event_types = super.select do |event_type|
|
||||
providers = Redmine::Activity.providers[event_type].map(&:constantize)
|
||||
qbo = providers.select { |provider| provider.respond_to?(:qbo_activity_visible_to?) }
|
||||
qbo.empty? || (project.nil? && qbo.any? { |provider| provider.qbo_activity_visible_to?(user) })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,69 @@
|
||||
#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 RedmineQbo
|
||||
module Patches
|
||||
# Shows the customer and estimate by name, and invoices attached by the QuickBooks webhook, in an
|
||||
# issue's history, emails and PDF. Redmine records the customer and estimate by id, like its own
|
||||
# fields, but only looks up names for the fields it knows about. The ids stay in the database, so
|
||||
# existing history entries show names too.
|
||||
module IssuesHelperPatch
|
||||
JOURNAL_NAMED_FIELDS = { 'customer_id' => 'Customer', 'estimate_id' => 'Estimate' }.freeze
|
||||
|
||||
def show_detail(detail, no_html = false, options = {})
|
||||
return qbo_invoice_detail(detail, no_html, options) if detail.property == 'invoice'
|
||||
|
||||
model = detail.property == 'attr' && JOURNAL_NAMED_FIELDS[detail.prop_key]
|
||||
return super unless model
|
||||
|
||||
# Hand Redmine a copy with names in place of ids, so it formats it like any other change
|
||||
named = JournalDetail.new(property: detail.property, prop_key: detail.prop_key,
|
||||
old_value: qbo_journal_name(model, detail.old_value),
|
||||
value: qbo_journal_name(model, detail.value))
|
||||
named.id = detail.id
|
||||
named.journal_id = detail.journal_id
|
||||
super(named, no_html, options)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# "Invoice 1042 added", recorded when the QuickBooks webhook attaches an invoice to the issue
|
||||
# (see InvoiceAttachmentService). The number links to the invoice PDF while the invoice exists.
|
||||
def qbo_invoice_detail(detail, no_html, options)
|
||||
label = l(:field_invoice)
|
||||
value = detail.value
|
||||
old_value = detail.old_value
|
||||
|
||||
unless no_html
|
||||
label = content_tag('strong', label)
|
||||
if value.present?
|
||||
value = Invoice.exists?(detail.prop_key) ? link_to(value, invoice_url(detail.prop_key, only_path: options[:only_path] != false)) : content_tag('i', value)
|
||||
end
|
||||
old_value = content_tag('del', content_tag('i', old_value)) if old_value.present?
|
||||
end
|
||||
|
||||
if detail.value.present?
|
||||
l(:text_journal_added, label: label, value: value).html_safe
|
||||
else
|
||||
l(:text_journal_deleted, label: label, old: old_value).html_safe
|
||||
end
|
||||
end
|
||||
|
||||
# The record's name, or the id when it no longer exists. Cached for the request, since a
|
||||
# history can mention the same customer many times.
|
||||
def qbo_journal_name(model, id)
|
||||
return id if id.blank?
|
||||
|
||||
@qbo_journal_names ||= {}
|
||||
@qbo_journal_names[[model, id]] ||= model.constantize.find_by(id: id)&.to_s.presence || id
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,31 @@
|
||||
#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 RedmineQbo
|
||||
module Patches
|
||||
# Links the Customer column in issue lists to the customer's page, for users who may open it
|
||||
# (View customers). Others see the name as plain text. CSV and PDF exports are unchanged.
|
||||
module QueriesHelperPatch
|
||||
def column_value(column, item, value)
|
||||
return super unless column.name == :customer && value.is_a?(Customer) && qbo_can_view_customers?
|
||||
|
||||
link_to(value.to_s, customer_path(value))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def qbo_can_view_customers?
|
||||
return @qbo_can_view_customers if defined?(@qbo_can_view_customers)
|
||||
|
||||
@qbo_can_view_customers = User.current.allowed_to?(:view_customers, nil, global: true)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -32,7 +32,6 @@ module RedmineQbo
|
||||
unless @available_columns
|
||||
@available_columns = self.class.available_columns.dup
|
||||
@available_columns << QueryColumn.new(:customer, sortable: "#{Issue.table_name}.customer_id", groupable: true, caption: :field_customer)
|
||||
@available_columns << QueryColumn.new(:billed, sortable: "#{TimeEntry.table_name}.billed", groupable: true, caption: :field_billed)
|
||||
end
|
||||
super
|
||||
end
|
||||
|
||||
@@ -17,7 +17,7 @@ module RedmineQbo
|
||||
def available_columns
|
||||
unless @available_columns
|
||||
@available_columns = self.class.available_columns.dup
|
||||
@available_columns << QueryColumn.new(:billed, sortable: "#{TimeEntry.table_name}.name", groupable: true, caption: :field_billed)
|
||||
@available_columns << QueryColumn.new(:billed, sortable: "#{TimeEntry.table_name}.billed", groupable: "#{TimeEntry.table_name}.billed", caption: :field_billed)
|
||||
end
|
||||
super
|
||||
end
|
||||
|
||||
@@ -24,6 +24,8 @@ module RedmineQboBilling
|
||||
MISSING_FIELD_CACHE_TIME = 5.minutes
|
||||
# Recent estimates also show which custom fields exist, for companies whose preferences don't list them
|
||||
RECENT_ESTIMATES = 20
|
||||
# QuickBooks' legacy sales form custom fields; its newer custom fields have larger DefinitionIds
|
||||
LEGACY_FIELD_IDS = (1..3).freeze
|
||||
|
||||
# The sales person field is found loosely, e.g. "Sales Person", "Salesperson", "Sales Rep"
|
||||
SALES_PERSON_NAME = /sales|\brep\b/i
|
||||
@@ -32,11 +34,26 @@ module RedmineQboBilling
|
||||
|
||||
module_function
|
||||
|
||||
# All enabled sales form custom fields, in QuickBooks' order
|
||||
# The sales form custom fields the plugin uses, in QuickBooks' order: all those found, except the ones
|
||||
# hidden in the plugin settings. Everything else (the editor, the sales person, other plugins' VIN and
|
||||
# mileage) goes through here, so a hidden field is never shown or filled in.
|
||||
def custom_fields
|
||||
hidden = hidden_field_ids
|
||||
all_custom_fields.reject { |f| hidden.include?(f.id.to_i) }
|
||||
end
|
||||
|
||||
# Every custom field found, hidden ones included (listed on the plugin settings page)
|
||||
def all_custom_fields
|
||||
Array(settings[:custom_fields]).map { |f| Field.new(f[:id], f[:name]) }
|
||||
end
|
||||
|
||||
# Fields hidden in the plugin settings. QuickBooks doesn't tell apps which custom fields are inactive,
|
||||
# and a field stays on its old estimates after it's turned off (e.g. a renamed "Sales Rep old"), so the
|
||||
# plugin can't leave those out by itself.
|
||||
def hidden_field_ids
|
||||
Array((Setting.plugin_redmine_qbo || {})['hidden_custom_field_ids']).map(&:to_i).reject(&:zero?)
|
||||
end
|
||||
|
||||
# The first enabled custom field whose name matches, e.g. a plugin's VIN field (/\bvin\b/i), or nil
|
||||
def custom_field_matching(pattern)
|
||||
custom_fields.detect { |f| f.name.to_s =~ pattern }
|
||||
@@ -105,9 +122,15 @@ module RedmineQboBilling
|
||||
nil
|
||||
end
|
||||
|
||||
# Fields named in the preferences come first; fields only seen on estimates are added after them.
|
||||
# QuickBooks' newer custom fields (e.g. on Advanced and sandbox companies) may not be in the preferences.
|
||||
# QuickBooks' newer custom fields (made under Settings > Custom fields) aren't in the preferences, which
|
||||
# keep listing the legacy fields (DefinitionIds 1-3) after a company moves to the newer ones, often
|
||||
# renamed or inactive (e.g. "Sales Rep old"). Once estimates show any newer field, those are the
|
||||
# company's fields and the legacy ones are left out. Otherwise the legacy fields named in the
|
||||
# preferences come first, then any only seen on estimates.
|
||||
def merge_fields(from_preferences, from_estimates)
|
||||
newer = from_estimates.reject { |f| LEGACY_FIELD_IDS.cover?(f[:id]) }
|
||||
return newer if newer.any?
|
||||
|
||||
ids = from_preferences.map { |f| f[:id] }
|
||||
from_preferences + from_estimates.reject { |f| ids.include?(f[:id]) }
|
||||
end
|
||||
|
||||
@@ -291,11 +291,17 @@ module RedmineQboBilling
|
||||
# QuickBooks' own subtotal (always after the last line), discount and group lines aren't edited here
|
||||
end
|
||||
|
||||
# The custom field's value, found by the field QuickBooks' preferences name, or by name on the estimate
|
||||
# The custom field's value on a QuickBooks estimate or invoice ("" when it isn't there)
|
||||
def custom_value(remote, field, name_pattern = nil)
|
||||
fields = Array(remote.custom_fields)
|
||||
match = fields.detect { |f| field && f.id.to_i == field.id.to_i } ||
|
||||
(name_pattern && fields.detect { |f| f.name.to_s =~ name_pattern })
|
||||
# The company's field, by id. A document without it has no value: falling back to a similarly named
|
||||
# field picked up retired ones, e.g. a "Sales Rep old" dropdown, which QuickBooks returns as its option
|
||||
# number ("4"), and that number was copied onto invoices as the sales person.
|
||||
return fields.detect { |f| f.id.to_i == field.id.to_i }&.string_value.to_s if field
|
||||
|
||||
# The field isn't known (QuickBooks' settings couldn't be read): found by name, never a hidden one
|
||||
hidden = Company.hidden_field_ids
|
||||
match = name_pattern && fields.detect { |f| f.name.to_s =~ name_pattern && !hidden.include?(f.id.to_i) }
|
||||
match&.string_value.to_s
|
||||
end
|
||||
|
||||
|
||||
Reference in new issue
Block a user