Class: Ecom::Core::AttendanceSheet

Inherits:
ApplicationRecord show all
Defined in:
app/models/ecom/core/attendance_sheet.rb

Constant Summary collapse

OPEN =
'Open'.freeze
SUBMITTED =
'Submitted'.freeze
APPROVED =
'Approved'.freeze

Class Method Summary collapse

Class Method Details

.create_current(project_id) ⇒ Object

Attendance sheet should be created using the method below only. This is because we need to check if there is an open attendance already, and also that we have only one attendace sheet per day.



42
43
44
45
46
47
48
49
50
# File 'app/models/ecom/core/attendance_sheet.rb', line 42

def self.create_current(project_id)
  raise 'Attendance sheet already created for the day.' if AttendanceSheet.exists_for_today?(project_id)

  if AttendanceSheet.open_exists?(project_id)
    raise 'There is an open attendance sheet which needs to be submitted before creating a new one.'
  end

  AttendanceSheet.create(date: Date.today, opened_at: Time.now, status: OPEN, project_id: project_id)
end

.exists_for_today?(project_id) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/models/ecom/core/attendance_sheet.rb', line 29

def self.exists_for_today?(project_id)
  AttendanceSheet.current(project_id).exists?
end

.open_exists?(project_id) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/models/ecom/core/attendance_sheet.rb', line 25

def self.open_exists?(project_id)
  AttendanceSheet.open(project_id).exists?
end

.open_exists_for_today?(project_id) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/models/ecom/core/attendance_sheet.rb', line 33

def self.open_exists_for_today?(project_id)
  AttendanceSheet.current_open(project_id).exists?
end

.open_for_date(date, project_id) ⇒ Object



21
22
23
# File 'app/models/ecom/core/attendance_sheet.rb', line 21

def self.open_for_date(date, project_id)
  AttendanceSheet.find_by(status: OPEN, date: date, project_id: project_id)
end

.submit(date, project_id) ⇒ Object

This method should be used by privileged users to submit the attendance sheet after the date has passed. Normally, timekeepers need to open and close an attendance sheet of a date on the specific date.



67
68
69
70
71
72
73
74
75
# File 'app/models/ecom/core/attendance_sheet.rb', line 67

def self.submit(date, project_id)
  sheet = AttendanceSheet.open_for_date(date, project_id)
  raise 'There is no open attendance sheet to submit for the selected day.' unless sheet

  sheet.closed_at = Time.now
  sheet.status = SUBMITTED
  sheet.save
  sheet
end

.submit_current(project_id) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'app/models/ecom/core/attendance_sheet.rb', line 52

def self.submit_current(project_id)
  sheet = AttendanceSheet.find_by(date: Date.today, status: OPEN, project_id: project_id)

  raise 'There is no open attendance sheet to submit.' if sheet.nil?

  sheet.closed_at = Time.now
  sheet.status = SUBMITTED
  sheet.save
  sheet
end