cirandas.net

ref: master

lib/tasks/sies_update.rake


  1
  2
  3
  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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
desc "Creates new enterprises from the csv file - SIES Data"
task :sies_new_enterprises do
  require 'script/lib/fbes_enterprise_call'
  require 'script/lib/sies_new_enterprises'

  if ENV["FILE"].blank?
    puts "sintax:"
    puts "  rake sies_new_enterprises FILE=filename"
    return
  end

  init_log "sies-new-enterprises.log"
  enterprises = load_sheet ENV["FILE"]

  puts "Carregando empreendimentos"
  existing = Enterprise.where(['created_at > ? AND visible = true AND enabled = false', Date.today - 10.days]).order('id ASC')
  sies_enterprise_map = {}; existing.each do |e|
    id_sies = e.data[:id_sies]
    sies_enterprise_map[id_sies] = e
  end

  puts "Iniciando importação..."
  enterprises.each do |data|
    enterprise = sies_enterprise_map[data[:data][:id_sies]]

    if enterprise
      #enterprise.save!
    else
      #enterprise = create_enterprise data
    end
    data[:record] = enterprise
  end

  puts "Exportando CSV com dados importados"
  export_imported enterprises

  puts "Importação concluída!"
end

desc "Put private data"
task :sies_put_private_data do
  require 'script/lib/fbes_enterprise_call'
  require 'script/lib/sies_new_enterprises'

  if ENV["FILE"].blank?
    puts "sintax:"
    puts "  rake sies_put_private_data FILE=filename"
    return
  end

  enterprises = []
  rows = CSV.open ENV["FILE"], 'r'
  rows.shift
  rows.each do |row|
    enterprises << {
      :data => {
        :id_sies => row[0].to_i,
        :private => {
          :contact_phone1 => row[9].to_s,
          :contact_phone2 => row[11].to_s,
          :contact_phone3 => row[13].to_s,
          :contact_phone4 => row[15].to_s,
          :contact_phone5 => row[17].to_s,
        },
      },
    }
  end

  puts "Carregando empreendimentos"
  existing = Enterprise.where(['visible = true']).order('id ASC')
  sies_enterprise_map = {}; existing.each do |e|
    id_sies = e.data[:id_sies]
    sies_enterprise_map[id_sies] = e
  end

  puts "Inserindo telefones..."
  enterprises.each do |data|
    enterprise = sies_enterprise_map[data[:data][:id_sies]]
    next puts "Não pode encontrar empreendimento com ID SIES #{data[:data][:id_sies]}" if enterprise.blank?

    enterprise.data.update data[:data]
    enterprise.save!
  end
end

desc "Export the full list of enterprises"
task :sies_export_all_enterprises do
  require 'script/lib/fbes_enterprise_call'
  require 'script/lib/sies_new_enterprises'

  puts "Carregando empreendimentos"
  enterprises = Enterprise.where(['visible = true']).order('id ASC').map do |enterprise|
    {
      :id_sies => enterprise.data[:id_sies],
      :record => enterprise,
    }
  end

  puts "Exportando CSV com dados importados"
  export_imported enterprises
end

desc "Remove enterprises from the csv file - SIES Data"
task :sies_remove_enterprises do
  require 'script/lib/fbes_enterprise_call'
  require 'script/lib/sies_new_enterprises'

  if ENV["FILE"].blank?
    puts "sintax:"
    puts "  rake sies_remove_enterprises FILE=filename"
    return
  end

  init_log "sies-remove-enterprises.log"

  rows = CSV.open ENV['FILE'], 'r'
  rows.shift
  ids = rows.map{ |row| row[2] }
  enterprises = Enterprise.where(:id => ids)

  #filename = "sies_removed_enterprise_list.csv"
  #CSV.open filename, "w" do |csv|
  #  csv << ['Nome do empreendimento', 'URL do empreendimento no Cirandas', 'ID do SIES', 'UF', 'Cidade']

  #  enterprises.each do |e|
  #    url = "#{$environment.top_url}/#{e.identifier}"
  #    city = e.region
  #    state = e.region.parent
  #    csv << [e.name, url, e.data[:id_sies], state.name, city.name]
  #  end
  #end

  enterprises.find_each do |e|
    next $log.error "Empreendimento #{e.inspect} já foi ativado!" if e.enabled
    e.destroy
  end
end

desc "Update disabled enterprises from the csv file - SIES Data"
task :sies_update_enterprises do
  require 'script/lib/fbes_enterprise_call'
  require 'script/lib/sies_new_enterprises'

  if ENV["FILE"].blank?
    puts "sintax:"
    puts "  rake sies_update_enterprises FILE=filename"
    return
  end

  init_log "sies-update-enterprises.log"

  id_data_map = {}

  rows = CSV.open ENV['FILE'], 'r'
  rows.shift
  rows.each do |row|
    id = row[0].to_i
    id_data_map[id] = {
      :data => {
        :id_sies => row[2].to_i,
        :cnpj => row[3].to_s,
      },
      :zip_code => row[8].to_s.squish,
      :state => row[9].to_s.squish,
      :city => row[10].to_s.squish.titleize,
      :address => "#{row[11].to_s}, #{row[12].to_s}".titleize,
      :contact_email => row[13].to_s.normalize_email,
      :organization_website => row[14].to_s,
      :contact_person => row[15].to_s.squish.titleize,
      :contact_phone => row[16].to_s.squish,
    }
  end

  enterprises = Enterprise.find id_data_map.keys
  enterprises.each do |e|
    data = id_data_map[e.id]

    puts "Atualizando empreendimento '#{e.identifier}'"

    e.data.merge! data.delete(:data)
    e.update! data

    data[:record] = e
  end

  puts "Exportando CSV com dados importados"
  export_imported id_data_map.values
end