.qi_header
  
  -if params[:offre].to_s != "true"
    
    -if current_admin.has_permission?("customers-c")
      .right= link_to ic(:plus)+' Ajouter un client', new_admin_p_customer_path(), :class => "btn btn-primary btn-ap-add", :remote => false
  -else
    .right
      =link_to "< Retour aux demandes de commande", admin_p_customer_sheets_path
  
  %h1
    Clients
 
  


.qi_search_row
  =form_tag "", :method => "get",  :onsubmit => "" do
    -params[:search] =params[:search] || {}
    %table
      %tr
        =hidden_field_tag :offre,  params[:offre]
        
        %td=text_field_tag :code, params[:code],:class => "form-control", :placeholder => "Code client"
        
        %td=text_field_tag :name, params[:name],:class => "form-control", :placeholder => "Nom"
        %td=text_field_tag :city, params[:city],:class => "form-control", :placeholder => "Ville"
        
        %td=text_field_tag :tel, params[:tel],:class => "form-control", :placeholder => "Tel"
        
        %td
          Actif :
          =select_tag "search[enabled]", options_for_select([[""], "Oui", "Non"], params[:search][:enabled])
        
        %td
          Raison de blocage :
          =select_tag "search[disabled_raison]", options_for_select(PCustomer.group("disabled_raison").map{|p| [p.disabled_raison]}, params[:search][:disabled_raison])
        
        
        -if false
          %td=text_field_tag :document_number, params[:document_number],:class => "form-control", :placeholder => "N° document"
    
    
    
    %table
      %tr

        %td
          Catégorie :
          =select_tag "search[p_customer_cat_id]", options_for_select([["",""],["Aucune","null"]]+PCustomerCat.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:p_customer_cat_id])
    
        -if false
          %td
            Commercial :
            =select_tag "search[p_commercial_id]", options_for_select([["",""],["Aucun","null"]]+PCommercial.order(:name).all.map{|a| [a.member_label, a.id]}, params[:search][:p_commercial_id])
    
        
        

      
      
  
    =render :partial => "qi/qi_ordered_table_search_footer", :locals => {:collection_object => @p_customers, :csv => true}
    
