Sha256: 0ad41847ed1d54aeaef32427f50e8f59b57060868a1366de2d1dcc5a498a5b7e

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 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

      def self.exists_for_date?(date, project_id)
        OvertimeSheet.by_project(project_id).by_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.exists_for_date?(date, project_id)
          raise 'There is already an 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

2 entries across 2 versions & 1 rubygems

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