-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1a49dc
commit 3e61d1b
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require 'uuid' | ||
|
||
module UTA::Models | ||
# An instance of <http://uptheasset.org/ontology#Transaction>. | ||
# | ||
# A transaction is the record of an exchange between two or more accounts in | ||
# a journal. Every transaction should have at least two entries (one credit, | ||
# one debit). | ||
class Transaction | ||
include Spira::Resource | ||
|
||
base_uri "this:transactions#" | ||
type RDF::UTA.Transaction | ||
|
||
property :id, :predicate => RDF::DC.identifier | ||
property :date, :predicate => RDF::DC.date | ||
property :label, :predicate => RDF::RDFS.label | ||
property :comment, :predicate => RDF::RDFS.comment | ||
|
||
has_many :entries, :predicate => RDF::UTA.entry, :type => :Entry | ||
|
||
# Generate and save a new transaction (with its own UUID). | ||
# | ||
# @param [Hash] attributes | ||
# An optional hash of attributes to set on the generated transaction. | ||
# | ||
# @yield [Transaction] The generated transaction (not yet saved). | ||
# | ||
# @return [Transaction] The saved transaction. | ||
# | ||
# @example Record a sale | ||
# Transaction.generate do |t| | ||
# t.credit 30, RDF::URI.new("accounts#revenue") | ||
# t.debit 30, RDF::URI.new("accounts#cash") | ||
# end | ||
def self.generate(attributes = {}) | ||
uuid = ::UUID.new.generate | ||
transaction = self.for(uuid) | ||
transaction.id = RDF::UUID[uuid] | ||
transaction.date = Date.today | ||
transaction.update(attributes) | ||
yield transaction if block_given? | ||
transaction.save! | ||
end | ||
|
||
# Add a (saved) credit {Entry} to this transaction. | ||
# | ||
# @param [RDF::Value] amount | ||
# The amount by which to credit. | ||
# @param [Account, RDF::URI] account | ||
# The account to credit. | ||
# @return [Entry] the created entry. | ||
def credit(amount, account) | ||
self.entries << Entry.credit(amount, account).save! | ||
end | ||
|
||
# Add a (saved) debit {Entry} to this transaction. | ||
# | ||
# @param [RDF::Value] amount | ||
# The amount by which to debit. | ||
# @param [Account, RDF::URI] account | ||
# The account to debit. | ||
# @return [Entry] the created entry. | ||
def debit(amount, account) | ||
self.entries << Entry.debit(amount, account).save! | ||
end | ||
end | ||
end |