Class: HrLite::Admin::AttendancesController

Inherits:
BaseController show all
Defined in:
app/controllers/hr_lite/admin/attendances_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject

Team day view: everyone × their punch/status for one date.



5
6
7
8
9
10
# File 'app/controllers/hr_lite/admin/attendances_controller.rb', line 5

def index
  @date = parse_date_param(params[:date])
  @employees = HrLite.employees
  @records = AttendanceRecord.for_date(@date).where(user_id: @employees.map(&:id)).index_by(&:user_id)
  @flagged_count = @records.values.count(&:flagged?)
end

#showObject

One employee's month + the regularization form for ?date=.



13
14
15
16
17
18
19
20
# File 'app/controllers/hr_lite/admin/attendances_controller.rb', line 13

def show
  @employee = HrLite.user_klass.find(params[:user_id])
  @month = parse_month_param(params[:month])
  @day_status = DayStatus.new(user: @employee, range: @month.beginning_of_month..@month.end_of_month)
  @counts = @day_status.counts
  @edit_date = params[:date].present? ? parse_date_param(params[:date]) : nil
  @edit_record = @edit_date && AttendanceRecord.find_or_initialize_by(user_id: @employee.id, date: @edit_date)
end

#updateObject

Regularization: fix punches with a mandatory note, fully audited. Clearing both punch times deletes the record (that is how an erroneous punch is removed).



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
# File 'app/controllers/hr_lite/admin/attendances_controller.rb', line 25

def update
  @employee = HrLite.user_klass.find(params[:user_id])
  date = parse_date_param(params[:date])
  record = AttendanceRecord.find_or_initialize_by(user_id: @employee.id, date: date)

  note = params.dig(:attendance_record, :regularization_note).to_s.strip
  if note.blank?
    return redirect_to admin_attendance_path(@employee.id, date: date, month: date.strftime("%Y-%m")),
                       alert: "A regularization note is required."
  end

  attrs = params.require(:attendance_record).permit(:check_in_at, :check_out_at, :status)
  if attrs[:check_in_at].blank? && attrs[:check_out_at].blank?
    record.destroy if record.persisted?
    log_regularization(record, note, removed: true)
    return redirect_to admin_attendance_path(@employee.id, month: date.strftime("%Y-%m")),
                       notice: "Punch removed."
  end

  record.assign_attributes(attrs)
  record.status = "present" if record.status.blank?
  record.regularized_by_id = hr_current_user.id
  record.regularized_at = Time.current
  record.regularization_note = note

  if record.save
    log_regularization(record, note, removed: false)
    redirect_to admin_attendance_path(@employee.id, month: date.strftime("%Y-%m")),
                notice: "Attendance updated."
  else
    redirect_to admin_attendance_path(@employee.id, date: date, month: date.strftime("%Y-%m")),
                alert: record.errors.full_messages.to_sentence
  end
end