Sha256: d5102eb8b8ea99ca36da2a9bf1403989d6244e408e3c4b1d2005f140365bf4e8

Contents?: true

Size: 1.73 KB

Versions: 10

Compression:

Stored size: 1.73 KB

Contents

module Ecom
  module Core
    class OvertimeSheet < ApplicationRecord
      validates :date, :opened_at, presence: true, uniqueness: { scope: :project_id }
      validates :status, inclusion: StatusConstants::STATUSES

      belongs_to :project

      has_many :overtime_sheet_entries
      has_many :crew_overtimes, through: :overtime_sheet_entries

      scope :by_project, ->(id) { where(project_id: id) }
      scope :by_date, ->(date) { where(date: date) }
      scope :by_status, ->(status) { where(status: status) }
      scope :by_date_between, ->(from, to) { where('date BETWEEN ? AND ?', from, to) }
      scope :open, ->(id) { where(status: StatusConstants::OPEN, project_id: id) }
      scope :submitted, ->(id) { where(status: StatusConstants::SUBMITTED, project_id: id) }

      def self.open_exists?(project_id)
        OvertimeSheet.open(project_id).exists?
      end

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

        if OvertimeSheet.open_exists?(project_id)
          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: StatusConstants::OPEN, project_id: project_id)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
ecom_core-1.2.18 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.2.17 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.2.16 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.2.15 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.2.14 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.2.13 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.2.12 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.2.11 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.2.10 app/models/ecom/core/overtime_sheet.rb
ecom_core-1.2.9 app/models/ecom/core/overtime_sheet.rb