Module: Crm::Reports::GrossSalesReportHelper

Defined in:
app/helpers/crm/reports/gross_sales_report_helper.rb

Instance Method Summary collapse

Instance Method Details

#acc_format(value) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 129

def acc_format(value)
  return unless value

  if value.positive?
    value.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
  elsif value.negative?
    '(' + (value * -1).to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse + ')'
  end
end


301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 301

def acc_ov_customers_link_to(num_customers, program_pricing_id, sales_rep_ids, style)
  pp_ids = [1316, 2422, 2423, 2424, 2425, 582, 583]
  catalogs = Catalog.where("id in (1,2)").pluck(:id).sort.uniq
  options = {}
  options[:num_records] = num_customers
  options[:style] = style
  options[:query_params] = {}
  options[:query_params][:catalog_id_in] = catalogs if program_pricing_id != 999998
  options[:query_params][:catalog_id_not_in] = catalogs if program_pricing_id == 999998
  options[:query_params][:tier2_program_pricing_id_in] = program_pricing_id if program_pricing_id <= 999997
  options[:query_params][:tier2_program_pricing_id_not_in] = pp_ids if program_pricing_id == 999999
  options[:query_params][:primary_sales_rep_id_includes] = sales_rep_ids if sales_rep_ids.any?
  link, counter = query_link(CustomerSearch, nil, options)
  link.html_safe
end


226
227
228
229
230
231
232
233
234
235
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 226

def accounting_record_link(year, month, company_id, )
  params = {}
  params[:command] = {}
  params[:command][:period1_gteq] = Date.new(year, month)
  params[:command][:period1_lteq] = Date.new(year, month).end_of_month
  params[:command][:company_ids] = [company_id]
  params[:command][:ledger_account_ids] = []
  params[:command][:from_report] =  <= 4999 ? 1 : 2
  reports_accounting_records_path(params)
end


317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 317

def bo_num_offices_customers_link_to(num_customers, program_pricing_id, number_of_offices, sales_rep_ids, style)
  pp_ids = [1316, 2422, 2423, 2424, 2425, 582, 583]
  catalogs = Catalog.where("id in (1,2)").pluck(:id).sort.uniq
  options = {}
  options[:num_records] = num_customers
  options[:style] = style
  options[:query_params] = {}
  options[:query_params][:catalog_id_in] = catalogs if program_pricing_id != 999998
  options[:query_params][:catalog_id_not_in] = catalogs if program_pricing_id == 999998
  options[:query_params][:tier2_program_pricing_id_in] = program_pricing_id if program_pricing_id <= 999997
  options[:query_params][:tier2_program_pricing_id_not_in] = pp_ids if program_pricing_id == 999999
  options[:query_params][:number_of_offices] = number_of_offices
  options[:query_params][:primary_sales_rep_id_includes] = sales_rep_ids if sales_rep_ids.any?
  link, counter = query_link(CustomerSearch, nil, options)
  link.html_safe
end


334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 334

def bo_projects_customers_link_to(num_customers, program_pricing_id, projects, sales_rep_ids, style)
  pp_ids = [1316, 2422, 2423, 2424, 2425, 582, 583]
  catalogs = Catalog.where("id in (1,2)").pluck(:id).sort.uniq
  options = {}
  options[:num_records] = num_customers
  options[:style] = style
  options[:query_params] = {}
  options[:query_params][:catalog_id_in] = catalogs if program_pricing_id != 999998
  options[:query_params][:catalog_id_not_in] = catalogs if program_pricing_id == 999998
  options[:query_params][:tier2_program_pricing_id_in] = program_pricing_id if program_pricing_id <= 999997
  options[:query_params][:tier2_program_pricing_id_not_in] = pp_ids if program_pricing_id == 999999
  options[:query_params][:projects_per_year] = projects
  options[:query_params][:primary_sales_rep_id_includes] = sales_rep_ids if sales_rep_ids.any?
  link, counter = query_link(CustomerSearch, nil, options)
  link.html_safe
end


237
238
239
240
241
242
243
244
245
246
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 237

def business_units_link(year, month, company_id, , bu_id)
  params = {}
  params[:command] = {}
  params[:command][:period1_gteq] = Date.new(year, month)
  params[:command][:period1_lteq] = Date.new(year, month).end_of_month
  params[:command][:company_ids] = [company_id]
  params[:command][:ledger_account_ids] = []
  params[:command][:business_units_ids] = [bu_id]
  reports_accounting_records_path(params)
end


214
215
216
217
218
219
220
221
222
223
224
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 214

