organized code into Smr module
[smr.git] / gui / app / controllers / figures_controller.rb
blob42338a1b8930a4b60b34b56dfae83bb1b47bc168
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 'figures'
19 # Smr::Figures are to record and follow fundamental information across fiscal
20 # quarters and years.
21 class FiguresController < ApplicationController
22     def index
23         smr_menu_addsubitem('figures', {'+ data'=>:new_figure})
25         begin daemon = Smr::DaemonClient.new
26         rescue 
27             daemon = false
28             flash[:notice] = 'Autofigures unavailable: %s' % $!
29         end
31         @datatable = Smr::FiguresDataTable.new
32         @have_data = false
33         @keep_form_open = session[:figures_keep_form_open]
34         @scale_value = session[:scale_value]
36         @securities = smr_securities_list
37         if params[:id_stock] and params[:id_stock].to_i>1 then
38             session[:figures_security_index] = @securities.index{|s|s.id==params[:id_stock].to_i}
39         end
41         case params[:show]
42             when 'previous' then session[:figures_security_index] -= 1
43             when 'next' then session[:figures_security_index] += 1
44         end
45         session[:figures_security_index] = 0 if not (0..@securities.count-1) === session[:figures_security_index]
46         @selected_security = @securities[ session[:figures_security_index] ]
48         # obtain + create autofigures
49         if daemon
50             autofigures = Array.new
51             autovars = FigureVar.where(:id_user=>[0,current_user.id]).where.not(:expression=>'')
52             parsed_exprs = daemon.parse_math_expressions( autovars.collect{|v| v.expression} )
53             for i in 0..autovars.length-1
54                 autofigures << Smr::Autofigure.new(autovars[i], parsed_exprs[i])
55             end
56         end
58         # obtain + figure data
59         FigureData.where(:id_stock=>@selected_security.id)
60             .joins(:FigureVar).where('figure_var.id_user IN (%i,%i)' % [0,current_user.id])
61             .each do |fd|
62             @datatable.add(fd)
63             @have_data = true
65             if daemon and not autofigures.empty?
66                 autofigures.each { |af| af.add(fd) }
67             end
68         end
70         # obtain autofigure data objects + solve their expressions
71         if daemon
72             autodatafigures = autofigures.collect { |af| af.get }.flatten(1)
74             expressions=Array.new; values=Array.new
75             autodatafigures.each do |ad|
76                 expressions << ad.expression
77                 values << ad.solving_data
78             end
79             results = daemon.solve_math_expressions(expressions, values)
81             # add results to SmrAutofigureData objects + push them into @datatable
82             for i in 0..autodatafigures.length-1
83                 autodatafigures[i].value = results[i] if results[i]
84                 @datatable.add(autodatafigures[i])
85             end
86         end
88         @datatable.render if @have_data
89     end
91     ##
92     # makes new FigureData object
93     def new
94         self.index
96         if session[:figures_keep_form_open]
97             # pre-populate new object with some fields from the one last added/edited
98             last=FigureData.where(:id_stock=>@selected_security.id).last
99             @figuredata = FigureData.new(
100                 :id_stock=>@selected_security.id,
101                 :date=>last.time,
102                 :id_figure_var=>last.id_figure_var,
103                 :period=>last.period,
104                 :analyst=>last.analyst,
105                 :is_expected=>last.is_expected,
106                 :is_audited=>last.is_audited
107             )
108         else
109             @figuredata = FigureData.new(:id_stock=>@selected_security.id, :date=>smr_browse_date)
110         end
112         @figurevariables = FigureVar.where(:id_user=>[0,current_user.id])
114         render :index
115     end
117     ##
118     # edits FigureData object
119     def edit
120         self.index
122         @figuredata = FigureData.where(:id=>params[:id].to_i).first
123         @figurevariables = FigureVar.where(:id_user=>[0,current_user.id])
125         render :index
126     end
128     ##
129     # handles creates and updates
130     def create
131         session[:figures_keep_form_open] = if params[:keep_form_open] then true else false end
132         session[:scale_value] = if params[:scale_value].to_i > 1 then params[:scale_value].to_i end
134         if params[:figure_data][:id].to_i > 0 then
135             fd = FigureData.where(:id=>params[:figure_data][:id])
136                 .joins(:FigureVar).where('figure_var.id_user IN (%i,%i)' % [0,current_user.id])
137                 .first
138         else
139             fd = FigureData.new(figuredata_params)
140             fd.value *= session[:scale_value] if session[:scale_value]
141         end
143         fd.date = Time.parse(params[:figure_data][:time]).to_i
145         if params[:figure_data][:id].to_i > 0 then
146             fd.update(figuredata_params)
147         else fd.save end
149         if session[:figures_keep_form_open] then
150             redirect_to new_figure_path, :notice=>fd.errors.full_messages.uniq.join(', ')
151         else
152             redirect_to figures_path, :notice=>fd.errors.full_messages.uniq.join(', ')
153         end
154     end
156  protected
158     ##
159     # internal helper defining parameters acceptable for FigureData create/update
160     def figuredata_params
161       params.require(:figure_data).permit(
162         :id_stock, :id_figure_var, :period, :date, :analyst, :value, :is_expected, :is_audited, :comment
163       )
164     end