has_accounts 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +10 -7
- data/lib/booking.rb~ +92 -0
- data/lib/has_accounts.rb +1 -0
- data/lib/has_accounts.rb~ +3 -1
- data/lib/has_accounts/core_ext/rounding.rb~ +31 -0
- data/lib/has_accounts/model.rb +0 -5
- data/lib/has_accounts/railtie.rb~ +10 -0
- metadata +13 -15
data/README.md
CHANGED
@@ -15,16 +15,19 @@ In Rails 3 simply add
|
|
15
15
|
Example
|
16
16
|
=======
|
17
17
|
|
18
|
-
|
19
|
-
by this plugin:
|
18
|
+
A few models are available:
|
20
19
|
|
21
|
-
|
20
|
+
class Booking
|
21
|
+
class Account
|
22
|
+
class AccountType
|
22
23
|
|
23
|
-
|
24
|
+
There's also a ready to use module available to attach accountable
|
25
|
+
functionality to existing models.
|
26
|
+
|
27
|
+
To use it, simply add the following to your Model:
|
28
|
+
|
29
|
+
include HasAccounts::Model
|
24
30
|
|
25
|
-
class Doctor < ActiveRecord::Base
|
26
|
-
has_vcards
|
27
|
-
end
|
28
31
|
|
29
32
|
License
|
30
33
|
=======
|
data/lib/booking.rb~
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
class Booking < ActiveRecord::Base
|
2
|
+
# Validation
|
3
|
+
validates_presence_of :debit_account, :credit_account, :title, :amount, :value_date
|
4
|
+
validates_time :value_date
|
5
|
+
|
6
|
+
belongs_to :debit_account, :foreign_key => 'debit_account_id', :class_name => "Account"
|
7
|
+
belongs_to :credit_account, :foreign_key => 'credit_account_id', :class_name => "Account"
|
8
|
+
|
9
|
+
# Scoping
|
10
|
+
named_scope :by_value_date, lambda {|value_date| { :conditions => { :value_date => value_date } } }
|
11
|
+
named_scope :by_value_period, lambda {|from, to| { :conditions => { :value_date => from..to } } }
|
12
|
+
|
13
|
+
named_scope :by_account, lambda {|account_id|
|
14
|
+
{ :conditions => ["debit_account_id = :account_id OR credit_account_id = :account_id", {:account_id => account_id}] }
|
15
|
+
} do
|
16
|
+
# Returns array of all booking titles.
|
17
|
+
def titles
|
18
|
+
find(:all, :group => :title).map{|booking| booking.title}
|
19
|
+
end
|
20
|
+
|
21
|
+
# Statistics per booking title.
|
22
|
+
#
|
23
|
+
# The statistics are an array of hashes with keys title, count, sum, average.
|
24
|
+
def statistics
|
25
|
+
find(:all, :select => "title, count(*) AS count, sum(amount) AS sum, avg(amount) AS avg", :group => :title).map{|stat| stat.attributes}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns array of all years we have bookings for
|
30
|
+
def self.fiscal_years
|
31
|
+
with_exclusive_scope do
|
32
|
+
find(:all, :select => "year(value_date) AS year", :group => "year(value_date)").map{|booking| booking.year}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.scope_by_value_date(value_date)
|
37
|
+
scoping = self.default_scoping - [@by_value_scope]
|
38
|
+
|
39
|
+
@by_value_scope = {:find => {:conditions => {:value_date => value_date}}}
|
40
|
+
scoping << @by_value_scope
|
41
|
+
|
42
|
+
Thread.current["#{self}_scoped_methods"] = nil
|
43
|
+
self.default_scoping = scoping
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.filter(controller, &block)
|
47
|
+
if controller.value_date_scope
|
48
|
+
with_scope(:find => {:conditions => {:value_date => controller.value_date_scope}}, &block)
|
49
|
+
else
|
50
|
+
block.call
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Standard methods
|
55
|
+
def to_s(format = :default)
|
56
|
+
case format
|
57
|
+
when :short
|
58
|
+
"#{value_date.strftime('%d.%m.%Y')}: #{credit_account.code} / #{debit_account.code} CHF #{amount_as_string} "
|
59
|
+
else
|
60
|
+
"#{value_date.strftime('%d.%m.%Y')}: #{credit_account.title} (#{credit_account.code}) an #{debit_account.title} (#{debit_account.code}) CHF #{amount_as_string}, #{title} " +
|
61
|
+
(comments.blank? ? "" :"(#{comments})")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Helpers
|
66
|
+
def accounted_amount(account)
|
67
|
+
if credit_account == account
|
68
|
+
return amount
|
69
|
+
elsif debit_account == account
|
70
|
+
return -(amount)
|
71
|
+
else
|
72
|
+
return 0.0
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def amount_as_string
|
77
|
+
'%0.2f' % amount
|
78
|
+
end
|
79
|
+
|
80
|
+
def amount_as_string=(value)
|
81
|
+
self.amount = value
|
82
|
+
end
|
83
|
+
|
84
|
+
# Reference
|
85
|
+
belongs_to :reference, :polymorphic => true
|
86
|
+
after_save :notify_references
|
87
|
+
|
88
|
+
private
|
89
|
+
def notify_references
|
90
|
+
reference.booking_saved(self) if reference.respond_to?(:booking_saved)
|
91
|
+
end
|
92
|
+
end
|
data/lib/has_accounts.rb
CHANGED
data/lib/has_accounts.rb~
CHANGED
@@ -0,0 +1,31 @@
|
|
1
|
+
module HasAccounts #:nodoc:
|
2
|
+
module CoreExtensions #:nodoc:
|
3
|
+
module Rounding
|
4
|
+
# Rounds the float according to currency rules.
|
5
|
+
# Currently targeted to Swiss Francs (CHF), usable
|
6
|
+
# for all currencies having 0.05 as smallest unit.
|
7
|
+
#
|
8
|
+
# x = 1.337
|
9
|
+
# x.round # => 1.35
|
10
|
+
def currency_round
|
11
|
+
if self.nil?
|
12
|
+
return 0.0
|
13
|
+
else
|
14
|
+
return (self * 20).round / 20.0
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module BigDecimal
|
20
|
+
module Rounding
|
21
|
+
def currency_round
|
22
|
+
if self.nil?
|
23
|
+
return BigDecimal.new("0")
|
24
|
+
else
|
25
|
+
return (self * 20).round / 20
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/has_accounts/model.rb
CHANGED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'has_accounts'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module HasAccounts
|
5
|
+
class Railtie < Rails::Engine
|
6
|
+
Float.send :include, HasAccounts::CoreExtensions::Rounding
|
7
|
+
Fixnum.send :include, HasAccounts::CoreExtensions::Rounding
|
8
|
+
BigDecimal.send :include, HasAccounts::CoreExtensions::Rounding
|
9
|
+
end
|
10
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_accounts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
4
|
+
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- "Simon H\xC3\xBCrlimann (CyT)"
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2011-04-
|
17
|
+
date: 2011-04-11 00:00:00 +02:00
|
19
18
|
default_executable:
|
20
19
|
dependencies: []
|
21
20
|
|
@@ -32,15 +31,18 @@ files:
|
|
32
31
|
- app/models/account_scope_extension.rb
|
33
32
|
- app/models/bank.rb
|
34
33
|
- app/models/account.rb
|
35
|
-
- app/models/bank_account.rb
|
36
|
-
- app/models/account_type.rb
|
37
34
|
- app/models/booking.rb
|
38
|
-
-
|
35
|
+
- app/models/account_type.rb
|
36
|
+
- app/models/bank_account.rb
|
37
|
+
- lib/has_accounts.rb~
|
38
|
+
- lib/has_accounts.rb
|
39
|
+
- lib/booking.rb~
|
39
40
|
- lib/has_accounts/class_methods.rb
|
41
|
+
- lib/has_accounts/railtie.rb~
|
42
|
+
- lib/has_accounts/core_ext/rounding.rb~
|
40
43
|
- lib/has_accounts/core_ext/rounding.rb
|
44
|
+
- lib/has_accounts/railtie.rb
|
41
45
|
- lib/has_accounts/model.rb
|
42
|
-
- lib/has_accounts.rb~
|
43
|
-
- lib/has_accounts.rb
|
44
46
|
- MIT-LICENSE
|
45
47
|
- Rakefile
|
46
48
|
- README.md
|
@@ -54,27 +56,23 @@ rdoc_options: []
|
|
54
56
|
require_paths:
|
55
57
|
- lib
|
56
58
|
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
59
|
requirements:
|
59
60
|
- - ">="
|
60
61
|
- !ruby/object:Gem::Version
|
61
|
-
hash: 3
|
62
62
|
segments:
|
63
63
|
- 0
|
64
64
|
version: "0"
|
65
65
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
-
none: false
|
67
66
|
requirements:
|
68
67
|
- - ">="
|
69
68
|
- !ruby/object:Gem::Version
|
70
|
-
hash: 3
|
71
69
|
segments:
|
72
70
|
- 0
|
73
71
|
version: "0"
|
74
72
|
requirements: []
|
75
73
|
|
76
74
|
rubyforge_project:
|
77
|
-
rubygems_version: 1.
|
75
|
+
rubygems_version: 1.3.6
|
78
76
|
signing_key:
|
79
77
|
specification_version: 3
|
80
78
|
summary: HasAccounts provides models for financial accounting.
|