
.qi_header
  %h2 Importation client

-@import= true

-i = 0
-@csv.each do |csv|
  -i += 1
  -logger.error "ligne : "+i.to_s
  -if csv["Code client"].to_s != "" and PCustomerSheet.where(:npiece => csv["N Piece"]).count ==0
    -logger.error "IMPORT COMMANDE "+csv["N Piece"].to_s
    -if !(p_customer = PCustomer.where(:code => csv["Code client"]).first)
      -p_customer = PCustomer.new(:code => csv["Code client"], :imported => true)
  
      
      
      
  
      -if false
        -if csv["Categorie"].to_s != "" and p_c_c = PCustomerCat.where(:name => csv["Categorie"].to_s).first
        -else
          -p_c_c = PCustomerCat.create(:name => csv["Categorie"].to_s)
      
      -p_customer.p_customer_cat = p_c_c
      
      -p_customer.save
      
      
      
    -if particular = p_customer.particulars.where(:firstname => csv["Prenom"],:address_2 => csv["Adresse1"], :address_3 => csv["Adresse2"], :cp => csv["CP"], :city => csv["Ville"]).first #:civilite => csv["Type"]
  
    -else
    
  
  
      -particular = p_customer.particulars.new(:civilite => csv["Type"],:address_2 => csv["Adresse1"], :address_3 => csv["Adresse2"], :cp => csv["CP"], :city => csv["Ville"]) #:firstname => csv["Prenom"]
    
      -if false
        -if csv["Categorie"] != " Particulier"
          -particular.organisation = csv["Organisation"]
        -else
          -particular.name = csv["Organisation"]

      -particular.organisation = csv["Organisation"]
      
      

      -particular.p_contacts << PContact.new(:tel => csv["tel1"]) if csv["tel1"].to_s != "" and csv["tel1"].to_s != " "
      -particular.p_contacts << PContact.new(:tel => csv["tel2"]) if csv["tel2"].to_s != "" and csv["tel2"].to_s != " "
      -particular.p_contacts << PContact.new(:tel => csv["tel3"]) if csv["tel3"].to_s != "" and csv["tel3"].to_s != " "
      
      -particular.save
      -p_customer.save
      
      
      
      
      
      
  
    -if csv["Code produit"].to_s != "" 
      -if pp = PProduct.where(:code => csv["Code produit"]).first
      -else
        -pp = PProduct.create(:code => csv["Code produit"], :name => csv["nom-produit"], :create_matiere => 1, :p_product_cat_id => 9)
    
      -qte = csv["Qte"].to_f
    
      -prix_tot = (csv["Prix"].to_s.gsub(",",".")).to_f
    
      -if qte != 0.0
        -prix = prix_tot / qte
      -else
        -prix = 0.0
    
      -#puts csv
      -p_customer_sheet = p_customer.p_customer_sheets.new(:stock_done => true, :imported => true, :npiece => csv["N Piece"], :state => "facturée", :created_at => csv["Date"], :a_total_cost_ok => (qte*prix - (csv["Marge"].to_s.gsub(",",".")).to_f),:a_ok_total => prix_tot)
      -p_customer_sheet.particular_bill = particular
      -p_customer_sheet.particular_send = particular

      -p_customer_sheet.p_sheet_lines << PSheetLine.new(:imported => true, :p_customer_sheet => p_customer_sheet, :p_product => pp, :qte => qte, :price => prix, :price_tot => prix_tot, :ok_qte => qte, :ok_price => prix, :ok_price_tot => prix_tot,:ok_at => csv["Date"], :ok => true, :lock => true, :externe => true, :shiped => true, :ship => true, :bl => true, :stock_done => true, :a_total_cost_ok => (qte*prix - (csv["Marge"].to_s.gsub(",",".")).to_f), :piece => csv["N Piece"])

      -p_customer_sheet.save
    
      -p_customer_sheet.generate_f(p_customer_sheet.npiece, true)
    
  -else
    -logger.error "Déjà importé"+csv["N Piece"].to_s
  -if i == 100
    -#break


