Class: Ecom::Core::AttendanceSheet

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

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.



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

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: StatusConstants::OPEN,
                         project_id: project_id)
end

.exists_for_today?(project_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?(project_id)
  AttendanceSheet
    .by_project(project_id)
    .by_date(Date.today)
    .exists?
end

.open_exists?(project_id) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.open_exists?(project_id)
  AttendanceSheet
    .by_project(project_id)
    .by_status(StatusConstants::OPEN)
    .exists?
end

.open_exists_for_today?(project_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?(project_id)
  AttendanceSheet
    .by_project(project_id)
    .by_date(Date.today)
    .by_status(StatusConstants::OPEN)
    .exists?
end

.open_for_date(date, project_id) ⇒ Object



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

def self.open_for_date(date, project_id)
  AttendanceSheet.find_by(status: StatusConstants::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.



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

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 = StatusConstants::
  sheet.save
  sheet
end

.submit_current(project_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(project_id)
  sheet = AttendanceSheet.find_by(date: Date.today, status: StatusConstants::OPEN, project_id: project_id)

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

  sheet.closed_at = Time.now
  sheet.status = StatusConstants::
  sheet.save
  sheet
end