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

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

  before_action :admin_space
  
  def admin_space
    @admin_space = "stocks"
  end
  
  def index
    
    @title ="Références"
    
    @p_product_refs = PProductRef.joins(:p_product)
    
    @purchase_p_product_cats = PProductCat.where(:purchase => true).order(:name).all
    

    
    if params[:search][:code].to_s != ""
      @p_product_refs = @p_product_refs.where("p_product_refs.ref LIKE ?","#{params[:search][:code]}%")
    end
    
    if params[:search][:fournisseur_ref].to_s != ""
      @p_product_refs = @p_product_refs.joins(:fournisseur_product_refs).where("fournisseur_product_refs.ref LIKE ?","%#{params[:search][:fournisseur_ref]}%")
    end
  
    if params[:search][:name].to_s != ""
      @p_product_refs = @p_product_refs.where("p_product_refs.ct_sub_name LIKE ? or p_product_refs.cc_name LIKE ?","%#{params[:search][:name]}%", "%#{params[:search][:name]}%")
    end
    
    if params[:search][:p_product_ref_cat_id].to_s != ""
      if params[:search][:p_product_ref_cat_id].to_s == "null"
        @p_product_refs = @p_product_refs.where(:cc_p_product_cat_id => nil)
      else
        @p_product_refs = @p_product_refs.where(:cc_p_product_cat_id => params[:search][:p_product_ref_cat_id])
      end
    end
    
    
    if params[:search][:s_brand_id].to_s != ""
      @p_product_refs = @p_product_refs.where(:s_brand_id => params[:search][:s_brand_id])
    end
    
    @p_product_refs = sort_by_sorting(@p_product_refs, "id DESC")
    respond_to do |format|
      format.html{
      
        params[:search][:per_page] = params[:search][:per_page] || 100
    		per_page = params[:search][:per_page]
    		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
    		@p_product_refs = @p_product_refs.page(page).per(per_page)
      
      }
    end
    
   
  end

  def show
    @p_product_ref = PProductRef.find(params[:id])

  end

  def new
    @p_product_ref = PProductRef.new
  
  end

  def edit
    @p_product_ref = PProductRef.find(params[:id])
    
  end

  def create
    @p_product_ref = PProductRef.new(params.require(:p_product_ref).permit!)

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

  end


  def update
    @p_product_ref = PProductRef.find(params[:id])


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


  def destroy
    @p_product_ref = PProductRef.find(params[:id])
    @p_product_ref.destroy
    
  end
  
  def autocomplete
    @p_product_refs = PProductRef.for_search(params[:search]).limit(50)
    
    if params[:p_customer_id]
      @p_customer = PCustomer.where(:id => params[:p_customer_id]).first
      #@p_product_refs = @p_product_refs.where("p_products.p_customer_id IS NULL or p_products.p_customer_id = ? ", @p_customer.id).joins(:p_customer_cats).where(:p_customer_cats => {:id => @p_customer.p_customer_cat_id})
    end
    
    @p_product_refs = @p_product_refs.where(:p_products => {:enabled => true})
    
    if params[:not_assembled].to_s == "true"
    
      @p_product_refs = @p_product_refs.not_assembleds
      
    end
    
    result = []
    
    
    @p_product_refs.distinct.all.each do |p_product_ref|
      result << {:member_label => p_product_ref.member_label, :id => p_product_ref.id }
    end
    
    respond_to do |format|
        format.json { render json: result}
    end
  end
  
  
  def get_infos

    @p_product_ref = PProductRef.find(params[:id])
    
    if params[:type] == "price"
      price = @p_product_ref.get_price(:p_customer_id => params[:p_customer_id], :qte => params[:qte])
      


      if params[:p_customer_id].to_s != ""
        p_customer = PCustomer.find(params[:p_customer_id])
        accounting_zone_id = p_customer.accounting_zone_id

        if p_customer.tva_rate
          tva_account_id = p_customer.tva_rate.id
          tva_account_value = p_customer.tva_rate.rate

        else
          tva_account_id = @p_product_ref.p_product.tva_rate(accounting_zone_id).id if @p_product_ref.p_product.tva_rate(accounting_zone_id)
          tva_account_value = @p_product_ref.p_product.tva_rate(accounting_zone_id).rate if @p_product_ref.p_product.tva_rate(accounting_zone_id)


        end

      elsif params[:p_fournisseur_id].to_s != ""
        p_fournisseur = PFournisseur.find(params[:p_fournisseur_id])
        accounting_zone_id = p_fournisseur.accounting_zone_id
        
        if p_fournisseur.tva_rate
          tva_account_id = p_fournisseur.tva_rate.id
          tva_account_value = p_fournisseur.tva_rate.rate
        
        else
          tva_account_id = @p_product_ref.p_product.tva_rate(accounting_zone_id).id if @p_product_ref.p_product.tva_rate(accounting_zone_id)
          tva_account_value = @p_product_ref.p_product.tva_rate(accounting_zone_id).rate if @p_product_ref.p_product.tva_rate(accounting_zone_id)
      
          
        end
        
      end
      puts "TEST"
      puts accounting_zone_id
      
      
      
      
      respond_to do |format|
          format.json { render json: {:p_product_cat_id =>(@p_product_ref.p_product.p_product_cat_id if @p_product_ref.p_product), :weight => @p_product_ref.weight, :price => price, :tva_account_id => tva_account_id, :tva_account_value => tva_account_value} }
      end
    end
      
      
  end
  
  def autocomplete_apercu
    @p_product_ref = PProductRef.find(params[:id])
    
  end
  
end
