Class: TimeOffBalanceCalculator
- Inherits:
-
Object
- Object
- TimeOffBalanceCalculator
- Defined in:
- app/services/time_off_balance_calculator.rb
Instance Method Summary collapse
- #call ⇒ Object
- #formatted_balances ⇒ Object
-
#initialize(employee:, date: Date.current, time_off_type: nil) ⇒ TimeOffBalanceCalculator
constructor
The calculator looks at the past balance first and then calculates future data based on requests.
- #vacation_summary ⇒ Object
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
#call ⇒ Object
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_balances ⇒ Object
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_summary ⇒ Object
41 42 43 |
# File 'app/services/time_off_balance_calculator.rb', line 41 def vacation_summary calculate_vacation_summary end |