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

class Admin::PProductsController < ApplicationController
  layout "admin"
  before_filter :auth_admin
  
  before_filter :admin_space
  
  def admin_space
    @admin_space = "ventes"
  end
  
  def export
    @p_products = PProduct.where(:archived => false, :enabled => true).all
    
    @file = ""

    @headers = [
      "ID Sys",
      "Ref produit",
      "Catégorie",
      "Nom",
      "Description",
      "Grammage",
      "Origine",
      "Certifications",
      "Qté/cartons",
      "Ref variante",
      "Taille",
      "Couleur"
 
    ]   
    
    @csv = CSV.generate(:headers => true, :col_sep => ";") do |csv|
      csv << @headers
      
      @p_products.each do |p_product|
        p_product.p_product_stocks.each do |pps|
          line = []
          
          line << p_product.id
          line << p_product.code
          line << (p_product.p_product_cat ? p_product.p_product_cat.name : "")
          line << p_product.name
          line << p_product.description
          if p_product.p_ep
            line << p_product.p_ep.ep
          else
            line << ""
          end
          if p_product.p_origine
            line << p_product.p_origine.name
          else
            line << ""
          end
          line << p_product.p_certifs.all.map{|pc| pc.name}.join(",")
          line << p_product.p_qtes.all.map{|pq| pq.qte}.join(",") 
          line << pps.code
          line << (pps.p_color ? pps.p_color.name : "")
          line << (pps.p_size ? pps.p_size.name : "")
              
          csv << line
          
        end
   
      end

    end
    
    
    @data_to_send = @csv.encode('iso-8859-1', :undef => :replace, :replace => '')
    
    file_path = Rails.root.join("pdf", "export.csv")
    
    File.open(file_path, "w+") do |f|
      f.write(@data_to_send)
    end
    
    respond_to do |format|
          format.html{
            require 'csv'    

            csv_text = File.read(file_path).encode('utf-8')
            @csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")    
          }
          format.csv {
            send_file file_path, :filename => "export-produits.csv", :type => 'text/csv; charset=iso-8859-1; header=present' 
        
          }
    end
    
    
    
    
  end
  
  def export_prix
    @p_products = PProduct.where(:archived => false, :enabled => true).all
    
    @file = ""

    @headers = [
      "ID Sys",
      "Ref produit",
      "Nom",
      "Ref variante",
      "Qte min.",
      "Prix U HT"
 
    ]   
    
    @csv = CSV.generate(:headers => true, :col_sep => ";") do |csv|
      csv << @headers
      
      @p_products.each do |p_product|
        p_product.p_product_stocks.each do |pps|
          line = []
          
          line << p_product.id
          line << p_product.code
          line << p_product.name
          line << pps.code
          line << 500
 
          p_price_cat = PPriceCat.where(:name => "R0").first
     
          p_degressifs = p_product.p_degressifs.joins(:p_price_cats).where(:p_price_cats => {:id => p_price_cat.id}).order("nbr ASC")
  
          p_degressif = p_degressifs.first
          
          line << ((p_degressif and p_degressif.price.to_f > 0.0) ? p_degressif.price : "")
          
          csv << line
          
        end
   
      end

    end
    
    
    @data_to_send = @csv.encode('iso-8859-1', :undef => :replace, :replace => '')
    
    file_path = Rails.root.join("pdf", "export.csv")
    
    File.open(file_path, "w+") do |f|
      f.write(@data_to_send)
    end
    
    respond_to do |format|
          format.html{
            require 'csv'    

            csv_text = File.read(file_path).encode('utf-8')
            @csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")    
          }
          format.csv {
            send_file file_path, :filename => "export-prix.csv", :type => 'text/csv; charset=iso-8859-1; header=present' 
        
          }
    end
    
    
    
    
  end


  def index
    @p_products = PProduct.joins(:p_product_cat).order("p_product_cats.name ASC, p_products.name ASC").all
    @p_products = @p_products.where(:archived => false)
   
  end

  def show
    @p_product = PProduct.find(params[:id])

  end

  def new
    
    
		@p_product = PProduct.new
    

  end

 

  def edit
    @p_product = PProduct.find(params[:id])
    
  end
  
  
  def stock
    @p_product = PProduct.find(params[:id])
    
  end

  def degressif
    @p_product = PProduct.find(params[:id])
    
  end
  

  def create
    
    if params[:p_product][:create_matiere] == "1"
      
      params[:p_product].delete(:p_product_brut_products_attributes)

    end
    @p_product = PProduct.new(params.require(:p_product).permit!)


    if @p_product.save
      
    else
      render action: "new"
 
    end

  end


  def update
    @p_product = PProduct.find(params[:id])


    if @p_product.update_attributes(params.require(:p_product).permit!)
      
      
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @p_product = PProduct.find(params[:id])
    @p_product.destroy
    
  end
  
  def autocomplete
    @p_products = PProduct.for_search(params[:search]).limit(50)
    
    
    
    
    respond_to do |format|
        format.json { render json: @p_products }
    end
  end
  
  def autocomplete_apercu
    @p_product = PProduct.find(params[:id])
    
  end
  
  
end
