Module: HrLite::AttendanceSummary

Defined in:
app/services/hr_lite/attendance_summary.rb

Overview

Month roll-up per user — the payroll contract. Exact math per date:

holiday / weekend / present / paid full-day leave  -> payable +1
unpaid full-day leave (LWP) / absent               -> lop +1
half-day punch (no leave)                          -> payable +0.5, lop +0.5
half-day PAID leave: leave half payable; other half payable if
punched, else lop (unpaid half-day leave: leave half is lop too)
future dates                                       -> upcoming (neither)
dates outside the employment window (before DOJ / after exit, when an
EmployeeProfile exists)                          -> out_of_window (neither)

The window lives HERE and only here — payroll consumes payable/lop as-is and never re-clips, so a mid-month joiner is never double-penalized. For closed months: payable + lop + upcoming + out_of_window == days_in_month.

Class Method Summary collapse

Class Method Details

.for(user:, month:) ⇒ Object



17
18
19
20
21
# File 'app/services/hr_lite/attendance_summary.rb', line 17

def self.for(user:, month:)
  range = month.beginning_of_month..month.end_of_month
  profile = EmployeeProfile.find_by(user_id: user.id)
  from_day_status(DayStatus.new(user: user, range: range), range, profile: profile)
end

.for_all(users:, month:) ⇒ Object

Batch variant for team/payroll screens: 3 queries total, not 3×N.



24
25
26
# File 'app/services/hr_lite/attendance_summary.rb', line 24

def self.for_all(users:, month:)
  users.index_with { |user| self.for(user: user, month: month) }.transform_keys(&:id)
end

.from_day_status(day_status, range, profile: nil) ⇒ Object



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/services/hr_lite/attendance_summary.rb', line 28

def self.from_day_status(day_status, range, profile: nil)
  summary = {
    present: 0.to_d, half_day: 0.to_d, paid_leave: 0.to_d, unpaid_leave: 0.to_d,
    holiday: 0, weekend: 0, absent: 0.to_d, upcoming: 0, out_of_window: 0,
    payable_days: 0.to_d, lop_days: 0.to_d, days_in_month: range.count
  }

  range.each do |date|
    if profile && !profile.active_on?(date)
      summary[:out_of_window] += 1
      next
    end

    day = day_status.for(date)
    case day.kind
    when :holiday
      summary[:holiday] += 1
      summary[:payable_days] += 1
    when :weekend
      summary[:weekend] += 1
      summary[:payable_days] += 1
    when :present
      summary[:present] += 1
      summary[:payable_days] += 1
    when :half_day
      summary[:half_day] += 1
      summary[:payable_days] += BigDecimal("0.5")
      summary[:lop_days] += BigDecimal("0.5")
    when :leave
      if day.leave.paid?
        summary[:paid_leave] += 1
        summary[:payable_days] += 1
      else
        summary[:unpaid_leave] += 1
        summary[:lop_days] += 1
      end
    when :half_day_leave
      apply_half_day_leave(summary, day)
    when :upcoming
      summary[:upcoming] += 1
    else # :absent
      summary[:absent] += 1
      summary[:lop_days] += 1
    end
  end

  summary
end