# -*- coding: utf-8 -*-
class DoneMaterial < ActiveRecord::Base
  def self.table_name() 
    db_name = Rails.configuration.database_configuration[Rails.env]["database"]
    db_name+".done_materials" 
  end
  belongs_to :material_detail, :foreign_key => :material_id
  belongs_to :report
  belongs_to :employee
  before_save :set_values
  before_create :set_date
#  default_scope :include => [:report, :employee]
  named_scope :soft, :conditions => "done_materials.department = 'soft'"
  named_scope :private, :conditions => "done_materials.department = 'private'"
  named_scope :reception, :conditions => "done_materials.department = 'reception'"
  named_scope :btul2, :conditions => "done_materials.department = 'btul2'"
  named_scope :office, :conditions => "done_materials.department = 'office'"
  named_scope :between_logins, :conditions => "done_materials.department = 'between_logins'"
  named_scope :to_login, :conditions => "done_materials.department = 'to_login'"
  named_scope :construction, :conditions => "done_materials.department = 'construction'"
  named_scope :in_month, lambda { |date| date.present? ? { :conditions => ['date >= ? AND date <= ?', date.to_date.beginning_of_month.to_time.beginning_of_day, date.to_date.end_of_month.to_time.end_of_day] } :{} }
  named_scope :in_dates, lambda { |from, to| from.present? ? {:conditions => ['DATE(date) BETWEEN DATE(?) AND DATE(?)', from.to_time.beginning_of_day, to.to_time.end_of_day] } :{} }
  named_scope :with_empl, lambda { |empl| empl.present? ? { :conditions => ['employee_id = ?', empl] } : { } }
  named_scope :with_inst_types, lambda { |tps| (tps.present? && tps.size > 0) ? (tps.include? "1") ? {:conditions => ['((done_materials.pay_type = 1 AND done_materials.inst_type = 1) OR (done_materials.pay_type IN (?)) OR (done_materials.inst_type IN (?)))', tps - ["1"], tps - ["1"]] } : {:conditions => ['((done_materials.pay_type IN (?)) OR (done_materials.inst_type IN (?)))', tps, tps - ["1"]] } : { } }

  
  def material_detail_number
    if material_detail 
      material_detail.number
    else
      0
    end
  end

  def preowned_name
    if preowned == true
      'Б/У'
    else
      ''
    end
  end
  
  def material_detail_section
    if material_detail 
      material_detail.section
    else
      0
    end
  end

  def material_detail_sum
    if material_detail 
      material_detail.sum
    else
      0
    end
  end

  def pay_type_name
    case pay_type.to_i
    when 1
      if inst_type.to_i == 1
        "Установка без оплаты"
      elsif inst_type.to_i == 4
        "Снятие с объекта сети"
      elsif inst_type.to_i == 6
        "Замена снятого с объекта сети"
      end
    when 2
      if inst_type.to_i == 4
        "Снятие с объекта сети"
      else
        "Аренда"  
      end      
    when 3
      "Продажа"
    when 4
      "Снятие с объекта сети"
    when 5
      "Аренда  в счет тарифа"
    when 6
      "Установка на пробный период"
    when 7
      "Продажа, списание со счёта"
    when 8
      "Продажа на К+"
    end
  end

  def self.inst_type_for_sql
    "CASE dm.inst_type WHEN 1 THEN (CASE dm.pay_type WHEN 1 THEN 'Установка' WHEN 2 THEN 'Аренда' WHEN 3 THEN 'Продажа' WHEN 4 THEN 'Снятие с объекта сети' WHEN 5 THEN 'Аренда  в счет тарифа' WHEN 6 THEN 'Установка на пробный период' WHEN 7 THEN 'Продажа, списание со счёта' WHEN 8 THEN 'Продажа на К+' END) WHEN 4  THEN 'Снятие с объекта сети' WHEN 6 THEN 'Замена' WHEN 7 THEN 'Снято с объекта на склад' WHEN 8 THEN 'Регистрация ранее установленных материалов' WHEN 9 THEN 'Передача материалов между логинами' END"
  end

  def inst_type_name
    case inst_type.to_i
    when 1
      "Установка"
    when 2
      "Установка на объект"
    when 4
      'Снятие с объекта сети'   
    when 6
      'Замена'
    when 7
      'Снято с объекта на склад'
    when 8
      'Регистрация ранее установленных материалов'
    when 9
      'Передача материалов между логинами'     
    end
  end


  private

  def set_date
      self.date = Date.today
  end

  def set_date
     self.date = Date.today
  end

  def set_values
    self.material_id = self.material_id
    self.employee_name = self.employee.login if self.employee && self.employee_name.to_s.blank?
    self.price = 0 unless [3, 7, 8].include? self.pay_type.to_i 
    self.bonus_sale = 0 unless [3, 7, 8].include? self.pay_type.to_i
    self.department = self.report.department if !department && report
  end

end
