.qi_header
  %h2 Importation lettrage

-@import= true

-i = 0
-pieces_number = []

-logger.error "-- DEBUT IMPORT"
%table.table
  -@csv.each do |csv|
    - pieces_number << csv["Pièce"]
            

%p
  =pieces_number.size
  
  =pdoc = PDocument.where("d_number IN(?)", pieces_number).where(:imported => true, :i_archive => false, :paid => false)
  =ids = pdoc.map{|pd| pd.id}
  
  =PDocument.where("id not IN(?)", ids).where(:imported => true, :i_archive => false, :paid => false).update_all(:i_archive => true, :i_ajust => true, :i_ajust_result => true)
%p
  =ppayment = PPayment.where("piece IN(?)", pieces_number).where(:imported => true, :i_archive => false)
  =ids = ppayment.map{|pd| pd.id}
  
  -PPayment.where("id not IN(?)", ids).where(:imported => true, :i_archive => false).each do |p_payment|  #.update_all(:i_archive => true, :i_ajust => true, :i_ajust_result => true) 
    -if p_payment.p_payment_documents.count == 0
      -p_payment.i_archive = true
      -p_payment.i_ajust = true
      -p_payment.i_ajust_result = true
      -p_payment.save
      
  
-PDocument.where(:i_ajust => true).all.each do |pd|
  =pd.save