Sha256: 3237447482e2edca219a603c4929835a01e65b14e5457ed1cdf3c681670c0064

Contents?: true

Size: 1012 Bytes

Versions: 1

Compression:

Stored size: 1012 Bytes

Contents

module Ecom
  module Core
    class OvertimeSheet < 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 :overtime_sheet_entries

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

      def self.open_exists?
        OvertimeSheet.where(status: OPEN).exists?
      end

      # Overtime sheet should be created using the
      # method below only. This is because we need to
      # check if there is an open overtime already,
      # and also that we have only one open overtime
      # sheet at a time.
      def self.create_new(date)
        if OvertimeSheet.open_exists?
          raise 'There is already an open overtime sheet. It has to be submitted before creating a new one.'
        end

        OvertimeSheet.create(date: date, opened_at: Time.now, status: OPEN)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ecom_core-1.1.17 app/models/ecom/core/overtime_sheet.rb