Import p_product ok

This commit is contained in:
Barnabé 2021-10-15 18:22:02 +02:00
parent 8965db5a59
commit ad135f6ff8
3 changed files with 139 additions and 49 deletions

View File

@ -70,6 +70,11 @@ class PProduct < ApplicationRecord
} }
before_validation do
if self.code.blank?
generate_code
end
end
def self.for_search(search) def self.for_search(search)
@ -166,6 +171,35 @@ class PProduct < ApplicationRecord
self.p_product_images.order(:position).first self.p_product_images.order(:position).first
end end
def generate_code
if !self.code
arr = []
brand = self.s_brand.code.to_s if self.s_brand
cat = self.p_product_cat.name.slice(0, 5).to_slug.upcase.to_s if self.p_product_cat
last_number = 1
arr << brand if brand.present?
arr << cat if cat.present?
arr << "%02d" % [last_number]
arr.prepend("PRODUCT") if arr.length < 2
code = arr.join('-')
while self.class.find_by(code: code)
arr = []
last_number += 1
arr << brand if brand.present?
arr << cat if cat.present?
arr << "%02d" % [last_number]
arr.prepend("PRODUCT") if arr.length < 2
code = arr.join('-')
end
self.code = code
end
end
acts_as_csv_import :fields => [ acts_as_csv_import :fields => [
:identifiant, :identifiant,
:gencode, :gencode,
@ -223,32 +257,36 @@ class PProduct < ApplicationRecord
# ap "*********************************************************************************************************" # ap "*********************************************************************************************************"
soreco_cat_hash = { soreco_cat_hash = {
"Smartphone" => "Smartphones & Téléphones mobiles", "Smartphone" => "Smartphones & Téléphones mobiles",
"Mobile" => "Smartphones & Téléphones mobiles",
"Tablette" => "Tablettes média et tablette PC", "Tablette" => "Tablettes média et tablette PC",
"MP4" => "Baladeur MP4" "MP4" => "Baladeur MP4",
"Clé USB" => "Clés USB",
"Carte Mémoire" => "Cartes mémoires",
"Boitier Multimédia" => "Décodeurs, téléviseurs, enregistreurs"
} }
list.each do |row| list.each do |row|
next if row["marque"].blank? next if row["marque"].blank?
n = self.find_or_initialize_by(s_brand: SBrand.find_by(name: row["marque"]), name: row["reference"])
n = self.new(imported: true) n.imported = true
ref = PProductRef.find_or_initialize_by(p_product: n, description: row["identifiant"]) n.sorecop_cat = SorecopCat.find_by(name: soreco_cat_hash[row["famille"]])
ref = n.p_product_refs.build
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 value.present?
eval "n.#{key} = value.to_s.gsub(' ', '').gsub(',', '.')" puts key
else
case key case key
when "identifiant" when "identifiant"
ref.description = value ref.description = value
when "gencode" when "gencode"
n.genecode = value ref.genecode = value
when "id" when "id"
when "marque" when "marque"
n.s_brand = SBrand.find_or_create_by(name: value) 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 n.name = value
@ -257,11 +295,11 @@ class PProduct < ApplicationRecord
ref.ct_sub_name = value if value.present? ref.ct_sub_name = value if value.present?
when "couleur" when "couleur"
ref.p_product_color = PProductColor.find_or_create_by(name: value) ref.p_product_color = PProductColor.find_or_create_by!(name: value)
when "color" when "color"
if ref.p_product_color.nil? if ref.p_product_color.nil?
ref.p_product_color = PProductColor.find_or_create_by(color: value) ref.p_product_color = PProductColor.find_or_create_by!(color: value)
end end
when "connectivite" when "connectivite"
arr = [] arr = []
@ -280,36 +318,36 @@ class PProduct < ApplicationRecord
arr.each do |value| arr.each do |value|
spec = nil spec = nil
if cellular.include?(value) 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)) 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) 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)) 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) 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")) 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) 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)) 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 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)) 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 end
import_csv.import_csv_elements << ImportCsvElement.new(:element => spec) import_csv.import_csv_elements << ImportCsvElement.new(:element => spec)
end end
when "sim" when "sim"
if ["DS", "SS"].include?(value) 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)) 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" 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)) 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 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)) 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+/) unit = value.match(/\D+/)
num = 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)) 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) 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)) 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) import_csv.import_csv_elements << ImportCsvElement.new(:element => spec)
when "reference_fabricant" when "reference_fabricant"
ref.manufacturer_ref = value ref.manufacturer_ref = value
@ -344,43 +382,47 @@ class PProduct < ApplicationRecord
when "sorecop" when "sorecop"
ref.p_product.sorecop_cat = SorecopCat.find_by(name: soreco_cat_hash[row["famille"]]) ref.p_product.sorecop_cat = SorecopCat.find_by(name: soreco_cat_hash[row["famille"]])
if ref.sorecop.kind_of?(String) if ref.sorecop.kind_of?(String)
ref.ct_sorecop = value ref.ct_sorecop = value if !value.blank?
end end
when "pmp_hors_sorecop" when "pmp_hors_sorecop"
ref.i_pmp_hors_sorecop = value ref.i_pmp_hors_sorecop = value
when "target_hors_sorecop" when "target_hors_sorecop"
ref.i_target_hors_sorecop = value ref.i_target_hors_sorecop = value
when "frs" when "frs"
ref.fournisseur_id = PFournisseur.find_or_create_by(name: value) ref.p_fournisseur_id = PFournisseur.find_or_create_by!(name: value).id
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)) 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))
import_csv.import_csv_elements << ImportCsvElement.new(:element => spec) import_csv.import_csv_elements << ImportCsvElement.new(:element => spec)
when "commentaires" when "commentaires"
ref.i_comment = value ref.i_comment = value
# champs ignorés # champs ignorés
when "date" when "date"
# colonne vide # colonne vide
when "dtl_targ" when "dtl_targ"
when "dtl" when "dtl"
when "com" when "com"
else else
eval "n.#{key} = value" eval "n.#{key} = value"
end end
# if self.type_for_attribute(key) and self.type_for_attribute(key).type == :decimal
# eval "n.#{key} = value.to_s.gsub(' ', '').gsub(',', '.')"
# end
end end
end end
# n.save n.save!
import_csv.import_csv_elements << ImportCsvElement.new(:element => ref)
import_csv.import_csv_elements << ImportCsvElement.new(:element => n)
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap n
ap n # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n.ref §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n.ref §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap ref
ap n.ref
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n.contacts §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n.contacts §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
@ -400,7 +442,6 @@ class PProduct < ApplicationRecord
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
# ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§"
import_csv.import_csv_elements << ImportCsvElement.new(:element => n)
end end
end end
end end