-if false

  .qi_kpi
  
    -params[:active_kpi] = [] if params[:active_kpi].to_s == ""
    -if params[:kg]
      =link_to params.permit!.merge({:kg => nil}) do
        .kpi.align_center{:style => "background:#296695;"}
          .desc
            mode
          .number
            KG
    -else
      =link_to params.permit!.merge({:kg => true}) do
        .kpi.align_center{:style => "background:#296695;"}
          .desc
            mode 
          .number
            €
  
  
    -if non_verifie = @all_p_customers.where(:disabled_raison => "Pas encore vérifié") and non_verifie.count > 0
  
      =link_to params.permit!.merge({:active_kpi => (params[:active_kpi]+["non-verifie"]).uniq, :p_customer_ids => non_verifie.ids.uniq}), :class => ("active" if params[:active_kpi].include?("non-verifie")) do
        .kpi{:style => "background:#ffc107;"}
          .number
            =non_verifie.count

          .label

          .desc
            client(s) en attente de validation
  
    -p_customer_sheets = PCustomerSheet.where(:state => ["brouillon"]).where(:p_customer_id => @all_p_customers.ids)
  
    =link_to params.permit!.merge({:active_kpi => (params[:active_kpi]+["commandes-en-attente"]).uniq, :p_customer_ids => p_customer_sheets.select(:p_customer_id).map{|p| p.p_customer_id}.uniq}), :class => ("active" if params[:active_kpi].include?("commandes-en-attente")) do
      .kpi{:style => "background:#3B7DDD;"}
        .number
          -if params[:kg]
            =number_with_delimiter(p_customer_sheets.sum(:cc_weight_tot).to_i, :delimiter => " ", :separator => ",")
            Kg
        
          -else
            =number_to_currency (p_customer_sheets.sum(:cc_tot_amount_ht))

        .label

        .desc
          =p_customer_sheets.count
          commandes en attente de validation
  
  
    
    -price_documents = PriceDocument.where(:price_document_type => [4]).where(:p_customer_id => @all_p_customers.ids).where(:cc_solded => false)
  
  
    =link_to params.permit!.merge({:active_kpi => (params[:active_kpi]+["Factures non soldées"]).uniq, :p_customer_ids => price_documents.select(:p_customer_id).map{|p| p.p_customer_id}.uniq}), :class => ("active" if params[:active_kpi].include?("Factures non soldées")) do
      .kpi{:style => "background:#f6c143;"}
    
        .number
          -if params[:kg]
            =number_with_delimiter(price_documents.sum(:cc_weight_tot).to_i, :delimiter => " ", :separator => ",")
            Kg
        
          -else
            =number_to_currency (price_documents.sum(:cc_tot_amount_ht))
      

        .label

        .desc
          =price_documents.count
          factures restantes à payer
   
  
    -price_documents = PriceDocument.where(:price_document_type => [4]).where(:p_customer_id => @all_p_customers.ids).where(:cc_solded => false).where("cc_payment_end_at < ?", Date.today)
  
   
    =link_to params.permit!.merge({:active_kpi => (params[:active_kpi]+["Factures en retard"]).uniq, :p_customer_ids => price_documents.select(:p_customer_id).map{|p| p.p_customer_id}.uniq}), :class => ("active" if params[:active_kpi].include?("Factures en retard"))  do
   
      .kpi.no_left_margin{:style => "background:#dd6b5a;"}
    
        .number
          -if params[:kg]
            =number_with_delimiter(price_documents.sum(:cc_weight_tot).to_i, :delimiter => " ", :separator => ",")
            Kg
        
          -else
            =number_to_currency (price_documents.sum(:cc_tot_amount_ht))

        .label

        .desc
          dont
          =price_documents.count
          factures en retard de paiement
  
  
    -start_date = Date.today.beginning_of_year - 1.year
    -end_date = start_date.end_of_year
    -price_documents = PriceDocument.where(:price_document_type => [4,5]).where("p_customer_id in(?) "+("or p_customer_id is null" if !params[:p_customer_ids] and (@all_p_customers.size.to_i == PCustomer.count.to_i)).to_s, @all_p_customers.ids).where("date >= ? and date <= ?", start_date, end_date)
   
    -if params[:kg]
      -tot_last = price_documents.sum(:cc_weight_tot)# + OrderHistLine.where(:p_customer_id => @all_p_customers.ids).where("date_piece >= ? and date_piece <= ?", start_date, end_date).sum(:qte)
      
    -else
      -tot_last = price_documents.sum(:cc_tot_amount_ht)# + OrderHist.where(:p_customer_id => @all_p_customers.ids).where("date_piece >= ? and date_piece <= ?", start_date, end_date).sum(:montant_ht_remise)
    
  
  
    -start_date = Date.today.beginning_of_year
    -end_date = Date.today.end_of_year
  
    
    -price_documents = PriceDocument.where(:price_document_type => [4,5]).where("p_customer_id in(?) "+("or p_customer_id is null" if !params[:p_customer_ids] and (@all_p_customers.size.to_i == PCustomer.count.to_i)).to_s, @all_p_customers.ids).where("date >= ? and date <= ?", start_date, end_date)
    -if params[:kg]
      -tot = price_documents.sum(:cc_weight_tot)# + OrderHistLine.where(:p_customer_id => @all_p_customers.ids).where("date_piece >= ? and date_piece <= ?", start_date, end_date).sum(:qte)
      
    -else
      -tot = price_documents.sum(:cc_tot_amount_ht)# + OrderHist.where(:p_customer_id => @all_p_customers.ids).where("date_piece >= ? and date_piece <= ?", start_date, end_date).sum(:montant_ht_remise)
    
  
  
    .kpi{:style => "background:#80cc9a;"}
      .number
        -if params[:kg].to_s != ""
        
          =number_with_delimiter(tot.to_i, :delimiter => " ", :separator => ",")
          Kg
        
        -else
          =number_to_currency (tot)
        
  
      .label
      
      .desc
        =Date.today.year
        ="-"
        =number_to_percentage (tot/tot_last )*100.0, :precision => 2
        de 
        =Date.today.year - 1
  
    .kpi.no_left_margin{:style => "background:#57a36d;"}
      .number
        -if params[:kg]
          =number_with_delimiter(tot_last.to_i, :delimiter => " ", :separator => ",")
          Kg
        
        -else
          =number_to_currency (tot_last)
  
      .label
     
      .desc
        =Date.today.year - 1
  
  
  
  
    .clear
  
    -if params[:p_customer_ids].to_s != ""
      .custom_customers
        Affichage des résultats spécifiques pour 
        =params[:p_customer_ids].size
        client(s)
    
        ="-"
        =link_to ic(:times)+" Supprimer ce filtre", params.permit!.merge({:active_kpi => nil, :p_customer_ids => nil})
    
  
=render :partial => "qi/qi_ordered_table", :locals => {:qi_ordered_table_collection => @p_customers}



