Class: Ecom::Core::AttendanceSheet

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

Constant Summary collapse

OPEN =
'Open'.freeze
CLOSED =
'Closed'.freeze

Class Method Summary collapse

Class Method Details

.closeObject

This method should be used by privileged users to close 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.



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

def self.close
  raise 'There is no attendance sheet to close.' unless AttendanceSheet.open_exists_for_today?

  sheet = AttendanceSheet.open
  sheet.closed_at = Time.now
  sheet.save
  sheet
end

.close_currentObject



33
34
35
36
37
38
39
40
41
42
# File 'app/models/ecom/core/attendance_sheet.rb', line 33

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

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

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

.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.



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

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

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

.exists_for_today?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'app/models/ecom/core/attendance_sheet.rb', line 14

def self.exists_for_today?
  AttendanceSheet.where(date: Date.today).exists?
end

.open_exists_for_today?Boolean

Returns:

  • (Boolean)


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

def self.open_exists_for_today?
  where(status: OPEN, date: Date.today).exists?
end