.qi_header
  .right{:style => "text-align:right;"}
    = link_to i(:pencil), edit_admin_p_customer_path(@p_customer), :remote => false
    
    
    
  %h1
    Clients
    %span
      Détail d'un client
    %span
      =link_to [:admin, @p_customer] do
        =@p_customer.code
        =@p_customer.show_name



.qi_row
  .qi_pannel.qi_plain.padding
    .right
      -if !@p_customer.enabled
        .badge.badge-danger
          =ic :lock
          Client bloqué :
          =@p_customer.disabled_raison
    
    
    
    =render :partial => "admin/p_customers/apercu.html.haml", :locals => {:particular => @p_customer.particular, :mask_fin => true}
   


.qi_kpi

  -@all_p_customers = PCustomer.where(:id => @p_customer.id)
  
  -params[:active_kpi] = [] if params[:active_kpi].to_s == ""

  -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 => @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) # + OrderHistLine.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 => @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) # + OrderHistLine.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
=#render :partial => "admin/p_customers/financier.html.haml", :locals => {:p_customer => @p_customer}
    

    
#customer_adresses
  =render(:partial => "admin/particulars/customer_preview", :locals => {:p_customer => @p_customer})
  





.qi_row
  .tab
    %ul.nav.nav-tabs{:role => "tablist"}
      
      
      -params[:tab] = params[:tab] || "offres"
      %li.nav-item=link_to "Demandes de commande", "?tab=offres#tabs", :class => "nav-link"+(" active" if params[:tab] == "offres").to_s
      -if false
        %li.nav-item=link_to "Etat de compte", "?tab=etat#tabs", :class => "nav-link"+(" active" if params[:tab] == "etat").to_s
      
      -if true or current_admin.has_permission?("bills")
        %li.nav-item=link_to "Factures & Avoirs", "?tab=bills#tabs", :class => "nav-link"+(" active" if params[:tab] == "bills").to_s
      -if current_admin.has_permission?("payments")
        %li.nav-item=link_to "Paiements", "?tab=payments#tabs", :class => "nav-link"+(" active" if params[:tab] == "payments").to_s
        
      
      
    .tab-content
      #tab-1.tab-pane.active{:role => "tabpanel"}
        -if params[:tab] == "offres"
          #offres
          
            .qi_tab_header
              .right{:style => "text-align:right;"}
                -if @p_customer.enabled
                  =link_to ic(:plus)+" Demande de commande", new_admin_p_customer_sheet_path(:p_customer_id => @p_customer.id), :class => "btn btn-primary btn-ap-add"

            .clear
       

         
         
         
            -params[:search][:per_page] = params[:search][:per_page] || 50
            -per_page = params[:search][:per_page]
            -page = (params[:page] and params[:page] != "") ? params[:page] : 1 
            -@p_customer_sheets =@p_customer.p_customer_sheets
            -@p_customer_sheets = sort_by_sorting(@p_customer_sheets, "created_at DESC")
            -@p_customer_sheets = @p_customer_sheets.page(page).per(per_page)
    
            =render :partial => "qi/qi_ordered_table", :locals => {:qi_ordered_table_collection => @p_customer_sheets}
         
          
      
        -if params[:tab] == "etat"
          #etat
            .qi_row
              .qi_pannel.qi_plain.padding
                .right
                  = link_to 'Ajouter un paiement', new_admin_p_payment_path(:p_customer_id => @p_customer.id), :class => "btn btn-primary bgbd-payments", :remote => true
    
                %h3 
                  Etat de compte
                
                
                .clear
              
            
                =render :partial => "search_etat_form"
            
        
                -if false
                  -@p_customer.p_compta_elements.destroy_all
                  -@p_customer.price_documents.each do |pd|
                    -pd.save
      
                  -@p_customer.p_payments.each do |pd|
                    -pd.save
    
    
                =render :partial => "admin/p_customers/etat", :locals => {:p_customer => @p_customer}
        
        -if params[:tab] == "bills"
          #bills
            -params[:search][:per_page] = params[:search][:per_page] || 50
            -per_page = params[:search][:per_page]
            -page = (params[:page] and params[:page] != "") ? params[:page] : 1 
          
            -@price_documents = @p_customer.price_documents.where(:price_document_type_id => [4,5]).order("date DESC, created_at DESC")
            -if params[:price_document_ids]
              -@price_documents = @price_documents.where(:id => params[:price_document_ids])
          

            -@price_documents = sort_by_sorting(@price_documents, "created_at DESC")
            -@price_documents = @price_documents.page(page).per(per_page)
            %br
          
            .right
              =link_to ic(:plus)+" Ajouter un avoir", new_admin_price_document_path(:p_customer_id => @p_customer.id, :action_type => "Avoir"), :class => "btn btn-primary bgbd-ventes"
              &nbsp;&nbsp;&nbsp;

            .clear 
            =render :partial => "qi/qi_ordered_table", :locals => {:qi_ordered_table_collection => @price_documents}
      


        
      
        -if params[:tab] == "payments"
          #payments
            .qi_tab_header
              .right{:style => "text-align:right;"}
                = link_to 'Ajouter un paiement', new_admin_p_payment_path(:p_customer_id => @p_customer.id), :class => "btn btn-primary bgbd-payments", :remote => true
          
              .clear
        
            -params[:search][:per_page] = params[:search][:per_page] || 50
            -per_page = params[:search][:per_page]
            -page = (params[:page] and params[:page] != "") ? params[:page] : 1 
            -@p_payments =@p_customer.p_payments
            -@p_payments = sort_by_sorting(@p_payments, "created_at DESC")
            -@p_payments = @p_payments.page(page).per(per_page)
  
            =render :partial => "qi/qi_ordered_table", :locals => {:qi_ordered_table_collection => @p_payments}
    
      


 
  