Sha256: f86cf39957c17be5b2cb23e77601009b93b622ed217c49821df79f3c6a1f6cf9

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

module Ecom
  module Core
    class AttendanceSheet < ApplicationRecord
      OPEN = 'Open'.freeze
      CLOSED = 'Closed'.freeze

      validates :date, :opened_at, presence: true, uniqueness: true
      validates :status, inclusion: [OPEN, CLOSED]

      has_many :attendance_sheet_entries

      scope :open, -> { find_by(status: OPEN) }

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

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

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

      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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ecom_core-1.1.12 app/models/ecom/core/attendance_sheet.rb
ecom_core-1.1.11 app/models/ecom/core/attendance_sheet.rb
ecom_core-1.1.10 app/models/ecom/core/attendance_sheet.rb
ecom_core-1.1.9 app/models/ecom/core/attendance_sheet.rb