def closed_activities_link_to(employee_id, ca_count, date, options = {})
  options[:num_records] = ca_count
  options[:query_params] = {}
  options[:query_params][:completion_datetime_gteq_time] = date.beginning_of_day
  options[:query_params][:completion_datetime_lteq_time] = date.end_of_day
  options[:query_params][:closed_by_id_in] = [employee_id]
  options[:query_params][:activity_type_id] = ['Any']
  options[:query_params][:activity_result_type_id] = ['AnyCompletion']
  link, counter = query_template_link(ActivitySearch, nil, options)
  link.html_safe
end

#commission_state(status) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 151

def commission_state(status)
  if status == 'in_progress'
    (:span, 'IP', class: 'badge bg-success')
  elsif status == 'pending_payout'
    (:span, 'PP', class: 'badge bg-warning')
  elsif status == 'paid_out'
    (:span, 'PO', class: 'badge bg-danger')
  end
end

#comparison_arrow(p1_value, p2_value) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 31

def comparison_arrow(p1_value, p2_value)
  if p1_value > p2_value
    (:i, '', class: 'fa-solid fa-arrow-trend-up text-success')
  elsif p1_value < p2_value
    (:i, '', class: 'fa-solid fa-arrow-trend-down text-danger')
  else
    (:span, '-', class: 'text-muted')
  end
end

#counter_span_current_month(number) ⇒ Object



143
144
145
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 143

def counter_span_current_month(number)
  (:span, number, class: 'badge bg-danger') if number.positive?
end

#counter_span_sr(number) ⇒ Object



139
140
141
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 139

def counter_span_sr(number)
  (:span, number, class: 'badge bg-secondary') if number.positive?
end

#counter_span_sr_yearly(number) ⇒ Object



147
148
149
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 147

def counter_span_sr_yearly(number)
  (:span, number, class: 'badge bg-success')
end


291
292
293
294
295
296
297
298
299
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 291

def customers_link_to(customers_ids, num_customers, style)
  options = {}
  options[:num_records] = num_customers
  options[:style] = style
  options[:query_params] = {}
  options[:query_params][:id_in] = customers_ids
  link, counter = query_link(CustomerSearch, nil, options)
  link.html_safe
end

#growth_indicator(var_value) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 21

def growth_indicator(var_value)
  return unless var_value

  if var_value.positive?
    (:i, '', class: 'fa fa-arrow-up text-success')
  elsif var_value.negative?
    (:i, '', class: 'fa fa-arrow-down text-danger')
  end
end

#hash_to_form_params(hash) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 258

def hash_to_form_params(hash)
  cleaned_hash = hash.reject { |k, v| v.blank? }
  pairs        = cleaned_hash.to_query.split(Rack::Utils::DEFAULT_SEP)
  flattened_hash_params = {} # This allows duplicate keys which is what we want for array input tags
  flattened_hash_params.compare_by_identity
  tags = pairs.map do |pair|
    key, value = pair.split('=', 2).map { |str| Rack::Utils.unescape(str) }
    next unless value.present?
    flattened_hash_params[key] = value
  end
  flattened_hash_params
end

#inbound_outbound_blank_boolean_collection_for_select(blank_name = 'All Directions') ⇒ Object



175
176
177
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 175

def inbound_outbound_blank_boolean_collection_for_select(blank_name = 'All Directions')
  inbound_outbound_boolean_collection_for_select(blank: blank_name)
end

#inbound_outbound_boolean_collection_for_select(blank: nil) ⇒ Object



169
170
171
172
173
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 169

def inbound_outbound_boolean_collection_for_select(blank: nil)
  r = [['Inbound', true], ['Outbound', false]]
  r.insert(0, [blank, '']) if blank
  r
end


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 179

def invoiced_link_to(employee, company_id = [1], amount = nil,
                     start_date = Date.current.beginning_of_month,
                     end_date = Date.current.end_of_month,
                     options = {})

  options.reverse_merge!(show_zero: true)

  if amount.nil?
    options[:aggregate_method] = :sum
    options[:aggregate_column] = :revenue_consolidated
  else
    options[:num_records] = amount
  end
  options[:format] = :currency
  # options[:link_method] = :link
  options[:query_params] = {}
  options[:query_params][:company_id_in] = company_id
  options[:query_params][:gl_date_gteq] = start_date
  options[:query_params][:gl_date_lteq] = end_date
  options[:query_params][:invoice_type_in] = [Invoice::SO] # if employee
  options[:query_params][:report_grouping] = Customer::ReportGrouping.direct_sales_report_grouping
  options[:query_params][:primary_sales_rep_id_includes] = employee if employee
  query_template_link(InvoiceSearch, nil, options)[0]
end


204
205
206
207
208
209
210
211
212
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 204

