-@import= true

-i = 0
-@csv.each do |csv|
  -i += 1

  -if csv["Code client"].to_s != ""
    -if !(p_customer = PCustomer.where(:code => csv["Code client"]).first)
      -p_customer = PCustomer.new(:code => csv["Code client"])
  
      -p_customer.particulars << Particular.new(:organisation => csv["Organisation"], :address_2 => csv["Adresse1"], :address_3 => csv["Adresse2"], :cp => csv["CP"], :city => csv["Ville"])

      -p_customer.p_contacts << PContact.new(:tel => csv["tel1"]) if csv["tel1"].to_s != "" and csv["tel1"].to_s != " "
      -p_customer.p_contacts << PContact.new(:tel => csv["tel2"]) if csv["tel2"].to_s != "" and csv["tel2"].to_s != " "
      -p_customer.p_contacts << PContact.new(:tel => csv["tel3"]) if csv["tel3"].to_s != "" and csv["tel3"].to_s != " "
      
      -p_customer.save
  
  
  -if csv["Code produit"].to_s != "" and pp = PProduct.where(:code => csv["Code produit"]).first
    -qte = csv["Qte"].to_f
    - if qte > 0.0
      -prix = (csv["Prix"].to_s.gsub(",",".")).to_f / qte
    -else
      -prix = 0.0
  
    -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,:a_ok_total => qte*prix)
    -p_customer_sheet.particular_bill = p_customer.particulars.first
    -p_customer_sheet.particular_send = p_customer.particulars.first
  
    -p_customer_sheet.p_sheet_lines << PSheetLine.new(:p_product => pp, :qte => qte, :price => prix, :ok_qte => qte, :ok_price => prix,:ok_at => csv["Date"], :ok => true, :lock => true, :externe => true, :shiped => true, :ship => true, :bl => true, :stock_done => true, :a_total_cost_ok => qte.to_f*prix)
  
    -p_customer_sheet.save
    
    
  -if i == 10
    -#break


.qi_header
  %h2 Importation
  
-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


