Sha256: 1c470d6b6aff5c8da87ffde9a989acac83dc4b2a724bee91304b2d057d7214bb

Contents?: true

Size: 1.27 KB

Versions: 17

Compression:

Stored size: 1.27 KB

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, -> { where(status: OPEN) }
      scope :for_date, ->(date) { where(date: date) }

      def self.open_exists?
        OvertimeSheet.open.exists?
      end

      def self.open_for_date_exists?(date)
        OvertimeSheet.open.for_date(date).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_for_date_exists?(date)
          raise 'There is already an open overtime sheet for the selected date.'
        end

        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

17 entries across 17 versions & 1 rubygems

Version Path
ecom_core-1.2.5 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.2.4 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.2.3 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.2.2 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.2.1 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.2.0 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.1.28 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.1.27 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.1.26 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.1.25 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.1.24 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.1.23 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.1.22 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.1.21 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.1.20 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.1.19 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.1.18 app/models/ecom/core/overtime_sheet.rb