From 992c113236943bfcda661dd744bd5821126414ab Mon Sep 17 00:00:00 2001 From: Nombre Usuario Date: Mon, 13 Nov 2023 14:34:13 -0500 Subject: [PATCH] Actualiza y mejora script para importar aportantes de Rama --- Gemfile.lock | 48 ++++++------ db/structure.sql | 3 +- lib/guiones/importa-donantes.rb | 130 +++++++++++++++++++------------- 3 files changed, 102 insertions(+), 79 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 9548ebc..fa857c6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -33,7 +33,7 @@ GIT GIT remote: https://gitlab.com/pasosdeJesus/cor1440_gen.git - revision: cc5a1d4572fabf53062627fb15922fa93ca7d98f + revision: f55d01eae7a0b3cf366fd42dfd728ae759cce1d1 branch: main specs: cor1440_gen (2.2.0.beta4) @@ -46,7 +46,7 @@ GIT GIT remote: https://gitlab.com/pasosdeJesus/heb412_gen.git - revision: 139b2a097cf7c272f1a10f32b965bfd73a51b707 + revision: 9d323f8019603a9cbe9929d9a9431955b0109d38 branch: main specs: heb412_gen (2.2.0.beta4) @@ -57,7 +57,7 @@ GIT GIT remote: https://gitlab.com/pasosdeJesus/jos19.git - revision: f2a86d2d8436567bfbede62d508fc856576cf9f6 + revision: 68b5a28a7cfab4a40e176adc39fbefdb443b919e branch: main specs: jos19 (2.2.0.beta4) @@ -65,7 +65,7 @@ GIT GIT remote: https://gitlab.com/pasosdeJesus/mr519_gen.git - revision: 43e4dfbf40204a240bfac5366aa9bc785e7579b3 + revision: ef2c0c30e6febb851dec6e7e0a699155309e959e branch: main specs: mr519_gen (2.2.0.beta4) @@ -74,7 +74,7 @@ GIT GIT remote: https://gitlab.com/pasosdeJesus/msip.git - revision: 82efa216107f1d83766756b4e01e673cf8c2718d + revision: 2455fc9e8a04611b747dc1f99473f051d4af83cd branch: main specs: msip (2.2.0.beta4) @@ -181,10 +181,9 @@ GEM babel-transpiler (0.7.0) babel-source (>= 4.0, < 6) execjs (~> 2.0) - base64 (0.1.1) bcrypt (3.1.19) bindex (0.8.1) - bootsnap (1.16.0) + bootsnap (1.17.0) msgpack (~> 1.2) brakeman (6.0.1) builder (3.2.4) @@ -219,10 +218,10 @@ GEM concurrent-ruby (1.2.2) connection_pool (2.4.1) crass (1.0.6) - cuprite (0.14.3) + cuprite (0.15) capybara (~> 3.0) - ferrum (~> 0.13.0) - date (3.3.3) + ferrum (~> 0.14.0) + date (3.3.4) debug (1.8.0) irb (>= 1.5.0) reline (>= 0.3.1) @@ -241,7 +240,7 @@ GEM railties (>= 3.2) erubi (1.12.0) execjs (2.9.1) - ferrum (0.13) + ferrum (0.14) addressable (~> 2.5) concurrent-ruby (~> 1.1) webrick (~> 1.7) @@ -256,7 +255,7 @@ GEM i18n (1.14.1) concurrent-ruby (~> 1.0) io-console (0.6.0) - irb (1.8.3) + irb (1.9.0) rdoc reline (>= 0.3.8) jbuilder (2.11.5) @@ -267,7 +266,7 @@ GEM json (2.6.3) kgio (2.11.4) language_server-protocol (3.17.0.3) - libxml-ruby (4.1.1) + libxml-ruby (4.1.2) loofah (2.21.4) crass (~> 1.0.2) nokogiri (>= 1.12.0) @@ -291,12 +290,12 @@ GEM minitest (>= 5.0) ruby-progressbar msgpack (1.7.2) - net-imap (0.4.2) + net-imap (0.4.4) date net-protocol net-pop (0.1.2) net-protocol - net-protocol (0.2.1) + net-protocol (0.2.2) timeout net-smtp (0.4.0) net-protocol @@ -327,7 +326,7 @@ GEM psych (5.1.1.1) stringio public_suffix (5.0.3) - racc (1.7.1) + racc (1.7.3) rack (2.2.8) rack-cors (2.0.1) rack (>= 2.0.0) @@ -375,12 +374,12 @@ GEM zeitwerk (~> 2.5) rainbow (3.1.1) raindrops (0.20.1) - rake (13.0.6) - rdoc (6.5.0) + rake (13.1.0) + rdoc (6.6.0) psych (>= 4.0.0) redcarpet (3.6.0) regexp_parser (2.8.2) - reline (0.3.9) + reline (0.4.0) io-console (~> 0.5) responders (3.1.1) actionpack (>= 5.2) @@ -389,8 +388,7 @@ GEM rspreadsheet (0.5.3) andand (>= 1.3) rubyzip (>= 1.3) - rubocop (1.57.1) - base64 (~> 0.1.1) + rubocop (1.57.2) json (~> 2.3) language_server-protocol (>= 3.17.0) parallel (~> 1.10) @@ -401,11 +399,11 @@ GEM rubocop-ast (>= 1.28.1, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 2.4.0, < 3.0) - rubocop-ast (1.29.0) + rubocop-ast (1.30.0) parser (>= 3.2.1.0) rubocop-minitest (0.33.0) rubocop (>= 1.39, < 2.0) - rubocop-rails (2.21.2) + rubocop-rails (2.22.1) activesupport (>= 4.2.0) rack (>= 1.1) rubocop (>= 1.33.0, < 2.0) @@ -442,12 +440,12 @@ GEM sprockets (>= 3.0.0) stimulus-rails (1.3.0) railties (>= 6.0.0) - stringio (3.0.8) + stringio (3.0.9) terrapin (0.6.0) climate_control (>= 0.0.3, < 1.0) thor (1.3.0) tilt (2.3.0) - timeout (0.4.0) + timeout (0.4.1) ttfunk (1.7.0) turbo-rails (1.5.0) actionpack (>= 6.0.0) diff --git a/db/structure.sql b/db/structure.sql index fcf4044..6d7d8e0 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -12797,6 +12797,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20231003222709'), ('20231007095930'), ('20231021134451'), -('20231027172038'); +('20231027172038'), +('20231113091147'); diff --git a/lib/guiones/importa-donantes.rb b/lib/guiones/importa-donantes.rb index 34c0319..f902dbb 100644 --- a/lib/guiones/importa-donantes.rb +++ b/lib/guiones/importa-donantes.rb @@ -2,22 +2,46 @@ # encoding: utf-8 # frozen_string_literal: true -# Importa base general de aportantes de fiscalía de un CSV +# Importa base general de aportantes de fiscalía / rama de un CSV d = Date.today.day require "csv" -if ARGV.length != 1 +if ARGV.length != 2 STDERR.puts "Primer argumento debe ser CSV por importar" + STDERR.puts "Segundo si se importa datos de [F]iscalía o de Rama [J]udicial" + exit 1 +end + +importade=ARGV[1] +if importade != "F" && importade != "J" + STDERR.puts "Segundo argumento debe ser F o J" exit 1 end region_region = { - "RISARALDA" => "EJE CAFETERO - RISARALDA" + "ARMENIA" => "EJE CAFETERO", + "BARRANQUILLA" => "ATLÁNTICO", + "BUCARAMANGA" => "SANTANDER", + "EJE CAFETERO - RISARALDA" => "EJE CAFETERO", + "MANIZALES CALDAS" => "EJE CAFETERO", + "MEDELLIN ANTIOQUIA" => "ANTIOQUIA", + "NORTE DE SANTANDER Y ARAUCA" => "NORTE DE SANTANDER", + "NEIVA" => "HUILA", + "PACIFICO" => "VALLE DEL CAUCA", + "PASTO" => "NARIÑO", + "PEREIRA RISARALDA" => "EJE CAFETERO", + "RISARALDA" => "EJE CAFETERO", + "SANTA MARTA" => "MAGDALENA", + "SANTANDER Y MAGDALENA MEDIO" => "SANTANDER", + "TUNJA" => "BOYACA", + "VALLEDUPAR CESAR" => "CESAR", + "VILLAVICENCIO" => "CESAR", } region_depto = { + "ANTIOQUIA CHOCO" => "ANTIOQUIA", "ARMENIA" => "QUINDÍO", "BOGOTÁ" => "BOGOTÁ, D.C.", "CARTAGENA" => "BOLIVAR", @@ -26,6 +50,7 @@ "EJE CAFETERO - CHOCO" => "CHOCO", "EJE CAFETERO - QUINDIO" => "QUINDÍO", "EJE CAFETERO - RISARALDA" => "RISARALDA", + "HULA Y CAQUETA" => "HUILA", "MEDELLIN ANTIOQUIA" => "ANTIOQUIA", "PASTO NARIÑO" => "NARIÑO", "POPAYAN" => "CAUCA", @@ -318,6 +343,7 @@ "VIDA -PALOQUEMAO" => [33, 31], } + trmes = { "ene" => 1, "feb" => 2, @@ -338,38 +364,21 @@ impcsv = CSV.read(nimp, headers: true) encimp = impcsv[0].headers STDERR.puts "Se leyeron #{impcsv.count} registros de #{nimp}" -if encimp.count != 29 || - !encimp.include?("#") || - !encimp.include?("Cedula") || - !encimp.include?("Apellidos") || - !encimp.include?("Nombres") || - !encimp.include?("abr-20") || - !encimp.include?("jun-20") || - !encimp.include?("jul-20") || - !encimp.include?("ago-20") || - !encimp.include?("sept-20") || - !encimp.include?("oct-20") || - !encimp.include?("nov-20") || - !encimp.include?("dic-20") || - !encimp.include?("ene-21") || - !encimp.include?("feb-21") || - !encimp.include?("mar-21") || - !encimp.include?("abr-21") || - !encimp.include?("may-21") || - !encimp.include?("jun-21") || - !encimp.include?("jul-21") || - !encimp.include?("ago-21") || - !encimp.include?("sept-21") || - !encimp.include?("oct-21") || - !encimp.include?("nov-21") || - !encimp.include?("dic-21") || - !encimp.include?("DEPARTAMENTO ") || - !encimp.include?("mail") || - !encimp.include?("celular") || - !encimp.include?("Cargo") || - !encimp.include?("Seccional") - puts "El CSV #{nimp} no tiene los encabezados esperados" - exit 1 +puts encimp.count +colesp = ["#", "Cedula", "Nombres", "Apellidos", "abr-20", "may-20", "jun-20", + "jul-20", "ago-20", "sep-20", "oct-20", "nov-20", "dic-20", + "ene-21", "feb-21", "mar-21", "abr-21", "may-21", "jun-21", + "jul-21", "ago-21", "sep-21", "oct-21", "nov-21", "dic-21", + "CIUDAD", "Departamento", "mail", "celular", "Despacho", "Cargo"] +if encimp.count != colesp.count + puts "El CSV #{nimp} tiene #{encimp.count} columnas pero se esperaban #{coles.count}" + exit 1 +end +colesp.each do |c| + if !encimp.include?(c) + puts "El CSV #{nimp} no tiene columna #{c}" + exit 1 + end end idrep = 0 @@ -377,7 +386,7 @@ nreg = 0 numerr = 0 prob = CSV.generate do |csvprob| - csvprob << ["#", "Cedula", "Apellidos", "Nombres", "abr-20", "jun-20", "jul-20", "ago-20", "sept-20", "oct-20", "nov-20", "dic-20", "ene-21", "feb-21", "mar-21", "abr-21", "may-21", "jun-21", "jul-21", "ago-21", "sept-21", "oct-21", "nov-21", "dic-21", "DEPARTAMENTO ", "mail", "celular", "Cargo", "Seccional", "Error"] + csvprob << colesp impcsv.each do |r| nreg += 1 STDERR.puts nreg @@ -409,10 +418,14 @@ menserror << "Nombre repetido con persona #{pnomrep.id} - #{pnomrep.nombres} #{pnomrep.apellidos} (#{pnomrep.tdocumento.sigla} #{pnomrep.numerodocumento}). " end - if region_depto[r["DEPARTAMENTO "]] - rds = region_depto[r["DEPARTAMENTO "]].split("-") + if region_depto[r["Departamento"]] + rds = region_depto[r["Departamento"]].split("-") else - rds = r["DEPARTAMENTO "].split("-") + if !r["Departamento"].nil? + rds = r["Departamento"].split("-") + else + rds = ["", ""] + end end rd = rds[0].strip pd = Msip::Departamento.where(pais_id: 170) @@ -428,7 +441,7 @@ rp["ultimo_departamento_trabajo_id"] = d.id end end - if rds.count == 1 || (rds.count == 2 && rd[1] = " correo laura ") + if rds.count == 1 || (rds.count == 2 && rd[1] == " correo laura ") rd = rds[0].strip else rd = rds[0].strip + " - " + rds[1].strip @@ -439,7 +452,8 @@ end pr = Regionpago.where('unaccent(nombre) ILIKE unaccent(?)', rd) if pr.count == 0 - debugger + #debugger + rp["ultima_regionpago_id"] = nil else rp["ultima_regionpago_id"] = pr.take.id end @@ -455,8 +469,20 @@ ce = cargo_entidad[r["Cargo"]] if ce rp["ultimo_cargoestado_id"] = ce[0] - rp["ultima_entidad_id"] = ce[1] - else + if importade == "F" + rp["ultima_entidad_id"] = ce[1] + else + rp["ultima_entidad_id"] = 60 # Rama judicial + end + else # En algunos registros el cargo está en la columna despacho + if importade == "J" + ce = cargo_entidad[r["Despacho"]] + if ce + rp["ultimo_cargoestado_id"] = ce[0] + rp["ultima_entidad_id"] = 60 # Rama judicial + end + end + preg << "Persona sin cargo/entidad (#{cargo})" end @@ -469,7 +495,7 @@ # Máxima id iba en 1907 end - ["abr-20","jun-20","jul-20","ago-20","sept-20","oct-20","nov-20","dic-20","ene-21","feb-21","mar-21","abr-21","may-21","jun-21","jul-21","ago-21","sept-21","oct-21","nov-21","dic-21"].each do |ma| + ["abr-20","may-20","jun-20","jul-20","ago-20","sept-20","oct-20","nov-20","dic-20","ene-21","feb-21","mar-21","abr-21","may-21","jun-21","jul-21","ago-21","sept-21","oct-21","nov-21","dic-21"].each do |ma| mad = ma.split("-") mes = trmes[mad[0]] anio = mad[1].to_i + 2000 @@ -488,15 +514,13 @@ else - csvprob << [ - r["#"],r["Cedula"],r["Apellidos"],r["Nombres"], - r["abr-20"],r["jun-20"],r["jul-20"],r["ago-20"],r["sept-20"], - r["oct-20"],r["nov-20"],r["dic-20"],r["ene-21"],r["feb-21"], - r["mar-21"],r["abr-21"],r["may-21"],r["jun-21"],r["jul-21"], - r["ago-21"],r["sept-21"],r["oct-21"],r["nov-21"],r["dic-21"], - r["DEPARTAMENTO "],r["mail"],r["celular"],r["Cargo"], - r["Seccional"],menserror - ] + nlin = [] + colesp.each do |c| + nlin << r[c] + end + nlin << menserror + + csvprob << nlin numerr += 1 end