organized code into Smr module
[smr.git] / gui / app / controllers / cashflow_controller.rb
blob4f164782303778c87a84332a8f1203e24f0c0676
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/>.
16 require 'cashflowlog'
19 # see Smr::CashflowLog
20 class CashflowController < ApplicationController
22     ##
23     # shows cashflows in adjustable manner
24     def index
25         smr_menu_addsubitem('cashflow', {'+ dividend'=>:new_cashflow})
27         # timeframes this report can work with
28         @timeframes = {
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,
34         }
36         # handle params
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
42         # make the log
43         @log = Smr::CashflowLog.new(Time.at(@timeframe), smr_browse_date, current_user.id)
44         @log.set_filter(@filter)
45     end
47     ##
48     # handle new cashflows (right now Dividend only)
49     def new
50         self.index
51         if params[:id] then
52             @dividend = Dividend.where(:id=>params[:id]).first
53         else @dividend = Dividend.new(:date=>smr_browse_date) end
55         render :index
56     end
58     ##
59     # handles creates and updates
60     def create
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)
69         else d.save end
71         redirect_to cashflow_index_path, :notice=>d.errors.full_messages.uniq.join(', ')
72     end
75  protected
77     ##
78     # internal helper defining parameters acceptable for Dividend create/update
79     def dividend_params
80         params.require(:dividend).permit(:id_stock, :date, :received)
81     end
83     ##
84     # internal helper defining parameters acceptable for filter specification
85     def filter_params
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
88       else
89             raise 'symbol "%s" is not accepted as filter by SmrCashflowLog' % params[:filter]
90       end
91     end
92 end