# -*- coding: utf-8 -*-
### СМЕНЫ. Быват двух типов: рабочие смены (WorkShift) и смены менеджеров (ManagerShift)
class Shift < ActiveRecord::Base
  belongs_to :employee
  belongs_to :regular_shift
  
  attr_accessor_with_default :s_checked, true

  default_scope :include => :employee #, :order =>"pricelist.pl_people.login"

  named_scope :soft, :conditions => "shifts.department = 'soft'"
  named_scope :corp, :conditions => "shifts.department = 'corp'"
  named_scope :mod, :conditions => "shifts.department = 'mod'"
  named_scope :welders, :conditions => "shifts.department = 'welder'"
  named_scope :for_department, lambda { |dep| dep.present? ? { :conditions => "shifts.department = '#{dep}'" }: { } }
  named_scope :for_departments, lambda { |deps| (deps.present? && deps.size > 0) ? { :conditions => ["shifts.department IN (?)", deps] }: { } }
  named_scope :for_month, lambda { |date| date.present? ? { :conditions => "shift_date BETWEEN DATE('#{date.beginning_of_month}') AND DATE('#{date.end_of_month}')", :order => :shift_date }: { } }
  named_scope :for_week, lambda { |date| date.present? ? { :conditions => "shift_date BETWEEN DATE('#{date.beginning_of_week}') AND DATE('#{date.end_of_week}')", :order => :shift_date }: { } }
  named_scope :for_date, lambda { |date| date.present? ? { :conditions => "shift_date = DATE('#{date.to_date}')" }: { } }
  named_scope :for_calendar, lambda { |date| date.present? ? { :conditions => "shift_date BETWEEN DATE('#{date.beginning_of_month.beginning_of_week}') AND DATE('#{date.end_of_month.end_of_week}')", :order => :shift_date }: { } }
  named_scope :with_employee, lambda { |empl| (empl.present? && empl != '0') ? { :conditions => "shifts.employee_id = '#{empl}'" }: { } }


  def employee_nic
    employee.login if employee
  end
  
  def work_time
    Russian::strftime(time_from, "%H:%M")+"-"+Russian::strftime(time_to, "%H:%M") if time_to && time_from
  end


  def salary      
    if self.employee
    salaries = Salary.find(:first, :conditions =>"salaries.division_id = '#{self.employee.division_id}' AND  salaries.position = '#{self.employee.position}' AND salaries.date_from <= '#{self.shift_date}'", :order =>'date_from DESC')
    else
      nil
    end
  end


  def status_change
    case status
    when 0, nil, 1
      "Утвердить"
    when 2
      "Отклонить"
    end
  end


end
