mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-20 18:40:37 -05:00
Allow unlocking estimates linked to issues
An estimate attached to an issue was read-only, so changing it after a customer asked for tweaks meant taking it off the work order first. A new global permission, "Edit estimates linked to issues" (on top of Write estimates), shows an "Unlock to edit" button on a linked estimate. After a confirmation the estimate opens for editing, and the issues stay linked when it's saved. EstimateWriter.save still refuses a linked estimate unless it was unlocked by a user with the permission. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
22af08cee7
commit
48f92f241f
9 files changed
+118
-4
No files matched your search
@@ -59,9 +59,11 @@ class QboEstimatesController < ApplicationController
|
||||
show_error(e, :new)
|
||||
end
|
||||
|
||||
# ?unlock=1 opens an estimate linked to an issue for editing anyway (after the editor's confirmation)
|
||||
def edit
|
||||
@form = RedmineQboBilling::EstimateWriter.load(params[:id])
|
||||
@linked_issues = RedmineQboBilling::EstimateWriter.linked_issues(@form.id).visible.to_a
|
||||
unlock_form if unlock?
|
||||
rescue *QBO_ERRORS => e
|
||||
flash[:error] = qbo_message(e)
|
||||
redirect_back_or_default home_path
|
||||
@@ -70,9 +72,10 @@ class QboEstimatesController < ApplicationController
|
||||
def update
|
||||
@form = RedmineQboBilling::EstimateForm.from_params(estimate_params, id: params[:id])
|
||||
@form.sales_person = params.dig(:estimate, :sales_person).to_s
|
||||
unlock_form if unlock?
|
||||
return show_invalid(:edit) unless @form.valid?
|
||||
|
||||
saved = RedmineQboBilling::EstimateWriter.save(@form, user: User.current)
|
||||
saved = RedmineQboBilling::EstimateWriter.save(@form, user: User.current, unlock: unlock?)
|
||||
finish_save(saved, l(:notice_estimate_saved_in_quickbooks, number: saved.doc_number))
|
||||
rescue RedmineQboBilling::EstimateWriter::Conflict => e
|
||||
@conflict = true
|
||||
@@ -296,6 +299,21 @@ class QboEstimatesController < ApplicationController
|
||||
)
|
||||
end
|
||||
|
||||
def unlock?
|
||||
params[:unlock].to_s == '1'
|
||||
end
|
||||
|
||||
# Shows an estimate linked to an issue editable, for users allowed to. EstimateWriter.save checks again.
|
||||
def unlock_form
|
||||
return unless RedmineQboBilling.linked_estimate_editor?
|
||||
|
||||
@linked_issues ||= RedmineQboBilling::EstimateWriter.linked_issues(@form.id).visible.to_a
|
||||
return unless @form.locked == :linked || (@form.locked.nil? && @linked_issues.any?)
|
||||
|
||||
@form.locked = nil
|
||||
@form.unlocked = true
|
||||
end
|
||||
|
||||
def save_and_send?
|
||||
params[:save_and_send].present?
|
||||
end
|
||||
|
||||
@@ -50,6 +50,8 @@
|
||||
<%= hidden_field_tag 'estimate[txn_status]', form.txn_status, id: nil %>
|
||||
<%# Shown again if the page is redisplayed; the saved value is worked out when saving %>
|
||||
<%= hidden_field_tag 'estimate[sales_person]', form.sales_person, id: nil if form.persisted? || form.invoice? %>
|
||||
<%# An estimate linked to an issue opened for editing (Unlock) stays unlocked if the page is shown again %>
|
||||
<%= hidden_field_tag 'unlock', '1', id: nil if !invoice && form.unlocked %>
|
||||
<% if invoice %>
|
||||
<%= hidden_field_tag 'estimate[balance]', form.balance, id: nil %>
|
||||
<%# A paid invoice opened for editing (Unlock) stays unlocked if the page is shown again %>
|
||||
|
||||
@@ -67,6 +67,21 @@
|
||||
<li><%= link_to_issue(issue, truncate: 60) %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<%# Opened for editing anyway after a confirmation (e.g. the customer asks for changes after the diagnosis) %>
|
||||
<% if RedmineQboBilling.linked_estimate_editor? %>
|
||||
<%= link_to sprite_icon('unlock', l(:button_unlock_estimate)), edit_qbo_estimate_path(form.id, unlock: 1),
|
||||
class: 'icon icon-unlock', id: 'qbo-unlock-estimate',
|
||||
data: { confirm: l(:text_unlock_estimate_confirmation) } %>
|
||||
<% end %>
|
||||
</div>
|
||||
<% elsif form.unlocked && !invoice %>
|
||||
<div class="warning qbo-estimate-notice">
|
||||
<%= l(:text_estimate_unlocked) %>
|
||||
<ul>
|
||||
<% Array(@linked_issues).each do |issue| %>
|
||||
<li><%= link_to_issue(issue, truncate: 60) %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% elsif form.locked == :paid %>
|
||||
<p class="warning">
|
||||
|
||||
@@ -99,6 +99,7 @@ en:
|
||||
permission_send_invoices: "Email invoices"
|
||||
permission_edit_partially_paid_invoices: "Edit partially paid invoices"
|
||||
permission_edit_paid_invoices: "Edit paid invoices"
|
||||
permission_edit_linked_estimates: "Edit estimates linked to issues"
|
||||
error_invoice_paid_not_allowed: "You're not allowed to change invoices with this much paid on them"
|
||||
permission_create_estimate_issues: "Create issues from estimates"
|
||||
permission_receive_payments: "Receive payments"
|
||||
@@ -169,6 +170,10 @@ en:
|
||||
error_discount_too_large: "The discount can't be more than the subtotal"
|
||||
error_invoice_below_paid: "The invoice total (about %{total} with tax) can't be less than the %{paid} already paid on it"
|
||||
button_unlock_invoice: "Unlock to edit"
|
||||
button_unlock_estimate: "Unlock to edit"
|
||||
text_unlock_estimate_confirmation: "This estimate is linked to an issue. Unlock it to make changes? The issues stay linked to it."
|
||||
text_estimate_unlocked: "Unlocked: this estimate is linked to these issues, and they stay linked when you save:"
|
||||
error_estimate_linked_not_allowed: "You're not allowed to change an estimate linked to an issue."
|
||||
text_unlock_invoice_confirmation: "A payment has been applied to this invoice. Unlock it to make changes, such as a discount? The payment stays applied, and the total can't go below what's been paid."
|
||||
text_invoice_unlocked: "Unlocked: %{paid} has been paid on this invoice. The payment stays applied when you save, and the total can't go below it."
|
||||
label_drag_to_reorder: "Drag to reorder"
|
||||
|
||||
@@ -42,6 +42,8 @@ Redmine::Plugin.register :redmine_qbo do
|
||||
# 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
|
||||
# Unlocking an estimate linked to an issue (on top of Write estimates); see RedmineQboBilling.linked_estimate_editor?
|
||||
permission :edit_linked_estimates, {}, 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],
|
||||
|
||||
@@ -93,6 +93,12 @@ module RedmineQboBilling
|
||||
user.allowed_to?(:view_payments, nil, global: true) || payment_receiver?(user)
|
||||
end
|
||||
|
||||
# True when the user may unlock and change an estimate linked to an issue (e.g. a customer asks for changes to
|
||||
# an estimate already on the diagnostic work order): "Edit estimates linked to issues" on top of "Write estimates"
|
||||
def self.linked_estimate_editor?(user = User.current)
|
||||
writer?(user) && user.allowed_to?(:edit_linked_estimates, nil, global: true)
|
||||
end
|
||||
|
||||
# True when the user may write invoices
|
||||
def self.invoice_writer?(user = User.current)
|
||||
user.allowed_to?(:write_invoices, nil, global: true)
|
||||
|
||||
@@ -112,7 +112,8 @@ module RedmineQboBilling
|
||||
# (discount: the discount amount QuickBooks worked out when it was last saved)
|
||||
:billing_address, :total, :total_tax, :discount, :email_status, :txn_status, :duplicated_from,
|
||||
:balance, :converted_from,
|
||||
# Invoices: a paid invoice opened for editing anyway (Unlock), and the amount paid on it
|
||||
# Invoices: a paid invoice opened for editing anyway (Unlock), and the amount paid on it.
|
||||
# Estimates: an estimate linked to an issue opened for editing anyway (Unlock).
|
||||
:unlocked, :amount_paid,
|
||||
# Estimates: the invoices it's billed on ([[id, number], ...])
|
||||
:invoiced_on,
|
||||
|
||||
@@ -105,6 +105,13 @@ module RedmineQboBilling
|
||||
Issue.where(estimate_id: estimate_id.to_s)
|
||||
end
|
||||
|
||||
# An estimate linked to an issue is only changed when unlocked by a user allowed to
|
||||
def check_linked(form, unlock, user)
|
||||
return unless linked_issues(form.id).exists?
|
||||
raise Error, I18n.t(:error_estimate_linked_to_issues) unless unlock
|
||||
raise Error, I18n.t(:error_estimate_linked_not_allowed) unless RedmineQboBilling.linked_estimate_editor?(user)
|
||||
end
|
||||
|
||||
# Loads the estimate from QuickBooks for editing
|
||||
def load(id)
|
||||
remote = QboEstimateService.with_service { |service| service.fetch_by_id(id) }
|
||||
@@ -114,7 +121,9 @@ module RedmineQboBilling
|
||||
|
||||
# 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)
|
||||
# unlock: an estimate linked to an issue may be changed (the user unlocked it, see
|
||||
# RedmineQboBilling.linked_estimate_editor?)
|
||||
def save(form, user: User.current, unlock: false)
|
||||
customer = Customer.find_by(id: form.customer_id)
|
||||
raise Error, I18n.t(:error_estimate_needs_customer) unless customer
|
||||
|
||||
@@ -125,7 +134,7 @@ module RedmineQboBilling
|
||||
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?
|
||||
check_linked(form, unlock, user)
|
||||
|
||||
estimate = Quickbooks::Model::Estimate.new
|
||||
estimate.id = remote.id
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#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.
|
||||
|
||||
require File.expand_path('../test_helper', __dir__)
|
||||
|
||||
# Estimates linked to an issue are read-only unless unlocked by a user with "Edit estimates linked to issues"
|
||||
class LinkedEstimateUnlockTest < ActiveSupport::TestCase
|
||||
Writer = RedmineQboBilling::EstimateWriter
|
||||
|
||||
def user(*permissions)
|
||||
user = User.new
|
||||
user.stubs(:allowed_to?).returns(false)
|
||||
permissions.each { |p| user.stubs(:allowed_to?).with(p, nil, global: true).returns(true) }
|
||||
user
|
||||
end
|
||||
|
||||
def form(linked:)
|
||||
Writer.stubs(:linked_issues).with('701').returns(stub(exists?: linked))
|
||||
RedmineQboBilling::EstimateForm.new.tap { |f| f.id = '701' }
|
||||
end
|
||||
|
||||
def test_editor_needs_write_estimates_and_edit_linked_estimates
|
||||
assert RedmineQboBilling.linked_estimate_editor?(user(:write_estimates, :edit_linked_estimates))
|
||||
assert_not RedmineQboBilling.linked_estimate_editor?(user(:write_estimates))
|
||||
assert_not RedmineQboBilling.linked_estimate_editor?(user(:edit_linked_estimates))
|
||||
end
|
||||
|
||||
def test_unlinked_estimate_saves_without_unlocking
|
||||
assert_nothing_raised { Writer.check_linked(form(linked: false), false, user(:write_estimates)) }
|
||||
end
|
||||
|
||||
def test_linked_estimate_is_locked_without_unlock
|
||||
error = assert_raises(Writer::Error) do
|
||||
Writer.check_linked(form(linked: true), false, user(:write_estimates, :edit_linked_estimates))
|
||||
end
|
||||
assert_equal I18n.t(:error_estimate_linked_to_issues), error.message
|
||||
end
|
||||
|
||||
def test_unlock_needs_the_permission
|
||||
error = assert_raises(Writer::Error) { Writer.check_linked(form(linked: true), true, user(:write_estimates)) }
|
||||
assert_equal I18n.t(:error_estimate_linked_not_allowed), error.message
|
||||
end
|
||||
|
||||
def test_unlocked_by_an_editor_saves
|
||||
assert_nothing_raised do
|
||||
Writer.check_linked(form(linked: true), true, user(:write_estimates, :edit_linked_estimates))
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user