Class: TimeOffBalanceCalculator

Inherits:
Object
  • Object
show all
Defined in:
app/services/time_off_balance_calculator.rb

Instance Method Summary collapse

Constructor Details

#initialize(employee:, date: Date.current, time_off_type: nil) ⇒ TimeOffBalanceCalculator

The calculator looks at the past balance first and then calculates future data based on requests.



4
5
6
7
8
9
# File 'app/services/time_off_balance_calculator.rb', line 4

def initialize(employee:, date: Date.current, time_off_type: nil)
  @employee = employee
  @date = date
  @time_off_type = time_off_type
  @vacation_time_off_type = TimeOffType.find(TimeOffType::VACATION_ID)
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/time_off_balance_calculator.rb', line 11

def call
  balances = {}
  time_off_types = @time_off_type ? [@time_off_type] : TimeOffType.all.order(:id)

  time_off_types.each do |type|
    balances[type.name] = if type.id == TimeOffType::VACATION_ID
                            # This Vacation type is the main type of the PTO module. Needs to be treated differently since there are more calculations
                            calculate_vacation_balance
                          else
                            calculate_balance_for_type(type)
                          end
  end

  balances
end

#formatted_balancesObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/time_off_balance_calculator.rb', line 27

def formatted_balances
  calculate.transform_values do |data|
    result = {
      time_used: format_number(data[:time_used]),
      unit: data[:unit],
      time_available: format_number(data[:time_available]),
      max_available: format_number(data[:max_available]),
      breakdown: data[:breakdown] || {}
    }
    result[:time_planned] = format_number(data[:time_planned]) if data[:time_planned].present?
    result
  end
end

#vacation_summaryObject



41
42
43
# File 'app/services/time_off_balance_calculator.rb', line 41

def vacation_summary
  calculate_vacation_summary
end