#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 # Reads QuickBooks estimates into an EstimateForm and writes forms back as estimates. # # Subtotal rows are saved the way QuickBooks saves the subtotals added on its own estimate screen: a # description-only line reading "Subtotal: $342.60", which prints where it was placed. The subtotal # QuickBooks always adds after the last line isn't shown (QuickBooks adds it back). The discount (a percent # or an amount) is QuickBooks' discount line after the lines, and estimates with bundle (group) lines are # read-only so a bundle can't be broken up. # # Estimate numbers are never typed: QuickBooks numbers new estimates, or when its custom transaction # numbers are on, the next number is filled in when the estimate is created. module EstimateWriter class Error < StandardError; end # The estimate was saved in QuickBooks (by someone else, or in another tab) after the form was opened class Conflict < Error; end # How QuickBooks writes a subtotal line, e.g. "Subtotal: $1,342.60" SUBTOTAL_TEXT = /\ASubtotal:\s*-?\$?-?[\d,]*(\.\d+)?\s*\z/i module_function # A blank estimate for the customer, filled in the way QuickBooks starts a new estimate def new_form(customer: nil, user: User.current) EstimateForm.new( customer_id: customer&.id, customer_name: customer&.name, email: customer_email(customer), billing_address: customer_address(customer), txn_date: user.today.iso8601, expiration_date: default_expiration_date(user.today)&.iso8601, sales_person: user.name, customer_memo: Company.estimate_message, lines: Array.new(2) { EstimateForm::Line.new(quantity: '', taxable: true) } ) end # Creates an estimate for another plugin (e.g. redmine_qbo_lineitems billing an issue's line items), so # estimates are only ever built here. Each line is a hash of :description, :item_id, :quantity, :rate, # :taxable (a line with only a description is a note line). Returns the saved QuickBooks estimate. # custom_fields: QuickBooks custom field values by field id (see Company.custom_field_matching) def create_estimate(customer:, lines:, private_note: nil, custom_fields: {}, user: nil, txn_date: Date.today) form = EstimateForm.new( customer_id: customer.id, customer_name: customer.name, email: customer_email(customer), txn_date: txn_date.iso8601, expiration_date: default_expiration_date(txn_date)&.iso8601, custom_fields: custom_fields, customer_memo: Company.estimate_message, private_note: private_note.to_s, lines: lines.map do |line| line = line.to_h.symbolize_keys EstimateForm::Line.new( item_id: line[:item_id]&.to_s.presence, description: line[:description].to_s, quantity: plain_number(line[:quantity]), rate: plain_number(line[:rate]), taxable: line.fetch(:taxable, true) ? true : false ) end ) save(form, user: user) end # A new estimate copied from an existing one: same customer, lines, discount, custom fields and messages, dated # today, with a new number and the current user as sales person (bundles aren't copied) def duplicate_form(id, user: User.current) source = load(id) today = user.today EstimateForm.new( customer_id: source.customer_id, customer_name: source.customer_name, email: source.email, billing_address: source.billing_address, txn_date: today.iso8601, expiration_date: default_expiration_date(today)&.iso8601, sales_person: user.name, custom_fields: source.custom_fields.dup, customer_memo: source.customer_memo, private_note: source.private_note, discount_type: source.discount_type, discount_value: source.discount_value, lines: source.lines.map do |line| EstimateForm::Line.new(kind: line.kind, item_id: line.item_id, item_name: line.item_name, description: line.description, quantity: line.quantity, rate: line.rate, taxable: line.taxable) end ).tap { |form| form.duplicated_from = source.doc_number } end # Issues linked to the estimate (redmine_qbo's issue estimate field) def linked_issues(estimate_id) return Issue.none if estimate_id.blank? Issue.where(estimate_id: estimate_id.to_s) end # Loads the estimate from QuickBooks for editing def load(id) remote = QboEstimateService.with_service { |service| service.fetch_by_id(id) } raise Error, I18n.t(:notice_estimate_not_found) unless remote form_from_remote(remote) end # Creates or updates the estimate in QuickBooks and returns the saved QuickBooks estimate. # The sales person is the user who creates the estimate; editing keeps the original one. def save(form, user: User.current) customer = Customer.find_by(id: form.customer_id) raise Error, I18n.t(:error_estimate_needs_customer) unless customer saved = QboEstimateService.with_service do |service| if form.persisted? 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? estimate = Quickbooks::Model::Estimate.new estimate.id = remote.id estimate.sync_token = remote.sync_token apply(form, estimate, customer: customer, remote: remote, user: user) service.update(estimate, sparse: true) else estimate = Quickbooks::Model::Estimate.new apply(form, estimate, customer: customer, remote: nil, user: user) service.create(estimate) end end raise Error, I18n.t(:error_estimate_not_saved) unless saved sync_local(saved) saved end # An estimate's custom field value (by field id or Company::Field), read with QuickBooks' newer custom # fields (a plain fetch, e.g. Estimate#details, leaves them out), or nil. Cached for a minute, since # creating issues from an estimate may ask once per issue. def custom_field_value(estimate_id, field) return nil if estimate_id.blank? || field.nil? id = field.respond_to?(:id) ? field.id : field values = Rails.cache.fetch("redmine_qbo:estimate_custom_fields:#{estimate_id}", expires_in: 1.minute) do remote = QboEstimateService.with_service { |service| service.fetch_by_id(estimate_id) } remote ? custom_field_values(remote) : {} end values[id.to_s].presence rescue StandardError => e Rails.logger.warn "[RedmineQboBilling] Could not read estimate #{estimate_id}'s custom fields: #{e.message}" nil end # A QuickBooks estimate's or invoice's text custom field values by field id: { "2" => "..." } def custom_field_values(remote) Array(remote.custom_fields).each_with_object({}) do |f, values| next unless f.type.to_s == 'StringType' || f.string_value.present? values[f.id.to_s] = f.string_value.to_s end end # Statuses an estimate moves from to Accepted when the work starts (issues are created from it) ACCEPTABLE_STATUSES = ['', 'Pending'].freeze # A sparse update that only changes an estimate's status. QuickBooks still requires the estimate's lines # in it ("At least 1 line item is required"), so they're sent back exactly as QuickBooks returned them. def status_update(estimate, status) update = Quickbooks::Model::Estimate.new update.id = estimate.id update.sync_token = estimate.sync_token update.customer_ref = estimate.customer_ref update.line_items = Array(estimate.line_items) update.txn_status = status update end AcceptResult = Struct.new(:outcome, :message) do def accepted?; outcome == :accepted; end def failed?; outcome == :failed; end end # Marks the estimate Accepted in QuickBooks when issues are created from it (the customer went ahead with # the work), accepted today by the customer, as QuickBooks' own Accept does. Only a pending estimate # changes; Accepted, Closed, Converted or Rejected ones are left as they are. Returns an AcceptResult: # :accepted, :unchanged, or :failed with QuickBooks' message (never raised: the issues stand). def accept(estimate_id, user: User.current) outcome, status = QboEstimateService.with_service do |service| estimate = service.fetch_by_id(estimate_id) next [:failed, I18n.t(:notice_estimate_not_found)] if estimate.nil? next [:unchanged, estimate.txn_status.to_s] unless ACCEPTABLE_STATUSES.include?(estimate.txn_status.to_s) update = status_update(estimate, 'Accepted') update.accepted_date = user.today update.accepted_by = estimate.customer_ref&.name.presence || Customer.find_by(id: estimate.customer_ref&.value)&.name saved = service.update(update, sparse: true) saved&.txn_status.to_s == 'Accepted' ? [:accepted, nil] : [:failed, saved&.txn_status.to_s.presence || 'no answer'] end if outcome == :accepted sync_local(Struct.new(:id).new(estimate_id)) AcceptResult.new(:accepted, nil) elsif outcome == :unchanged AcceptResult.new(:unchanged, status) else message = I18n.t(:error_estimate_not_accepted, status: status) Rails.logger.warn "[RedmineQboBilling] Estimate #{estimate_id}: #{message}" AcceptResult.new(:failed, message) end rescue StandardError => e message = e.respond_to?(:detail) && e.detail.present? ? "#{e.message}: #{e.detail}" : e.message Rails.logger.warn "[RedmineQboBilling] Could not mark estimate #{estimate_id} accepted: #{message}" AcceptResult.new(:failed, message) end # Emails the estimate; QuickBooks uses its own estimate email template def send_email(id, email) sent = QboEstimateService.with_service { |service| service.send_estimate(id, email) } raise Error, I18n.t(:error_estimate_not_sent) unless sent sent end # ---- 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, sync_token: remote.sync_token.to_s, customer_id: remote.customer_ref&.value.to_s, customer_name: remote.customer_ref&.name.presence || Customer.find_by(id: remote.customer_ref&.value)&.name, email: remote.bill_email&.address.to_s, billing_address: format_address(remote.billing_address), txn_date: remote.txn_date&.iso8601, expiration_date: remote.expiration_date&.iso8601, doc_number: remote.doc_number.to_s, sales_person: custom_value(remote, Company.sales_person_field, Company::SALES_PERSON_NAME), custom_fields: custom_field_values(remote), customer_memo: remote.customer_memo.to_s, private_note: remote.private_note.to_s, lines: tidy_subtotals(Array(remote.line_items).filter_map { |line| line_from_remote(line) }), total: remote.total, total_tax: remote.txn_tax_detail&.total_tax, discount: discount_total(remote), **discount_fields(remote), email_status: remote.email_status.to_s, txn_status: remote.txn_status.to_s, 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 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 def line_from_remote(line) if line.sales_item? detail = line.sales_line_item_detail EstimateForm::Line.new( id: line.id.to_s, item_id: detail&.item_ref&.value.to_s.presence, item_name: detail&.item_ref&.name.to_s, description: line.description.to_s, quantity: plain_number(detail&.quantity), rate: plain_number(detail&.unit_price), taxable: detail&.tax_code_ref&.value.to_s == 'TAX', time_activity_id: linked_time_activity_id(line) ) elsif line.description_only? && line.description.to_s.strip.match?(SUBTOTAL_TEXT) EstimateForm::Line.new(id: line.id.to_s, kind: EstimateForm::Line::SUBTOTAL) elsif line.description_only? EstimateForm::Line.new(id: line.id.to_s, description: line.description.to_s) end # QuickBooks' own subtotal (always after the last line), discount and group lines aren't edited here end # 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) # 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 # ---- form -> QuickBooks estimate ---- def apply(form, estimate, customer:, remote:, user:) estimate.customer_id = customer.id # QuickBooks copies the customer's billing address only when an estimate is created if remote.nil? || remote.customer_ref&.value.to_s != customer.id.to_s address = customer_details(customer)&.billing_address estimate.billing_address = address if address end estimate.bill_email = Quickbooks::Model::EmailAddress.new(form.email) if form.email.present? estimate.txn_date = form.txn_date_value estimate.expiration_date = form.expiration_date_value # Numbers aren't typed. With QuickBooks' custom transaction numbers on, it doesn't number estimates # made by apps, so the next number is filled in here. estimate.doc_number = next_doc_number if remote.nil? && Company.custom_txn_numbers? estimate.customer_memo = form.customer_memo.to_s estimate.private_note = form.private_note.to_s estimate.custom_fields = build_custom_fields(form, remote, user) estimate.line_items = build_lines(form, remote) discount_before_tax(estimate, form) estimate end # A discount comes off before sales tax: QuickBooks takes the discount from the subtotal, then works out # the tax on what's left (otherwise it taxes the full amount and discounts after) def discount_before_tax(document, form) document.apply_tax_after_discount = true if form.discount? end # Keeps every custom field already on the estimate, then sets the ones edited on the form, and the sales # person on create # (left empty for estimates made without a user, e.g. by a background job) # sales_person: who to name instead of the user (e.g. an invoice names the estimate's sales person) def build_custom_fields(form, remote, user, sales_person: nil) fields = {} Array(remote&.custom_fields).each do |f| next unless f.type.to_s == 'StringType' || f.string_value.present? fields[f.id.to_i] = string_field(f.id, f.name, f.string_value) end Company.editable_custom_fields.each do |field| next unless form.custom_fields.key?(field.id.to_s) fields[field.id.to_i] = string_field(field.id, field.name, form.custom_field(field)) end sales = Company.sales_person_field name = sales_person.presence || user&.name if sales && remote.nil? && name.present? fields[sales.id.to_i] = string_field(sales.id, sales.name, name) end fields.values end def string_field(id, name, value) field = Quickbooks::Model::CustomField.new field.id = id.to_i field.name = name field.type = 'StringType' field.string_value = value.to_s field end def build_lines(form, remote) remote_ids = Array(remote&.line_items).map { |l| l.id.to_s } section = BigDecimal('0') lines = tidy_subtotals(form.filled_lines).map.with_index(1) do |line, number| built = build_line(line, number, keep_id: remote_ids.include?(line.id.to_s), section_total: section) section = line.subtotal? ? BigDecimal('0') : section + (line.description_only? ? BigDecimal('0') : built.amount) built end # The discount goes after the lines, as on QuickBooks' own screen discount = discount_line(form, remote, lines.size + 1) lines << discount if discount lines end # QuickBooks' discount line for the form's discount, or nil (taken off before tax, see discount_before_tax). A percent discount is sent as a percent so # QuickBooks works it out from its own subtotal; the discount account already on the document is kept # (otherwise QuickBooks uses its default discount account). def discount_line(form, remote, number) return nil unless form.discount? account = Array(remote&.line_items).detect(&:discount_item?)&.discount_line_detail&.discount_account_ref line = Quickbooks::Model::InvoiceLineItem.new line.line_num = number line.amount = form.discount_amount line.discount_item! do |detail| detail.percent_based = form.percent_discount? detail.discount_percent = form.discount_value_number if form.percent_discount? detail.discount_account_ref = account if account end line end # The editor's discount fields from a document's discount lines: one percent line stays a percent; # an amount, or several discount lines, become their total in dollars def discount_fields(remote) discounts = Array(remote.line_items).select(&:discount_item?) return {} if discounts.empty? detail = discounts.first.discount_line_detail if discounts.size == 1 && detail&.percent_based?.to_s == 'true' && detail.discount_percent { discount_type: 'percent', discount_value: plain_number(detail.discount_percent) } else { discount_type: 'amount', discount_value: plain_number(discount_total(remote)) } end end def build_line(line, number, keep_id:, section_total: BigDecimal('0')) remote = Quickbooks::Model::InvoiceLineItem.new remote.id = line.id if keep_id remote.line_num = number # Saved like QuickBooks saves its subtotals: a description line with the total of its section if line.subtotal? remote.description = subtotal_text(section_total) remote.description_only! return remote end remote.description = line.description if line.description_only? remote.description_only! return remote end rate = line.rate_value rate ||= Item.find_by(id: line.item_id)&.unit_price if line.item? rate ||= BigDecimal('0') quantity = line.quantity_value || BigDecimal('1') remote.amount = (quantity * rate).round(2) link_time_activity(remote, line.time_activity_id) remote.sales_item! do |detail| detail.item_ref = Quickbooks::Model::BaseReference.new(line.item_id) if line.item? detail.quantity = quantity detail.unit_price = rate detail.tax_code_ref = Quickbooks::Model::BaseReference.new(line.taxable? ? 'TAX' : 'NON') end remote end # ---- helpers ---- TIME_ACTIVITY = 'TimeActivity'.freeze # The time activity an invoice line bills, if any def linked_time_activity_id(line) return nil unless line.respond_to?(:linked_transactions) Array(line.linked_transactions).detect { |t| t.txn_type.to_s == TIME_ACTIVITY }&.txn_id&.to_s end # Links an invoice line to the time activity it bills (TxnLineId is always 1 for time activities) def link_time_activity(remote_line, time_activity_id) return if time_activity_id.blank? || !remote_line.respond_to?(:linked_transactions=) remote_line.linked_transactions = [ Quickbooks::Model::LinkedTransaction.new(txn_id: time_activity_id.to_s, txn_type: TIME_ACTIVITY, txn_line_id: '1') ] end # Drops subtotals with nothing to total (at the top, or right after another subtotal) def tidy_subtotals(lines) tidy = [] lines.each do |line| next if line.subtotal? && (tidy.empty? || tidy.last.subtotal?) tidy << line end tidy end # "Subtotal: $1,342.60", as QuickBooks writes it def subtotal_text(amount) value = BigDecimal(amount.to_s).round(2) whole, cents = format('%.2f', value.abs).split('.') "Subtotal: #{'-' if value.negative?}$#{whole.reverse.scan(/\d{1,3}/).join(',').reverse}.#{cents}" end # What the document's discount lines take off, in dollars def discount_total(remote) discounts = Array(remote.line_items).select(&:discount_item?) discounts.empty? ? nil : discounts.sum(BigDecimal('0')) { |l| l.amount || BigDecimal('0') } end def group_lines?(remote) Array(remote.line_items).any?(&:group_line_detail?) end # Updates the local estimate record so it shows on the customer page right away def sync_local(saved) EstimateSyncService.new.sync_by_id(saved.id) rescue StandardError => e Rails.logger.warn "[RedmineQboBilling] Saved estimate #{saved.id} but could not sync it locally: #{e.message}" end # The plugin setting "Estimates expire after (days)"; blank or 0 leaves the expiration date empty def expiration_days Setting.plugin_redmine_qbo['expiration_days'].to_i rescue StandardError 0 end def default_expiration_date(from) expiration_days.positive? ? from + expiration_days : nil end # One more than the highest estimate (or invoice) number used so far def next_doc_number(model = Estimate) numbers = model.order(id: :desc).limit(500).pluck(:doc_number).filter_map { |n| Integer(n.to_s, 10) rescue nil } numbers.empty? ? nil : (numbers.max + 1).to_s end def customer_details(customer) customer&.details rescue StandardError => e Rails.logger.warn "[RedmineQboBilling] Could not load customer #{customer&.id} from QuickBooks: #{e.message}" nil end def customer_email(customer) customer_details(customer)&.email_address&.address.to_s end def customer_address(customer) format_address(customer_details(customer)&.billing_address) end # Address lines as QuickBooks prints them: street lines, then "City, ST 12345" def format_address(address) return '' unless address street = [address.line1, address.line2, address.line3, address.line4, address.line5].map { |l| l.to_s.strip }.reject(&:empty?) region = [address.country_sub_division_code, address.postal_code].map { |l| l.to_s.strip }.reject(&:empty?).join(' ') locality = [address.city.to_s.strip, region].reject(&:empty?).join(', ') (street + [locality]).reject(&:empty?).join("\n") end # 2.0 -> "2", 12.50 -> "12.5"; the editor formats rates for display def plain_number(value) return '' if value.nil? || value.to_s.strip.empty? BigDecimal(value.to_s).to_s('F').sub(/\.0+\z/, '').sub(/(\.\d*?)0+\z/, '\1') end end end