salix/db/changes/10120-december/00-vn2008Clean.sql

124 lines
4.1 KiB
SQL

DROP procedure IF EXISTS `vn2008`.`clean`;
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `vn2008`.`clean`(IN `v_full` TINYINT(1))
proc: BEGIN
DECLARE v_date DATETIME;
DECLARE v_date18 DATETIME;
DECLARE v_date26 DATETIME;
DECLARE v_date8 DATE;
DECLARE v_date6 DATE;
DECLARE v_date3Month DATE;
DECLARE vDate2000 DATE;
DECLARE vRangeDeleteTicket INT;
DECLARE strtable varchar(15) DEFAULT NULL;
DECLARE done BIT DEFAULT 0;
SET v_date = TIMESTAMPADD(MONTH, -2, CURDATE());
SET v_date18 = TIMESTAMPADD(MONTH, -18,CURDATE());
SET v_date26 = TIMESTAMPADD(MONTH, -26,CURDATE());
SET v_date3Month = TIMESTAMPADD(MONTH, -3, CURDATE());
SET v_date8 = TIMESTAMPADD(DAY, -8,CURDATE());
SET v_date6 = TIMESTAMPADD(DAY, -6,CURDATE());
SET vRangeDeleteTicket = 60;
DELETE FROM cdr WHERE calldate < v_date;
DELETE FROM Monitoring WHERE ODBC_TIME < v_date;
DELETE FROM Conteo WHERE Fecha < v_date;
DELETE FROM XDiario WHERE FECHA < v_date3Month OR FECHA IS NULL;
DELETE FROM mail WHERE DATE_ODBC < v_date;
-- DELETE FROM Cajas WHERE CajaFecha < v_date18;
DELETE rr FROM Recibos_recorded rr JOIN Recibos r ON rr.Id_Recibos = r.Id WHERE r.Fechacobro < v_date;
SELECT MAX(idTickets_dits)
INTO @id
FROM Tickets_dits
WHERE ODBC_DATE < v_date;
DELETE FROM Tickets_dits WHERE idTickets_dits <= @id;
DELETE FROM expeditions_deleted WHERE odbc_date < v_date26;
DELETE FROM Entradas_dits WHERE ODBC_DATE < v_date18;
DELETE FROM log_articles WHERE ODBC_DATE < v_date;
DELETE FROM Splits WHERE Fecha < v_date18;
DELETE ts FROM Tickets_stack ts JOIN Tickets t ON ts.Id_Ticket = t.Id_Ticket WHERE t.Fecha < v_date;
DELETE tobs FROM movement_label tobs JOIN Movimientos m ON tobs.Id_Movimiento = m.Id_Movimiento
JOIN Tickets t ON m.Id_Ticket = t.Id_Ticket WHERE t.Fecha < v_date;
DELETE FROM chat WHERE odbc_date < v_date;
DELETE FROM Extractos WHERE Fecha < v_date;
DELETE FROM Remesas WHERE `Fecha Remesa` < v_date18;
DELETE FROM Stockcontrol WHERE Datestart < v_date18;
-- DELETE FROM reference_rate WHERE date < v_date18;
DELETE FROM hedera.`order` WHERE date_send < v_date18;
-- DELETE FROM Ordenes WHERE odbc_date < v_date18; JGF 2018-12-21 Si estan en un turno no hay que borrarlas.
SELECT MAX(inter_id)
INTO @id
FROM vncontrol.inter
WHERE odbc_date < v_date18;
DELETE FROM vncontrol.inter WHERE inter_id <= @id;
DELETE FROM Entradas_dits WHERE ODBC_DATE < v_date;
DELETE FROM cyc_declaration WHERE Fecha < v_date18;
DELETE FROM travel_reserve WHERE odbc_date < v_date;
-- DELETE FROM syslog.systemevents WHERE odbc_date < v_date8;
DELETE FROM cache.departure_limit WHERE Fecha < TIMESTAMPADD(MONTH,-1,CURDATE());
DELETE co
FROM Compres_ok co JOIN Compres c ON c.Id_Compra = co.Id_Compra
JOIN Entradas e ON e.Id_Entrada = c.Id_Entrada
JOIN travel t ON t.id = e.travel_id
WHERE t.landing <= v_date;
DELETE FROM vn2008.scan WHERE odbc_date < v_date6 AND id <> 1;
SET vDate2000 = TIMESTAMPADD(YEAR, 2000 - YEAR(CURDATE()), CURDATE());
IF v_full
THEN
DELETE t FROM Tickets t
LEFT JOIN Tickets_turno tt ON tt.Id_Ticket = t.Id_Ticket
WHERE Fecha NOT IN ('2000-01-01','2000-01-02')
AND YEAR(Fecha) = 2000
AND ABS(DATEDIFF(Fecha,vDate2000)) > vRangeDeleteTicket
AND tt.Id_Ticket IS NULL;
DELETE e.* FROM Entradas e
LEFT JOIN recibida_entrada re ON e.Id_Entrada = re.Id_Entrada
WHERE travel_id IS NULL
AND re.Id_Entrada IS NULL;
END IF;
-- Tickets Nulos PAK 11/10/2016
UPDATE vn2008.Tickets
SET empresa_id = 965
WHERE Id_Cliente = 31
AND empresa_id != 965;
-- Equipos duplicados
DELETE w.*
FROM vn2008.workerTeam w
JOIN (SELECT id, team, workerFk, COUNT(*) - 1 as duplicated
FROM vn.workerTeam
GROUP BY team,workerFk
HAVING duplicated
) d ON d.team = w.team AND d.workerFk = w.user AND d.id != w.id;
-- CAP 29/10/2018 Mantenimiento tabla Movimientos_componentes
DELETE mc
FROM vn2008.Movimientos_componentes mc
JOIN vn2008.Movimientos mv
ON mv.Id_Movimiento=mc.Id_Movimiento
JOIN vn2008.Tickets t
ON t.Id_Ticket= mv.Id_Ticket
WHERE t.Fecha<v_date18;
END$$
DELIMITER ;