added security type models, improved cashflow, bug- and layout fixes along the way
[smr.git] / gui / app / controllers / objects / security_controller.rb
blob576868d566a90c0bfb5cd894a77221b4457f02b0
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 'daemon_client'
19 # handles CRUD on Security records
21 # NOTE:
22 # * this controller is called "Security" since the model is still Security. It
23 #   will become "Security" when support for different types of paper is
24 #   implemented (Bonds, Funds, Currency, ...).
25 # * until then we talk to the user about "Securities" already
27 class Objects::SecurityController < ObjectsController
29     ##
30     # List all Security records for editing.
31     def index
32         super
33         session[:objects_security_organization] = params[:id_organization] if params[:id_organization]
34         @id_organization = session[:objects_security_organization] || Smr::ID_UNIVERSE_ORGANIZATION
36         @securities, @total_pages = smr_paginate(
37             @page=smr_page,
38             Security.where(:id_organization=>@id_organization).all.to_a.sort
39         )
40         @organizations = Organization.all.order(:name).to_a
41         @symbolextensions = SecuritySymbolextension.all.order(:extension).to_a
42         @symbolextensions.insert(0, SecuritySymbolextension.new(:extension=>'', :fullname=>'<None>', :id=>0))
43         @quotesources = SecurityQuotesource.all.order(:source).to_a
44     end
46     ##
47     # defines empty Security to create() a new one
48     def new
49         self.index
50         @security = Security.new
51         render :index
52     end
54     ##
55     # retrieve Security for editing
56     # - NOTE: disabled @quote_retrieval_status because it hinders fluent
57     # security editing
58     def edit
59         self.index
61         #daemon = begin Smr::DaemonClient.new rescue false end
63         @security = Security.find(params[:id])
64         @security_type = @security.get_type
65         #if @security.fetch_quote and daemon then
66         #    daemon = Smr::DaemonClient.new
67         #    @quote_retrieval_status = daemon.status_security(@security.id)
68         #end
69         render :index
70     end
72     ##
73     # handles creates and updates
74     def create
75         n = Array.new
76         if params[:security]
77             if params[:security][:id_security_symbolextension].to_i == 0 then
78                 params[:security][:id_security_symbolextension] = nil
79             end
81             if params[:security][:id].to_i > 0 then
82                 s = Security.find(params[:security][:id])
83                 s.update(security_params)
84             else s = Security.create(security_params) end
85             n << s.errors.full_messages.uniq
86         end
88         if params[:security_stock]
89             if params[:security_stock][:id].to_i > 0 then
90                 st = SecurityStock.find(params[:security_stock][:id])
91                 st.update(security_stock_params)
92             else st = SecurityStock.create(security_stock_params) end
93             n << st.errors.full_messages.uniq
94         end
96         if params[:security_bond]
97             if params[:security_bond][:id].to_i > 0 then
98                 st = SecurityBond.find(params[:security_bond][:id])
99                 st.update(security_bond_params)
100             else st = SecurityBond.create(security_bond_params) end
101             n << st.errors.full_messages.uniq
102         end
104         if params[:security_fund]
105             if params[:security_fund][:id].to_i > 0 then
106                 st = SecurityFund.find(params[:security_fund][:id])
107                 st.update(security_fund_params)
108             else st = SecurityFund.create(security_fund_params) end
109             n << st.errors.full_messages.uniq
110         end
112         redirect_to objects_security_index_path, :notice=>n.flatten.join(', ')
113     end
115  protected
117     ##
118     # internal helper defining parameters acceptable for Security create/update
119     def security_params
120         params.require(:security).permit(
121           :symbol, :id_security_symbolextension, :id_security_quotesource,
122           :id_organization, :url, :brief, :description, :fetch_quote, :type,
123           :collateral_coverage_ratio
124         )
125     end
127     ##
128     # internal helper defining parameters acceptable for Security Type model create/update
129     def security_stock_params
130         params.require(:security_stock).permit(
131           :id_security, :dividend, :time_dividend_exdate, :dividend_interval, :type, :comment
132         )
133     end
135     def security_bond_params
136         params.require(:security_bond).permit(
137           :id_security,
138           :type, :currency, :denomination, :is_subordinated, :is_callable, :is_stepup, :is_stepdown,
139           :coupon, :coupon_interval, :interest_method, :time_first_coupon, :time_last_coupon,
140           :time_maturity, :redemption_price, :redemption_installments, :redemption_interval,
141           :comment
142         )
143     end
145     def security_fund_params
146         params.require(:security_fund).permit(
147           :id_security,
148           :type, :currency, :tranche, :time_inception, :time_fiscalyearend, :investment_minimum, :is_synthetic,
149           :distribution, :time_distribution, :distribution_interval, :is_retaining_profits,
150           :issue_surcharge, :management_fee, :redemption_fee, :redemption_period,
151           :comment
152         )
153     end