# -*- coding: utf-8 -*-
class DoneEmployee < ActiveRecord::Base

  def self.table_name()    
        db_name = Rails.configuration.database_configuration[Rails.env]["database"]
       db_name+".done_employees"
    #"done_employees"
  end


  default_scope :include => [:employee]


  belongs_to :report
  belongs_to :employee, :foreign_key => "empl_id"
  before_save :set_values

  attr_accessor_with_default(:hand_sum){ (report.sum.to_f * self.hand.to_f) if report}

  named_scope :in_month, lambda { |date| date.present? ? { :include => :report, :conditions => ['(reports.status != 1) AND DATE(reports.date) >= DATE(?) AND DATE(reports.date) <= 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_date, lambda { |date| date.present? ? { :include => :report, :conditions => ['reports.status != "1" AND DATE(reports.date) = DATE(?)', date.to_date] } :{} }
  named_scope :in_month_without_time, lambda { |date| date.present? ? { :include => :report, :conditions => ['(reports.status = 2 OR reports.status IS NULL) AND date_create_report >= ? AND date_create_report <= ?', date.to_date.beginning_of_month, date.to_date] } :{} }
  named_scope :with_empl, lambda { |empl| empl.present? ? { :conditions => ['empl_id = ?', empl] } : { } }
  named_scope :soft, :conditions => "done_employees.department = 'soft'"
  named_scope :priv, :conditions => "done_employees.department = 'private'"
  named_scope :mod, :conditions => "done_employees.department = 'mod'"
  named_scope :corp, :conditions => "done_employees.department = 'corp'"
  named_scope :with_category, lambda { |category| category.present? ? { :include => :report, :conditions => ['reports.category = ?', category] }: {} }



 def work_hours
   date_r = Date.today.to_s
   (((date_r+ " " + time_stopwork).to_time - (date_r + " " + time_addwork).to_time) / 3600.0)
 end


 def dop_work_hours_mod
   m = (report.date.to_date.to_s + " " + time_stopwork).to_time - ((report.date.to_date.to_s + " 19:00").to_time >  (report.date.to_date.to_s + " " + time_addwork).to_time ? (report.date.to_date.to_s + " 19:00").to_time : (report.date.to_date.to_s + " " + time_addwork).to_time ) 

   e = (report.date.to_date.to_s + " 10:00").to_time - (report.date.to_date.to_s + " " + time_addwork).to_time
   c = (m > 0 ? m : 0) + (e > 0 ? e : 0)
   if c > 0
     return c / 3600.0
   else
     0
   end
 end

 def dop_work_hours_corp
   m = (report.date.to_date.to_s + " " + time_stopwork).to_time - ((report.date.to_date.to_s + " 18:00").to_time >  (report.date.to_date.to_s + " " + time_addwork).to_time ? (report.date.to_date.to_s + " 18:00").to_time : (report.date.to_date.to_s + " " + time_addwork).to_time ) 
   e = (report.date.to_date.to_s + " 09:00").to_time - (report.date.to_date.to_s + " " + time_addwork).to_time
   c = (m > 0 ? m : 0) + (e > 0 ? e : 0)
   if c  > 0
     return c / 3600.0
   else
     0
   end
 end



 def work_in_holiday?
   w_day = WorkingDay.find(:first, :conditions => "month = #{report.date.to_date.beginning_of_month}")
   if w_day && w_day.weekends.split(", ").include?(report.date.day.to_s)
     return true     
   end
 end


  private

  def set_values
    if self.report
   self.date_create_report = report.date.to_datetime if self.date_create_report.to_s.blank?
   self.department = report.department
   @empl = Employee.find(self.empl_id) if self.empl_id.to_i != 0
   self.empl_name = @empl.login if @empl
   self.time_work = self.work_hours if  (self.department == 'mod' || self.department == 'corp') && (self.report.dhcp_login == 'new')
   self.ented_intime = self.dop_work_hours_mod  if  self.department == 'mod' 
   self.ented_intime = self.dop_work_hours_corp  if  self.department == 'corp'
   self.hand = 1 if self.hand.to_f == 0 
   self.sum = report.reload.sum.to_f * self.hand.to_f   
 end
  end



end
