mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-17 18:20:36 -05:00
- Estimates and invoices take a discount, as a percent of the subtotal or an amount, saved as QuickBooks' discount line and taken off before sales tax. Duplicated estimates and invoices made from an estimate keep it. The box is hidden when discounts are off in QuickBooks. - A paid invoice can be unlocked for editing after a confirmation. The payments stay linked, and the total can't go below what's been paid. Unlocking needs the new "Edit partially paid invoices" or "Edit paid invoices" permission, checked again when saving. - Saved payments can be edited: date, method, reference, memo, amount and the invoices they pay. Each invoice keeps its amount unless the amount or the invoices change. Payments that also apply credit memos stay read-only. - Locked estimates and invoices are readable again (disabled boxes had faint or invisible text, especially in Safari). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
176 lines
7.5 KiB
Ruby
176 lines
7.5 KiB
Ruby
#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.
|
|
|
|
# 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]
|
|
before_action :find_customer, only: :import_options
|
|
|
|
QBO_ERRORS = QboEstimatesController::QBO_ERRORS
|
|
|
|
# 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)
|
|
else
|
|
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)
|
|
end
|
|
|
|
# ?unlock=1 opens a paid invoice for editing anyway (after the editor's confirmation)
|
|
def edit
|
|
@form = RedmineQboBilling::InvoiceWriter.load(params[:id])
|
|
unlock_form if unlock?
|
|
rescue *QBO_ERRORS => e
|
|
flash[:error] = qbo_message(e)
|
|
redirect_back_or_default home_path
|
|
end
|
|
|
|
def update
|
|
@form = RedmineQboBilling::EstimateForm.from_params(invoice_params, id: params[:id], document: 'invoice')
|
|
@form.sales_person = params.dig(:estimate, :sales_person).to_s
|
|
unlock_form if unlock?
|
|
return show_invalid(:edit) unless @form.valid?
|
|
|
|
saved = RedmineQboBilling::InvoiceWriter.save(@form, user: User.current, unlock: unlock?)
|
|
finish_save(saved, l(:notice_invoice_saved_in_quickbooks, number: saved.doc_number))
|
|
rescue RedmineQboBilling::InvoiceWriter::Conflict => e
|
|
@conflict = true
|
|
show_error(e, :edit)
|
|
rescue *QBO_ERRORS => e
|
|
show_error(e, :edit)
|
|
end
|
|
|
|
# Emails an invoice that's already saved (e.g. resending it)
|
|
def send_email
|
|
sent = RedmineQboBilling::InvoiceWriter.send_email(params[:id], params[:email])
|
|
flash[:notice] = l(:notice_invoice_sent, number: sent.doc_number, email: sent.bill_email&.address || params[:email])
|
|
redirect_to edit_qbo_invoice_path(params[:id])
|
|
rescue *QBO_ERRORS => e
|
|
flash[:error] = qbo_message(e)
|
|
redirect_to edit_qbo_invoice_path(params[:id])
|
|
end
|
|
|
|
# The customer's billable time and open estimates, for the Import panel
|
|
def import_options
|
|
render json: RedmineQboBilling::InvoiceWriter.import_options(@customer, issue_ids: Array(params[:issue_ids]))
|
|
rescue *QBO_ERRORS => e
|
|
render json: { error: qbo_message(e) }, status: :unprocessable_entity
|
|
end
|
|
|
|
private
|
|
|
|
def find_customer
|
|
@customer = Customer.find_by(id: params[:customer_id])
|
|
render(json: { error: l(:error_estimate_needs_customer) }, status: :not_found) unless @customer
|
|
end
|
|
|
|
def invoice_params
|
|
params.require(:estimate).permit(
|
|
:sync_token, :total_tax, :discount, :email_status, :txn_status, :balance,
|
|
*RedmineQboBilling::EstimateForm::HEADER_ATTRIBUTES,
|
|
linked_estimate_ids: [],
|
|
custom_fields: {},
|
|
lines: RedmineQboBilling::EstimateForm::Line::PARAMS
|
|
)
|
|
end
|
|
|
|
def unlock?
|
|
params[:unlock].to_s == '1'
|
|
end
|
|
|
|
# Shows a paid invoice editable, with the amount paid on it (posted back with the form when saving), for
|
|
# users allowed to edit it (paid off or partly paid). InvoiceWriter.save checks again against QuickBooks.
|
|
def unlock_form
|
|
@form.amount_paid ||= RedmineQboBilling::EstimateForm.decimal(params.dig(:estimate, :amount_paid))
|
|
return unless @form.locked == :paid || @form.locked.nil?
|
|
return unless RedmineQboBilling.paid_invoice_editor?(@form.fully_paid?)
|
|
|
|
@form.locked = nil
|
|
@form.unlocked = true
|
|
end
|
|
|
|
def save_and_send?
|
|
params[:save_and_send].present?
|
|
end
|
|
|
|
def require_sender_for_save_and_send
|
|
deny_access if save_and_send? && !RedmineQboBilling.invoice_sender?
|
|
end
|
|
|
|
# After saving: email it if asked, then reopen the saved invoice
|
|
def finish_save(saved, notice)
|
|
if save_and_send?
|
|
begin
|
|
sent = RedmineQboBilling::InvoiceWriter.send_email(saved.id, @form.email)
|
|
notice = l(:notice_invoice_saved_and_sent, number: sent.doc_number, email: sent.bill_email&.address || @form.email)
|
|
rescue *QBO_ERRORS => e
|
|
flash[:error] = l(:error_estimate_saved_not_sent, message: qbo_message(e))
|
|
end
|
|
end
|
|
flash[:notice] = notice
|
|
redirect_to edit_qbo_invoice_path(saved.id)
|
|
end
|
|
|
|
def show_error(error, view)
|
|
logger.warn "[RedmineQboBilling] #{error.class}: #{qbo_message(error)}"
|
|
@form.errors.add(:base, qbo_message(error))
|
|
show_invalid(view)
|
|
end
|
|
|
|
# Shows the form again with what was typed (see QboEstimatesController#show_invalid)
|
|
def show_invalid(view)
|
|
@form.lines = @form.filled_lines
|
|
customer = Customer.find_by(id: @form.customer_id) if @form.customer_id.present?
|
|
@form.billing_address = RedmineQboBilling::EstimateWriter.customer_address(customer) if customer
|
|
render view, status: :unprocessable_entity
|
|
end
|
|
|
|
def qbo_message(error)
|
|
detail = error.respond_to?(:detail) ? error.detail.to_s : ''
|
|
message = error.message.to_s
|
|
detail.present? && !message.include?(detail) ? "#{message}: #{detail}" : message
|
|
end
|
|
end
|