81 lines
2.3 KiB
Ruby

# -*- encoding : utf-8 -*-
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
validates :sheet_type, :presence => true
#attr_accessible :donates_attributes, :sheet_type, :people_attributes, :sheet_years_attributes, :corporate, :address, :address2, :address3, :pc, :city, :country, :skills, :other_mail, :other_phone
accepts_nested_attributes_for :people, :allow_destroy => true
accepts_nested_attributes_for :sheet_years, :allow_destroy => true
accepts_nested_attributes_for :donates, :allow_destroy => true
SHEET_TYPE = {1 => "adherent",2 => "sympathisant"}
after_create do
self.generate_sheet_number
self.generate_token
self.save
end
def generate_sheet_number
self.sheet_number = ("%04d" % self.id)+("%03d" % SecureRandom.random_number(999))
end
def generate_token
self.token = (Digest::MD5.hexdigest "#{SecureRandom.hex(20)}-#{DateTime.now.to_s}")
end
before_save do
if !self.person and self.people.size > 0
self.person = self.people.first
end
end
def self.to_csv2(options = {})
puts "test"
CSV.generate(options) do |csv|
csv << ["hhgghgh", "jhhgghgh"]
end
end
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