prawn_report 1.9.21 → 1.9.22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +7 -0
  2. data/app/controllers/ac_filter_defs_controller.rb +16 -17
  3. data/app/controllers/custom_generate_report_controller.rb +18 -18
  4. data/app/controllers/generate_report_controller.rb +24 -24
  5. data/app/controllers/report_templates_controller.rb +40 -48
  6. data/app/models/ac_filter.rb +15 -15
  7. data/app/models/ac_filter_def.rb +38 -40
  8. data/app/models/ac_filter_option.rb +5 -5
  9. data/app/models/report_template.rb +22 -27
  10. data/lib/ac_filters_utils.rb +187 -189
  11. data/lib/active_record_helpers.rb +218 -219
  12. data/lib/bands/band.rb +23 -23
  13. data/lib/bands/footer_band.rb +7 -7
  14. data/lib/bands/header_band.rb +9 -9
  15. data/lib/bands/summary_band.rb +7 -7
  16. data/lib/custom_report_controller.rb +48 -60
  17. data/lib/generators/prawn_report/install/templates/20131107172133_add_excluir_to_report_template.rb +0 -1
  18. data/lib/prawn_report.rb +21 -21
  19. data/lib/prawn_report/version.rb +3 -0
  20. data/lib/prawn_report_seeds.rb +45 -59
  21. data/lib/report.rb +189 -195
  22. data/lib/report_helpers.rb +82 -83
  23. data/lib/report_info.rb +27 -27
  24. data/repo/bands/footers/footer_001.rb +20 -20
  25. data/repo/bands/headers/header_001.rb +61 -61
  26. data/repo/bands/headers/header_002.rb +31 -31
  27. data/repo/reports/column_group.rb +4 -3
  28. data/repo/reports/listing.rb +1 -59
  29. data/repo/reports/simple_listing.rb +130 -149
  30. metadata +58 -90
  31. data/app/serializers/ac_filter_def_serializer.rb +0 -10
  32. data/app/serializers/ac_filter_option_serializer.rb +0 -7
  33. data/app/serializers/ac_filter_serializer.rb +0 -9
  34. data/app/serializers/report_template_basic_serializer.rb +0 -4
  35. data/app/serializers/report_template_serializer.rb +0 -9
  36. data/lib/generators/prawn_report/install/templates/20140529153300_add_description_to_report_template.rb +0 -6
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8ac0f4bda9d0e5f4d0d4a46aaea5ec30ba229daea9d10e2859aac8a88161d07c
4
+ data.tar.gz: 4b878ad54793ecf6c4f6fe297fb185cacd03a49460c3827af0a52a261b01eb79
5
+ SHA512:
6
+ metadata.gz: 6af39f47a21627534da02cf0749efb4bcf21fcb33c107ef98e1b2cecbf8931568a432933021b94ace7d75c2ad669b199e223ea1f86828ff9a72ec4858fe4e328
7
+ data.tar.gz: d2feea5408f041b6694524d8b52402af0cc440854fe5db1ee55fd99f426676f8103c4801ec4ba24349ee3d2908685111427baa863e20c0f67fd13eeb3e562cd2
@@ -1,17 +1,16 @@
1
- class AcFilterDefsController < ApplicationController
2
-
3
- unloadable
4
-
5
- def index
6
- @ac_filter_defs = AcFilterDef.find(:all)
7
-
8
- respond_to do |format|
9
- format.html # index.html.erb
10
- format.xml { render :xml => @ac_filter_defs }
11
- format.fxml { render :fxml => @ac_filter_defs.to_fxml(
12
- {:include => {:ac_filters => {:include => { :ac_filter_options => {}}}}} )}
13
- end
14
-
15
- end
16
-
17
- end
1
+ class AcFilterDefsController < ApplicationController
2
+
3
+ unloadable
4
+
5
+ def index
6
+ @ac_filter_defs = AcFilterDef.find(:all)
7
+ respond_to do |format|
8
+ format.html # index.html.erb
9
+ format.xml { render :xml => @ac_filter_defs }
10
+ format.fxml { render :fxml => @ac_filter_defs.to_fxml(
11
+ {:include => {:ac_filters => {:include => { :ac_filter_options => {}}}}} )}
12
+ end
13
+ end
14
+
15
+ end
16
+
@@ -1,18 +1,18 @@
1
- require "custom_report_controller"
2
-
3
- class CustomGenerateReportController < ApplicationController
4
-
5
- unloadable
6
-
7
- include PrawnReportController
8
-
9
- def get_system_criteria
10
- {}
11
- end
12
-
13
- def get_pr_suggested_filename
14
- @report_template = ReportTemplate.find(params["report_template_id"])
15
- @report_template.report_class.underscore + '_' + Date.today.to_s + '.pdf'
16
- end
17
-
18
- end
1
+ require "custom_report_controller"
2
+
3
+ class CustomGenerateReportController < ApplicationController
4
+
5
+ unloadable
6
+
7
+ include PrawnReportController
8
+
9
+ def get_system_criteria
10
+ {}
11
+ end
12
+
13
+ def get_pr_suggested_filename
14
+ @report_template = ReportTemplate.find(params["report_template_id"])
15
+ @report_template.report_class.underscore + '_' + Date.today.to_s + '.pdf'
16
+ end
17
+
18
+ end
@@ -1,24 +1,24 @@
1
- class GenerateReportController < PrawnReport.parent_controller.constantize
2
-
3
- unloadable
4
-
5
- def get_pr_report_class
6
- @report_template = ReportTemplate.find(params["report_template_id"])
7
- Kernel.const_get(@report_template.report_class)
8
- end
9
-
10
- def get_pr_report_params; end
11
-
12
- def get_pr_report_data
13
- @report_template = ReportTemplate.find(params["report_template_id"])
14
- mc=Kernel
15
- @report_template.ac_filter_def.model_class.split('::').each {|c| mc=mc.const_get(c)}
16
- mc.apply_ac_filter(parse_ac_filters(params), get_system_criteria)
17
- end
18
-
19
- def get_pr_filters
20
- @report_template = ReportTemplate.find(params["report_template_id"])
21
- get_ac_filters_applied(params, @report_template.ac_filter_def)
22
- end
23
-
24
- end
1
+ class GenerateReportController < PrawnReport.parent_controller.constantize
2
+
3
+ unloadable
4
+
5
+ def get_pr_report_class
6
+ @report_template = ReportTemplate.find(params["report_template_id"])
7
+ Kernel.const_get(@report_template.report_class)
8
+ end
9
+
10
+ def get_pr_report_params; end
11
+
12
+ def get_pr_report_data
13
+ @report_template = ReportTemplate.find(params["report_template_id"])
14
+ mc=Kernel
15
+ @report_template.ac_filter_def.model_class.split('::').each {|c| mc=mc.const_get(c)}
16
+ mc.apply_ac_filter(parse_ac_filters(params), get_system_criteria)
17
+ end
18
+
19
+ def get_pr_filters
20
+ @report_template = ReportTemplate.find(params["report_template_id"])
21
+ get_ac_filters_applied(params, @report_template.ac_filter_def)
22
+ end
23
+
24
+ end
@@ -1,48 +1,40 @@
1
- class ReportTemplatesController < ApplicationController
2
-
3
- unloadable
4
-
5
- def index
6
- conditions = []
7
- conditions = ['report_type in (?)', parse_array(params['report_type'])] unless params['report_type'].blank?
8
-
9
- @templates = ReportTemplate.find(:all,
10
- :conditions => conditions)
11
-
12
- respond_to do |format|
13
- format.html # index.html.erb
14
- format.xml { render :xml => @templates }
15
- format.fxml do
16
- render :fxml => @templates.to_fxml(
17
- { :include => {
18
- :ac_filter_def => { :include => {
19
- :ac_filters => { :include => :ac_filter_options}
20
- }}
21
- }
22
- }
23
- )
24
- end
25
- format.json { render :json => @templates }
26
- end
27
-
28
- end
29
-
30
- def show
31
- @report_template = ReportTemplate.find(params[:id])
32
-
33
- respond_to do |format|
34
- format.json { render :json => @report_template, :root => 'reports' }
35
- end
36
- end
37
-
38
- private
39
-
40
- def parse_array(s_array)
41
- retorno = []
42
- s_array.split(',').each{|s|
43
- retorno << s
44
- }
45
- retorno
46
- end
47
-
48
- end
1
+ class ReportTemplatesController < ApplicationController
2
+
3
+ unloadable
4
+
5
+ def index
6
+ conditions = []
7
+ conditions = ['report_type in (?)', parse_array(params['report_type'])] unless params['report_type'].blank?
8
+
9
+ @templates = ReportTemplate.find(:all,
10
+ :conditions => conditions)
11
+
12
+ respond_to do |format|
13
+ format.html # index.html.erb
14
+ format.xml { render :xml => @templates }
15
+ format.fxml do
16
+ render :fxml => @templates.to_fxml(
17
+ { :include => {
18
+ :ac_filter_def => { :include => {
19
+ :ac_filters => { :include => :ac_filter_options}
20
+ }}
21
+ }
22
+ }
23
+ )
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+
30
+ private
31
+
32
+ def parse_array(s_array)
33
+ retorno = []
34
+ s_array.split(',').each{|s|
35
+ retorno << s
36
+ }
37
+ retorno
38
+ end
39
+
40
+ end
@@ -1,15 +1,15 @@
1
- class AcFilter < ActiveRecord::Base
2
- belongs_to :ac_filter_def
3
-
4
- has_many :ac_filter_options, :dependent => :destroy
5
- accepts_nested_attributes_for :ac_filter_options
6
-
7
- def has_filled_criteria?
8
- !self.filled_criteria.nil?
9
- end
10
-
11
- def has_unfilled_criteria?
12
- !self.unfilled_criteria.nil?
13
- end
14
-
15
- end
1
+ class AcFilter < ActiveRecord::Base
2
+ belongs_to :ac_filter_def
3
+
4
+ has_many :ac_filter_options, :dependent => :destroy
5
+ accepts_nested_attributes_for :ac_filter_options
6
+
7
+ def has_filled_criteria?
8
+ !self.filled_criteria.nil?
9
+ end
10
+
11
+ def has_unfilled_criteria?
12
+ !self.unfilled_criteria.nil?
13
+ end
14
+
15
+ end
@@ -1,40 +1,38 @@
1
- class AcFilterDef < ActiveRecord::Base
2
-
3
- has_many :ac_filters, :dependent => :destroy
4
- accepts_nested_attributes_for :ac_filters
5
-
6
- has_many :report_templates
7
-
8
- def joins_param=(value)
9
- write_marshal_attribute(:joins_param, value)
10
- end
11
-
12
- def joins_param
13
- read_marshal_attribute(:joins_param)
14
- end
15
-
16
- def include_param=(value)
17
- write_marshal_attribute(:include_param, value)
18
- end
19
-
20
- def include_param
21
- read_marshal_attribute(:include_param)
22
- end
23
-
24
- def has_sql_query?
25
- sql_query.to_s != ''
26
- end
27
-
28
- protected
29
-
30
- def write_marshal_attribute(name, value)
31
- write_attribute name, Marshal.dump(value)
32
- end
33
-
34
- def read_marshal_attribute(name)
35
- value = read_attribute name
36
- value.nil? ? nil : Marshal.load(value)
37
- end
38
-
39
-
40
- end
1
+ class AcFilterDef < ActiveRecord::Base
2
+
3
+ has_many :ac_filters, :dependent => :destroy
4
+ accepts_nested_attributes_for :ac_filters
5
+
6
+ def joins_param=(value)
7
+ write_marshal_attribute(:joins_param, value)
8
+ end
9
+
10
+ def joins_param
11
+ read_marshal_attribute(:joins_param)
12
+ end
13
+
14
+ def include_param=(value)
15
+ write_marshal_attribute(:include_param, value)
16
+ end
17
+
18
+ def include_param
19
+ read_marshal_attribute(:include_param)
20
+ end
21
+
22
+ def has_sql_query?
23
+ sql_query.to_s != ''
24
+ end
25
+
26
+ protected
27
+
28
+ def write_marshal_attribute(name, value)
29
+ write_attribute name, Marshal.dump(value)
30
+ end
31
+
32
+ def read_marshal_attribute(name)
33
+ value = read_attribute name
34
+ value.nil? ? nil : Marshal.load(value)
35
+ end
36
+
37
+
38
+ end
@@ -1,5 +1,5 @@
1
- class AcFilterOption < ActiveRecord::Base
2
-
3
- belongs_to :ac_filter_def
4
-
5
- end
1
+ class AcFilterOption < ActiveRecord::Base
2
+
3
+ belongs_to :ac_filter_def
4
+
5
+ end
@@ -1,27 +1,22 @@
1
- require 'yaml'
2
-
3
- class ReportTemplate < ActiveRecord::Base
4
- belongs_to :ac_filter_def
5
-
6
- has_many :filters, :through => :ac_filter_def, :source => :ac_filters
7
-
8
-
9
- def serialization_params=(value)
10
- if value.is_a? Hash
11
- value = Marshal.dump(value)
12
- end
13
- write_attribute :serialization_params, value
14
- end
15
-
16
- def serialization_params
17
- value = read_attribute :serialization_params
18
- #value.nil? ? {} : Marshal.load(value)
19
- begin
20
- value.nil? ? {} : Marshal.load(value)
21
- #value.nil? ? {} : YAML.load(value)
22
- rescue TypeError
23
-
24
- end
25
- end
26
-
27
- end
1
+ require 'yaml'
2
+
3
+ class ReportTemplate < ActiveRecord::Base
4
+ belongs_to :ac_filter_def
5
+
6
+ def serialization_params=(value)
7
+ if value.is_a? Hash
8
+ value = Marshal.dump(value)
9
+ end
10
+ write_attribute :serialization_params, value
11
+ end
12
+
13
+ def serialization_params
14
+ value = read_attribute :serialization_params
15
+ begin
16
+ value.nil? ? {} : Marshal.load(value)
17
+ rescue TypeError
18
+
19
+ end
20
+ end
21
+
22
+ end
@@ -1,189 +1,187 @@
1
- # encoding: utf-8
2
-
3
- def parse_ac_filters(params)
4
- parsed_filters = {}
5
- parsed_filters['filter_def_id'] = params['filter_def_id'].to_i
6
- params.each_pair do |k,v|
7
- md = /^ac_filter_(\d+)_(.+)/.match(k)
8
- if md
9
- filter_id = md[1]
10
- filter_result_name = md[2].underscore
11
- filter_value = v
12
- parsed_filters[filter_id.to_i] = {} unless parsed_filters[filter_id.to_i]
13
- parsed_filters[filter_id.to_i][filter_result_name] = filter_value
14
- end
15
- end
16
- parsed_filters
17
- end
18
-
19
- def get_ac_filters_applied(params, ac_filter_def)
20
- parsed_filter = parse_ac_filters(params)
21
- r = []
22
- ac_filter_def.ac_filters.each do |f|
23
- if f.query_user? && (parsed_filter[f.id]['is_filled'] == 'true')
24
- pf = parsed_filter[f.id]
25
- val = ''
26
- if ['text', 'options'].include? f.data_type
27
- val = pf['filter_value']
28
- elsif ['period', 'timezone_period'].include?(f.data_type)
29
- if (!pf['from_date'].empty?) && (!pf['to_date'].empty?)
30
- val = "#{Date.parse(pf['from_date']).strftime('%d/%m/%Y')} até #{Date.parse(pf['to_date']).strftime('%d/%m/%Y')}"
31
- elsif !pf['from_date'].empty?
32
- val = "Desde #{Date.parse(pf['from_date']).strftime('%d/%m/%Y')}"
33
- elsif
34
- val = "Até #{Date.parse(pf['to_date']).strftime('%d/%m/%Y')}"
35
- end
36
- elsif ['autocomplete', 'storecombo'].include?(f.data_type)
37
- val = Kernel.const_get(f.target_model).find(pf['filter_value']).send(f.target_field)
38
- end
39
- r << [f.label, val]
40
- end
41
- end
42
- r
43
- end
44
-
45
- def is_filled?(parsed_params, filter)
46
- !parsed_params[filter.id].nil?
47
- end
48
-
49
- def fill_params(f, fillings)
50
- if ['period', 'timezone_period'].include?(f.data_type)
51
- [fillings['from_date'], fillings['to_date']]
52
- elsif f.data_type == 'text'
53
- '%' + fillings['filter_value'] + '%'
54
- elsif f.data_type == 'options'
55
- fillings['filter_value']
56
- else
57
- fillings['filter_value']
58
- end
59
- end
60
-
61
- def parse_conditions(parsed_filter, system_params)
62
- system_params.symbolize_keys!
63
- filter_def = AcFilterDef.find(parsed_filter['filter_def_id'])
64
- conditions = []
65
- conditions[0] = ['1=1']
66
-
67
- filter_def.ac_filters.each do |f|
68
- if f.query_user?
69
- parse_condition(conditions, parsed_filter, f)
70
- elsif f.system_criteria.to_s != ''
71
- conditions[0] << f.filled_criteria
72
- conditions << system_params[f.system_criteria.to_sym]
73
- else
74
- conditions[0] << f.filled_criteria
75
- end
76
- end
77
-
78
- conditions[0] = conditions[0].join(' and ')
79
- conditions
80
- end
81
-
82
- def parse_condition(conditions, parsed_filter, filter)
83
- filled = parsed_filter[filter.id]['is_filled'] == 'true'
84
-
85
- if filter.data_type == 'checkbox'
86
- fill_with_checkbox(conditions, filled, parsed_filter, filter)
87
- elsif filter.data_type == 'options'
88
- fill_with_options(conditions, filled, parsed_filter, filter)
89
- elsif filter.data_type == 'period'
90
- fill_with_period(conditions, filled, parsed_filter, filter)
91
- elsif filter.data_type == 'timezone_period'
92
- fill_with_timezone_period(conditions, filled, parsed_filter, filter)
93
- else
94
- fill_with_others(conditions, filled, parsed_filter, filter)
95
- end
96
- end
97
-
98
- def fill_with_checkbox(conditions, filled, parsed_filter, filter)
99
- if filled && filter.has_filled_criteria?
100
- conditions[0] << filter.filled_criteria
101
- elsif filter.has_unfilled_criteria?
102
- conditions[0] << filter.unfilled_criteria
103
- end
104
- end
105
-
106
- def fill_with_options(conditions, filled, parsed_filter, filter)
107
- if !filled && filter.has_unfilled_criteria?
108
- conditions[0] << c.unfilled_criteria
109
- elsif filled
110
- fo = AcFilterOption.find_by_ac_filter_id_and_value(filter.id, parsed_filter[filter.id]['filter_value'])
111
- if fo && fo.filled_criteria.to_s != ''
112
- conditions[0] << fo.filled_criteria
113
- conditions << fill_params(filter, parsed_filter[filter.id])
114
- elsif filter.has_filled_criteria?
115
- conditions[0] << filter.filled_criteria
116
- conditions << fill_params(filter, parsed_filter[filter.id])
117
- end
118
- end
119
- end
120
-
121
- def fill_with_period(conditions, filled, parsed_filter, filter)
122
- if !filled && filter.has_unfilled_criteria?
123
- conditions[0] << c.unfilled_criteria
124
- elsif filled
125
- fp = fill_params(filter, parsed_filter[filter.id])
126
- if fp[0].to_s != ''
127
- conditions[0] << filter.filled_criteria_from
128
- conditions << fp[0]
129
- end
130
- if fp[1].to_s != ''
131
- conditions[0] << filter.filled_criteria_to
132
- conditions << fp[1]
133
- end
134
- end
135
- end
136
-
137
- def fill_with_timezone_period(conditions, filled, parsed_filter, filter)
138
- if !filled && filter.has_unfilled_criteria?
139
- conditions[0] << c.unfilled_criteria
140
- elsif filled
141
- fp = fill_params(filter, parsed_filter[filter.id])
142
- if fp[0].to_s != ''
143
- conditions[0] << filter.filled_criteria_from
144
- conditions << Time.zone.parse(fp[0])
145
- end
146
- if fp[1].to_s != ''
147
- conditions[0] << filter.filled_criteria_to
148
- conditions << Time.zone.parse(fp[1])+1.day
149
- end
150
- end
151
- end
152
-
153
- def fill_with_others(conditions, filled, parsed_filter, filter)
154
- if filled && filter.has_filled_criteria?
155
- fp = fill_params(filter, parsed_filter[filter.id])
156
- conditions[0] << filter.filled_criteria
157
- conditions << fp
158
- elsif filter.has_unfilled_criteria?
159
- conditions[0] << filter.unfilled_criteria
160
- end
161
- end
162
-
163
- def get_param_by_label(params, label)
164
- filter = AcFilter.find(:first, :conditions => ['ac_filter_def_id = ? and label = ?',params['filter_def_id'], label])
165
- 'ac_filter_'+filter.id.to_s
166
- end
167
-
168
- module AcFilters
169
- def apply_ac_filter(parsed_filter, system_params)
170
- conditions = parse_conditions(parsed_filter, system_params)
171
- find_params = {:conditions => conditions}
172
- filter_def = AcFilterDef.find(parsed_filter['filter_def_id'])
173
- if filter_def.has_sql_query?
174
- sql_to_execute = ActiveRecord::Base.send(:sanitize_sql_array, conditions)
175
- r = []
176
- #ActiveRecord::Base.connection.instance_exec(sql_to_execute).each(:as => :hash) {|i| r << i}
177
- # Ticket_id: 52283 - Seed não exclui mais relatórios existentes
178
- ActiveRecord::Base.connection.instance_exec(sql_to_execute).each(as :hash) {|i| r << i}
179
- else
180
- find_params[:select] = filter_def.select_sql.to_s unless filter_def.select_sql.nil?
181
- find_params[:order] = filter_def.order_sql.to_s unless filter_def.order_sql.nil?
182
- find_params[:joins] = filter_def.joins_param unless filter_def.joins_param.nil?
183
- find_params[:include] = filter_def.include_param unless filter_def.include_param.nil?
184
- find_params[:group] = filter_def.group_param unless filter_def.group_param.nil?
185
- r = find(:all, find_params)
186
- end
187
- r
188
- end
189
- end
1
+ # encoding: utf-8
2
+
3
+ def parse_ac_filters(params)
4
+ parsed_filters = {}
5
+ parsed_filters['filter_def_id'] = params['filter_def_id'].to_i
6
+ params.each_pair do |k,v|
7
+ md = /^ac_filter_(\d+)_(.+)/.match(k)
8
+ if md
9
+ filter_id = md[1]
10
+ filter_result_name = md[2].underscore
11
+ filter_value = v
12
+ parsed_filters[filter_id.to_i] = {} unless parsed_filters[filter_id.to_i]
13
+ parsed_filters[filter_id.to_i][filter_result_name] = filter_value
14
+ end
15
+ end
16
+ parsed_filters
17
+ end
18
+
19
+ def get_ac_filters_applied(params, ac_filter_def)
20
+ parsed_filter = parse_ac_filters(params)
21
+ r = []
22
+ ac_filter_def.ac_filters.each do |f|
23
+ if f.query_user? && (parsed_filter[f.id]['is_filled'] == 'true')
24
+ pf = parsed_filter[f.id]
25
+ val = ''
26
+ if ['text', 'options'].include? f.data_type
27
+ val = pf['filter_value']
28
+ elsif ['period', 'timezone_period'].include?(f.data_type)
29
+ if (!pf['from_date'].empty?) && (!pf['to_date'].empty?)
30
+ val = "#{Date.parse(pf['from_date']).strftime('%d/%m/%Y')} até #{Date.parse(pf['to_date']).strftime('%d/%m/%Y')}"
31
+ elsif !pf['from_date'].empty?
32
+ val = "Desde #{Date.parse(pf['from_date']).strftime('%d/%m/%Y')}"
33
+ elsif
34
+ val = "Até #{Date.parse(pf['to_date']).strftime('%d/%m/%Y')}"
35
+ end
36
+ elsif f.data_type == 'autocomplete'
37
+ val = Kernel.const_get(f.target_model).find(pf['filter_value']).send(f.target_field)
38
+ end
39
+ r << [f.label, val]
40
+ end
41
+ end
42
+ r
43
+ end
44
+
45
+ def is_filled?(parsed_params, filter)
46
+ !parsed_params[filter.id].nil?
47
+ end
48
+
49
+ def fill_params(f, fillings)
50
+ if ['period', 'timezone_period'].include?(f.data_type)
51
+ [fillings['from_date'], fillings['to_date']]
52
+ elsif f.data_type == 'text'
53
+ '%' + fillings['filter_value'] + '%'
54
+ elsif f.data_type == 'options'
55
+ fillings['filter_value']
56
+ else
57
+ fillings['filter_value']
58
+ end
59
+ end
60
+
61
+ def parse_conditions(parsed_filter, system_params)
62
+ system_params.symbolize_keys!
63
+ filter_def = AcFilterDef.find(parsed_filter['filter_def_id'])
64
+ conditions = []
65
+ conditions[0] = ['1=1']
66
+
67
+ filter_def.ac_filters.each do |f|
68
+ if f.query_user?
69
+ parse_condition(conditions, parsed_filter, f)
70
+ elsif f.system_criteria.to_s != ''
71
+ conditions[0] << f.filled_criteria
72
+ conditions << system_params[f.system_criteria.to_sym]
73
+ else
74
+ conditions[0] << f.filled_criteria
75
+ end
76
+ end
77
+
78
+ conditions[0] = conditions[0].join(' and ')
79
+ conditions
80
+ end
81
+
82
+ def parse_condition(conditions, parsed_filter, filter)
83
+ filled = parsed_filter[filter.id]['is_filled'] == 'true'
84
+
85
+ if filter.data_type == 'checkbox'
86
+ fill_with_checkbox(conditions, filled, parsed_filter, filter)
87
+ elsif filter.data_type == 'options'
88
+ fill_with_options(conditions, filled, parsed_filter, filter)
89
+ elsif filter.data_type == 'period'
90
+ fill_with_period(conditions, filled, parsed_filter, filter)
91
+ elsif filter.data_type == 'timezone_period'
92
+ fill_with_timezone_period(conditions, filled, parsed_filter, filter)
93
+ else
94
+ fill_with_others(conditions, filled, parsed_filter, filter)
95
+ end
96
+ end
97
+
98
+ def fill_with_checkbox(conditions, filled, parsed_filter, filter)
99
+ if filled && filter.has_filled_criteria?
100
+ conditions[0] << filter.filled_criteria
101
+ elsif filter.has_unfilled_criteria?
102
+ conditions[0] << filter.unfilled_criteria
103
+ end
104
+ end
105
+
106
+ def fill_with_options(conditions, filled, parsed_filter, filter)
107
+ if !filled && filter.has_unfilled_criteria?
108
+ conditions[0] << c.unfilled_criteria
109
+ elsif filled
110
+ fo = AcFilterOption.find_by_ac_filter_id_and_value(filter.id, parsed_filter[filter.id]['filter_value'])
111
+ if fo && fo.filled_criteria.to_s != ''
112
+ conditions[0] << fo.filled_criteria
113
+ conditions << fill_params(filter, parsed_filter[filter.id])
114
+ elsif filter.has_filled_criteria?
115
+ conditions[0] << filter.filled_criteria
116
+ conditions << fill_params(filter, parsed_filter[filter.id])
117
+ end
118
+ end
119
+ end
120
+
121
+ def fill_with_period(conditions, filled, parsed_filter, filter)
122
+ if !filled && filter.has_unfilled_criteria?
123
+ conditions[0] << c.unfilled_criteria
124
+ elsif filled
125
+ fp = fill_params(filter, parsed_filter[filter.id])
126
+ if fp[0].to_s != ''
127
+ conditions[0] << filter.filled_criteria_from
128
+ conditions << fp[0]
129
+ end
130
+ if fp[1].to_s != ''
131
+ conditions[0] << filter.filled_criteria_to
132
+ conditions << fp[1]
133
+ end
134
+ end
135
+ end
136
+
137
+ def fill_with_timezone_period(conditions, filled, parsed_filter, filter)
138
+ if !filled && filter.has_unfilled_criteria?
139
+ conditions[0] << c.unfilled_criteria
140
+ elsif filled
141
+ fp = fill_params(filter, parsed_filter[filter.id])
142
+ if fp[0].to_s != ''
143
+ conditions[0] << filter.filled_criteria_from
144
+ conditions << Time.zone.parse(fp[0])
145
+ end
146
+ if fp[1].to_s != ''
147
+ conditions[0] << filter.filled_criteria_to
148
+ conditions << Time.zone.parse(fp[1])+1.day
149
+ end
150
+ end
151
+ end
152
+
153
+ def fill_with_others(conditions, filled, parsed_filter, filter)
154
+ if filled && filter.has_filled_criteria?
155
+ fp = fill_params(filter, parsed_filter[filter.id])
156
+ conditions[0] << filter.filled_criteria
157
+ conditions << fp
158
+ elsif filter.has_unfilled_criteria?
159
+ conditions[0] << filter.unfilled_criteria
160
+ end
161
+ end
162
+
163
+ def get_param_by_label(params, label)
164
+ filter = AcFilter.find(:first, :conditions => ['ac_filter_def_id = ? and label = ?',params['filter_def_id'], label])
165
+ 'ac_filter_'+filter.id.to_s
166
+ end
167
+
168
+ module AcFilters
169
+ def apply_ac_filter(parsed_filter, system_params)
170
+ conditions = parse_conditions(parsed_filter, system_params)
171
+ find_params = {:conditions => conditions}
172
+ filter_def = AcFilterDef.find(parsed_filter['filter_def_id'])
173
+ if filter_def.has_sql_query?
174
+ sql_to_execute = ActiveRecord::Base.send(:sanitize_sql_array, conditions)
175
+ r = []
176
+ ActiveRecord::Base.connection.instance_exec(sql_to_execute).each(as :hash) {|i| r << i}
177
+ else
178
+ find_params[:select] = filter_def.select_sql.to_s unless filter_def.select_sql.nil?
179
+ find_params[:order] = filter_def.order_sql.to_s unless filter_def.order_sql.nil?
180
+ find_params[:joins] = filter_def.joins_param unless filter_def.joins_param.nil?
181
+ find_params[:include] = filter_def.include_param unless filter_def.include_param.nil?
182
+ find_params[:group] = filter_def.group_param unless filter_def.group_param.nil?
183
+ r = find(:all, find_params)
184
+ end
185
+ r
186
+ end
187
+ end