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/>.
18 # handles CRUD on Portfolio records
19 class Objects::PortfolioController < ObjectsController
22 @portfolios, @total_pages = smr_paginate(@page = smr_page, smr_portfolios_list(:show_all=>true))
26 # defines empty Portfolio to create() a new one
29 @portfolio = Portfolio.new(:date_created=>Time.now.to_i)
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
47 # handles creates and updates
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?
59 if params[:portfolio][:id].to_i > 0 then
60 p.update(portfolio_params)
63 redirect_to objects_portfolio_index_path, :notice=>p.errors.full_messages.uniq.join(', ')
67 # handles Position transfers to another Portfolio
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
78 n << 'Positions have been transfered sucessfully.'
80 a << 'Please specify a portfolio as transfer destination and select at lease one position.'
83 redirect_to objects_portfolio_index_path, :notice=>n.join(','), :alert=>a.join(',')
89 # internal helper defining parameters acceptable for Portfolio create/update
91 params.require(:portfolio).permit(
92 :id, :name, :date_created, :taxallowance, :order, :comment