Added accounts to allow for assinging income account to items, and selecting default income account. Also added item type selection

This commit is contained in:
2026-03-16 22:59:54 -04:00
parent 7088550184
commit f81fe0ef87
13 changed files with 283 additions and 70 deletions

View File

@@ -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

View File

@@ -33,6 +33,19 @@ class ItemsController < ApplicationController
else else
render :new render :new
end 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 end
def destroy def destroy
@@ -41,6 +54,10 @@ class ItemsController < ApplicationController
end end
def edit def edit
rescue => e
log "Failed to edit item"
flash[:error] = e.message
render_404
end end
def index def index
@@ -71,9 +88,20 @@ class ItemsController < ApplicationController
def find_item def find_item
@item = Item.find(params[:id]) @item = Item.find(params[:id])
rescue => e
log "Failed to find item"
flash[:error] = e.message
render_404
end end
def item_params 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 end
private
def log(msg)
Rails.logger.info "[ItemsController] #{msg}"
end
end end

37
app/models/account.rb Normal file
View File

@@ -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

View File

@@ -10,12 +10,20 @@
class Item < QboBaseModel class Item < QboBaseModel
belongs_to :issue belongs_to :issue
belongs_to :account
validates_presence_of :id, :description validates_presence_of :id, :description
validates :unit_price, numericality: { greater_than_or_equal_to: 0 } validates :unit_price, numericality: { greater_than_or_equal_to: 0 }
self.primary_key = :id self.primary_key = :id
self.inheritance_column = :_type_disabled
qbo_sync push: true 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 # Updates Both local & remote DB description
def description=(s) def description=(s)
details.description = s details.description = s
@@ -34,6 +42,12 @@ class Item < QboBaseModel
super super
end end
# Updates Both local & remote DB type
def type=(s)
details.type = s.to_s
super
end
# Updates Both local & remote DB price # Updates Both local & remote DB price
def unit_price=(s) def unit_price=(s)
details.unit_price = s details.unit_price = s

View File

@@ -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

View File

@@ -14,23 +14,7 @@ class ItemService < ServiceBase
def build_qbo_remote def build_qbo_remote
log "Building new QBO Item" log "Building new QBO Item"
account = default_income_account Quickbooks::Model::Item.new(type: Account.get_default&.ref)
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
end end
end end

View File

@@ -23,6 +23,6 @@ class ItemSyncService < SyncServiceBase
end end
map_attribute :active, :active? map_attribute :active, :active?
map_attributes :description, :id, :name, :sku, :unit_price map_attributes :description, :id, :name, :sku, :type, :unit_price
end end

View File

@@ -0,0 +1,39 @@
<h1>Accounts</h1>
<%= form_with url: set_default_accounts_path, method: :patch, local: true do %>
<table style="width:100%; border-collapse: collapse; margin-bottom: 1em;">
<thead style="background-color: #f2f2f2;">
<tr>
<th style="padding: 8px; border: 1px solid #ddd;">Default</th>
<th style="padding: 8px; border: 1px solid #ddd;">Name</th>
<th style="padding: 8px; border: 1px solid #ddd;">Description</th>
<th style="padding: 8px; border: 1px solid #ddd;">Classification</th>
<th style="padding: 8px; border: 1px solid #ddd;">Active</th>
</tr>
</thead>
<tbody>
<% @accounts.each_with_index do |account, index| %>
<tr style="background-color: <%= index.even? ? '#ffffff' : '#f9f9f9' %>;">
<td style="text-align: center; border: 1px solid #ddd; padding: 8px;">
<%= radio_button_tag "default_account_id", account.id, account.default %>
</td>
<td style="border: 1px solid #ddd; padding: 8px;"><%= account.name %></td>
<td style="border: 1px solid #ddd; padding: 8px;"><%= account.description %></td>
<td style="border: 1px solid #ddd; padding: 8px;"><%= account.classification %></td>
<td style="text-align: center; border: 1px solid #ddd; padding: 8px;">
<%= account.active ? "Yes" : "No" %>
</td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr>
<td colspan="5" style="padding: 8px; text-align: right;">
<%= submit_tag "Save Default Account", style: "padding: 6px 12px; font-weight: bold;" %>
</td>
</tr>
</tfoot>
</table>
<% end %>

View File

