module Ecom module Core class Task < ApplicationRecord include AASM has_ancestry NOT_INSPECTED = 'Not Inspected'.freeze IN_PROGRESS = 'In Progress'.freeze PASS = 'Pass'.freeze FAIL = 'Fail'.freeze INSPECTION_STATUSES = [NOT_INSPECTED, IN_PROGRESS, PASS, FAIL].freeze belongs_to :work_product belongs_to :task_template belongs_to :work_package, optional: true belongs_to :work_order, optional: true belongs_to :plan, optional: true belongs_to :performer, class_name: 'Ecom::Core::User', optional: true belongs_to :client_consultant, class_name: 'Ecom::Core::User', optional: true belongs_to :foreman, class_name: 'Ecom::Core::User', optional: true belongs_to :inspector, class_name: 'Ecom::Core::User', optional: true belongs_to :change_order_for, class_name: 'Ecom::Core::User', optional: true has_many :task_resources has_one :takeoff has_one :change_order, foreign_key: :change_order_for_id, class_name: 'Ecom::Core::Task' validates_with DateRangeValidator validates :code, :name, :status, :inspection_status, :percent_completed, presence: true validates_numericality_of :percent_completed, only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 100 validates :percentage_contribution, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100, only_integer: true }, allow_nil: true validates_inclusion_of :inspection_status, in: INSPECTION_STATUSES scope :by_status, ->(status) { where(status: status) } # State: planned -> The task has be planned for execution # State: ready_to_start -> All resources for the task has been set and is ready to start aasm column: 'status' do state :new, initial: true state :planned state :ready_to_start state :in_progress state :submitted state :under_review state :completed event :start do transitions from: :new, to: :in_progress end event :submit do transitions from: :in_progress, to: :submitted end event :review do transitions from: :submitted, to: :under_review end event :complete do transitions from: %i[submitted under_review], to: :completed end event :rework do transitions from: :under_review, to: :in_progress end end end end end