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 # see Smr::CashflowLog
20 class CashflowController < ApplicationController
23 # shows cashflows in adjustable manner
25 smr_menu_addsubitem('cashflow', {'+ dividend'=>:new_cashflow})
27 # timeframes this report can work with
29 'This Month' => smr_browse_date.beginning_of_month.to_i,
30 'This Year' => smr_browse_date.beginning_of_year.to_i,
31 '3 Years' => (smr_browse_date - 3.years).to_i,
32 '5 Years' => (smr_browse_date - 3.years).to_i,
33 'All Time' => Time.new(2000,1,1).to_i,
37 @filter = if params[:filter] then filter_params else :none end
38 @timeframe = if params[:start_date] then params[:start_date].to_i
39 else smr_browse_date.beginning_of_year.to_i end
40 @securities = smr_securities_list
43 @log = Smr::CashflowLog.new(Time.at(@timeframe), smr_browse_date, current_user.id)
44 @log.set_filter(@filter)
48 # handle new cashflows (right now Dividend only)
52 @dividend = Dividend.where(:id=>params[:id]).first
53 else @dividend = Dividend.new(:date=>smr_browse_date) end
59 # handles creates and updates
61 if params[:dividend][:id].to_i > 0 then
62 d = Dividend.where(:id=>params[:dividend][:id]).first
63 else d = Dividend.new(dividend_params) end
65 d.date = Time.now.to_i
67 if params[:dividend][:id].to_i > 0 then
68 d.update(dividend_params)
71 redirect_to cashflow_index_path, :notice=>d.errors.full_messages.uniq.join(', ')
78 # internal helper defining parameters acceptable for Dividend create/update
80 params.require(:dividend).permit(:id_stock, :date, :received)
84 # internal helper defining parameters acceptable for filter specification
86 if Smr::CashflowLog.new(Time.now,Time.now,1).filters.values.include?(params[:filter].parameterize.to_sym) then
87 params[:filter].parameterize.to_sym
89 raise 'symbol "%s" is not accepted as filter by SmrCashflowLog' % params[:filter]