WIP Import

This commit is contained in:
Barnabé 2021-10-14 18:33:20 +02:00
parent 6ce208d553
commit 2de306d5e6

View File

@ -167,6 +167,7 @@ class PProduct < ApplicationRecord
end end
acts_as_csv_import :fields => [ acts_as_csv_import :fields => [
:identifiant,
:gencode, :gencode,
:id, :id,
:marque, :marque,
@ -185,7 +186,7 @@ class PProduct < ApplicationRecord
:code_hs, :code_hs,
:indice_reparabilite, :indice_reparabilite,
:duree_dispo_pces_detachees, :duree_dispo_pces_detachees,
:das_tête, :das_tete,
:das_corps, :das_corps,
:das_membre, :das_membre,
:poids, :poids,
@ -211,7 +212,7 @@ class PProduct < ApplicationRecord
# ap "*********************************************************************************************************" # ap "*********************************************************************************************************"
# ap "*********************************************************************************************************" # ap "*********************************************************************************************************"
ap "*********************************************************************************************************" ap "*********************************************************************************************************"
ap list.size # ap list.size
# ap list.class # ap list.class
# ap "*********************************************************************************************************" # ap "*********************************************************************************************************"
# ap "*********************************************************************************************************" # ap "*********************************************************************************************************"
@ -220,96 +221,151 @@ class PProduct < ApplicationRecord
# ap "*********************************************************************************************************" # ap "*********************************************************************************************************"
# ap "*********************************************************************************************************" # ap "*********************************************************************************************************"
# ap "*********************************************************************************************************" # ap "*********************************************************************************************************"
soreco_cat_hash = {
"Smartphone" => "Smartphones & Téléphones mobiles",
"Tablette" => "Tablettes média et tablette PC",
"MP4" => "Baladeur MP4"
}
list.each do |row| list.each do |row|
next if row["marque"].blank? next if row["marque"].blank?
n = self.new(imported: true) n = self.new(imported: true)
ref = PProductRef.find_or_initialize_by(p_product: n, description: row["identifiant"])
row.each do |key, value| row.each do |key, value|
if self.type_for_attribute(key) and self.type_for_attribute(key).type == :decimal if self.type_for_attribute(key) and self.type_for_attribute(key).type == :decimal
eval "n.#{key} = value.to_s.gsub(' ', '').gsub(',', '.')" eval "n.#{key} = value.to_s.gsub(' ', '').gsub(',', '.')"
else else
case key case key
when "identifiant"
ref.description = value
when "gencode" when "gencode"
n.genecode = value
when "id" when "id"
when "marque" when "marque"
n.s_brand = SBrand.find_or_create_by(name: value)
when "famille" when "famille"
n.p_product_cat = PProductCat.find_or_create_by(name: value) n.p_product_cat = PProductCat.find_or_create_by(name: value)
when "reference" when "reference"
n.name = value
when "modele" when "modele"
ref.ct_sub_name = value if value.present?
when "couleur" when "couleur"
ref.p_product_color = PProductColor.find_or_create_by(name: value)
when "color" when "color"
if ref.p_product_color.nil?
ref.p_product_color = PProductColor.find_or_create_by(color: value)
end
when "connectivite" when "connectivite"
arr = []
cellular = [ "5G", "3G", "3G+", "4G", "4G+", "2G", "wifi", "BT"]
video = ["4K","HD" ]
power = ["5W", "12W", "18W", "20W", "96W", "40W", "10W", "40W"]
mix = ["<87W/4K", "5G/4K", "wifi/BT" ]
geoloc = [ "GPS" ]
if mix.include?(value)
arr = value.split('/')
else
arr << value
end
arr.each do |value|
spec = nil
if cellular.include?(value)
spec = PProductRefSpec.create(p_product_ref: ref, p_spec_type: PSpecType.find_by(name: "Connectivité"), p_spec_value: PSpecValue.find_or_create_by(value: value))
elsif video.include?(value)
spec = PProductRefSpec.create(p_product_ref: ref, p_spec_type: PSpecType.find_by(name: "Résolution"), p_spec_value: PSpecValue.find_or_create_by(value: value))
elsif power.include?(value)
PProductRefSpec.create(p_product_ref: ref, p_spec_type: PSpecType.find_by(name: "Puissance éléctrique"), p_spec_value: PSpecValue.find_or_create_by(value: value.gsub('W', ''), unit: "W"))
elsif geoloc.include?(value)
spec = PProductRefSpec.create(p_product_ref: ref, p_spec_type: PSpecType.find_by(name: "Géolocalisation"), p_spec_value: PSpecValue.find_or_create_by(value: value))
else
spec = PProductRefSpec.create(p_product_ref: ref, p_spec_type: PSpecType.find_by(name: "Autre"), p_spec_value: PSpecValue.find_or_create_by(value: value))
end
import_csv.import_csv_elements << ImportCsvElement.new(:element => spec)
end
when "sim" when "sim"
if ["DS", "SS"].include?(value)
PProductRefSpec.create(p_product_ref: ref, p_spec_type: PSpecType.find_by(name: "Sim"), p_spec_value: PSpecValue.find_or_create_by(value: value))
elsif value == "4G"
PProductRefSpec.create(p_product_ref: ref, p_spec_type: PSpecType.find_by(name: "Connectivité"), p_spec_value: PSpecValue.find_or_create_by(value: value))
end
when "taille" when "taille"
PProductRefSpec.create(p_product_ref: ref, p_spec_type: PSpecType.find_by(name: "Taille"), p_spec_value: PSpecValue.find_or_create_by(value: value.to_s))
when "capacite" when "capacite"
unit = value.match(/\D+/)
num = value.match(/\d+/)
spec = PProductRefSpec.create(p_product_ref: ref, p_spec_type: PSpecType.find_by(name: "Stockage"), p_spec_value: PSpecValue.find_or_create_by(value: num.to_s, unit: unit.to_s))
import_csv.import_csv_elements << ImportCsvElement.new(:element => spec)
when "infos_supp" when "infos_supp"
spec = PProductRefSpec.create(p_product_ref: ref, p_spec_type: PSpecType.find_by(name: "Autre"), p_spec_value: PSpecValue.find_or_create_by(value: value))
import_csv.import_csv_elements << ImportCsvElement.new(:element => spec)
when "reference_fabricant" when "reference_fabricant"
ref.manufacturer_ref = value
when "etat" when "etat"
ref.used_state = value
when "code_hs" when "code_hs"
ref.i_code_hs = value
when "indice_reparabilite" when "indice_reparabilite"
ref.prepair_score = value
when "duree_dispo_pces_detachees" when "duree_dispo_pces_detachees"
ref.part_delay = value
when "das_tête" when "das_tete"
ref.das_head = value
when "das_corps" when "das_corps"
ref.das_body = value
when "das_membre" when "das_membre"
ref.das_membre = value
when "poids" when "poids"
ref.weight = value
when "nb_pcs_colis" when "nb_pcs_colis"
ref.nb_pcs_colis = value
when "code_om" when "code_om"
ref.code_om = value
when "dtl"
when "gbh_code_971" when "gbh_code_971"
ref.gbh_code_971 = value
when "gbh_code_972" when "gbh_code_972"
ref.gbh_code_972 = value
when "gbh_code_973" when "gbh_code_973"
ref.gbh_code_973 = value
when "code_art" when "code_art"
ref.code_art = value
when "sorecop" when "sorecop"
ref.p_product.sorecop_cat = SorecopCat.find_by(name: soreco_cat_hash[row["famille"]])
if ref.sorecop.kind_of?(String)
ref.ct_sorecop = value
end
when "pmp_hors_sorecop" when "pmp_hors_sorecop"
ref.i_pmp_hors_sorecop = value
when "target_hors_sorecop" when "target_hors_sorecop"
ref.i_target_hors_sorecop = value
when "frs" when "frs"
ref.fournisseur_id = PFournisseur.find_or_create_by(name: value)
when "date"
when "spec" when "spec"
spec = PProductRefSpec.create(p_product_ref: ref, p_spec_type: PSpecType.find_by(name: "Version ROM"), p_spec_value: PSpecValue.find_or_create_by(value: value))
when "dtl_targ" import_csv.import_csv_elements << ImportCsvElement.new(:element => spec)
when "commentaires" when "commentaires"
ref.i_comment = value
# champs ignorés
when "date"
# colonne vide
when "dtl_targ"
when "dtl"
when "com" when "com"
else else
# eval "n.#{key} = value" eval "n.#{key} = value"
end end
end end
end end
@ -317,14 +373,14 @@ class PProduct < ApplicationRecord
# n.save # n.save
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
# ap n ap n
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ contact §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n.ref §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
# ap contact ap n.ref
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n.contacts §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n.contacts §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
@ -344,7 +400,7 @@ class PProduct < ApplicationRecord
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
# import_csv.import_csv_elements << ImportCsvElement.new(:element => n) import_csv.import_csv_elements << ImportCsvElement.new(:element => n)
end end
end end
end end