# -*- 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 
  		
    elsif !params[:start]
  		@start = Date.today.beginning_of_year
    else
      @start = Date.parse("2000-01-01")
		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"])
   
   
   
    params[:search][:per_page] = params[:search][:per_page] || 500000
    per_page = params[:search][:per_page]
    page = (params[:page] and params[:page] != "") ? params[:page] : 1 
    
    @m_odr_primes = MOdrPrime.where("m_odr_primes.created_at >= ? and m_odr_primes.created_at < ?",@start.beginning_of_year, @stop).where(:state => ["Virement envoyé"])
    @m_odr_primes = @m_odr_primes.joins(:p_customer)
    @m_odr_primes = @m_odr_primes.where("m_odr_primes.created_at >= ?", @start)
    @m_odr_primes = sort_by_sorting(@m_odr_primes, "m_odr_primes.created_at DESC")
    @m_odr_primes = @m_odr_primes.page(page).per(per_page)
   
     
    respond_to do |format|
      format.html{
    

    
        params[:per_page] = params[:per_page] || 50
    
    		per_page = params[:per_page]
    		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
    		@m_odr_primes = @m_odr_primes.page(page).per(per_page)
    
      }

      format.csv { 
          @headers = []
          @columns = []
          MOdrPrime.qi_table_order.each do |key, value|
          
            if value.instance_of? Hash
              name = value[:name]
            else
              name = value
            end
          
            if name != "Actions"
              @headers << name.to_s
              @columns << key
            end
          
          end
    
    
    
          xlsx_package = Axlsx::Package.new
            wb = xlsx_package.workbook
            wb.add_worksheet(name: "import") do |sheet|
              sheet.add_row @headers
              
              @m_odr_primes.each do |m_odr_prime|
                line = []
            
                @columns.each do |column|
              
                  if (m_odr_prime.respond_to?("csv_"+column.to_s))
                    line << m_odr_prime.send("csv_"+column.to_s)
                  elsif (m_odr_prime.respond_to?(column))
                    if m_odr_prime.send(column.to_s).class.to_s == "BigDecimal"
                      line << m_odr_prime.send(column.to_s).to_s.gsub('.', ',')
                    else
                      line << m_odr_prime.send(column.to_s)
                    end
                  else
                    line << column.to_s
                  end
              
                end
            
                
                sheet.add_row line, types: line.map{|t| self.cell_type_from_value(t)}
              end
              
              
            end
            
            @final_file = "#{Rails.root}/private_medias/export-primes-#{Time.now.to_s.to_slug}.xlsx"
      

            xlsx_package.serialize(@final_file)  
  
        
            send_file @final_file
        
  
  
        }

    end
   
   
   
  end

  
  
  
end
