diff --git a/app/models/p_product.rb b/app/models/p_product.rb index 4f8c65e..2b2a73a 100644 --- a/app/models/p_product.rb +++ b/app/models/p_product.rb @@ -70,6 +70,11 @@ class PProduct < ApplicationRecord } + before_validation do + if self.code.blank? + generate_code + end + end def self.for_search(search) @@ -166,6 +171,35 @@ class PProduct < ApplicationRecord self.p_product_images.order(:position).first 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 => [ :identifiant, :gencode, @@ -223,32 +257,36 @@ class PProduct < ApplicationRecord # ap "*********************************************************************************************************" soreco_cat_hash = { "Smartphone" => "Smartphones & Téléphones mobiles", + "Mobile" => "Smartphones & Téléphones mobiles", "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| next if row["marque"].blank? - - n = self.new(imported: true) - ref = PProductRef.find_or_initialize_by(p_product: n, description: row["identifiant"]) + n = self.find_or_initialize_by(s_brand: SBrand.find_by(name: row["marque"]), name: row["reference"]) + n.imported = true + n.sorecop_cat = SorecopCat.find_by(name: soreco_cat_hash[row["famille"]]) + ref = n.p_product_refs.build row.each do |key, value| - if self.type_for_attribute(key) and self.type_for_attribute(key).type == :decimal - eval "n.#{key} = value.to_s.gsub(' ', '').gsub(',', '.')" - else + if value.present? + puts key case key when "identifiant" ref.description = value when "gencode" - n.genecode = value + ref.genecode = value when "id" when "marque" - n.s_brand = SBrand.find_or_create_by(name: value) + n.s_brand = SBrand.find_or_create_by!(name: value) 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" n.name = value @@ -257,11 +295,11 @@ class PProduct < ApplicationRecord ref.ct_sub_name = value if value.present? 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" 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 when "connectivite" arr = [] @@ -280,36 +318,36 @@ class PProduct < ApplicationRecord 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)) + 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)) + 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")) + 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)) + 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)) + 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" 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" - 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 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" 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)) + 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" - 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) when "reference_fabricant" ref.manufacturer_ref = value @@ -344,43 +382,47 @@ class PProduct < ApplicationRecord 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 + ref.ct_sorecop = value if !value.blank? end when "pmp_hors_sorecop" ref.i_pmp_hors_sorecop = value when "target_hors_sorecop" ref.i_target_hors_sorecop = value 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" - 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) when "commentaires" ref.i_comment = value + # champs ignorés when "date" # colonne vide when "dtl_targ" when "dtl" when "com" - else eval "n.#{key} = value" 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 - # 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 "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" - ap n - ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" - ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" - ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n.ref §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" - ap n.ref + # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" + # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" + # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" + # ap n + # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" + # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" + # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n.ref §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" + # ap ref # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ n.contacts §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" @@ -400,7 +442,6 @@ class PProduct < ApplicationRecord # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" # ap "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§" - import_csv.import_csv_elements << ImportCsvElement.new(:element => n) end end end diff --git a/app/models/p_product_ref.rb b/app/models/p_product_ref.rb index 5f8a453..5790117 100644 --- a/app/models/p_product_ref.rb +++ b/app/models/p_product_ref.rb @@ -15,7 +15,7 @@ class PProductRef < ApplicationRecord has_many :p_article_serial_nums, through: :p_articles 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 #validates :ct_price_ht, :presence => true @@ -52,7 +52,7 @@ class PProductRef < ApplicationRecord :actions => {:name => "Actions", :reorder => false} } - acts_as_caching :fields => [:sorecop, :deee] + acts_as_caching :fields => [:sorecop, :deee, :name, :code] attr_accessor :price_line_id @@ -89,9 +89,35 @@ class PProductRef < ApplicationRecord def ca_name - if self.p_product - self.p_product.name + " - " + self.ct_sub_name - end + storage = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Stockage")).first + cellular = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Connectivité")).first + 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 def ca_code @@ -169,14 +195,14 @@ class PProductRef < ApplicationRecord end 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? - if self.cc_sorecop - return self.cc_sorecop - else + # if self.cc_sorecop and self.cc_sorecop > 0.0 + # return self.cc_sorecop + # else find_sorecop_tax - end + # end end def find_sorecop_tax @@ -184,10 +210,12 @@ class PProductRef < ApplicationRecord return "Pas de stockage" if p_product_ref_spec.blank? 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 - 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 + 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 if tax.fixed_price diff --git a/app/models/s_brand.rb b/app/models/s_brand.rb index c5db009..4f87015 100644 --- a/app/models/s_brand.rb +++ b/app/models/s_brand.rb @@ -1,4 +1,25 @@ class SBrand < ApplicationRecord 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