.qi_header
  .right= link_to 'Ajouter un paiement', new_admin_p_payment_path(), :class => "btn btn-primary", :remote => true
  %h1
    Paiements
    %span
      Relances

-if params[:age]
  .qi_row
    .qi_pannel.qi_plain.padding
      :scss
        .with_border{
          tr,thead,tbody,td,th{
            border:1px solid black !important;
        
          }
          th{
            vertical-align:middle;
            text-align:center;
          }
        }
      -p_customers = PCustomer.where("cache_encours > 0.0").joins(:particular).order("particulars.organisation ASC, particulars.name ASC, particulars.firstname ASC").uniq.limit(500)
      %p
        =p_customers.count
        clients
        
      %table.table.with_border
        %thead
          %tr
            %th{:rowspan => 1, :colspan => 2} Tiers
            %th{:rowspan => 2} Solde du compte
            %th{:rowspan => 1, :colspan => 4, :style => "border-right:3px black solid !important;"} Echus
            %th{:rowspan => 1, :colspan => 2} Non echus
            %th{:style => "border:0 !important;"}
          %tr
            
            %th Client
            %th Cpt.client
            -headers = ["de plus de 90 j","de 61 à 90 j","de 31 à 60 j", "de 0 à 30 j", "de 1 à 30 j","de plus de 30 j"]
            -headers.each do |h|
              %th{:style => ("border-right:3px black solid !important;" if h == "de 0 à 30 j")}= h
            
            %th{:style => "border:0 !important;"}
           
        -problemes = 0
        -p_customers.all.each do |p_customer|
          %tr
            %td=link_to p_customer.show_name, [:admin, p_customer]
            %td=link_to p_customer.code, [:admin, p_customer]
            %td.numeraire
              -encours = p_customer.cache_encours
              =link_to number_to_currency(encours), admin_p_customer_path(:anchor => "tabs", :id => p_customer.id, :tab => "etat")
            
            -tot = 0.0
            -i = 0
            -[[nil,91], [90, 61], [60,31], [30,0], [-1,-30], [-30, nil]].each do |val|
              
              %td.numeraire{:style => ("border-right:3px black solid !important;" if i == 3)}
                -docs = p_customer.p_documents.where(:p_document_type_id => [4,7], :paid => false, :i_archive => false)
                -docs = docs.where("echeance_date >= ?", (Time.now - val[0].days).end_of_day) if val[0]
                -docs = docs.where("echeance_date <= ?", (Time.now - val[1].days).end_of_day) if val[1]
                
            
                -t = docs.sum(:cache_to_paid)
                -tot += t
                -if t != 00
                  -if !val[0] or val[0] > 0
                    - h = "échus "
                  -else
                    - h = "non échus "
                    
                  -h += headers[i]
                  =link_to number_to_currency(t),  admin_p_customer_path(:doc_label => h, :anchor => "tabs", :id => p_customer.id, :tab => "bills", :p_document_ids => docs.ids)
  
              -i += 1
            %td{:style => "border:0 !important;"}
              -if encours.to_f != tot.to_f
                %span{:style => "color:red;"}
                  -problemes += 1
                  =ic :warning
                  
      %p{:style => "color:red;"}
        =problemes
        clients avec un encours différent du solde des factures à payer
            
-else
  .qi_row
    .qi_pannel.qi_plain.padding
      .right=link_to "Balance agée", "?age=true"
    
      =form_tag "", :method => "get",  :onsubmit => "" do
    
    
      
        %table
          %tr
            %td Jours de retard :
            %td
              =select_tag :day, options_for_select(["0","30","60"], params[:day]),:class => "form-control", include_blank: true
        
          %tr
            %td Inclure les importés ?
            %td
              =select_tag :importeds, options_for_select([["Oui", "1"], ["Non", "0"]], params[:importeds]),:class => "form-control", include_blank: true

        
        =submit_tag "Recherche", :class => "btn btn-default", :style => "margin-bottom: 10px;"
    
    
      %p
        ="(#{@p_documents.total_count}) résultat(s) (#{@p_document_bruts.where(:imported => false).count})"
    
      %table.table
        %tr
        
          %th Date
          %th N° offre
          %th Client
        
          %th Type
          %th Numéro
          %th Total HT
          %th Total TTC
          -if params[:p_document_type_id].to_i == 4
            %th
              Th. Payée
            %th
              Payée
          %th
            Echeance
          %th Jours de retard
          %th Etat
        

  

        %tbody#p_customer_sheets_rows
          =render @p_documents
        
    
      .qi_pagination
        = paginate @p_documents
    