class PFournisseur < ApplicationRecord # has_many :particulars, :as => :owner, :dependent => :destroy has_many :p_contacts, :as => :contactable, :dependent => :destroy has_many :p_fournisseur_refs acts_as_csv_import :fields => [ :nom_fournisseur, :address, :cp, :ville, :pays, :tva_intracom, :delai_paiement, :interlocuteur, :fonction, :tel, :skype, :email_1, :email_2, :email_3, :iban, :bic, :reglement_par ] def self.custom_csv_import(list, import_csv) # ap "*********************************************************************************************************" # ap "*********************************************************************************************************" # ap "*********************************************************************************************************" # ap list # ap list.class # ap "*********************************************************************************************************" # ap "*********************************************************************************************************" # ap "*********************************************************************************************************" # ap import_csv # ap "*********************************************************************************************************" # ap "*********************************************************************************************************" # ap "*********************************************************************************************************" list.each do |row| n = self.new # particular = Particular.new(pro: true) contact_1 = PContact.new contact_2 = nil contact_3 = nil 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 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" if value.present? 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 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 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