has_accounts 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/has_accounts.rb CHANGED
@@ -1 +1,3 @@
1
- require 'has_accounts/railtie' if defined?(::Rails::Railtie)
1
+ require 'has_accounts/model'
2
+
3
+ require 'has_accounts/railtie' if defined?(::Rails::Railtie)
@@ -0,0 +1 @@
1
+ require 'has_accounts/railtie' if defined?(::Rails::Railtie)
@@ -0,0 +1,56 @@
1
+ module HasAccounts
2
+ module Model
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_inheritable_accessor :direct_account
7
+
8
+ has_many :bookings, :as => :reference, :dependent => :destroy do
9
+ # TODO: duplicated in Booking (without parameter)
10
+ def direct_balance(value_date = nil, direct_account = nil)
11
+ return BigDecimal.new('0') unless proxy_owner.direct_account
12
+
13
+ direct_account ||= proxy_owner.direct_account
14
+ balance = BigDecimal.new('0')
15
+
16
+ direct_bookings = scoped
17
+ direct_bookings = direct_bookings.where("value_date <= ?", value_date) if value_date
18
+
19
+ for booking in direct_bookings.all
20
+ balance += booking.accounted_amount(direct_account)
21
+ end
22
+
23
+ balance
24
+ end
25
+ end
26
+ end
27
+
28
+ module ClassMethods
29
+ def sidebar(options = true)
30
+ if options.is_a? TrueClass
31
+ self.sidebars = ["#{controller_name}/sidebar"]
32
+ end
33
+ end
34
+ end
35
+
36
+ module InstanceMethods
37
+ # Delegate to class
38
+ def direct_account
39
+ self.class.direct_account
40
+ end
41
+
42
+ def build_booking
43
+ booking_template = BookingTemplate.find_by_code(self.class.to_s.underscore + ':invoice')
44
+
45
+ booking = booking_template.build_booking(:value_date => value_date, :amount => amount)
46
+ bookings << booking
47
+
48
+ booking
49
+ end
50
+
51
+ def balance(value_date = nil)
52
+ bookings.direct_balance(value_date)
53
+ end
54
+ end
55
+ end
56
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_accounts
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Simon H\xC3\xBCrlimann (CyT)"
@@ -38,6 +38,8 @@ files:
38
38
  - lib/has_accounts/railtie.rb
39
39
  - lib/has_accounts/class_methods.rb
40
40
  - lib/has_accounts/core_ext/rounding.rb
41
+ - lib/has_accounts/model.rb
42
+ - lib/has_accounts.rb~
41
43
  - lib/has_accounts.rb
42
44
  - MIT-LICENSE
43
45
  - Rakefile