# -*- encoding : utf-8 -*-
class SheetYear < ActiveRecord::Base
  belongs_to :sheet
  
  validates_presence_of :year
  
  attr_accessible :year, :join_type, :benefactor, :gift_year, :paid_at, :amount, :payment_type, :bank, :check_number, :sheet_id, :plume
  
  JOIN_TYPE = {1 => "Individuel", 2 => "Familial", 3 =>"Association"}
  PAYMENT_TYPE = {1 => "Chèque", 2 => "Liquide"}
  
  
	 after_save do
		sy = self.sheet.sheet_years.find(:first, :order => "year DESC")
		if sy
			self.sheet.last_year = sy.year
			self.sheet.first_year = self.sheet.sheet_years.find(:first, :order => "year ASC").year
			

      if sy.plume
        self.sheet.plume = true
      else
        self.sheet.plume = false
      end
      
      self.sheet.save

      
  
		end



	end


	after_destroy do
		sy = self.sheet.sheet_years.find(:first, :order => "year DESC")
	    if sy
  			self.sheet.last_year = sy.year
  			self.sheet.first_year = self.sheet.sheet_years.find(:first, :order => "year ASC").year
  			

        if sy.plume
          self.sheet.plume = true
        else
          self.sheet.plume = false
        end

        
        
      else
        self.sheet.plume = false
	    end
      
      self.sheet.save
		
	end


end
