diff --git a/README.md b/README.md index d3d931c..7c5d934 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,9 @@ This plugin allows **billable line items** to be attached to a Redmine issue. Wh * **Redmine:** 6.1+ * **Parent Plugin:** [Redmine QuickBooks Online](https://github.com/rickbarrette/redmine_qbo) (must be installed and configured) - + +* **Billing Plugin:** [Redmine QuickBooks Billing](https://github.com/rickbarrette/redmine_qbo_billing). It holds the QuickBooks product/service list, the income accounts, and the estimate writing this plugin uses. + --- @@ -20,6 +22,7 @@ This plugin allows **billable line items** to be attached to a Redmine issue. Wh | Plugin Version | Redmine Version | Parent Plugin Version | | --- | --- | --- | | 2026.3.8+ | 6.1.x | 2026.3.9+ | +| 2026.11.0+ | 7.0.x | 2026.10.1+, with redmine_qbo_billing 2026.10.0+ | --- @@ -86,9 +89,11 @@ Before using this plugin: 2. Ensure your **QuickBooks Online** company file is connected. -3. Sync Accounts & Items via plugin settings +3. Install redmine_qbo_billing, then sync accounts and items and set the default income account for new items in its plugin settings -4. Set default income account for new items via plugin settings +### Upgrading from a version that managed items and accounts + +Items and accounts moved to redmine_qbo_billing. Install redmine_qbo_billing next to this plugin and run the plugin migrations as usual: billing finds the existing `items` and `accounts` tables and keeps using them, so no data is moved or lost. The Items and Accounts pages keep their URLs. --- diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb deleted file mode 100644 index 99f6f13..0000000 --- a/app/controllers/accounts_controller.rb +++ /dev/null @@ -1,37 +0,0 @@ -#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 - 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_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 - Account.sync - redirect_to :home, flash: { notice: I18n.t(:label_syncing) } - end -end \ No newline at end of file diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb deleted file mode 100644 index a2d8637..0000000 --- a/app/controllers/items_controller.rb +++ /dev/null @@ -1,106 +0,0 @@ -#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 ItemsController < ApplicationController - 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 - def autocomplete - term = ActiveRecord::Base.sanitize_sql_like(params[:q].to_s) - - items = Item.where("description LIKE :t OR name LIKE :t OR sku LIKE :t", t: "%#{term}%") - .where(active: true) - .order(:description) - .limit(20) - - render json: items.map { |i| - { id: i.id, name: i.name, sku: i.sku, description: i.description, price: i.unit_price } - } - end - - def create - @item = Item.new(item_params) - - if @item.save - redirect_to item_path(@item), notice: l(:notice_successful_create) - else - render :new, status: :unprocessable_entity - 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 - @item.destroy - redirect_to items_path, notice: l(:notice_successful_delete) - end - - def edit - end - - def index - @items = Item.order(:name) - end - - def new - @item = Item.new - end - - def show - end - - def sync - Item.sync - redirect_to :home, flash: { notice: I18n.t(:label_syncing) } - end - - def update - if @item.update(item_params) - redirect_to item_path(@item), notice: l(:notice_successful_update) - else - render :edit, status: :unprocessable_entity - end - end - - private - - def find_item - @item = Item.find(params[:id]) - rescue ActiveRecord::RecordNotFound - render_404 - end - - def item_params - params.require(:item).permit(:name, :description, :sku, :unit_price, :active, :account_id, :type, :taxable) - end - - def log(msg) - Rails.logger.info "[ItemsController] #{msg}" - end - -end \ No newline at end of file diff --git a/app/jobs/bill_line_items_job.rb b/app/jobs/bill_line_items_job.rb index d06c216..7aad48f 100644 --- a/app/jobs/bill_line_items_job.rb +++ b/app/jobs/bill_line_items_job.rb @@ -24,10 +24,7 @@ class BillLineItemsJob < ActiveJob::Base unbilled_entries = issue.line_items.where(billed: [false, nil]).lock return if unbilled_entries.blank? - qbo = QboConnectionService.current! - qbo.perform_authenticated_request do |access_token| - create_estimate(issue, unbilled_entries, access_token, qbo) - end + create_estimate(issue, unbilled_entries) # Only mark billed AFTER successful QBO creation unbilled_entries.update_all(billed: true) @@ -42,35 +39,28 @@ class BillLineItemsJob < ActiveJob::Base private - # Create an Estimate record in QBO for each unbilled line item - def create_estimate(issue, unbilled_entries, access_token, qbo) - log "Creating Estimate records in QBO for #{issue.customer.name} from issue ##{issue.id}" - - estimate = Quickbooks::Model::Estimate.new(customer_id: issue.customer.id) - estimate_service = Quickbooks::Service::Estimate.new( company_id: qbo.realm_id, access_token: access_token) + # Creates the QuickBooks estimate with redmine_qbo_billing, which writes every estimate: a first line + # saying where the lines came from, then the unbilled line items. The VIN comes from the issue's vehicle. + def create_estimate(issue, unbilled_entries) + log "Creating estimate in QBO for #{issue.customer.name} from issue ##{issue.id}" memo = "Added from: #{issue.tracker} ##{issue.id}: #{issue.subject}" - estimate.private_note = memo - estimate.line_items << Quickbooks::Model::InvoiceLineItem.new(description: memo, detail_type: 'DescriptionOnly' ) - - unbilled_entries.each do |item| - log "Creating Line Item for #{item.description}" - line = Quickbooks::Model::InvoiceLineItem.new - line.amount = item.line_total - line.description = item.description - line.sales_item! do |detail| - detail.unit_price = item.unit_price - detail.quantity = item.quantity - # Assign "TAX" only if the item is taxable or unknown - if item.item.nil? || item.item.taxable - detail.tax_code_ref = Quickbooks::Model::BaseReference.new("TAX") - end - end - estimate.line_items << line + lines = [{ description: memo }] + unbilled_entries.each do |entry| + lines << { + item_id: entry.item_id, + description: entry.description, + quantity: entry.quantity, + rate: entry.unit_price, + # Taxable unless the item says otherwise (or the line has no item) + taxable: entry.item.nil? || entry.item.taxable != false + } end - e = estimate_service.create(estimate) - log "Created estimate ##{e.doc_number}" + vin = issue.respond_to?(:vehicle) ? issue.vehicle&.vin : nil + estimate = RedmineQboBilling::EstimateWriter.create_estimate(customer: issue.customer, lines: lines, + private_note: memo, vin: vin) + log "Created estimate ##{estimate.doc_number}" end def log(msg) diff --git a/app/models/account.rb b/app/models/account.rb deleted file mode 100644 index 1aef2a5..0000000 --- a/app/models/account.rb +++ /dev/null @@ -1,41 +0,0 @@ -#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 - - def to_s - name - 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 deleted file mode 100644 index d9a5f87..0000000 --- a/app/models/item.rb +++ /dev/null @@ -1,68 +0,0 @@ -#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 Item < QboBaseModel - belongs_to :account, optional: true - - 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 - after_initialize :set_defaults, if: :new_record? - before_destroy :make_inactive - - # Updates Both local & remote DB account ref (a blank or unknown account leaves QBO's ref unchanged) - def account_id=(id) - account = Account.find_by(id: id) if id.present? - details.income_account_ref = account.ref if account - super - end - - # Updates Both local & remote DB description - def description=(s) - details.description = s - super - end - - # Updates Both local & remote DB name - def name=(s) - details.name = s - super - end - - def make_inactive - details.active = false - push_to_qbo - end - - def set_defaults - self.taxable = true if taxable.nil? - end - - # Updates Both local & remote DB sku - def sku=(s) - details.sku = s - 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 - super - end - -end \ No newline at end of file diff --git a/app/services/account_sync_service.rb b/app/services/account_sync_service.rb deleted file mode 100644 index f7bea75..0000000 --- a/app/services/account_sync_service.rb +++ /dev/null @@ -1,23 +0,0 @@ -#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 deleted file mode 100644 index e19e108..0000000 --- a/app/services/item_service.rb +++ /dev/null @@ -1,26 +0,0 @@ -#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 ItemService < ServiceBase - - private - - def build_qbo_remote - log "Building new QBO Item" - # 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 deleted file mode 100644 index a963e52..0000000 --- a/app/services/item_sync_service.rb +++ /dev/null @@ -1,35 +0,0 @@ -#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 ItemSyncService < SyncServiceBase - - private - - # Specify the local model this service syncs - def self.model_class - Item - end - - # Specify a page size of 20, as the API only returns 20 items at a time. - def self.page_size - 20 - end - - 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_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 - -end \ No newline at end of file diff --git a/app/views/accounts/index.html.erb b/app/views/accounts/index.html.erb deleted file mode 100644 index c476dd5..0000000 --- a/app/views/accounts/index.html.erb +++ /dev/null @@ -1,37 +0,0 @@ -
| <%= l(:label_default) %> | -<%= l(:field_name) %> | -<%= l(:field_description) %> | -<%= l(:field_classification) %> | -<%= l(:field_active) %> | -
|---|---|---|---|---|
| - <%= radio_button_tag "default_account_id", account.id, account.default %> - | -<%= account.name %> | -<%= truncate(account.description, length: 80) %> | -<%= account.classification %> | -- <%= checked_image account.active %> - | -
- <%= f.text_field :name, required: true, size: 60 %> -
- -- <%= f.text_field :sku, size: 30 %> -
- -- <%= f.text_area :description, rows: 4, class: 'wiki-edit' %> -
- -- <%= f.text_field :unit_price, - class: "price-field", - inputmode: "decimal", - autocomplete: "off"%> -
- -- <%= f.check_box :taxable %> -
- -- <%= f.label :account_id, l(: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 } %> -
- -- <%= f.select :type, - Quickbooks::Model::Item::ITEM_TYPES.map { |t| [t, t] }, - { selected: @item.type || Quickbooks::Model::Item::NON_INVENTORY_TYPE } %> -
- -- <%= f.check_box :active %> -
-| <%= l(:field_name) %> | -<%= l(:field_sku) %> | -<%= l(:field_description) %> | -<%= l(:field_unit_price) %> | -<%= l(:field_taxable) %> | -<%= l(:field_active) %> | -- |
|---|---|---|---|---|---|---|
| <%= link_to item.name, item_path(item) %> | -<%= item.sku %> | -<%= truncate(item.description, length: 60) %> | -<%= number_to_currency(item.unit_price) %> | -- <% if item.taxable %> - <%= l(:general_text_Yes) %> - <% else %> - <%= l(:general_text_No) %> - <% end %> - | -- <% if item.active %> - <%= l(:general_text_Yes) %> - <% else %> - <%= l(:general_text_No) %> - <% end %> - | -- <%= link_to l(:button_edit), edit_item_path(item), class: 'icon icon-edit' %> - <%= link_to l(:button_delete), item_path(item), - method: :delete, - data: { confirm: l(:text_are_you_sure) }, - class: 'icon icon-del' %> - | -
<%= l(:label_no_data) %>
-<% end %> \ No newline at end of file diff --git a/app/views/items/new.html.erb b/app/views/items/new.html.erb deleted file mode 100644 index f2e4270..0000000 --- a/app/views/items/new.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -<%=t(:field_sku)%>: <%= @item.sku.presence || "-" %>
-<%=t(:label_type)%>: <%= @item.type.presence || "-" %>
-<%=t(:field_unit_price)%>: <%= number_to_currency(@item.unit_price) %>
-<%=t(:label_account)%>: <%= @item.account&.name || "-" %>
-- <%=t(:field_taxable)%>: - <% if @item.taxable %> - <%=t(:yes)%> - <% else %> - <%=t(:no)%> - <% end %> -
-- <%=t(:label_active)%>: - <% if @item.active %> - <%=t(:yes)%> - <% else %> - <%=t(:no)%> - <% end %> -
-<%=t(:label_description)%>:
-- - <%= Item.count %> - - (@ <%= Item.last_sync %>) - -
-- - <%= Account.count %> - - (@ <%= Account.last_sync %>) - -
-- - <%= Qbo.exists? ? Qbo.last_sync : l(:label_never_synced) %> -
-- - <%= Account.get_default %> -
-