From f81fe0ef8783af4cb91c4990f2bed8ddedad0e05 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Mon, 16 Mar 2026 22:59:54 -0400 Subject: [PATCH] Added accounts to allow for assinging income account to items, and selecting default income account. Also added item type selection --- app/controllers/accounts_controller.rb | 22 ++++++ app/controllers/items_controller.rb | 30 +++++++- app/models/account.rb | 37 ++++++++++ app/models/item.rb | 16 +++- app/services/account_sync_service.rb | 23 ++++++ app/services/item_service.rb | 18 +---- app/services/item_sync_service.rb | 2 +- app/views/accounts/index.html.erb | 39 ++++++++++ app/views/items/_form.html.erb | 74 +++++++++++++------ app/views/items/show.html.erb | 57 ++++++++------ config/routes.rb | 6 ++ db/migrate/004_create_accounts.rb | 25 +++++++ .../hooks/qbo_hook_listener.rb | 4 +- 13 files changed, 283 insertions(+), 70 deletions(-) create mode 100644 app/controllers/accounts_controller.rb create mode 100644 app/models/account.rb create mode 100644 app/services/account_sync_service.rb create mode 100644 app/views/accounts/index.html.erb create mode 100644 db/migrate/004_create_accounts.rb diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb new file mode 100644 index 0000000..db22345 --- /dev/null +++ b/app/controllers/accounts_controller.rb @@ -0,0 +1,22 @@ +#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. + +class AccountsController < ApplicationController + + 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." + end +end \ No newline at end of file diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb index 09b8dd0..a195c4d 100644 --- a/app/controllers/items_controller.rb +++ b/app/controllers/items_controller.rb @@ -33,6 +33,19 @@ class ItemsController < ApplicationController else render :new end + rescue => e + log "Unexpected error creating item: #{e.message}" + + # Regex now matches across line breaks + existing_id = e.message[/Duplicate Name Exists Error:[\s\S]*Id=(\d+)/, 1]&.to_i + + if existing_id + flash[:error] = "Name already exists. Redirecting to existing item." + redirect_to item_path(existing_id) + else + flash[:error] = e.message + redirect_to new_item_path + end end def destroy @@ -41,6 +54,10 @@ class ItemsController < ApplicationController end def edit + rescue => e + log "Failed to edit item" + flash[:error] = e.message + render_404 end def index @@ -71,9 +88,20 @@ class ItemsController < ApplicationController def find_item @item = Item.find(params[:id]) + rescue => e + log "Failed to find item" + flash[:error] = e.message + render_404 end def item_params - params.require(:item).permit(:name, :description, :sku, :unit_price, :active) + params.require(:item).permit(:name, :description, :sku, :unit_price, :active, :account_id, :type) end + + private + + def log(msg) + Rails.logger.info "[ItemsController] #{msg}" + end + end \ No newline at end of file diff --git a/app/models/account.rb b/app/models/account.rb new file mode 100644 index 0000000..15b613b --- /dev/null +++ b/app/models/account.rb @@ -0,0 +1,37 @@ +#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. + +class Account < QboBaseModel + has_many :items + validates_presence_of :id, :name + self.primary_key = :id + qbo_sync push: false + before_save :clear_other_defaults, if: :default? + + # Returns the account marked as default + def self.get_default + find_by(default: true) + end + + # Returns QBO Refrence object for the account + def ref + r = Quickbooks::Model::BaseReference.new + r.value = id + r.name = name + return r + end + + private + + def clear_other_defaults + Account.where.not(id: id).update_all(default: false) + end + +end \ No newline at end of file diff --git a/app/models/item.rb b/app/models/item.rb index 1eefd6e..56f4eb7 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -10,12 +10,20 @@ class Item < QboBaseModel belongs_to :issue - + belongs_to :account + validates_presence_of :id, :description validates :unit_price, numericality: { greater_than_or_equal_to: 0 } self.primary_key = :id + self.inheritance_column = :_type_disabled qbo_sync push: true + # Updates Both local & remote DB account ref + def account_id=(id) + details.income_account_ref = Account.find(id).ref + super + end + # Updates Both local & remote DB description def description=(s) details.description = s @@ -34,6 +42,12 @@ class Item < QboBaseModel super end + # Updates Both local & remote DB type + def type=(s) + details.type = s.to_s + super + end + # Updates Both local & remote DB price def unit_price=(s) details.unit_price = s diff --git a/app/services/account_sync_service.rb b/app/services/account_sync_service.rb new file mode 100644 index 0000000..f7bea75 --- /dev/null +++ b/app/services/account_sync_service.rb @@ -0,0 +1,23 @@ +#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. + +class AccountSyncService < SyncServiceBase + + private + + # Specify the local model this service syncs + def self.model_class + Account + end + + map_attribute :active, :active? + map_attributes :classification, :description, :id, :name + +end \ No newline at end of file diff --git a/app/services/item_service.rb b/app/services/item_service.rb index 4bcc3ba..d99e710 100644 --- a/app/services/item_service.rb +++ b/app/services/item_service.rb @@ -14,23 +14,7 @@ class ItemService < ServiceBase def build_qbo_remote log "Building new QBO Item" - account = default_income_account - log "Account: #{account.id} - #{account.name}" - income = Quickbooks::Model::BaseReference.new - income.value = account.id - income.name = account.name - - Quickbooks::Model::Item.new( - type: Quickbooks::Model::Item::NON_INVENTORY_TYPE, - income_account_ref: income - ) - end - - def default_income_account - log "Looking up sales income account" - QboConnectionService.with_qbo_service(entity: Invoice) do |service| - service.query("SELECT * FROM Account WHERE AccountType='Income' AND Name LIKE '%Sales%'").first - end + Quickbooks::Model::Item.new(type: Account.get_default&.ref) 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 59bcd70..e606687 100644 --- a/app/services/item_sync_service.rb +++ b/app/services/item_sync_service.rb @@ -23,6 +23,6 @@ class ItemSyncService < SyncServiceBase end map_attribute :active, :active? - map_attributes :description, :id, :name, :sku, :unit_price + map_attributes :description, :id, :name, :sku, :type, :unit_price end \ No newline at end of file diff --git a/app/views/accounts/index.html.erb b/app/views/accounts/index.html.erb new file mode 100644 index 0000000..90750b5 --- /dev/null +++ b/app/views/accounts/index.html.erb @@ -0,0 +1,39 @@ +

