implemented Cash positions
[smr.git] / gui / app / controllers / assets_controller.rb
blob79d8f2b9876b99ce0ddc7efa541e9d1b40f18e1e
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 'asset'
19 # Shows total Assets.
20 class AssetsController < ApplicationController
22     ##
23     # Generates lists of currently open and closed positions where "currently"
24     # is defined by #smr_browse_date.
25     #
26     # It also stores some of these lists in the Session. Other controllers
27     # might check for and make use if this to avoid expensive database queries. There is:
28     #  * :open_positions_id_stock  => collection of Stock#id values of the
29     #                                 positions currently open
30     #
31     def index
32         smr_menu_addsubitem('assets', {
33             '+ position'=>:new_position,
34             '+ quote'=>:new_asset,
35         })
37         @assets = Smr::Asset.new(
38             current_user.id,
39             smr_browse_date,
40             params.fetch(:asset_limit_portfolio, false)
41         )
42         @open_positions = @assets.open_positions
44         # exports for use by other controllers
45         session[:open_positions_id_stock] = @assets.open_positions.collect{ |p| p.stock.id }
47         @securities = smr_securities_list
48         @portfolios = smr_portfolios_list
49     end
51     ##
52     # handle new/updated Quote
53     def new
54         self.index
55         if params[:id] then
56             @quote = Quote.where(:id=>params[:id]).first
57         else @quote = Quote.new(:date=>Time.now.to_i) end
59         render :index
60     end
62     ##
63     # handles creates and updates for Quote
64     def create
65         if params[:quote][:id].to_i > 0 then
66             q = Quote.where(:id=>params[:quote][:id]).first
67         else q = Quote.new(quote_params) end
69         q.date = Time.parse(params[:time]).to_i if params[:time]
70         q.exchange = 'Quotation by SMR User' if q.exchange.empty?
72         if params[:quote][:id].to_i > 0 then
73             q.update(quote_params)
74         else q.save end
76         # FIXME: bug in rails?
77         # - this :notice flash message it lost when using redirect_to
78         # - it is not lost if the default template is rendered: app/views/assets/create.html.erb
79         #redirect_to assets_path, :notice=>q.errors.full_messages.uniq.join(', ')
80         flash.notice = 'Adding Quote failed: ' + q.errors.full_messages.uniq.join(', ') if not q.errors.empty?
82         if URI(request.referer).path == new_asset_path then
83             index
84             render :index
85         else redirect_to :back end
86     end
89  protected
91     ##
92     # internal helper defining parameters acceptable for Quote create/update
93     def quote_params
94         params.require(:quote).permit(
95             :id, :id_stock, :date, :quote, :low,
96             :high, :volume, :exchange
97         )
98     end