added watchlist and position transfer feature
[smr.git] / gui / app / controllers / objects / portfolio_controller.rb
blob4c2adf80319d5ac2ae88a139c976842c8e64d9ad
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/>.
18 # handles CRUD on Portfolio records
19 class Objects::PortfolioController < ObjectsController
20     def index
21         super
22         @portfolios, @total_pages = smr_paginate(@page = smr_page, smr_portfolios_list(:show_all=>true))
23     end
25     ##
26     # defines empty Portfolio to create() a new one
27     def new
28         self.index
29         @portfolio = Portfolio.new(:date_created=>Time.now.to_i)
30         render :index
31     end
33     ##
34     # edit Portfolio
35     def edit
36         self.index
37         @portfolio = Portfolio.find(params[:id])
39         @portfolios = smr_portfolios_list(:show_all=>true)
40         @assets = Smr::Asset.new(current_user.id, smr_browse_date,
41             :id_portfolio=>@portfolio.id, :skip_cash_positions=>true)
42         @open_positions = @assets.open_positions
43         render :index
44     end
46     ##
47     # handles creates and updates
48     def create
49         if params[:portfolio][:id].to_i > 0 then
50             p = Portfolio.where(:id=>params[:portfolio][:id], :id_user=>current_user.id).first
51         else p = Portfolio.new(portfolio_params) end
53         # hard-wired so noone spoofes it in here
54         p.id_user = current_user.id
55         p.date_created = Time.parse(params[:time_created]).to_i if params[:time_created]
57         # FIXME: not used any longer but required by schema, drop it?
58         p.type = ''
59         if params[:portfolio][:id].to_i > 0 then
60             p.update(portfolio_params)
61         else p.save end
63         redirect_to objects_portfolio_index_path, :notice=>p.errors.full_messages.uniq.join(', ')
64     end
66     ##
67     # handles Position transfers to another Portfolio
68     def patch
69         n = Array.new
70         a = Array.new
72         if params[:id_portfolio_transfer] and not params[:positions].empty? then
73             Position.where(:id=>params[:positions])
74                 .joins(:Portfolio).where(:portfolio=>{:id_user=>current_user.id}).each do |p|
75                 p.id_portfolio = params[:id_portfolio_transfer]
76                 p.save || a << 'transfering position #%i failed' % p.id
77             end
78             n << 'Positions have been transfered sucessfully.'
79         else
80             a << 'Please specify a portfolio as transfer destination and select at lease one position.'
81         end
83         redirect_to objects_portfolio_index_path, :notice=>n.join(','), :alert=>a.join(',')
84     end
86  protected
88     ##
89     # internal helper defining parameters acceptable for Portfolio create/update
90     def portfolio_params
91         params.require(:portfolio).permit(
92           :id, :name, :date_created, :taxallowance, :order, :comment
93         )
94     end
95 end