Accounts

+ +<%= form_with url: set_default_accounts_path, method: :patch, local: true do %> + + + + + + + + + + + + + <% @accounts.each_with_index do |account, index| %> + + + + + + + + <% end %> + + + + + + + +
DefaultNameDescriptionClassificationActive
+ <%= radio_button_tag "default_account_id", account.id, account.default %> + <%= account.name %><%= account.description %><%= account.classification %> + <%= account.active ? "Yes" : "No" %> +
+ <%= submit_tag "Save Default Account", style: "padding: 6px 12px; font-weight: bold;" %> +
+<% end %> \ No newline at end of file diff --git a/app/views/items/_form.html.erb b/app/views/items/_form.html.erb index b2afcb4..1ef10af 100644 --- a/app/views/items/_form.html.erb +++ b/app/views/items/_form.html.erb @@ -1,8 +1,8 @@ <%= form_with model: @item, local: true do |f| %> <% if @item.errors.any? %> -
-

<%= pluralize(@item.errors.count, "error") %>

+
+

<%= pluralize(@item.errors.count, "error") %> prohibited this item from being saved:

    <% @item.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • @@ -11,33 +11,59 @@
<% end %> -

- <%= f.label :name %>
- <%= f.text_field :name, required: true %> -

+ + + + + + -

- <%= f.label :sku %>
- <%= f.text_field :sku %> -

+ + + + -

- <%= f.label :description %>
- <%= f.text_area :description, rows: 3 %> -

+ + + + -

- <%= f.label :unit_price %>
- <%= f.number_field :unit_price, step: 0.01 %> -

+ + + + -

- <%= f.label :active %> - <%= f.check_box :active %> -

+ + + + -

- <%= f.submit %> +

