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_currentObject

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.



38
39
40
41
42
43
44
# File 'app/models/ecom/core/attendance_sheet.rb', line 38

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

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

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

.exists_for_today?Boolean

Returns:

  • (Boolean)


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

def self.exists_for_today?
  AttendanceSheet.current.exists?
end

.open_exists?Boolean

Returns:

  • (Boolean)


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

def self.open_exists?
  AttendanceSheet.open.exists?
end

.open_exists_for_today?Boolean

Returns:

  • (Boolean)


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

def self.open_exists_for_today?
  AttendanceSheet.current_open.exists?
end

.open_for_date(date) ⇒ Object



17
18
19
# File 'app/models/ecom/core/attendance_sheet.rb', line 17

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

.submit(date) ⇒ 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.



61
62
63
64
65
66
67
68
69
# File 'app/models/ecom/core/attendance_sheet.rb', line 61

def self.submit(date)
  sheet = AttendanceSheet.open_for_date(date)
  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_currentObject



46
47
48
49
50
51
52
53
54
55
# File 'app/models/ecom/core/attendance_sheet.rb', line 46

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

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

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