Class: DashboardsController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper
Defined in:
app/controllers/dashboards_controller.rb

Instance Method Summary collapse

Methods included from AuthentificationHelper

#accessible_company_ids_from_tenant_and_user, #application_mode_from_hostname, #current_company_id_from_accessible_company_ids, #current_tenant_from_user, #current_user_and_locale_from_token

Instance Method Details

#ediObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/dashboards_controller.rb', line 17

def edi
  @metrics = ActiveRecord::Base.execute_procedure('pr_get_dashboard_edi_section_1',
                                                  Time.now.strftime('%Y-%m-%d'), current_company_id)[0]

  @transaction_types = extract_transaction_types(@metrics)

  # Dynamically fetch partner data for each transaction type
  @partners_data = {}
  @transaction_types.each do |transaction_type|
    @partners_data[transaction_type] = to_pie_chart_data(
      ActiveRecord::Base.execute_procedure('pr_get_dashboard_edi_section_3', transaction_type, current_company_id)
    )
  end

  transfers_by_month = ActiveRecord::Base.execute_procedure('pr_get_dashboard_edi_section_2', 'M', current_company_id)
  @total_transfers_by_month_chart_data = to_line_chart_data(filter_transfers(transfers_by_month, 'INOUT'))

  @incoming_transfers_by_month_chart_data = to_line_chart_data(filter_transfers(transfers_by_month, 'Incoming'))
  @outgoing_transfers_by_month_chart_data = to_line_chart_data(filter_transfers(transfers_by_month, 'Outgoing'))

  transfers_by_day = ActiveRecord::Base.execute_procedure('pr_get_dashboard_edi_section_2', 'D', current_company_id)
  @total_transfers_by_day_chart_data = to_line_chart_data(filter_transfers(transfers_by_day, 'INOUT'))

  @incoming_transfers_by_day_chart_data = to_line_chart_data(filter_transfers(transfers_by_day, 'Incoming'))
  @outgoing_transfers_by_day_chart_data = to_line_chart_data(filter_transfers(transfers_by_day, 'Outgoing'))

  # Keep the existing calls for now
  @orders_by_partner = to_pie_chart_data(ActiveRecord::Base.execute_procedure('pr_get_dashboard_edi_section_3',
                                                                              '204', current_company_id))
  @invoices_by_partner = to_pie_chart_data(ActiveRecord::Base.execute_procedure('pr_get_dashboard_edi_section_3',
                                                                                '210', current_company_id))
  @asns_by_partner = [{ value: 13_229, name: "Mark's Merchandise Payment", percentage: nil }]

  transfers_by_year = ActiveRecord::Base.execute_procedure('pr_get_dashboard_edi_section_2', 'Y', current_company_id)
  incoming_transfers_by_year = filter_transfers(transfers_by_year, 'Incoming')
  outgoing_transfers_by_year = filter_transfers(transfers_by_year, 'Outgoing')
  @total_incoming_transfers = incoming_transfers_by_year.map { |k, _v| k['size_in_KB'] }.sum.to_i
  @total_outgoing_transfers = outgoing_transfers_by_year.map { |k, _v| k['size_in_KB'] }.sum.to_i

  total_transfers = @total_incoming_transfers + @total_outgoing_transfers
  @total_incoming_transfers_percentage = percentage(@total_incoming_transfers, total_transfers)
  @total_outgoing_transfers_percentage = percentage(@total_outgoing_transfers, total_transfers)
end

#edi_transportationObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/dashboards_controller.rb', line 71

def edi_transportation
  partner_ids = current_company.edi_partners.where(edi_partner_id: params[:partner_ids]).pluck(:edi_partner_id)
  start_date = params[:start_date]
  end_date = params[:end_date]

  partners = current_company.edi_partners.where(edi_partner_id: partner_ids)
  partner_names = partners.pluck(:description)

  service = EdiPartners::DashboardService.new(
    partner_ids: partner_ids,
    start_date: start_date,
    end_date: end_date
  )
  dashboard_stats = service.dashboard_stats

  render json: {
    data: {
      x_values: partner_names,
      approval_responses: dashboard_stats[:approval_responses],
      status_messages: dashboard_stats[:status_messages],
      status_messages_codes: dashboard_stats[:status_messages_codes]
    }
  }
end

#edi_transportation_formObject



61
62
63
64
65
66
67
68
69
# File 'app/controllers/dashboards_controller.rb', line 61

def edi_transportation_form
  render json: {
    data: {
      partners: current_company.edi_partners.as_json(
        only: [:edi_partner_id, :description]
      )
    }
  }
end

#payrollObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/dashboards_controller.rb', line 96

def payroll
  # to be refactored
  # we should have a model for each data visualization (chart, cards...).
  # Also, we should have arrays of what we receive from the procedure and itterate over it
  # to create the data from a to_json method in the model
  ActiveRecord::Base.execute_procedure(
    'pr_get_dashboard_payroll_section_1',
    current_tenant.tenant_id,
    current_company.company_id
  )[0]

  grouped_payroll_periods = current_company.payroll_periods_data(
    group_by_year: true,
    must_have_timesheet: false,
    no_future: true
  )
  current_period = current_company.current_payroll_period
  chosen_period = current_company.payroll_periods.find_by(payroll_period_id: params[:payroll_period_id])

  period_data = %w[01 02 03 04].map! do |result_type|
    ActiveRecord::Base.execute_procedure(
      'pr_get_dashboard_payroll_section_2',
      current_company.company_id,
      params[:payroll_period_id] || current_period&.payroll_period_id,
      result_type
    )
  end

  chosen_period_data = period_data.dig(0, 0) || {}
  previous_period_data = period_data.dig(0, 1) || {}
  previous_year_period_data = period_data.dig(1, 1) || {}
  ytd_data = period_data.dig(2, 0) || {}
  years_period = period_data.dig(3) || []

  ytd = ytd_data.merge(
    payroll_year: ytd_data['payroll_year']
  )

  render json: {
    cards: {
      emloyees_count: current_company.employees.uniq.count,
      inactive_employees_count: current_company.employees.where(active: false).count,
      terminated_employees_count: current_company.employees.where.not(termination_date: nil).count,
      employees_this_period: chosen_period ? chosen_period.employees.uniq.count : current_period.employees.uniq.count
    },
    can_resume_payroll: current_company.can_resume_payroll?,
    grouped_payroll_periods: grouped_payroll_periods,
    current_period: if current_period
                      current_period.as_json(
                        methods: [:payroll_frequency_description]
                      )
                    else
                      {}
                    end,
    chosen_period: if chosen_period
                     chosen_period.as_json(
                       methods: [:payroll_frequency_description]
                     )
                   end,
    years_period: years_period.map { |period| period_stats(period) },
    current: period_stats(chosen_period_data),
    previous: period_stats(previous_period_data),
    previous_year: period_stats(previous_year_period_data),
    ytd: ytd
  }
end

#payroll_walletObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/controllers/dashboards_controller.rb', line 163

def payroll_wallet
  grouped_payroll_periods = current_company.payroll_periods_data(
    group_by_year: true,
    must_have_timesheet: false,
    no_future: true
  )
  current_payroll_period_id = current_company.current_payroll_period_id

  transactions_breakdown = PayrollVirtualWalletTransaction::View.break_down(
    company_id: current_company.company_id,
    payroll_year: params['year'] || current_company.current_payroll_period_year
  )

  render json: {
    transactions_breakdown: transactions_breakdown,
    current_payroll_period_id: current_payroll_period_id,
    grouped_payroll_periods: grouped_payroll_periods
  }
end