def open_activities_link_to(employee_id, oa_count, date, options = {})
  options[:num_records] = oa_count
  options[:query_params] = {}
  options[:query_params][:open_on_date] = date
  options[:query_params][:assigned_resource_id] = [employee_id]
  options[:query_params][:activity_type_id] = ['Any']
  link, counter = query_template_link(ActivitySearch, nil, options)
  link.html_safe
end

#opposite_growth(prev_value, cur_value) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 67

def opposite_growth(prev_value, cur_value)
  return unless cur_value

  if cur_value > prev_value
    (:i, '', class: 'fa fa-arrow-up', style: 'color:darkred')
  elsif cur_value < prev_value
    (:i, '', class: 'fa fa-arrow-down', style: 'color:darkgreen')
  else
    (:i, '-', class: 'fa', style: 'color:darkyellow')
  end
end


271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 271

def query_link(search_klass, name_key = nil, options = {})
  st = 'font-weight: bold; font-size: 12px;' if options[:style] == 0
  st = 'font-size: 12px;' if options[:style] == 1
  st = 'font-size: 16px;' if options[:style] == 2
  qt = search_klass.instantiate_query_template(name_key, options)
  link_names = []
  num_records = options[:num_records].to_f.round
  link_names << (:span, values_no_symbol(num_records))

  [
    button_to(
      searches_path,
      method: :post,
      params: hash_to_form_params(qt.to_params),
      class: 'btn-link btn p-0 m-0',
      style: st) { link_names.compact.join.html_safe },
      num_records
  ]
end


248
249
250
251
252
253
254
255
256
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 248

def reconciliation_link(year, month, company_id, )
  params = {}
  params[:command] = {}
  params[:command][:years] = year
  params[:command][:months] = month
  params[:command][:company_ids] = [company_id]
  params[:command][:ledger_account_ids] = []
  reports_reconciliation_path(params)
end

#sc_state(status) ⇒ Object



161
162
163
164
165
166
167
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 161

def sc_state(status)
  if status == 'paid_out'
    (:span, status.titleize.downcase, class: 'badge bg-danger')
  else
    (:span, status.titleize.downcase, class: 'badge bg-warning')
  end
end

#value_ptg(var_value) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 79

def value_ptg(var_value)
  return unless var_value

  if var_value.positive? || var_value.negative?
    (:i, '') + " #{var_value} %"
  else
    (:i, '-')
  end
end

#values_format(value, currency = "USD") ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 89

def values_format(value, currency = "USD")
  return unless value

  if value.present? && (value.positive? || value.negative?)
    Money.from_amount(value, currency).format
  else
    (:span, '-')
  end
end

#values_format_with_not_decimals(value, currency = "USD") ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 99

def values_format_with_not_decimals(value, currency = "USD")
  return unless value

  if value.present? && (value.positive? || value.negative?)
    Money.from_amount(value, currency).format.slice(0...-3)
  else
    (:span, '-')
  end
end

#values_format_without_symbol(value) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 109

def values_format_without_symbol(value)
  return unless value

  if value.positive? || value.negative?
    value.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
  else
    (:span, '-')
  end
end

#values_no_symbol(value) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 119

def values_no_symbol(value)
  return unless value

  if value.positive? || value.negative?
    value.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
  else
    (:span, '-')
  end
end

#var_percentage(var_value) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 3

def var_percentage(var_value)
  return (:span, '-', class: 'text-muted') unless var_value

  if var_value.positive?
    (:span, class: 'text-success') do
      (:i, '', class: 'fa-solid fa-arrow-up me-1') +
        (:span, "#{var_value}%")
    end
  elsif var_value.negative?
    (:span, class: 'text-danger') do
      (:i, '', class: 'fa-solid fa-arrow-down me-1') +
        (:span, "#{var_value}%")
    end
  else
    (:span, '-', class: 'text-muted')
  end
end

#var_percentage_opposite(var_value) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 55

def var_percentage_opposite(var_value)
  return unless var_value

  if var_value.positive?
    (:i, '', class: 'fa fa-arrow-up', style: 'color:darkred') + " #{var_value} %"
  elsif var_value.negative?
    (:i, '', class: 'fa fa-arrow-down', style: 'color:darkgreen') + " #{var_value} %"
  else
    (:i, '-')
  end
end

#variance_color_class(value) ⇒ Object

Returns a Bootstrap text color class based on the sign of a variance value.
Positive values get text-success, negative get text-danger, zero gets nothing.



43
44
45
46
47
48
49
50
51
52
53
# File 'app/helpers/crm/reports/gross_sales_report_helper.rb', line 43

def variance_color_class(value)
  return '' unless value

  if value.positive?
    'text-success'
  elsif value.negative?
    'text-danger'
  else
    ''
  end
end