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
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/>.
19 # Dividend receivend on a AssetPosition at some point in time.
21 # FIXME: re-implement! the hash made by #query_payments is not nice
22 class AssetPositionDividend
24 # initialize with Smr::Position
25 def initialize (position)
26 raise 'position must be a SmrPosition object' unless position.is_a?(Smr::AssetPosition)
31 @_queried_payments = false
37 # returns array of payments received, uses Dividend model to figure it
39 # hash keys for each payment are: :time, :received, :shares and :total
46 # returns total dividend received
59 # queries all payments
60 # - result will be cached right here so ActiveRecord is invoked only once
62 if @_queried_payments then return end
64 dp = Dividend.where('id_stock = %i' % @position.stock.id).where('date <= %i' % @position.date)
67 @position.revisions.each do |r|
68 if r.date <= d.date then
71 :received => d.received,
73 :total => r.shares * d.received,
74 :position_invested => r.invested
80 @payments.each do |p| @received += p[:total] end
83 @_queried_payments = true