Sha256: 48c5bd88c1a6102ac2bc1a7e1cbb5cbf2956e5a90534a1cdf9318ac64768e6f8

Contents?: true

Size: 1.65 KB

Versions: 9

Compression:

Stored size: 1.65 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
      validates :hours, presence: true, numericality: { greater_than_or_equal_to: 0 }
      validates :overtime_type_id,
                uniqueness: { scope: :overtime_sheet_entry_id,
                              message: 'There exists a crew overtime for the given overtime sheet and overtime 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 = if finish >= start
                 ((finish - start) / 3600).round
               else
                 ((start - finish) / 3600).round
               end
        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

9 entries across 9 versions & 1 rubygems

Version Path
ecom_core-1.3.14 app/models/ecom/core/crew_overtime.rb
ecom_core-1.3.13 app/models/ecom/core/crew_overtime.rb
ecom_core-1.3.12 app/models/ecom/core/crew_overtime.rb
ecom_core-1.3.11 app/models/ecom/core/crew_overtime.rb
ecom_core-1.3.10 app/models/ecom/core/crew_overtime.rb
ecom_core-1.3.9 app/models/ecom/core/crew_overtime.rb
ecom_core-1.3.8 app/models/ecom/core/crew_overtime.rb
ecom_core-1.3.7 app/models/ecom/core/crew_overtime.rb
ecom_core-1.3.6 app/models/ecom/core/crew_overtime.rb