From 8f92692d79fb618193fd799fdcca42138e5d6cc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A9?= Date: Wed, 13 Oct 2021 18:13:47 +0200 Subject: [PATCH] refacto import --- app/models/p_fournisseur.rb | 125 +++++++++++++++++++++++++----------- 1 file changed, 89 insertions(+), 36 deletions(-) diff --git a/app/models/p_fournisseur.rb b/app/models/p_fournisseur.rb index 0b6de85..44a7f77 100644 --- a/app/models/p_fournisseur.rb +++ b/app/models/p_fournisseur.rb @@ -4,27 +4,27 @@ class PFournisseur < ApplicationRecord has_many :p_fournisseur_refs acts_as_csv_import :fields => [ - :name, - :address1, + :nom_fournisseur, + :address, :cp, - :city, - :country, - :email, + :ville, + :pays, + :tva_intracom, + :delai_paiement, + :interlocuteur, + :fonction, :tel, - :p_contact_name, - :p_contact_comment, - :p_contact_tel, - :p_contact_skype, - :p_contact_email_1, - :p_contact_email_2, - :p_contact_email_3, - :bic, + :skype, + :email_1, + :email_2, + :email_3, :iban, - :payment_delais, - :tva_num, - :p_payment_type + :bic, + :reglement_par ] + + def self.custom_csv_import(list, import_csv) # ap "*********************************************************************************************************" # ap "*********************************************************************************************************" @@ -50,33 +50,86 @@ class PFournisseur < ApplicationRecord if self.type_for_attribute(key) and self.type_for_attribute(key).type == :decimal eval "n.#{key} = value.to_s.gsub(' ', '').gsub(',', '.')" - elsif key == "p_contact_name" - contact_1.name = value - elsif key == "p_contact_comment" - contact_1.p_contact_types << PContactType.find_or_create_by(name: value) - elsif key == "p_contact_tel" - contact_1.tel = value - elsif key == "p_contact_skype" - contact_1.skype = value - elsif key == "p_contact_email_1" - contact_1.email = value - elsif key == "p_contact_email_2" - contact_2 = PContact.new(email: value) - elsif key == "p_contact_email_3" - contact_3 = PContact.new(email: value) - elsif key == "p_payment_type" - n.p_payment_type_id = PPaymentType.find_or_create_by(name: value).id else - eval "n.#{key} = value" + case key + when "nom_fournisseur" + n.name = value + when "address" + n.address1 = value + when "ville" + n.city = value + when "pays" + n.country = value + when "tva_intracom" + n.tva_num = value + when "delai_paiement" + next if value.nil? + case value + when "A la commande" + n.payment_delais = 0 + when "A réception" + n.payment_delais = 0 + when "7 jours net" + n.payment_delais = 7 + when "21 jours net" + n.payment_delais = 21 + when "30 jours" + n.payment_delais = 30 + when "30 jours net" + n.payment_delais = 30 + when "30 jours fin de mois" + n.payment_delais = 30 + when "35 jours net" + n.payment_delais = 35 + when "40 jours net" + n.payment_delais = 40 + when "60 jours net" + n.payment_delais = 60 + when "60 jours fin de mois" + n.payment_delais = 60 + else + n.payment_delais = value + end + when "interlocuteur" + contact_1.name = value + when "fonction" + contact_1.p_contact_types << PContactType.find_or_create_by(name: value) + when "tel" + contact_1.tel = value + when "skype" + contact_1.skype = value + when "email_1" + contact_1.email = value + when "email_2" + contact_2 = PContact.new(email: value) + when "email_3" + contact_3 = PContact.new(email: value) + when "reglement_par" + n.p_payment_type_id = PPaymentType.find_or_create_by(name: value).id + else + eval "n.#{key} = value" + end end end # particular.organisation = self.name n.save - n.p_contacts << contact_1 if (contact_1.name || contact_1.tel || contact_1.skype || contact_1.email) - n.p_contacts << contact_2 if contact_2 - n.p_contacts << contact_3 if contact_3 + if (contact_1.name || contact_1.tel || contact_1.skype || contact_1.email) + n.p_contacts << contact_1 + import_csv.import_csv_elements << ImportCsvElement.new(:element => contact_1) + end + if contact_2 + n.p_contacts << contact_2 + import_csv.import_csv_elements << ImportCsvElement.new(:element => contact_2) + end + + if contact_3 + n.p_contacts << contact_3 + import_csv.import_csv_elements << ImportCsvElement.new(:element => contact_3) + end + import_csv.import_csv_elements << ImportCsvElement.new(:element => n) + end end end