Sha256: fcd13cfe91e5bae688b42f694a53f906b99b036187a4aa128bce4b8127715863

Contents?: true

Size: 1.23 KB

Versions: 16

Compression:

Stored size: 1.23 KB

Contents

module Ecom
  module Core
    class CrewOvertime < ApplicationRecord
      belongs_to :overtime_type
      belongs_to :overtime_sheet_entry
      belongs_to :revision_to, class_name: 'Ecom::Core::CrewOvertime', optional: true
      belongs_to :created_by, class_name: 'Ecom::Core::User'

      has_one :revision, class_name: 'Ecom::Core::CrewOvertime', foreign_key: :revision_to_id

      validates :hours, :raw_hours, presence: true
      validate :validate_overtime_by_type

      scope :by_overtime, lambda { |id|
        joins(:overtime_sheet_entry).where(ecom_core_overtime_sheet_entries: { overtime_sheet_id: id })
      }
      scope :revised, ->(revised) { where(revised: revised) }

      before_save :calculate_hours

      def validate_overtime_by_type
        return unless overtime_type

        start = Time.zone.parse(overtime_type.from)
        finish = Time.zone.parse(overtime_type.to)
        diff = ((finish - start) / 3600).round

        return unless raw_hours > diff

        errors.add(:crew_overtime, 'The selected overtime type does not allow the specified amount of hours.')
      end

      def calculate_hours
        rate = OvertimeType.find(overtime_type_id).rate
        self.hours = raw_hours * rate
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ecom_core-1.3.5 app/models/ecom/core/crew_overtime.rb
ecom_core-1.3.4 app/models/ecom/core/crew_overtime.rb
ecom_core-1.3.3 app/models/ecom/core/crew_overtime.rb
ecom_core-1.3.2 app/models/ecom/core/crew_overtime.rb
ecom_core-1.3.1 app/models/ecom/core/crew_overtime.rb
ecom_core-1.3.0 app/models/ecom/core/crew_overtime.rb
ecom_core-1.2.36 app/models/ecom/core/crew_overtime.rb
ecom_core-1.2.35 app/models/ecom/core/crew_overtime.rb
ecom_core-1.2.34 app/models/ecom/core/crew_overtime.rb
ecom_core-1.2.33 app/models/ecom/core/crew_overtime.rb
ecom_core-1.2.32 app/models/ecom/core/crew_overtime.rb
ecom_core-1.2.31 app/models/ecom/core/crew_overtime.rb
ecom_core-1.2.30 app/models/ecom/core/crew_overtime.rb
ecom_core-1.2.29 app/models/ecom/core/crew_overtime.rb
ecom_core-1.2.28 app/models/ecom/core/crew_overtime.rb
ecom_core-1.2.27 app/models/ecom/core/crew_overtime.rb