namespace :qi do
  desc "TODO"
  task migrate_all: :environment do
    
    
    puts "test"
    Dir["#{Rails.root.join('db','migrate')}/*"].sort.each do |mf|
      version = mf.split('/')[-1].split('_')[0]
      puts version
      ENV['VERSION'] = version
      Rake::Task["db:migrate"].execute
      
      sleep(10)
    end
    
    
  end


  task update_customer_code: :environment do
    
  
  
    require 'csv'    

    csv_text = File.read("#{Rails.root}/import_csv/20.10.07_table_correspondance_new_code_client_MDMB.csv") #.force_encoding('ISO-8859-1')
    @csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")
    
    
    @csv.each do |row|
      if row["id"].to_s != ""
        p_customer = PCustomer.where(:id => row["id"]).first
        if p_customer
          puts p_customer.show_name
          p_customer.past_code = p_customer.code
          p_customer.code = row["NEW_CODE"]
          p_customer.save
        else
          puts "Erreur"
          puts row["id"]
          raise
        end
        
      end
    end
    
  end
  
  
  task update_inventaire: :environment do
    
  
  
    require 'csv'    

    csv_text = File.read("#{Rails.root}/import_csv/exportinventairemdmb2.csv") #.force_encoding('ISO-8859-1')
    @csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")
    
    @stock_movement = StockMovement.new(:date => Date.parse("2020/10/31"), :description => "Inventaire 31/10/2020", :movement_type => "cred")
    
    @csv.each do |row|
      
      if row["ID"].to_s != "" and row["Qte"].to_s != "" and row["Qte"].to_s != "0"
        p_product = PProduct.where(:id => row["ID"]).first
        if p_product and p_product_ref = p_product.p_product_refs.first
          puts p_product.name
         
           @stock_movement.stock_movement_lines << StockMovementLine.new(:p_product_ref_id => p_product_ref.id, :qte => row["Qte"], :dluo => row["DLUO"], :ct_tot_amount_ht => row["Qte"].to_f * row["Prix dachat"].to_s.gsub(",", ".").to_f)
         
        else
          #puts "Erreur"
          puts row["ID"]
          raise
        end
        
      end
    end
    
    puts @stock_movement.save
    puts @stock_movement.errors.messages
    
  end
  
  
  task update_inventaire_rsto: :environment do
    
  
  
    require 'csv'    

    csv_text = File.read("#{Rails.root}/import_csv/stocksresto.csv") #.force_encoding('ISO-8859-1')
    @csv = CSV.parse(csv_text, :headers => true, :col_sep => ";")
    
    @stock_movement = StockMovement.new(:date => Date.parse("2020/11/27"), :description => "Inventaire 27/11/2020", :movement_type => "cred")
    
    @csv.each do |row|

      if row["Code produit"].to_s != "" and row["Quantité physique"].to_s != "" and row["Quantité physique"].to_s != "0"
        p_product = PProduct.where(:code => row["Code produit"]).first
        if p_product and p_product_ref = p_product.p_product_refs.first
          #puts p_product.name
          if p_product.ct_purchase_price_ht?
            #puts p_product.ct_purchase_price_ht
          else
            puts "Erreur PAS DE PRIX : "+row["Code produit"]
          end
          
           @stock_movement.stock_movement_lines << StockMovementLine.new(:p_product_ref_id => p_product_ref.id, :qte => row["Quantité physique"].to_f, :ct_tot_amount_ht => row["Quantité physique"].to_f * p_product.ct_purchase_price_ht.to_f)
         
        else
          puts "Erreur PAS DE PRODUIT : "+row["Code produit"]
      
          
        end
        
      end
    end
    
    puts @stock_movement.save
    puts @stock_movement.errors.messages
    
  end
  
  
  task generate_stat_lines: :environment do
    PriceDocument.where(:cc_label => ["Avoir","Facture"]).order("id DESC").find_each do |p|
      p.generate_stat_lines
    end
  end
  

end
