# -*- encoding : utf-8 -*-

class Admin::SheetsController < ApplicationController
  
  before_filter :auth_admin
 
  layout "admin"

  require 'csv'
#require 'iconv'

#  require_permission 'adherent.show'
#  require_permission 'adherent.add', :only => [:new, :create]
#  require_permission 'adherent.edit', :only => [:edit, :update]
#  require_permission 'adherent.delete', :only => [:destroy]
    



	def recus
		date = Date.today
		date = DateTime.new(2014, 1, 1).beginning_of_year
		
		@sheet_years = SheetYear.find(:all, :conditions => ["paid_at >= ? and paid_at <= ?",date ,date.end_of_year])
		@donates = Donate.find(:all, :conditions => ["paid_at >= ? and paid_at <= ?",date ,date.end_of_year])
		render :layout => false
	end
	


  def index


    if !params[:sheet_filter] and session[:sheet_filter_index] 
      params[:sheet_filter] =  session[:sheet_filter_index] 
    end

    if params[:sheet_filter]
      session[:sheet_filter_index] = params[:sheet_filter]



      
      
    end

    if params[:sheet_filter]
      @sheets =  Sheet.search_by_params(params[:sheet_filter])
    else
      @sheets = Sheet.all(:limit => 100)
    end

    

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @sheets }
      format.js {}
	  	format.csv { send_data @sheets.to_csv2 }
      format.xls 

	
    end
  end


  def show
    @sheet = Sheet.find(params[:id])

  end


  def new
    @sheet = Sheet.new
    @sheet.people.build
  
  end

  def edit
    @sheet = Sheet.find(params[:id])
    respond_to do |format|
      format.html # new.html.erb
      format.js { render :layout => false }
    end
  end


  def create
    @sheet = Sheet.new(params.require(:sheet).permit!)

    respond_to do |format|
      if @sheet.save
        flash[:notice] = 'Sheet was successfully created.'
      
        format.js { render :action => "show" }
      else
        format.html { render :action => "new" }
        format.js { render :action => "new" }
        format.xml  { render :xml => @sheet.errors, :status => :unprocessable_entity }
      end
    end
  end


  def update
    @sheet = Sheet.find(params[:id])


      if @sheet.update_attributes(params.require(:sheet).permit!)
        @sheet = Sheet.find(params[:id])

      else
        
      end
  
  end


  def destroy
    @sheet = Sheet.find(params[:id])
    @sheet.destroy

    redirect_to(admin_sheets_url)
    
  end
  
  
  def stats

   
    
    @year = params[:year].to_i || Date.today.year
    
    @start_date = Date.new(@year, 1, 1 )
    @end_date = Date.new(@year, 12, 31 )


    @gift_years = {
      :nbr_foyers => SheetYear.count(:conditions => ['year = ? AND gift_year = ? ',@year, true]), 
      :nbr_familles => SheetYear.count(:conditions => ['year = ? AND gift_year = ? AND join_type = ?',@year, true,2]), 
      :nbr_associations =>  SheetYear.count(:conditions => ['year = ? AND gift_year = ? AND join_type = ?',@year, true,3]), 
      :nbr_individuel => SheetYear.count(:conditions => ['year = ? AND gift_year = ? AND join_type = ?',@year, true,1]), 
      :nbr_benefactors =>  SheetYear.count(:conditions => ['year = ? AND gift_year = ? AND benefactor = ?',@year, true,true])
  
      }
    @gift_years[:pouvoirs] = (@gift_years[:nbr_familles] + @gift_years[:nbr_associations])*2 + @gift_years[:nbr_individuel]
      
      
      @renewed_prev_year = {
        :nbr_foyers => SheetYear.count(:conditions => ['year = ? AND paid_at < ? ',@year, @start_date]), 
        :nbr_familles => SheetYear.count(:conditions => ['year = ? AND  paid_at < ?  AND join_type = ?',@year, @start_date,2]), 
        :nbr_associations =>  SheetYear.count(:conditions => ['year = ? AND  paid_at < ? AND join_type = ?',@year, @start_date,3]), 
        :nbr_individuel => SheetYear.count(:conditions => ['year = ? AND  paid_at < ?  AND join_type = ?',@year, @start_date,1]), 
        :nbr_benefactors =>  SheetYear.count(:conditions => ['year = ? AND  paid_at < ?  AND benefactor = ?',@year, @start_date,true])

        }
     @renewed_prev_year[:pouvoirs] = (@renewed_prev_year[:nbr_familles] + @renewed_prev_year[:nbr_associations])*2 + @renewed_prev_year[:nbr_individuel]
    @months= {}
    @months["0"] = {
      :cumul_foyers => @gift_years[:nbr_foyers].to_i + @renewed_prev_year[:nbr_foyers].to_i,
      :cumul_familles => @gift_years[:nbr_familles].to_i + @renewed_prev_year[:nbr_familles].to_i,
      :cumul_associations => @gift_years[:nbr_associations].to_i + @renewed_prev_year[:nbr_associations].to_i,
      :cumul_individuel => @gift_years[:nbr_individuel].to_i + @renewed_prev_year[:nbr_individuel].to_i,
      :cumul_benefactors => @gift_years[:nbr_benefactors].to_i + @renewed_prev_year[:nbr_benefactors].to_i,
      :cumul_pouvoirs => @gift_years[:pouvoirs].to_i + @renewed_prev_year[:pouvoirs].to_i
    }
    
    @foyers_to_stats = []
    @familles_to_stats  = []
    @associations_to_stats  = []
    @individuel_to_stats  = []
    @benefactors_to_stats  = []
    @pouvoirs_to_stats  = []

    month = @start_date
    while month <= @end_date
      @months[month.month.to_s] = {
        :month => month.month,
        :nbr_foyers => SheetYear.count(:conditions => ['year = ? AND paid_at >= ? and paid_at <= ? ',@year, month, month.end_of_month]), 
        :nbr_familles => SheetYear.count(:conditions => ['year = ? AND  paid_at >= ? and paid_at <= ?  AND join_type = ?',@year, month, month.end_of_month,2]), 
        :nbr_associations =>  SheetYear.count(:conditions => ['year = ? AND paid_at >= ? and paid_at <= ? AND join_type = ?',@year, month, month.end_of_month,3]), 
        :nbr_individuel => SheetYear.count(:conditions => ['year = ? AND  paid_at >= ? and paid_at <= ? AND join_type = ?',@year, month, month.end_of_month,1]), 
        :nbr_benefactors =>  SheetYear.count(:conditions => ['year = ? AND  paid_at >= ? and paid_at <= ? AND benefactor = ?',@year, month, month.end_of_month,true])

        }
        
      @months[month.month.to_s][:pouvoirs] = (@months[month.month.to_s][:nbr_familles] + @months[month.month.to_s][:nbr_associations])*2 + @months[month.month.to_s][:nbr_individuel]
      
      @months[month.month.to_s][:cumul_foyers] = @months[(month.month-1).to_s][:cumul_foyers].to_i + @months[month.month.to_s][:nbr_foyers].to_i
      @months[month.month.to_s][:cumul_familles] = @months[(month.month-1).to_s][:cumul_familles].to_i + @months[month.month.to_s][:nbr_familles].to_i
      @months[month.month.to_s][:cumul_associations] = @months[(month.month-1).to_s][:cumul_associations].to_i + @months[month.month.to_s][:nbr_associations].to_i
      @months[month.month.to_s][:cumul_individuel] = @months[(month.month-1).to_s][:cumul_individuel].to_i + @months[month.month.to_s][:nbr_individuel].to_i
      @months[month.month.to_s][:cumul_benefactors] = @months[(month.month-1).to_s][:cumul_benefactors].to_i + @months[month.month.to_s][:nbr_benefactors].to_i
      @months[month.month.to_s][:cumul_pouvoirs] = @months[(month.month-1).to_s][:cumul_pouvoirs].to_i + @months[month.month.to_s][:pouvoirs].to_i
      
      @foyers_to_stats << @months[month.month.to_s][:cumul_foyers]
      @familles_to_stats  << @months[month.month.to_s][:cumul_familles]
      @associations_to_stats  << @months[month.month.to_s][:cumul_associations]
      @individuel_to_stats << @months[month.month.to_s][:cumul_individuel]
      @benefactors_to_stats << @months[month.month.to_s][:cumul_benefactors]
      @pouvoirs_to_stats  << @months[month.month.to_s][:cumul_pouvoirs]
      
      month = month.next_month
    end


    chart_months = ["Jan.", "Fév.", "Mar.", "Avr.", "Mai", "Juin", "Juil.", "Août", "Sep.", "Oct.", "Nov.", "Dec."]
    
    #chart_months = ["01", "02", "03","04","05","06","07","08","09","10","11","12"]
    
    
    
    @chart_values = []
    
    i = 0
    chart_months.each do |month|
        
        @chart_values << {
          y: month.to_s,
          foyers: @foyers_to_stats[i],
          familles:@familles_to_stats[i],
          associations:@associations_to_stats[i],
          individuel:@individuel_to_stats[i],
          bienfaiteurs:@benefactors_to_stats[i],
          pouvoirs:@pouvoirs_to_stats[i]
    
	
          
        }
        i = i+1
    end
  
  
  
  
  
  
   end
  
  
end