Last active
November 13, 2021 05:05
-
-
Save raulsalinas/9ffddedb0823ce17f2a9babc6ab801c8 to your computer and use it in GitHub Desktop.
Depuración de tabla tesoreria.cliente e inserción a tabla contabilidad.adm_contri y comercial.com_cliente
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALTER TABLE tesoreria.cliente | |
ADD COLUMN duplicado BOOLEAN; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- https://kb.objectrocket.com/postgresql/how-to-use-postgres-to-remove-duplicates-from-a-table-1265 | |
UPDATE | |
tesoreria.cliente | |
SET | |
duplicado = TRUE | |
WHERE | |
id_cliente IN( | |
SELECT | |
id_cliente FROM ( | |
SELECT | |
id_cliente, ROW_NUMBER() OVER (PARTITION BY trim(nombre) ORDER BY id_cliente) AS row_num FROM tesoreria.cliente | |
WHERE | |
estado = 1) t | |
WHERE | |
t.row_num > 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALTER TABLE tesoreria.cliente | |
ADD COLUMN id_cliente_padre INTEGER; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- PASO 1: Crear funcion | |
CREATE OR REPLACE FUNCTION tesoreria.updateidclientepadre() | |
RETURNS SETOF tesoreria.cliente | |
LANGUAGE plpgsql | |
AS $function$ | |
DECLARE | |
r tesoreria.cliente%rowtype; | |
BEGIN | |
FOR r IN | |
(select * from tesoreria.cliente where duplicado ISNULL and estado =1) | |
LOOP | |
IF EXISTS (SELECT nombre FROM tesoreria.cliente WHERE nombre= r.nombre and cliente.duplicado =true) | |
THEN | |
update tesoreria.cliente set id_cliente_padre=r.id_cliente | |
WHERE nombre= r.nombre and cliente.duplicado =true; | |
END IF; | |
RETURN NEXT r; -- return current row of SELECT | |
END LOOP; | |
RETURN; | |
END; | |
$function$ | |
-- PASO 2: Ejecutar function | |
SELECT * FROM tesoreria.updateIdClientePadre(); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- PASO 1: Crear funcion | |
CREATE OR REPLACE FUNCTION tesoreria.updateVentaidcliente() | |
RETURNS SETOF tesoreria.cliente | |
LANGUAGE plpgsql | |
AS $function$ | |
DECLARE | |
r tesoreria.cliente%rowtype; | |
BEGIN | |
FOR r IN | |
(select * from tesoreria.cliente where duplicado =true) | |
LOOP | |
IF EXISTS (SELECT id_cliente FROM tesoreria.venta WHERE id_cliente= r.id_cliente ) | |
THEN | |
update tesoreria.venta set id_cliente=r.id_cliente_padre | |
WHERE id_cliente= r.id_cliente; | |
END IF; | |
RETURN NEXT r; -- return current row of SELECT | |
END LOOP; | |
RETURN; | |
END; | |
$function$ | |
-- PASO 2: Ejecutar function | |
SELECT * FROM tesoreria.updateVentaIdCliente(); | |
select * from tesoreria.venta | |
inner join tesoreria.cliente on venta.id_cliente = cliente.id_cliente | |
where cliente.duplicado =true | |
ORDER BY venta.id_cliente; | |
select * from tesoreria.cliente where id_cliente = 1143 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- PASO 1: Crear funcion | |
CREATE OR REPLACE FUNCTION tesoreria.updateCobranzaidcliente() | |
RETURNS SETOF tesoreria.cliente | |
LANGUAGE plpgsql | |
AS $function$ | |
DECLARE | |
r tesoreria.cliente%rowtype; | |
BEGIN | |
FOR r IN | |
(select * from tesoreria.cliente where duplicado =true) | |
LOOP | |
IF EXISTS (SELECT id_cliente FROM tesoreria.cobranza WHERE id_cliente= r.id_cliente ) | |
THEN | |
update tesoreria.cobranza set id_cliente=r.id_cliente_padre | |
WHERE id_cliente= r.id_cliente; | |
END IF; | |
RETURN NEXT r; -- return current row of SELECT | |
END LOOP; | |
RETURN; | |
END; | |
$function$ | |
-- PASO 2: Ejecutar function | |
SELECT * FROM tesoreria.updateCobranzaIdCliente(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- reiniciar secuencia de tabla | |
ALTER SEQUENCE contabilidad.id_contribuyente_seq RESTART WITH 1; | |
-- crear funcion para insertar los clientes en la tabla de contribuyentes | |
CREATE OR REPLACE FUNCTION tesoreria.insertarClientesComoContribuyente() | |
RETURNS SETOF tesoreria.cliente | |
LANGUAGE plpgsql | |
AS $function$ | |
DECLARE | |
r tesoreria.cliente%rowtype; | |
varIdContribuyente INTEGER; | |
BEGIN | |
FOR r IN | |
(select * from tesoreria.cliente where duplicado ISNULL and estado =1 ORDER by id_cliente asc) | |
LOOP | |
IF NOT EXISTS (SELECT razon_social FROM contabilidad.adm_contri WHERE razon_social = r.nombre and r.estado =1 and r.duplicado ISNULL) | |
THEN | |
INSERT INTO contabilidad.adm_contri (id_tipo_contribuyente, id_doc_identidad, nro_documento, razon_social, telefono, celular, direccion_fiscal, ubigeo, id_pais, estado, fecha_registro, transportista) | |
VALUES (null, null, r.ruc, r.nombre, null, null, null, null, null, 1, NOW()::timestamp, FALSE) RETURNING id_contribuyente into varIdContribuyente; | |
INSERT INTO comercial.com_cliente (id_cliente, id_contribuyente, codigo, estado, fecha_registro) | |
VALUES (r.id_cliente, varIdContribuyente, null,1,NOW()::timestamp); | |
END IF; | |
RETURN NEXT r; -- return current row of SELECT | |
END LOOP; | |
RETURN; | |
END; | |
$function$ | |
-- PASO 2: Ejecutar function | |
SELECT * FROM tesoreria.insertarClientesComoContribuyente(); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALTER SEQUENCE comercial.id_cliente_seq RESTART WITH 2066; | |
ALTER SEQUENCE contabilidad.id_contribuyente_seq RESTART WITH 1859; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with personas as ( | |
select rpe.email, rt.id_trabajador FROM configuracion.sis_usua cu | |
INNER JOIN rrhh.rrhh_trab rt ON rt.id_trabajador = cu.id_trabajador | |
INNER JOIN rrhh.rrhh_postu rp ON rp.id_postulante = rt.id_postulante | |
INNER JOIN rrhh.rrhh_perso rpe ON rpe.id_persona = rp.id_persona | |
WHERE cu.id_trabajador = rt.id_trabajador | |
) | |
update configuracion.sis_usua | |
set email = per.email | |
from personas per | |
where per.id_trabajador = sis_usua.id_trabajador; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH rows AS ( | |
INSERT INTO contabilidad.adm_contri (id_tipo_contribuyente, id_doc_identidad, nro_documento, razon_social, telefono, celular, direccion_fiscal, ubigeo, id_pais, estado, fecha_registro, transportista) | |
select null, 2, prov.ruc_auxi, prov.nom_auxi, prov.tel_auxi, null, prov.dir_auxi, null, null, 1, NOW()::timestamp, FALSE | |
FROM logistica.proveedores_csv prov RETURNING id_contribuyente | |
) | |
INSERT INTO logistica.log_prove (id_contribuyente,estado,fecha_registro) | |
SELECT id_contribuyente,1, NOW()::timestamp | |
FROM rows | |
RETURNING id_proveedor; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment