Polish: permissions, billed-line locking, sync and billing fixes

Permissions
- Item catalog and accounts pages, and their syncs, are admin-only (they
  push to / pull from QuickBooks); shown in the Administration layout
- Item autocomplete is allowed with Add or Edit line items
- Permissions now map to real actions (they pointed at a non-existent
  line_items controller); line_items_attributes is only accepted from
  users with a line item permission
- Line item changes are checked on the server: adding, editing and
  deleting each need their permission, and billed lines (already on a
  QBO estimate) can't be changed or removed

Bugs
- Billing on close now runs from an Issue after_commit, so closing via
  bulk edit or the context menu bills too (replaces the save hook);
  the job takes an issue id
- Full sync runs Accounts before Items, and item sync no longer fails
  when an item has no income account or its account hasn't synced
- New QBO items default to Non-inventory in the default income account
  (the account ref was being assigned to the item type)
- Choosing a blank account on an item no longer raises; set_default
  handles no selection
- Item autocomplete escapes item names, SKUs and descriptions (they were
  inserted into the page as HTML)
- Items list showed "translation missing" for Yes/No

Polish
- Fractional quantities allowed (0.5 hrs, 2.5 qts)
- Billed lines show a Billed badge and are read-only; remove button only
  where allowed; add button only with Add line items
- Removed dead code (Item belongs_to :issue, Item#ref, no-op in new,
  swallowed errors); validation failures return 422

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
Claude committed 2026-10-28 22:37:41 -04:00
1 parent f0d6b08a3d
commit 39f6b64049
20 files changed
+182 -138

No files matched your search

@@ -1,31 +0,0 @@
#The MIT License (MIT)
#
#Copyright (c) 2017 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 LineItems
module Hooks
class IssuesSaveHookListener < Redmine::Hook::Listener
# Called After Issue Saved
def controller_issues_edit_after_save(context={})
log "Enqueuing billing of item items for issue ##{context[:issue]}"
issue = context[:issue]
BillLineItemsJob.perform_later(issue) if issue.status.is_closed?
end
private
def log(msg)
Rails.logger.info "[IssuesSaveHookListener] #{msg}"
end
end
end
end
+3 -2
View File
@@ -21,8 +21,9 @@ module LineItems
# Called by the QboSyncDispatcher
def qbo_full_sync (context={})
log "Adding Item to QBO sync dispatcher"
return [Item, Account]
log "Adding Account and Item to QBO sync dispatcher"
# Accounts first: items reference their income account
return [Account, Item]
end
private
+1 -1
View File
@@ -27,7 +27,7 @@ module LineItems
# Add the line items form to the issue edit page
def view_issues_edit_notes_bottom(context = {})
return if context[:issue].closed?
return unless User.current.allowed_to?(:edit_line_items, nil, global: true)
return unless %i[add_line_items edit_line_items delete_line_items].any? { |p| User.current.allowed_to?(p, nil, global: true) }
context[:controller].send(:render_to_string, { partial: 'line_items/issue_form', locals: { f: context[:form] } })
end
+36 -19
View File
@@ -14,6 +14,18 @@ module LineItems
prepended do
has_many :line_items, dependent: :destroy
# Bill unbilled line items to a QBO estimate when the issue is closed, however it was closed
# (issue form, bulk edit, context menu or API)
after_commit :enqueue_line_items_billing, on: :update
def enqueue_line_items_billing
return unless closed?
return unless line_items.where(billed: [false, nil]).exists?
Rails.logger.info "[LineItems::IssuePatch] Enqueuing line item billing for issue ##{id}"
BillLineItemsJob.perform_later(id)
end
accepts_nested_attributes_for :line_items, allow_destroy: true, reject_if: proc { |attrs| attrs['description'].blank? }
# Returns line items for immediate children
@@ -40,33 +52,38 @@ module LineItems
line_items.sum(:line_total)
end
# Applies line item changes from the issue form, enforcing permissions on the server (the form
# hides buttons, but a request can be crafted by hand or sent through the API):
# - new lines need "Add line items", changes need "Edit line items", removals need "Delete line items"
# - billed lines are already on a QuickBooks estimate, so they can't be changed or removed
# Lines left out of the submission are untouched (Rails never deletes omitted nested records).
def line_items_attributes=(attrs)
attrs = attrs.stringify_keys
user = User.current
can_add = user.allowed_to?(:add_line_items, nil, global: true)
can_edit = user.allowed_to?(:edit_line_items, nil, global: true)
can_delete = user.allowed_to?(:delete_line_items, nil, global: true)
billed_ids = line_items.select(&:billed?).map { |li| li.id.to_s }
# IDs submitted in the form
submitted_ids = attrs.values.map { |a| a['id'] }.compact.map(&:to_s)
attrs = attrs.each_with_index.to_h { |a, i| [i.to_s, a] } if attrs.is_a?(Array)
# Existing IDs in DB
existing_ids = line_items.pluck(:id).map(&:to_s)
filtered = {}
attrs.to_h.each do |key, item_attrs|
item_attrs = item_attrs.to_h.stringify_keys
id = item_attrs['id'].to_s.presence
destroy = ActiveModel::Type::Boolean.new.cast(item_attrs['_destroy'])
# Find missing ones (these would be implicitly deleted by Rails)
missing_ids = existing_ids - submitted_ids
if id
next if billed_ids.include?(id)
next if destroy ? !can_delete : !can_edit
else
# New rows: removed-before-saving or blank rows are dropped
next if destroy || !can_add || item_attrs['description'].blank?
end
# Re-add missing records so Rails doesn't delete them
missing_ids.each do |id|
attrs["preserve_#{id}"] = { 'id' => id }
end
# Only allow explicit deletes or valid updates/creates
filtered = attrs.select do |_, item_attrs|
item_attrs['_destroy'] == '1' ||
item_attrs['id'].present? ||
item_attrs['description'].present?
filtered[key.to_s] = item_attrs
end
super(filtered)
rescue => e
logger.error "Error processing line items attributes: #{e.message}"
end
end
-1
View File
@@ -16,7 +16,6 @@ module RedmineQboLineItems
Issue.prepend LineItems::Patches::IssuePatch
# Hooks
LineItems::Hooks::IssuesSaveHookListener
LineItems::Hooks::QboHookListener
LineItems::Hooks::ViewHookListener