Sha256: 4a0cd6d53b9e4d77d3bc636eed936f325eed5242e470af08dd83bb6ca84a2728

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

module Ecom
  module Core
    class AttendanceSheet < ApplicationRecord
      OPEN = 'Open'.freeze
      SUBMITTED = 'Submitted'.freeze
      APPROVED = 'Approved'.freeze

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

      has_many :attendance_sheet_entries

      scope :open, -> { find_by(status: OPEN) }
      scope :current_open, -> { where(status: OPEN, date: Date.today) }

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

      # 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.
      def submit
        raise 'This attendance sheet is not open. Therefore it cannot be submitted' unless status == OPEN

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ecom_core-1.1.15 app/models/ecom/core/attendance_sheet.rb