@@ -1,8 +1,8 @@
<%= form_with model: @item, local: true do |f| %> <%= form_with model: @item, local: true do |f| %>
<% if @item.errors.any? %> <% if @item.errors.any? %>
<div id="errorExplanation"> <div id="errorExplanation" style="border: 1px solid #f00; padding: 10px; background-color: #fee; margin-bottom: 1em;">
<h2><%= pluralize(@item.errors.count, "error") %></h2> <h2 style="color: #900;"><%= pluralize(@item.errors.count, "error") %> prohibited this item from being saved:</h2>
<ul> <ul>
<% @item.errors.full_messages.each do |msg| %> <% @item.errors.full_messages.each do |msg| %>
<li><%= msg %></li> <li><%= msg %></li>
@@ -11,33 +11,59 @@
</div> </div>
<% end %> <% end %>
<p> <table style="width: 100%; border-collapse: collapse;">
<%= f.label :name %><br> <tbody>
<%= f.text_field :name, required: true %> <tr>
</p> <td style="padding: 8px; vertical-align: top;"><%= f.label :name %></td>
<td style="padding: 8px;"><%= f.text_field :name, required: true, style: "width: 100%;" %></td>
</tr>
<p> <tr>
<%= f.label :sku %><br> <td style="padding: 8px; vertical-align: top;"><%= f.label :sku %></td>
<%= f.text_field :sku %> <td style="padding: 8px;"><%= f.text_field :sku, style: "width: 100%;" %></td>
</p> </tr>
<p> <tr>
<%= f.label :description %><br> <td style="padding: 8px; vertical-align: top;"><%= f.label :description %></td>
<%= f.text_area :description, rows: 3 %> <td style="padding: 8px;"><%= f.text_area :description, rows: 3, style: "width: 100%;" %></td>
</p> </tr>
<p> <tr>
<%= f.label :unit_price %><br> <td style="padding: 8px; vertical-align: top;"><%= f.label :unit_price %></td>
<%= f.number_field :unit_price, step: 0.01 %> <td style="padding: 8px;"><%= f.number_field :unit_price, step: 0.01, style: "width: 100%;" %></td>
</p> </tr>
<p> <tr>
<%= f.label :active %> <td style="padding: 8px; vertical-align: top;"><%= f.label :account %></td>
<%= f.check_box :active %> <td style="padding: 8px;">
</p> <%= 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%;" } %>
</td>
</tr>
<p> <tr>
<%= f.submit %> <td style="padding: 8px; vertical-align: top;"><%= f.label :type %></td>
<td style="padding: 8px;">
<%= f.select :type,
Quickbooks::Model::Item::ITEM_TYPES.map { |t| [t, t] },
{ selected: @item.type || Quickbooks::Model::Item::NON_INVENTORY_TYPE },
{ style: "width: 100%;" } %>
</td>
</tr>
<tr>
<td style="padding: 8px; vertical-align: middle;"><%= f.label :active %></td>
<td style="padding: 8px; vertical-align: middle;"><%= f.check_box :active %></td>
</tr>
</tbody>
</table>
<p style="text-align: right; margin-top: 1em;">
<%= f.submit "Save Item", style: "padding: 6px 12px; font-weight: bold;" %>
</p> </p>
<% end %> <% end %>

View File

@@ -1,26 +1,35 @@
<h2><%= @item.name %></h2> <h2 style="margin-bottom: 1em; border-bottom: 2px solid #ccc; padding-bottom: 4px;"><%= @item.name %></h2>
<p> <table style="width: 100%; border-collapse: collapse; margin-bottom: 1.5em;">
<strong>SKU:</strong> <tbody>
<%= @item.sku %> <tr style="background-color: #f9f9f9;">
</p> <td style="padding: 8px; font-weight: bold; width: 150px;">SKU:</td>
<td style="padding: 8px;"><%= @item.sku.presence || "-" %></td>
</tr>
<tr>
<td style="padding: 8px; font-weight: bold;">Description:</td>
<td style="padding: 8px;"><%= @item.description.presence || "-" %></td>
</tr>
<tr style="background-color: #f9f9f9;">
<td style="padding: 8px; font-weight: bold;">Unit Price:</td>
<td style="padding: 8px;"><%= number_to_currency(@item.unit_price) %></td>
</tr>
<tr>
<td style="padding: 8px; font-weight: bold;">Type:</td>
<td style="padding: 8px;"><%= @item.type.presence || "-" %></td>
</tr>
<tr style="background-color: #f9f9f9;">
<td style="padding: 8px; font-weight: bold;">Account:</td>
<td style="padding: 8px;"><%= @item.account&.name || "-" %></td>
</tr>
<tr>
<td style="padding: 8px; font-weight: bold;">Active:</td>
<td style="padding: 8px;"><%= @item.active ? "Yes" : "No" %></td>
</tr>
</tbody>
</table>
<p> <div style="margin-top: 1em;">
<strong>Description:</strong> <%= link_to "Edit", edit_item_path(@item), class: "btn btn-primary", style: "margin-right: 8px;" %>
<%= @item.description %> <%= link_to "Back", items_path, class: "btn btn-secondary" %>
</p> </div>
<p>
<strong>Unit Price:</strong>
<%= number_to_currency(@item.unit_price) %>
</p>
<p>
<strong>Active:</strong>
<%= @item.active ? "Yes" : "No" %>
</p>
<p>
<%= link_to "Edit", edit_item_path(@item), class: "icon icon-edit" %>
<%= link_to "Back", items_path %>
</p>

View File

@@ -14,3 +14,9 @@ resources :items do
get :sync get :sync
end end
end end
resources :accounts do
collection do
patch :set_default
end
end

View File

@@ -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

View File

@@ -16,13 +16,13 @@ module RedmineQboLineItems
# Called by WebhookProcessJob # Called by WebhookProcessJob
def qbo_additional_entities(context={}) def qbo_additional_entities(context={})
log "Added QBO Item to allowed webook entities" log "Added QBO Item to allowed webook entities"
return "Item" return ["Item", "Account"]
end end
# Called by the QboSyncDispatcher # Called by the QboSyncDispatcher
def qbo_full_sync (context={}) def qbo_full_sync (context={})
log "Adding Item to QBO sync dispatcher" log "Adding Item to QBO sync dispatcher"
return Item return [Item, Account]
end end
private private