Class: LaGraineInformatique::Vinifera::EntitiesExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/la_graine_informatique/vinifera/entities_exchanger.rb

Instance Attribute Summary

Attributes inherited from ActiveExchanger::Base

#file, #supervisor

Instance Method Summary collapse

Methods inherited from ActiveExchanger::Base

build, check, check_by_default, exchanger_name, export, exporters, find, human_name, import, import!, importers, importers_selection, inherited, #initialize, register_exchanger

Constructor Details

This class inherits a constructor from ActiveExchanger::Base

Instance Method Details

#importObject


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/exchangers/la_graine_informatique/vinifera/entities_exchanger.rb', line 4

def import
  # Unzip file
  dir = w.tmp_dir
  Zip::File.open(file) do |zile|
    zile.each do |entry|
      entry.extract(dir.join(entry.name))
    end
  end

  custom_file = dir.join('custom_fields.csv')
  custom_fields = create_custom_fields(custom_file, dir)

  file = dir.join('entities.csv')
  rows = CSV.read(file, headers: true, encoding: 'cp1252', col_sep: ';')
  w.count = rows.count

  # FILE STRUCTURE
  # 0 CATEGORY
  # 1 CODE
  # 2 FULL NAME
  # 3,4,5 ADRESSE
  # 6 POSTAL CODE AND TOWN
  # 7 POSTAL CODE
  # 8 PHONE
  # 9 FAX
  # 10 PHONE 2
  # 11 CONTACT NAME
  # 12 TYPE CLIENT / client_types_transcode
  # 33 ORIGIN (Transcode) - col 1 perso
  # 34 CLIENT QUALITY (Transcode) - col 2 perso
  # 35 - - col 3 perso
  # 36 - col 4 perso
  # 59 NOTE
  # 67 EMAIL
  # 68 DATE CREATION

  country_preference = Preference[:country]

  rows.each do |row|
    r = {
      number: row[1].blank? ? '' : row[1].to_s,
      full_name: row[2].blank? ? '' : row[2].to_s.strip,
      last_name: row[2].blank? ? '' : row[2].to_s.strip,
      type: 'Entity',
      address_line_1: row[3].blank? ? nil : row[3].to_s,
      address_line_2: row[4].blank? ? nil : row[4].to_s,
      address_line_3: row[5].blank? ? nil : row[5].to_s,
      postal_code_city: row[6].blank? ? nil : row[6].to_s,
      phone_number: row[8].blank? ? nil : row[8].to_s,
      fax_number: row[9].blank? ? nil : row[9].to_s,
      cell_number: row[10].blank? ? nil : row[10].to_s,
      country: row[13].blank? ? country_preference : row[13].to_s.downcase,
      client_type: row[12].blank? ? nil : row[12].to_s,
      client_price_type: row[13].blank? ? nil : row[13].to_s,
      client_origin: row[33].blank? ? nil : row[33].to_s,
      client_quality: row[34].blank? ? nil : row[34].to_s,
      client_evolution: row[36].blank? ? nil : row[36].to_s,
      email: row[67].blank? ? nil : row[67].to_s,
      description: row[59].blank? ? nil : row[59].to_s,
      active: row[15].blank? ? false : true,
      prospect: row[16].blank? ? false : true
    }.to_struct

    nature = :organization
    { 'madame et monsieur' => :contact,
      'monsieur et madame' => :contact,
      'monsieur' => :contact,
      'madame' => :contact }.each do |expr, name|
      next unless r.full_name =~ /^#{expr}/i
      nature = name
      r.title = expr.upcase
      r.last_name = r.full_name.gsub(/^#{expr}/i, '').strip
      break
    end

    if person = Entity.where('full_name ILIKE ?', r.full_name.strip).first
      person.country = r.country if person.country.blank?
    elsif
      person = Entity.new(
        number: r.number,
        title: r.title,
        last_name: r.last_name,
        full_name: r.full_name,
        country: r.country,
        nature: nature,
        description: r.number
      )
    end

    if r.client_type
       = if r.client_type == 'X' || r.client_type == 'Y' || r.client_type == 'Z'
                         412
                       else
                         411
                       end
      person.client = true
      person. = Account.find_or_initialize_by(number: )
      person..name ||= person.full_name
      person..save!
    end

    custom_fields.each do |cf|
      val = r.send(cf.name).to_s.strip.gsub(/[[:space:]\_]+/, '-')
      person.set_custom_value(cf.field, val) unless val.blank?
    end

    person.save!

    # Add mail address if given
    mail_attributes = {}
    mail_attributes.store(:mail_line_3, r.address_line_1) if r.address_line_1
    mail_attributes.store(:mail_line_4, r.address_line_2) if r.address_line_2
    mail_attributes.store(:mail_line_5, r.address_line_3) if r.address_line_3
    mail_attributes.store(:mail_line_6, r.postal_code_city) if r.postal_code_city
    mail_attributes.store(:mail_country, r.country) if r.country
    unless r.postal_code_city.blank? || person.mails.where('mail_line_6 ILIKE ?', r.postal_code_city).where(mail_country: r.country).any?
      person.mails.create!(mail_attributes)
    end

    # Add phone number if given
    unless r.phone_number.blank? || person.phones.where(coordinate: r.phone_number).any?
      person.phones.create!(coordinate: r.phone_number)
    end

    # Add cell phone number if given
    unless r.cell_number.blank? || person.mobiles.where(coordinate: r.cell_number).any?
      person.mobiles.create!(coordinate: r.cell_number)
    end

    # Add fax phone number if given
    unless r.fax_number.blank? || person.faxes.where(coordinate: r.fax_number).any?
      person.faxes.create!(coordinate: r.fax_number)
    end

    # Add email if given
    person.emails.create!(coordinate: r.email) unless r.email.blank?

    w.check_point
  end
end