This repository has been archived on 2021-11-24. You can view files and clone it, but cannot push or open issues or pull requests.
phone_app/app/models/p_fournisseur.rb
2021-10-26 20:26:38 +02:00

153 lines
5.0 KiB
Ruby

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
]
include PgSearch::Model
pg_search_scope :global_search,
against: [:name],
using: {
tsearch: { prefix: true }
}
acts_as_sorting :fields => {
:name => {:name => "Nom", :reorder => true},
:address => {:name => "Adresse", :reorder => false},
:actions => {:name => "Actions"}
}
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 if !value.blank?
when "address"
n.address1 = value if !value.blank?
when "ville"
n.city = value if !value.blank?
when "pays"
n.country = value if !value.blank?
when "tva_intracom"
n.tva_num = value if !value.blank?
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 if !value.blank?
end
end
when "interlocuteur"
contact_1.name = value if !value.blank?
when "fonction"
contact_1.p_contact_types << PContactType.find_or_create_by!(name: value)
when "tel"
contact_1.tel = value if !value.blank?
when "skype"
contact_1.skype = value if !value.blank?
when "email_1"
contact_1.email = value if !value.blank?
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 if !value.blank?"
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