# -*- coding: utf-8 -*-
class Division < ActiveRecord::Base
  set_table_name :divisions
#  set_table_name :pl_divisions
  has_many :employees, :order => "nic"
  has_many :pricelist_divisions, :table_name =>"pricelist.pl_items_divisions"
  has_many :pricelist, :through => :pricelist_divisions, :conditions =>"pl_sections.hidden = 'N' AND pl_items.hidden = 'N'", :include => :section, :order =>"pl_sections.ordered, pl_sections.name, pl_items.ordered"
 # do
 #  def personnel
 #    find(:all, :include => :section, :conditions => "pl_items.hidden = 'N' AND client_type IN ('P', 'A')", :order =>"pl_sections.name, pl_items.name")
 #  end
 #  def commertial
 #    find(:all, :include => :section, :conditions => "pl_items.hidden = 'N' AND client_type IN ('C', 'A')", :order =>"pl_sections.name,  pl_items.name")
 #  end
  #  end
  
named_scope :for_sdelka, :conditions  => ["id IN (68, 676)"], :order =>"name"

  def self.all_for_form
    [["Частные исталляторы", "152"], 
     ["Софт группа", "5"], 
     ["Альпинисты", "158"], 
     ["АМС", "151"], 
     ["Корп. отдел", "155"], 
     ["Сварщики", "168"], 
     ["Подземное строительство", "183"], 
     ["Отдел модернизации", "241"]
    ]
  end

  def self.for_select
    Division.all.map do |div|
      [div.name, div.employees.map { |c| [c.login, c.id] }]
    end
  end

  def self.for_select_only_working(employees=[])
    Division.all.map do |div|
      [div.name, div.employees.select{|i| i.quitted != true || (employees.include? i.id)}.map { |c| [c.nic, c.id] }]
    end
  end

  def self.alp
    Division.find 1
  end

  def self.mod
    Division.find 1
  end


  def self.soft
    Division.find 251
  end

  def self.corp
    Division.find 1
  end

  def self.welder
    Division.find 1
  end

  def self.underground
    Division.find 1
  end

  def self.private
    Division.find 1 #220
  end
  def self.private_employees
    Division.find 1
  end

  def self.couriers
    Division.find 1  
  end

  def self.ams
    Division.find 1
  end

  def self.reception
    Division.find 1
  end

  def self.btul2
    Division.find 1
  end
  def self.btul
    Division.find 1
  end


  def self.office
    Division.find 1
  end
  
  def self.other_works
    Division.find 1
  end

  def self.construction
    Division.find 1
  end

  def self.report
        Division.find 1
  end
  
  def self.all_for_form
    [
     ["Монтажные подряды ЛС", "construction"] 
    ]
  end

  def self.name_for_form(department)
    case department
    when 'mod'
      'Модернизаторы'
    when 'alp'
      'Альпинисты'
    when 'private'
      'Частные инсталляторы'
    when 'corp'
      'Корп. отдел'
    when 'ams'
      'АМС'
    when 'soft'
      'Софт отдел'
    when 'soft_fast'
      'Софт отдел: фаст'
    when 'welder'
      'Сварочная бригада'
    when 'underground'
      'Подземное строительство'
    when 'reception'
      'Ресепшн'
    when 'btul2'
      'Офис btul2'
    when 'office'
      'Установка офисного оборудования'
    when 'construction'
      'Монтажные подряды ЛС'
    when 'between_logins'
      'Передача материалов между логинами'
    when 'to_login'
      'Регистрация ранее установленных материалов'
   when 'from_login'
      'Снятие материала с объекта'
    else
      ''
    end
  end

  def self.section_name(department)
    case department
      when 'reception'
      'receptions'
      when 'btul2'
      'btul2'
      when 'office'
      'office_mats'
      when 'construction'
      'constr_works'
      when 'mod'
      'mod2'
      when 'corp'
      'corp2'
      else
      department
    end
  end


    def self.dep_for_new_tags(dep_name)
      case dep_name
      when 'private'
        return 5
      when 'corp'
        return 7
      when 'mod'
        return 9
      when 'soft'
        return 11
      when 'ams'
        return 46
      else
        return 1
      end
    end

end
