Class: Ecom::Core::AttendanceSheet

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_current(site_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.



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

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

  if AttendanceSheet.open_exists?(site_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: StatusConstants::OPEN,
                         site_id: site_id)
end

.exists_for_today?(site_id) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.exists_for_today?(site_id)
  AttendanceSheet
    .by_site(site_id)
    .by_date(Date.today)
    .exists?
end

.open_exists?(site_id) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.open_exists?(site_id)
  AttendanceSheet
    .by_site(site_id)
    .by_status(StatusConstants::OPEN)
    .exists?
end

.open_exists_for_today?(site_id) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.open_exists_for_today?(site_id)
  AttendanceSheet
    .by_site(site_id)
    .by_date(Date.today)
    .by_status(StatusConstants::OPEN)
    .exists?
end

.open_for_date(date, site_id) ⇒ Object



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

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

.submit(date, site_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.



74
75
76
77
78
79
80
81
82
# File 'app/models/ecom/core/attendance_sheet.rb', line 74

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

  sheet. = DateTime.now
  sheet.status = StatusConstants::
  sheet.save
  sheet
end

.submit_current(site_id) ⇒ Object



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

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

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

  sheet. = DateTime.now
  sheet.status = StatusConstants::
  sheet.save
  sheet
end

Instance Method Details

#approveObject



92
93
94
95
96
97
98
# File 'app/models/ecom/core/attendance_sheet.rb', line 92

def approve
  raise 'Attendance sheet is not submitted and cannot be approved' unless status == StatusConstants::

  self.status = StatusConstants::APPROVED
  self.approved_at = DateTime.now
  save
end

#submitObject



84
85
86
87
88
89
90
# File 'app/models/ecom/core/attendance_sheet.rb', line 84

def submit
  raise 'Attendance sheet is not open and cannot be submitted' if status != StatusConstants::OPEN

  self. = DateTime.now
  self.status = StatusConstants::
  save
end