Files
redmine_qbo_lineitems/app/models/line_item.rb
T
rickyandClaude Opus 5.5 063da99904 Record added, changed and removed line items in the issue's history
Line item changes go into the journal of the issue update that made them, shown like
Redmine's own changes (e.g. "Line item Brake pads: 2 × $45.00 added"). They're only
visible to users who may view line items, in the history, emails, PDF, API and webhooks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 21:38:02 -05:00

48 lines
2.3 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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 LineItem < ApplicationRecord
belongs_to :issue
belongs_to :item, optional: true
validates :description, presence: true
validates :quantity, numericality: { greater_than: 0 }
validates :unit_price, numericality: { greater_than_or_equal_to: 0 }
before_save :total
# The fields whose changes are recorded in the issue's history
JOURNALED_ATTRIBUTES = %w[description quantity unit_price item_id].freeze
# A one-line summary for the issue's history, e.g. "Brake pads: 2 × $45.00".
# With saved: true it describes the line as it is in the database, before any unsaved changes.
def journal_summary(saved: false)
desc, qty, price = %w[description quantity unit_price].map { |a| saved ? attribute_in_database(a) : self[a] }
qty = ActiveSupport::NumberHelper.number_to_rounded(qty.to_d, precision: 4, strip_insignificant_zeros: true)
price = ActiveSupport::NumberHelper.number_to_currency(price.to_d)
"#{desc.to_s.squish}: #{qty} × #{price}"
end
# True when a field shown in the issue's history has an unsaved change
def journaled_changes?
(changed & JOURNALED_ATTRIBUTES).any?
end
private
def total
log "Updating line total"
self.line_total = self.unit_price * self.quantity
end
def log(msg)
Rails.logger.info "[LineItem] #{msg}"
end
end