.qi_header
  %h2 Importation

-if !@csv
  =form_tag "", multipart: true do
    =file_field_tag :csv_file
    
    =submit_tag "importer", :class => "btn btn-primary"
    
-else
  -@import= true

  -i = 0
  -noms = []

  -logger.error "-- DEBUT IMPORT"
  %table.table
    -@csv.each do |csv|
      -i += 1

      -logger.error "ligne : "+i.to_s
    
      -if csv["Code client"].to_s != "" and csv["Journal"].to_s != "Avoirs" and csv["Journal"].to_s != "Ventes P P" and csv["Pièce"] != ""
        -if true
          -logger.error "- "+csv["Pièce"].to_s
          -if (p_customer = PCustomer.where(:code => csv["Code client"]).first)
            -logger.error "--CLIENT OK"
            -if PPayment.where(:piece => csv["Pièce"]).count == 0
              -logger.error "---PAIEMENT CREE"
              -if p_payment_type = PPaymentType.where(:name => csv["Mode de paiement"].to_s).first
              -else
                -p_payment_type = PPaymentType.create(:name => csv["Mode de paiement"].to_s)
      
              -p_payment = PPayment.new(:p_customer => p_customer, :p_payment_type => p_payment_type, :imported => true)
              -p_payment.theo_date = csv["Echéance"]
              -p_payment.paid_at = csv["Date"]
              -p_payment.label = csv["Libellé"]
              -p_payment.piece = csv["Pièce"]
              -p_payment.amount = ((csv["Solde"].to_s.gsub(",",".")).to_f) * (-1.0)
              -if p_payment.save
              -else
                %p
                  Errreur :
                  =csv["Pièce"]
            -else
              -logger.error "---PAIEMENT DEJA EXISTANT"
          -else
            -logger.error "--CLIENT NON TROUVE"
            
            -if true
              -if !noms.include?(csv["Nom client"].to_s)
                %tr
                  %td=csv["Code client"]
                  %td=csv["Nom client"].to_s
        
              -noms << csv["Nom client"] 
            




    
      
        
        
          -if false
            %tr
              -csv.each do |c|
          
                %td= c[1].to_s
      
      
    
      -if i == 30
        -#break
  -logger.error "-- FIN IMPORT"
  -logger.error ""


  
-if false
  =semantic_form_for [:admin, @j_contrat], :url => import_save_admin_j_lines_path(), :html => {:method => :post}, :remote => false do |f|
  
    =hidden_field_tag :id, @j_contrat.id
  
    .j_lines_form
      =f.semantic_fields_for :j_lines do |form|
        =render :partial => "admin/j_lines/inline_form", :locals => {:f => form}
      
    %center
      =f.submit "Importer", :class => "btn btn-primary"
      %br
      %br
      %br


