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

class Admin::PStatsController < ApplicationController
  layout "admin"
  before_action :auth_admin

  before_action :admin_space
  

  def admin_space
    @admin_space = "statistiques"
  end

  def index
    
    
 
    
    
    date_regex = /^(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20)\d\d$/i 
    
    if params[:start] and params[:start] =~ date_regex
  		@start = Date.parse(params[:start]).beginning_of_day 
  		
		else
  		@start = Date.today.beginning_of_month
		end
    params[:start]= @start.strftime('%d/%m/%Y')
    
    
		if params[:stop] and params[:stop] =~ date_regex
			@stop = Date.parse(params[:stop]).end_of_day 
			
	  else
		 	@stop = Date.today.end_of_month
		end
    params[:stop]= @stop.strftime('%d/%m/%Y')
    
    
    
    
    @p_customer_sheets = PCustomerSheet.where("created_at >= ? and created_at <= ?",@start, @stop).order("id DESC").all

    @sans_marges = PSheetLine.where(:p_customer_sheet_id => @p_customer_sheets.ids).where(:externe => false, :stock_done => false, :shiped => true)

    @finished = @p_customer_sheets.where(:state => ["livrée","facturée"])
   
  end

  
  
  
end