class Sheet < ActiveRecord::Base
  has_many :people, :dependent => :destroy
  has_many :sheet_years, :order => "year DESC", :dependent => :destroy
  has_many :donates, :order => "paid_at DESC", :dependent => :destroy
  has_one :last_year_m, :order => "year DESC", :class_name => "SheetYear"
  has_one :first_year_m, :order => "year", :class_name => "SheetYear"
  
  
  belongs_to :person, :class_name => "Person", :foreign_key => :first_people
  belongs_to :first_people, :class_name => "Person", :foreign_key => :first_people
  
  SHEET_TYPE = {1 => "adherent",2 => "sympathisant"}
  
  
  def self.search_by_params(input)
    requette = ""
    requette += "(last_year IN(?))" if input[:sheet_year]
    requette += " AND " if input[:sheet_year] and input[:sheet_type]
    requette += "(sheet_type IN(?))" if input[:sheet_type]
    requette += " AND " if (input[:sheet_year] or input[:sheet_type]) and input[:sheet_city]
    requette += "(city IN(?))" if input[:sheet_city]
    conditions = []
    conditions << requette
    conditions << input[:sheet_year] if input[:sheet_year]
    conditions << input[:sheet_type] if input[:sheet_type]
    conditions << input[:sheet_city] if input[:sheet_city]
    
    
    
    return Sheet.all(:conditions => conditions,:include => :person)
    
  end
  
end