+ + + + + + + + + +
<%= f.label :name %><%= f.text_field :name, required: true, style: "width: 100%;" %>
<%= f.label :sku %><%= f.text_field :sku, style: "width: 100%;" %>
<%= f.label :description %><%= f.text_area :description, rows: 3, style: "width: 100%;" %>
<%= f.label :unit_price %><%= f.number_field :unit_price, step: 0.01, style: "width: 100%;" %>
<%= f.label :account %> + <%= f.collection_select :account_id, + Account.where(classification: 'Revenue').order(:name), + :id, + :name, + { selected: @item.account_id || Account.get_default&.id }, + { include_blank: true, style: "width: 100%;" } %> +
<%= f.label :type %> + <%= f.select :type, + Quickbooks::Model::Item::ITEM_TYPES.map { |t| [t, t] }, + { selected: @item.type || Quickbooks::Model::Item::NON_INVENTORY_TYPE }, + { style: "width: 100%;" } %> +
<%= f.label :active %><%= f.check_box :active %>
+ +

+ <%= f.submit "Save Item", style: "padding: 6px 12px; font-weight: bold;" %>

<% end %> \ No newline at end of file diff --git a/app/views/items/show.html.erb b/app/views/items/show.html.erb index bbc4e29..61f559b 100644 --- a/app/views/items/show.html.erb +++ b/app/views/items/show.html.erb @@ -1,26 +1,35 @@ -

<%= @item.name %>

+

<%= @item.name %>

-

- SKU: - <%= @item.sku %> -

+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
SKU:<%= @item.sku.presence || "-" %>
Description:<%= @item.description.presence || "-" %>
Unit Price:<%= number_to_currency(@item.unit_price) %>
Type:<%= @item.type.presence || "-" %>
Account:<%= @item.account&.name || "-" %>
Active:<%= @item.active ? "Yes" : "No" %>
-

- Description: - <%= @item.description %> -

- -

- Unit Price: - <%= number_to_currency(@item.unit_price) %> -

- -

- Active: - <%= @item.active ? "Yes" : "No" %> -

- -

- <%= link_to "Edit", edit_item_path(@item), class: "icon icon-edit" %> - <%= link_to "Back", items_path %> -

\ No newline at end of file +
+ <%= link_to "Edit", edit_item_path(@item), class: "btn btn-primary", style: "margin-right: 8px;" %> + <%= link_to "Back", items_path, class: "btn btn-secondary" %> +
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 5b0bca5..2a02a82 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,4 +13,10 @@ resources :items do get :autocomplete get :sync end +end + +resources :accounts do + collection do + patch :set_default + end end \ No newline at end of file diff --git a/db/migrate/004_create_accounts.rb b/db/migrate/004_create_accounts.rb new file mode 100644 index 0000000..6250649 --- /dev/null +++ b/db/migrate/004_create_accounts.rb @@ -0,0 +1,25 @@ +#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. + +class CreateAccounts < ActiveRecord::Migration[7.0] + def change + create_table :accounts do |t| + t.text :name, null: false + t.text :description + t.boolean :active, default: true, null: false + t.boolean :default, default: false, null: false + t.text :classification + t.timestamps + end + + add_reference :items, :account, foreign_key: true + add_column :items, :type, :string + end +end \ No newline at end of file diff --git a/lib/redmine_qbo_line_items/hooks/qbo_hook_listener.rb b/lib/redmine_qbo_line_items/hooks/qbo_hook_listener.rb index b36e6f5..4f77a8d 100644 --- a/lib/redmine_qbo_line_items/hooks/qbo_hook_listener.rb +++ b/lib/redmine_qbo_line_items/hooks/qbo_hook_listener.rb @@ -16,13 +16,13 @@ module RedmineQboLineItems # Called by WebhookProcessJob def qbo_additional_entities(context={}) log "Added QBO Item to allowed webook entities" - return "Item" + return ["Item", "Account"] end # Called by the QboSyncDispatcher def qbo_full_sync (context={}) log "Adding Item to QBO sync dispatcher" - return Item + return [Item, Account] end private