diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb
index f9a9009..99f6f13 100644
--- a/app/controllers/accounts_controller.rb
+++ b/app/controllers/accounts_controller.rb
@@ -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
diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
index 5fc7a13..a2d8637 100644
--- a/app/controllers/items_controller.rb
+++ b/app/controllers/items_controller.rb
@@ -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
diff --git a/app/jobs/bill_line_items_job.rb b/app/jobs/bill_line_items_job.rb
index e427f17..d06c216 100644
--- a/app/jobs/bill_line_items_job.rb
+++ b/app/jobs/bill_line_items_job.rb
@@ -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
diff --git a/app/models/item.rb b/app/models/item.rb
index 9f8bfbb..d9a5f87 100644
--- a/app/models/item.rb
+++ b/app/models/item.rb
@@ -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
diff --git a/app/services/item_service.rb b/app/services/item_service.rb
index d99e710..e19e108 100644
--- a/app/services/item_service.rb
+++ b/app/services/item_service.rb
@@ -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
\ No newline at end of file
diff --git a/app/services/item_sync_service.rb b/app/services/item_sync_service.rb
index aad3ad7..a963e52 100644
--- a/app/services/item_sync_service.rb
+++ b/app/services/item_sync_service.rb
@@ -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
diff --git a/app/views/items/index.html.erb b/app/views/items/index.html.erb
index 9b3d4e9..79d3d26 100644
--- a/app/views/items/index.html.erb
+++ b/app/views/items/index.html.erb
@@ -27,16 +27,16 @@
\ No newline at end of file
diff --git a/app/views/line_items/_settings.html.erb b/app/views/line_items/_settings.html.erb
index c68479f..d74df87 100644
--- a/app/views/line_items/_settings.html.erb
+++ b/app/views/line_items/_settings.html.erb
@@ -15,7 +15,7 @@