mirror of
https://github.com/rickbarrette/redmine_qbo_labels.git
synced 2026-11-17 17:50:36 -05:00
Print label in the sidebar, filled in from the page
A Labels section in the sidebar of every page: Print label fills the label in from the page's customer (their name), estimate, invoice, appointment (its customer and estimate) or work order, or opens a blank label anywhere else. When there's a customer, the PO number field offers their latest estimates, invoices and open work orders. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3e29f14f76
commit
98a9bcc1bf
11 files changed
+239
-18
No files matched your search
@@ -10,8 +10,16 @@
|
||||
|
||||
module RedmineQboLabels
|
||||
module Hooks
|
||||
# "Print label" in the top-right actions of issues, estimates and invoices
|
||||
# "Print label" in the top-right actions of issues, estimates and invoices, and in the sidebar of every page
|
||||
class ViewHookListener < Redmine::Hook::ViewListener
|
||||
# Pages the sidebar's Print label fills the label from: [controller, action] => how to find the record
|
||||
SIDEBAR_SOURCES = {
|
||||
%w[issues show] => ['issue', ->(c) { c.instance_variable_get(:@issue) }],
|
||||
%w[qbo_estimates edit] => ['estimate', ->(c) { c.instance_variable_get(:@form) }],
|
||||
%w[qbo_invoices edit] => ['invoice', ->(c) { c.instance_variable_get(:@form) }],
|
||||
%w[customers show] => ['customer', ->(c) { c.instance_variable_get(:@customer) }],
|
||||
%w[customer_appointments show] => ['appointment', ->(c) { c.instance_variable_get(:@appointment) }]
|
||||
}.freeze
|
||||
|
||||
# The print dialog's script and styles
|
||||
def view_layouts_base_html_head(context = {})
|
||||
@@ -39,8 +47,37 @@ module RedmineQboLabels
|
||||
context[:hook_caller].render(partial: 'label_hooks/issue_print_link', locals: { issue: issue })
|
||||
end
|
||||
|
||||
# The sidebar's Labels section: Print label for what the page shows (a customer, estimate, invoice,
|
||||
# appointment or work order), or a blank label anywhere else
|
||||
def view_layouts_base_sidebar(context = {})
|
||||
return '' unless RedmineQboLabels.printing_available?
|
||||
|
||||
source_type, record = sidebar_source(context[:controller])
|
||||
context[:hook_caller].render(partial: 'label_hooks/sidebar',
|
||||
locals: { source_type: source_type, source_id: record&.id,
|
||||
title: sidebar_title(source_type, record) })
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def sidebar_source(controller)
|
||||
return [nil, nil] unless controller
|
||||
|
||||
type, finder = SIDEBAR_SOURCES[[controller.controller_name, controller.action_name]]
|
||||
record = finder&.call(controller)
|
||||
record&.id.present? ? [type, record] : [nil, nil]
|
||||
end
|
||||
|
||||
# What the page shows, from what the page already loaded (no QuickBooks lookups on every page)
|
||||
def sidebar_title(source_type, record)
|
||||
case source_type
|
||||
when 'issue' then "#{l(:label_qbo_labels_source_issue)} ##{record.id}"
|
||||
when 'estimate', 'invoice' then "#{l("label_qbo_labels_source_#{source_type}")} ##{record.doc_number}"
|
||||
when 'customer' then record.name
|
||||
when 'appointment' then record.try(:subject).presence || "#{l(:label_qbo_labels_source_appointment)} ##{record.id}"
|
||||
end
|
||||
end
|
||||
|
||||
def print_link(context, source_type, source_id)
|
||||
return '' if source_id.blank? || !RedmineQboLabels.printing_available?
|
||||
|
||||
|
||||
@@ -9,51 +9,87 @@
|
||||
#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 RedmineQboLabels
|
||||
# What goes on a part label for an issue, estimate or invoice: the customer's last name and the PO number
|
||||
# parts are ordered with (Sales SOP): E + estimate number, I + invoice number, W + work order (issue) number
|
||||
# What goes on a part label: the customer's last name and the PO number parts are ordered with (Sales SOP):
|
||||
# E + estimate number, I + invoice number, W + work order (issue) number.
|
||||
#
|
||||
# From an issue, estimate or invoice, both lines are filled in; from a customer, their name (with their
|
||||
# estimates, invoices and open work orders offered as PO numbers); from an appointment (redmine_qbo_calendar),
|
||||
# its customer and its estimate. A blank label (no type) is for anything else.
|
||||
class LabelSource
|
||||
TYPES = %w[issue estimate invoice].freeze
|
||||
TYPES = %w[issue estimate invoice customer appointment].freeze
|
||||
PREFIXES = { 'issue' => 'W', 'estimate' => 'E', 'invoice' => 'I' }.freeze
|
||||
NAME_SUFFIXES = /\A(jr|sr|ii|iii|iv|v|esq)\.?\z/i
|
||||
# PO numbers offered in the print dialog, of each kind
|
||||
SUGGESTIONS = 8
|
||||
|
||||
attr_reader :type, :record
|
||||
|
||||
# The issue, estimate or invoice the current user may see, or nil
|
||||
# The record of that type the current user may see (as a LabelSource), or nil. No type: a blank label.
|
||||
def self.find(type, id)
|
||||
type = type.to_s
|
||||
return blank if type.empty?
|
||||
return nil unless TYPES.include?(type)
|
||||
|
||||
record = case type
|
||||
when 'issue' then Issue.visible.find_by(id: id)
|
||||
when 'estimate' then Estimate.find_by(id: id) if billing_visible?(:write_estimates)
|
||||
when 'invoice' then Invoice.find_by(id: id) if billing_visible?(:write_invoices)
|
||||
when 'customer' then Customer.find_by(id: id) if customers_visible?
|
||||
when 'appointment' then find_appointment(id)
|
||||
end
|
||||
record && new(type, record)
|
||||
end
|
||||
|
||||
def self.blank
|
||||
new(nil, nil)
|
||||
end
|
||||
|
||||
def self.find_appointment(id)
|
||||
return nil unless defined?(CustomerAppointment) &&
|
||||
User.current.allowed_to?(:view_customer_appointments, nil, global: true)
|
||||
|
||||
CustomerAppointment.find_by(id: id)
|
||||
end
|
||||
private_class_method :find_appointment
|
||||
|
||||
# Estimates and invoices are seen by those who view customers or write them (redmine_qbo's permissions)
|
||||
def self.billing_visible?(write_permission, user = User.current)
|
||||
[:view_customers, write_permission].any? { |permission| user.allowed_to?(permission, nil, global: true) }
|
||||
end
|
||||
private_class_method :billing_visible?
|
||||
|
||||
def self.customers_visible?(user = User.current)
|
||||
user.allowed_to?(:view_customers, nil, global: true)
|
||||
end
|
||||
private_class_method :customers_visible?
|
||||
|
||||
def initialize(type, record)
|
||||
@type = type
|
||||
@record = record
|
||||
end
|
||||
|
||||
def blank?
|
||||
record.nil?
|
||||
end
|
||||
|
||||
def id
|
||||
record.id
|
||||
record&.id
|
||||
end
|
||||
|
||||
def customer
|
||||
return nil if blank?
|
||||
return record if type == 'customer'
|
||||
|
||||
record.respond_to?(:customer) ? record.customer : nil
|
||||
end
|
||||
|
||||
# E1234, I1234 or W1234
|
||||
# E1234, I1234 or W1234 (an appointment's is its estimate's); blank for a customer or a blank label
|
||||
def po_number
|
||||
number = type == 'issue' ? record.id : record.doc_number
|
||||
"#{PREFIXES[type]}#{number}"
|
||||
case type
|
||||
when 'issue' then "W#{record.id}"
|
||||
when 'estimate', 'invoice' then "#{PREFIXES[type]}#{record.doc_number}"
|
||||
when 'appointment' then record.estimate ? "E#{record.estimate.doc_number}" : ''
|
||||
else ''
|
||||
end
|
||||
end
|
||||
|
||||
def last_name
|
||||
@@ -65,10 +101,38 @@ module RedmineQboLabels
|
||||
[last_name, po_number]
|
||||
end
|
||||
|
||||
# Shown in the job list: "Estimate #1234"
|
||||
# The customer's estimates, invoices and open work orders the user may see, newest first, as
|
||||
# [[po_number, description]], offered for the PO number in the print dialog
|
||||
def po_suggestions
|
||||
customer = self.customer
|
||||
return [] unless customer
|
||||
|
||||
suggestions = []
|
||||
if self.class.billing_visible?(:write_estimates)
|
||||
customer.estimates.where.not(doc_number: [nil, '']).order(txn_date: :desc).limit(SUGGESTIONS).each do |estimate|
|
||||
suggestions << ["E#{estimate.doc_number}", describe(:label_qbo_labels_source_estimate, estimate.txn_date)]
|
||||
end
|
||||
end
|
||||
if self.class.billing_visible?(:write_invoices)
|
||||
customer.invoices.where.not(doc_number: [nil, '']).order(txn_date: :desc).limit(SUGGESTIONS).each do |invoice|
|
||||
suggestions << ["I#{invoice.doc_number}", describe(:label_qbo_labels_source_invoice, invoice.txn_date)]
|
||||
end
|
||||
end
|
||||
Issue.visible.open.where(customer_id: customer.id).order(id: :desc).limit(SUGGESTIONS).each do |issue|
|
||||
suggestions << ["W#{issue.id}", issue.subject.to_s.truncate(50)]
|
||||
end
|
||||
suggestions.uniq(&:first)
|
||||
end
|
||||
|
||||
# What it's for, e.g. "Estimate #1234", "Customer Jane Smith"; nil for a blank label
|
||||
def title
|
||||
number = type == 'issue' ? record.id : record.doc_number
|
||||
"#{I18n.t("label_qbo_labels_source_#{type}")} ##{number}"
|
||||
case type
|
||||
when nil then nil
|
||||
when 'issue' then "#{I18n.t(:label_qbo_labels_source_issue)} ##{record.id}"
|
||||
when 'estimate', 'invoice' then "#{I18n.t("label_qbo_labels_source_#{type}")} ##{record.doc_number}"
|
||||
when 'customer' then "#{I18n.t(:label_qbo_labels_source_customer)} #{record.name}"
|
||||
when 'appointment' then "#{I18n.t(:label_qbo_labels_source_appointment)} ##{record.id}"
|
||||
end
|
||||
end
|
||||
|
||||
# The customer's last name: their family name in QuickBooks, or for a business their full name, or else
|
||||
@@ -94,5 +158,11 @@ module RedmineQboLabels
|
||||
nil
|
||||
end
|
||||
private_class_method :qbo_details
|
||||
|
||||
private
|
||||
|
||||
def describe(label, date)
|
||||
[I18n.t(label), date.respond_to?(:strftime) ? I18n.l(date.to_date) : date].compact_blank.join(' ')
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user