mirror of
https://github.com/rickbarrette/redmine_qbo_lineitems.git
synced 2026-11-17 19:20:36 -05:00
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:
20 files changed
+182
-138
No files matched your search
@@ -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
|
||||
|
||||
Reference in new issue
Block a user