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
@@ -9,15 +9,25 @@
|
||||
#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.
|
||||
|
||||
class AccountsController < ApplicationController
|
||||
|
||||
layout 'admin'
|
||||
menu_item :redmine_qbo_lineitems
|
||||
|
||||
# Accounts come from QuickBooks; choosing the default and syncing are admin-only
|
||||
before_action :require_admin
|
||||
|
||||
def index
|
||||
@accounts = Account.where(classification: 'Revenue').order(:name)
|
||||
end
|
||||
|
||||
def set_default
|
||||
account = Account.find(params[:default_account_id])
|
||||
account.update(default: true)
|
||||
redirect_to accounts_path, notice: "Default account updated."
|
||||
account = Account.find_by(id: params[:default_account_id])
|
||||
|
||||
if account&.update(default: true)
|
||||
flash[:notice] = l(:notice_default_account_updated)
|
||||
else
|
||||
flash[:error] = l(:error_default_account_not_selected)
|
||||
end
|
||||
redirect_to accounts_path
|
||||
end
|
||||
|
||||
def sync
|
||||
|
||||
@@ -9,7 +9,13 @@
|
||||
#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.
|
||||
|
||||
class ItemsController < ApplicationController
|
||||
before_action :require_login
|
||||
layout 'admin'
|
||||
menu_item :redmine_qbo_lineitems
|
||||
|
||||
# The item catalog is pushed to QuickBooks, so managing it is admin-only. The autocomplete feeds the
|
||||
# line item form on issues and is allowed with the "Add line items" or "Edit line items" permission.
|
||||
before_action :require_admin, except: :autocomplete
|
||||
before_action :authorize_global, only: :autocomplete
|
||||
before_action :find_item, only: [:show, :edit, :update, :destroy]
|
||||
|
||||
# Used for autocomplete form
|
||||
@@ -32,7 +38,7 @@ class ItemsController < ApplicationController
|
||||
if @item.save
|
||||
redirect_to item_path(@item), notice: l(:notice_successful_create)
|
||||
else
|
||||
render :new
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
rescue => e
|
||||
log "Unexpected error creating item: #{e.message}"
|
||||
@@ -55,10 +61,6 @@ class ItemsController < ApplicationController
|
||||
end
|
||||
|
||||
def edit
|
||||
rescue => e
|
||||
log "Failed to edit item"
|
||||
flash[:error] = e.message
|
||||
render_404
|
||||
end
|
||||
|
||||
def index
|
||||
@@ -67,7 +69,6 @@ class ItemsController < ApplicationController
|
||||
|
||||
def new
|
||||
@item = Item.new
|
||||
@item.taxable.nil? ? true : @item.taxable
|
||||
end
|
||||
|
||||
def show
|
||||
@@ -82,7 +83,7 @@ class ItemsController < ApplicationController
|
||||
if @item.update(item_params)
|
||||
redirect_to item_path(@item), notice: l(:notice_successful_update)
|
||||
else
|
||||
render :edit
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
@@ -90,9 +91,7 @@ class ItemsController < ApplicationController
|
||||
|
||||
def find_item
|
||||
@item = Item.find(params[:id])
|
||||
rescue => e
|
||||
log "Failed to find item"
|
||||
flash[:error] = e.message
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
render_404
|
||||
end
|
||||
|
||||
|
||||
@@ -14,8 +14,10 @@ class BillLineItemsJob < ActiveJob::Base
|
||||
|
||||
# Creates an estimate for a line items attached to an issue, and then marking those entries as billed in Redmine.
|
||||
# This job is typically triggered after an issue is closed.
|
||||
def perform(issue)
|
||||
return unless issue.customer
|
||||
# Takes an issue id (jobs queued by older versions pass the Issue itself, which still works)
|
||||
def perform(issue_or_id)
|
||||
issue = issue_or_id.is_a?(Issue) ? issue_or_id : Issue.find_by(id: issue_or_id)
|
||||
return unless issue&.customer
|
||||
|
||||
log "Starting billing for issue ##{issue.id}"
|
||||
issue.with_lock do
|
||||
@@ -34,7 +36,7 @@ class BillLineItemsJob < ActiveJob::Base
|
||||
log "Completed billing of for issue ##{issue.id}"
|
||||
Qbo.update_time_stamp
|
||||
rescue => e
|
||||
log "Billing failed for issue ##{issue.id} - #{e.message}"
|
||||
log "Billing failed for issue ##{issue&.id || issue_or_id} - #{e.message}"
|
||||
raise e
|
||||
end
|
||||
|
||||
|
||||
+4
-8
@@ -9,8 +9,7 @@
|
||||
#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.
|
||||
|
||||
class Item < QboBaseModel
|
||||
belongs_to :issue
|
||||
belongs_to :account
|
||||
belongs_to :account, optional: true
|
||||
|
||||
validates_presence_of :id, :description
|
||||
validates :unit_price, numericality: { greater_than_or_equal_to: 0 }
|
||||
@@ -20,9 +19,10 @@ class Item < QboBaseModel
|
||||
after_initialize :set_defaults, if: :new_record?
|
||||
before_destroy :make_inactive
|
||||
|
||||
# Updates Both local & remote DB account ref
|
||||
# Updates Both local & remote DB account ref (a blank or unknown account leaves QBO's ref unchanged)
|
||||
def account_id=(id)
|
||||
details.income_account_ref = Account.find(id).ref
|
||||
account = Account.find_by(id: id) if id.present?
|
||||
details.income_account_ref = account.ref if account
|
||||
super
|
||||
end
|
||||
|
||||
@@ -43,10 +43,6 @@ class Item < QboBaseModel
|
||||
push_to_qbo
|
||||
end
|
||||
|
||||
def ref
|
||||
Quickbooks::Model::BaseReference.new
|
||||
end
|
||||
|
||||
def set_defaults
|
||||
self.taxable = true if taxable.nil?
|
||||
end
|
||||
|
||||
@@ -14,7 +14,13 @@ class ItemService < ServiceBase
|
||||
|
||||
def build_qbo_remote
|
||||
log "Building new QBO Item"
|
||||
Quickbooks::Model::Item.new(type: Account.get_default&.ref)
|
||||
# New items default to a non-inventory item in the default income account; the item form then
|
||||
# sets the type and account the user chose.
|
||||
item = Quickbooks::Model::Item.new
|
||||
item.type = Quickbooks::Model::Item::NON_INVENTORY_TYPE
|
||||
default_account = Account.get_default
|
||||
item.income_account_ref = default_account.ref if default_account
|
||||
item
|
||||
end
|
||||
|
||||
end
|
||||
@@ -24,8 +24,11 @@ class ItemSyncService < SyncServiceBase
|
||||
|
||||
map_attribute :active, :active?
|
||||
map_attribute :taxable, :taxable?
|
||||
# Items without an income account (e.g. categories), or whose account hasn't synced yet, get none
|
||||
# instead of failing the whole sync; the next sync fills it in.
|
||||
map_attribute :account do |remote|
|
||||
Account.find remote.income_account_ref.value.to_i
|
||||
account_id = remote.income_account_ref&.value
|
||||
account_id.present? ? Account.find_by(id: account_id.to_i) : nil
|
||||
end
|
||||
map_attributes :description, :id, :name, :sku, :type, :unit_price
|
||||
|
||||
|
||||
@@ -27,16 +27,16 @@
|
||||
<td class="unit_price"><%= number_to_currency(item.unit_price) %></td>
|
||||
<td class="taxable center">
|
||||
<% if item.taxable %>
|
||||
<span class="icon icon-ok" style="color: green;"><%=t(:yes)%></span>
|
||||
<span class="icon icon-ok" style="color: green;"><%= l(:general_text_Yes) %></span>
|
||||
<% else %>
|
||||
<span class="icon icon-not-ok" style="color: #999;"><%=t(:no)%></span>
|
||||
<span class="icon icon-not-ok" style="color: #999;"><%= l(:general_text_No) %></span>
|
||||
<% end %>
|
||||
</td>
|
||||
<td class="active center">
|
||||
<% if item.active %>
|
||||
<span class="icon icon-ok" style="color: green;"><%=t(:yes)%></span>
|
||||
<span class="icon icon-ok" style="color: green;"><%= l(:general_text_Yes) %></span>
|
||||
<% else %>
|
||||
<span class="icon icon-not-ok" style="color: #999;"><%=t(:no)%></span>
|
||||
<span class="icon icon-not-ok" style="color: #999;"><%= l(:general_text_No) %></span>
|
||||
<% end %>
|
||||
</td>
|
||||
<td class="buttons">
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
<% readonly = @issue.status.is_closed? %>
|
||||
<%
|
||||
readonly = @issue.status.is_closed?
|
||||
can_add = User.current.allowed_to?(:add_line_items, nil, global: true)
|
||||
can_edit = User.current.allowed_to?(:edit_line_items, nil, global: true)
|
||||
can_delete = User.current.allowed_to?(:delete_line_items, nil, global: true)
|
||||
permissions = { can_edit: can_edit, can_delete: can_delete }
|
||||
%>
|
||||
|
||||
<% @issue.line_items.build if @issue.line_items.empty? && !readonly %>
|
||||
<% @issue.line_items.build if @issue.line_items.empty? && !readonly && can_add %>
|
||||
|
||||
<div class="box tabular"
|
||||
data-nested-form
|
||||
@@ -25,7 +31,8 @@
|
||||
<%= f.fields_for :line_items do |item_form| %>
|
||||
<%= render "line_items/line_item_fields",
|
||||
f: item_form,
|
||||
readonly: readonly %>
|
||||
readonly: readonly,
|
||||
**permissions %>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -35,24 +42,22 @@
|
||||
<span id="line-items-grand-total">0.00</span>
|
||||
</p>
|
||||
|
||||
<% unless readonly %>
|
||||
<% if !readonly && can_add %>
|
||||
<template data-nested-form-template>
|
||||
<%= f.fields_for :line_items, LineItem.new, child_index: "NEW_RECORD" do |item_form| %>
|
||||
<%= render "line_items/line_item_fields",
|
||||
f: item_form,
|
||||
readonly: false %>
|
||||
readonly: false,
|
||||
**permissions %>
|
||||
<% end %>
|
||||
</template>
|
||||
|
||||
<p>
|
||||
|
||||
<% if User.current.allowed_to?(:add_line_items, nil, global: true) %>
|
||||
<button type="button"
|
||||
class="icon icon-add"
|
||||
data-nested-form-add>
|
||||
<%= l(:button_add) %>
|
||||
</button>
|
||||
<% end %>
|
||||
<button type="button"
|
||||
class="icon icon-add"
|
||||
data-nested-form-add>
|
||||
<%= l(:button_add) %>
|
||||
</button>
|
||||
</p>
|
||||
<% end %>
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
<% if @issue.line_items.any? || @issue.descendant_line_items_total > 0%>
|
||||
<% if issue.line_items.any? || issue.descendant_line_items_total > 0%>
|
||||
<hr/>
|
||||
<div>
|
||||
<p><strong><%= t :label_line_items %></strong></p>
|
||||
|
||||
<% total = 0 %>
|
||||
|
||||
<% if @issue.line_items.any?%>
|
||||
<% if issue.line_items.any?%>
|
||||
|
||||
<table class="list line-items-table">
|
||||
<thead>
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
<tbody>
|
||||
|
||||
<% @issue.line_items.each do |item| %>
|
||||
<% issue.line_items.each do |item| %>
|
||||
<% line_total = item.quantity.to_f * item.unit_price.to_f %>
|
||||
<% total += line_total %>
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
<% end %>
|
||||
|
||||
<tfoot>
|
||||
<% if @issue.line_items.any?%>
|
||||
<% if issue.line_items.any?%>
|
||||
<tr>
|
||||
<td colspan="3" style="text-align:right;"><strong><%= t :label_total %></strong></td>
|
||||
<td>
|
||||
@@ -44,11 +44,11 @@
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
<% if @issue.descendant_line_items_total > 0 %>
|
||||
<% if issue.descendant_line_items_total > 0 %>
|
||||
<tr>
|
||||
<td colspan="3" style="text-align:right;"><strong><%= t :label_running_total %></strong></td>
|
||||
<td>
|
||||
<strong>(<%= number_to_currency(@issue.descendant_line_items_total + total) %>)</strong>
|
||||
<strong>(<%= number_to_currency(issue.descendant_line_items_total + total) %>)</strong>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -1,4 +1,12 @@
|
||||
<tr class="line-item">
|
||||
<%
|
||||
item = f.object
|
||||
billed = item.persisted? && item.billed?
|
||||
# Billed lines are on a QuickBooks estimate already; existing lines need "Edit line items" to change
|
||||
locked = readonly || billed || (item.persisted? && !can_edit)
|
||||
# Unsaved rows can always be removed; saved ones need "Delete line items"
|
||||
removable = !readonly && !billed && (item.new_record? || can_delete)
|
||||
%>
|
||||
<tr class="line-item<%= ' billed' if billed %>">
|
||||
<%= f.hidden_field :id %>
|
||||
<%= f.hidden_field :_destroy %>
|
||||
<%= f.hidden_field :item_id, class: "item-id-field" %>
|
||||
@@ -9,17 +17,21 @@
|
||||
class: "line-item-description",
|
||||
autocomplete: "off",
|
||||
no_label: true,
|
||||
disabled: readonly %>
|
||||
disabled: locked %>
|
||||
<% if billed %>
|
||||
<span class="line-item-billed-badge" title="<%= l(:text_line_item_billed) %>"><%= l(:label_billed) %></span>
|
||||
<% end %>
|
||||
</td>
|
||||
|
||||
<td data-label="<%= t :label_qty %>">
|
||||
<%# Fractional quantities are allowed (e.g. 0.5 hours, 2.5 quarts) %>
|
||||
<%= f.number_field :quantity,
|
||||
step: 1,
|
||||
min: 1,
|
||||
value: (f.object.quantity.to_i > 0) ? f.object.quantity : 1,
|
||||
step: "any",
|
||||
min: 0,
|
||||
value: (item.quantity.to_f > 0) ? number_with_precision(item.quantity, precision: 4, strip_insignificant_zeros: true) : 1,
|
||||
class: "qty-field",
|
||||
no_label: true,
|
||||
disabled: readonly %>
|
||||
disabled: locked %>
|
||||
</td>
|
||||
|
||||
<td data-label="<%= t :label_price %>">
|
||||
@@ -28,22 +40,22 @@
|
||||
inputmode: "decimal",
|
||||
autocomplete: "off",
|
||||
no_label: true,
|
||||
disabled: readonly %>
|
||||
disabled: locked %>
|
||||
</td>
|
||||
|
||||
<td class="line-total" data-label="<%= t :label_total %>">
|
||||
0.00
|
||||
</td>
|
||||
|
||||
<% if User.current.allowed_to?(:delete_line_items, nil, global: true) %>
|
||||
<% unless readonly %>
|
||||
<td class="actions">
|
||||
<% unless readonly %>
|
||||
<td class="actions">
|
||||
<% if removable %>
|
||||
<button type="button"
|
||||
class="icon-only icon-del"
|
||||
title="<%= l(:label_remove) %>"
|
||||
data-nested-form-remove>
|
||||
</button>
|
||||
</td>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</td>
|
||||
<% end %>
|
||||
</tr>
|
||||
@@ -15,7 +15,7 @@
|
||||
</p>
|
||||
<p>
|
||||
<label><strong><%= t(:label_last_sync) %> (QBO)</strong></label>
|
||||
<%= Qbo.exists? ? Qbo.last_sync : 'Never synced' %>
|
||||
<%= Qbo.exists? ? Qbo.last_sync : l(:label_never_synced) %>
|
||||
</p>
|
||||
<p>
|
||||
<label><strong><%= t(:label_default_account) %></strong></label>
|
||||
@@ -24,7 +24,7 @@
|
||||
</div>
|
||||
|
||||
<fieldset class="box">
|
||||
<legend>Management & Synchronization</legend>
|
||||
<legend><%= l(:label_management_and_sync) %></legend>
|
||||
|
||||
<div style="margin-bottom: 15px;">
|
||||
<%= link_to t(:label_sync_now_items), sync_items_path, class: 'button icon icon-reload' %>
|
||||
|
||||
Reference in new issue
Block a user