implemented Cash positions
[smr.git] / gui / lib / smr / asset_position_dividend.rb
blobdb3444533588a2024c07c8d71e723a82f55f9f45
2 # This file is part of SMR.
4 # SMR is free software: you can redistribute it and/or modify it under the
5 # terms of the GNU General Public License as published by the Free Software
6 # Foundation, either version 3 of the License, or (at your option) any later
7 # version.
9 # SMR is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # SMR.  If not, see <http://www.gnu.org/licenses/>.
17 module Smr  #:nodoc:
18   ##
19   # Dividend receivend on a AssetPosition at some point in time.
20   #
21   # FIXME: re-implement! the hash made by #query_payments is not nice
22   class AssetPositionDividend
23       ##
24       # initialize with Smr::Position
25       def initialize (position)
26           raise 'position must be a SmrPosition object' unless position.is_a?(Smr::AssetPosition)
27           @position = position
28   
29           @payments = []
30           @received = 0.0
31           @_queried_payments = false
32       end
33   
34       public
35   
36       ##
37       # returns array of payments received, uses Dividend model to figure it
38       #
39       # hash keys for each payment are: :time, :received, :shares and :total
40       def payments
41           query_payments
42           @payments
43       end
44   
45       ##
46       # returns total dividend received
47       def received
48           query_payments
49           @received
50       end
51   
52       def empty?
53           @payments.empty?
54       end
55   
56       protected
57   
58       ##
59       # queries all payments
60       # - result will be cached right here so ActiveRecord is invoked only once
61       def query_payments
62           if @_queried_payments then return end
63   
64           dp = Dividend.where('id_stock = %i' % @position.stock.id).where('date <= %i' % @position.date)
65           if not dp.empty? then
66               dp.each do |d|
67                   @position.revisions.each do |r|
68                       if r.date <= d.date then
69                           @payments[d.id] = {
70                               :time => d.time,
71                               :received => d.received,
72                               :shares => r.shares,
73                               :total => r.shares * d.received,
74                               :position_invested => r.invested
75                           }
76                       end
77                   end
78               end
79               @payments.compact!
80               @payments.each do |p| @received += p[:total] end
81           end
82   
83           @_queried_payments = true
84       end
85   end
87 end # module