View File

@ -15,7 +15,7 @@ class PProductRef < ApplicationRecord
has_many :p_article_serial_nums, through: :p_articles has_many :p_article_serial_nums, through: :p_articles
accepts_nested_attributes_for :p_article_serial_nums, allow_destroy: true accepts_nested_attributes_for :p_article_serial_nums, allow_destroy: true
has_many :p_product_ref_specs has_many :p_product_ref_specs, dependent: :destroy
accepts_nested_attributes_for :p_product_ref_specs, allow_destroy: true, reject_if: :all_blank accepts_nested_attributes_for :p_product_ref_specs, allow_destroy: true, reject_if: :all_blank
#validates :ct_price_ht, :presence => true #validates :ct_price_ht, :presence => true
@ -52,7 +52,7 @@ class PProductRef < ApplicationRecord
:actions => {:name => "Actions", :reorder => false} :actions => {:name => "Actions", :reorder => false}
} }
acts_as_caching :fields => [:sorecop, :deee] acts_as_caching :fields => [:sorecop, :deee, :name, :code]
attr_accessor :price_line_id attr_accessor :price_line_id
@ -89,9 +89,35 @@ class PProductRef < ApplicationRecord
def ca_name def ca_name
if self.p_product storage = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Stockage")).first
self.p_product.name + " - " + self.ct_sub_name cellular = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Connectivité")).first
end screen = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Résolution")).first
power = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Puissance éléctrique")).first
geoloc = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Géolocalisation")).first
sim = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Sim")).first
size = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Taille")).first
version = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Version ROM")).first
arr = []
arr << self.p_product.name if self.p_product
arr << self.ct_sub_name
arr << storage.p_spec_value.member_label if storage
arr << cellular.p_spec_value.member_label if cellular
arr << screen.p_spec_value.member_label if screen
arr << power.p_spec_value.member_label if power
arr << geoloc.p_spec_value.member_label if geoloc
arr << sim.p_spec_value.member_label if sim
arr << size.p_spec_value.member_label if size
arr << version.p_spec_value.member_label if version
arr << self.p_product_color.name if p_product_color
arr.compact.each{|el| el.strip!}.join(' ')
# if self.p_product
# self.p_product.name + " - " + self.ct_sub_name
# end
end end
def ca_code def ca_code
@ -169,14 +195,14 @@ class PProductRef < ApplicationRecord
end end
def ca_sorecop def ca_sorecop
return ct_sorecop if ct_sorecop # return ct_sorecop if ct_sorecop
return "Pas de catégorie Sorecop" if self.p_product.sorecop_cat.blank? return "Pas de catégorie Sorecop" if self.p_product.sorecop_cat.blank?
if self.cc_sorecop # if self.cc_sorecop and self.cc_sorecop > 0.0
return self.cc_sorecop # return self.cc_sorecop
else # else
find_sorecop_tax find_sorecop_tax
end # end
end end
def find_sorecop_tax def find_sorecop_tax
@ -184,10 +210,12 @@ class PProductRef < ApplicationRecord
return "Pas de stockage" if p_product_ref_spec.blank? return "Pas de stockage" if p_product_ref_spec.blank?
storage_capacity = p_product_ref_spec.p_spec_value.value.to_f storage_capacity = p_product_ref_spec.p_spec_value.value.to_f
if p_product_ref_spec.p_spec_value.unit.casecmp?("Go") if p_product_ref_spec.p_spec_value.unit.casecmp?("Gb")
tax = self.p_product.sorecop_cat.sorecop_taxes.where('critere_min < ? AND critere_max >= ?', storage_capacity, storage_capacity).first tax = self.p_product.sorecop_cat.sorecop_taxes.where('critere_min < ? AND critere_max >= ?', storage_capacity, storage_capacity).first
elsif p_product_ref_spec.p_spec_value.unit.casecmp?("To") elsif p_product_ref_spec.p_spec_value.unit.casecmp?("Tb")
tax = self.p_product.sorecop_cat.sorecop_taxes.where('critere_min < ? AND critere_max >= ?', storage_capacity * 1000, storage_capacity * 1000).first tax = self.p_product.sorecop_cat.sorecop_taxes.where('critere_min < ? AND critere_max >= ?', storage_capacity * 1000, storage_capacity * 1000).first
elsif p_product_ref_spec.p_spec_value.unit.casecmp?("Mb")
tax = self.p_product.sorecop_cat.sorecop_taxes.where('critere_min < ? AND critere_max >= ?', storage_capacity / 1000, storage_capacity / 1000).first
end end
if tax.fixed_price if tax.fixed_price

View File

@ -1,4 +1,25 @@
class SBrand < ApplicationRecord class SBrand < ApplicationRecord
validates :name, :presence => true, :uniqueness => true validates :name, :presence => true, :uniqueness => true
before_save do
if self.code.blank?
generate_code
end
end
def generate_code
if self.code.blank?
prefix = self.name.slice(0, 4).to_slug.upcase
last_number = 1
code = prefix + "%02d" % [last_number]
while self.class.find_by(code: code)
last_number += 1
code = prefix + "%02d" % [last_number]
end
self.code = code
end
end
end end