# -*- coding: utf-8 -*-
class Work < ActiveRecord::Base
  has_many :rep_works
  has_many :reports, :through => :rep_works
  belongs_to :of_work
  belongs_to :measure, :foreign_key => "measure"
  belongs_to :legal_entity
  default_scope :order => "sort"
  named_scope :alps, :conditions => "works.show IS TRUE AND department = 'alp'"
  named_scope :mod, :conditions => "works.show IS TRUE AND department = 'mod'"
  named_scope :soft, :conditions => "works.show IS TRUE AND department = 'soft'"
  named_scope :ams, :conditions => "works.show IS TRUE AND department = 'ams'"
  named_scope :corp, :conditions => "works.show IS TRUE AND department = 'corp'"
  named_scope :private, :conditions => "works.show IS TRUE AND department = 'private'"
  named_scope :welder, :conditions => "works.show IS TRUE AND department = 'welder'"
  named_scope :underground, :conditions => "works.show IS TRUE AND department = 'undergroung'"
  named_scope :couriers, :conditions => "works.show IS TRUE AND department = 'couriers'"
  named_scope :only_active,  :conditions => "works.show IS NOT FALSE"
  named_scope :with_category, lambda { |category| category.present? ? { :conditions => ["category LIKE ('%?%')", category.to_i] }: {} }
  named_scope :without_category, :conditions => "category IS NULL"
  attr_reader :date_in, :date_start, :date_end, :position_type, :usr, :section_id, :client_type, :hidden

  def full_name
    department_name + ": " + name
  end

  def legal_entity_code
    if legal_entity
      legal_entity.code
    else
      ''
    end
  end

  def categories_names
    Category.find(self.category.split('-')).map{|i| i.name}.join(', ') if self.category
  end

  def measure_name
    if measure
      measure.name 
    else
      " шт. "
    end
  end


  def wage
    rate
  end

  def section_id
    '1723'
  end

  def section_name
    case department
    when 'soft', 'ams', 'corp', 'alp'
      'Дополнительные работы'
    when 'private'
      if section == 0
        "Каблирование"
      else
        "Дополнительные"
      end
      when 'mod'
      "Доп. работы"
    end
  end

  def department_name
    case department
    when 'soft'
      'Софт группа'      
    when 'alp'
      "Альпинисты"      
    when 'ams'
      'АМС'
    when 'private'
      'Частные исталляторы'
    when 'corp'
      'Корп. отдел'
    when 'welder'
      'Сварочная бригада'
    when 'underground'
      'Подземное строительство'
    when 'mod'
      'Отдел модернизации'
    else
      'Неизвестно'
    end
  end

  def depts
    department.to_a
  end

end
