feat: refs #6500 delete procedure
This commit is contained in:
parent
e83b0622cf
commit
2f0c1612c2
|
@ -3,27 +3,27 @@ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`riskAllClients`(max
|
|||
BEGIN
|
||||
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.client_list;
|
||||
CREATE TEMPORARY TABLE tmp.client_list
|
||||
(PRIMARY KEY (Id_Cliente))
|
||||
CREATE TEMPORARY TABLE tmp.client_list
|
||||
(PRIMARY KEY (Id_Cliente))
|
||||
ENGINE = MEMORY
|
||||
SELECT id Id_Cliente, null grade FROM vn.client;
|
||||
|
||||
CALL vn2008.risk_vs_client_list(maxRiskDate);
|
||||
|
||||
|
||||
CALL client_getDebt (maxRiskDate);
|
||||
|
||||
SELECT
|
||||
c.RazonSocial,
|
||||
c.Id_Cliente,
|
||||
c.Credito,
|
||||
CAST(r.risk as DECIMAL (10,2)) risk,
|
||||
CAST(c.Credito - r.risk as DECIMAL (10,2)) Diferencia,
|
||||
c.Id_Pais
|
||||
c.Id_Cliente,
|
||||
c.Credito,
|
||||
CAST(r.risk as DECIMAL (10,2)) risk,
|
||||
CAST(c.Credito - r.risk as DECIMAL (10,2)) Diferencia,
|
||||
c.Id_Pais
|
||||
FROM
|
||||
vn2008.Clientes c
|
||||
JOIN tmp.risk r ON r.Id_Cliente = c.Id_Cliente
|
||||
JOIN tmp.client_list ci ON c.Id_Cliente = ci.Id_Cliente
|
||||
GROUP BY c.Id_cliente;
|
||||
|
||||
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.risk;
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.client_list;
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.client_list;
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
|
|
@ -8,15 +8,15 @@ BEGIN
|
|||
ENGINE = MEMORY
|
||||
SELECT * FROM (SELECT cc.client Id_Cliente, ci.grade FROM vn.creditClassification cc
|
||||
JOIN vn.creditInsurance ci ON cc.id = ci.creditClassification
|
||||
WHERE dateEnd IS NULL
|
||||
ORDER BY ci.creationDate DESC
|
||||
WHERE dateEnd IS NULL
|
||||
ORDER BY ci.creationDate DESC
|
||||
LIMIT 10000000000000000000) t1 GROUP BY Id_Cliente;
|
||||
|
||||
CALL vn2008.risk_vs_client_list(util.VN_CURDATE());
|
||||
|
||||
|
||||
CALL client_getDebt (util.VN_CURDATE());
|
||||
|
||||
SELECT
|
||||
c.Id_Cliente, c.Cliente, c.Credito credito_vn, c.creditInsurance solunion, cast(r.risk as DECIMAL(10,0)) riesgo_vivo,
|
||||
cast(c.creditInsurance - r.risk as decimal(10,0)) margen_vivo,
|
||||
cast(c.creditInsurance - r.risk as decimal(10,0)) margen_vivo,
|
||||
f.Consumo consumo_anual, c.Vencimiento, ci.grade
|
||||
FROM
|
||||
vn2008.Clientes c
|
||||
|
@ -24,8 +24,8 @@ BEGIN
|
|||
JOIN tmp.client_list ci ON c.Id_Cliente = ci.Id_Cliente
|
||||
JOIN bi.facturacion_media_anual f ON c.Id_Cliente = f.Id_Cliente
|
||||
GROUP BY Id_cliente;
|
||||
|
||||
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.risk;
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.client_list;
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.client_list;
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `vn2008`.`raidUpdate`
|
||||
ON SCHEDULE EVERY 1 DAY
|
||||
STARTS '2017-12-29 00:05:00.000'
|
||||
ON COMPLETION PRESERVE
|
||||
ENABLE
|
||||
DO CALL raidUpdate$$
|
||||
DELIMITER ;
|
|
@ -1,28 +0,0 @@
|
|||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`raidUpdate`()
|
||||
BEGIN
|
||||
|
||||
UPDATE Entradas e
|
||||
JOIN Entradas_Auto ea USING (Id_Entrada)
|
||||
JOIN travel t ON t.id = e.travel_id
|
||||
JOIN (
|
||||
SELECT *
|
||||
FROM (
|
||||
SELECT id, landing, warehouse_id, warehouse_id_out
|
||||
FROM travel
|
||||
JOIN (
|
||||
SELECT warehouse_id, warehouse_id_out
|
||||
FROM Entradas_Auto ea
|
||||
JOIN Entradas e USING(Id_Entrada)
|
||||
JOIN travel t ON t.id = e.travel_id
|
||||
GROUP BY warehouse_id, warehouse_id_out
|
||||
) t USING (warehouse_id, warehouse_id_out)
|
||||
WHERE shipment > util.VN_CURDATE() AND delivered = FALSE
|
||||
ORDER BY landing
|
||||
LIMIT 10000000000000000000
|
||||
) t
|
||||
GROUP BY warehouse_id, warehouse_id_out
|
||||
) t USING (warehouse_id, warehouse_id_out)
|
||||
SET e.travel_id = t.id;
|
||||
END$$
|
||||
DELIMITER ;
|
|
@ -1,37 +0,0 @@
|
|||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`rateView`()
|
||||
BEGIN
|
||||
|
||||
SELECT
|
||||
t.year as año,
|
||||
t.month as mes,
|
||||
pagos.dolares,
|
||||
pagos.cambioPractico,
|
||||
CAST(sum(divisa) / sum(bi) as DECIMAL(5,4)) as cambioTeorico,
|
||||
pagos.cambioOficial
|
||||
FROM recibida r
|
||||
JOIN time t ON t.date = r.fecha
|
||||
JOIN recibida_iva ri ON r.id = ri.recibida_id
|
||||
JOIN
|
||||
(
|
||||
SELECT
|
||||
t.year as Año,
|
||||
t.month as Mes,
|
||||
cast(sum(divisa) as DECIMAL(10,2)) as dolares,
|
||||
cast(sum(divisa) / sum(importe) as DECIMAL(5,4)) as cambioPractico,
|
||||
cast(rr.rate * 0.998 as DECIMAL(5,4)) as cambioOficial
|
||||
FROM pago p
|
||||
JOIN time t ON t.date = p.fecha
|
||||
JOIN reference_rate rr ON rr.date = p.fecha AND moneda_id = 2
|
||||
WHERE divisa
|
||||
AND fecha >= '2015-01-11'
|
||||
GROUP BY t.year, t.month
|
||||
) pagos ON t.year = pagos.Año AND t.month = pagos.Mes
|
||||
WHERE moneda_id = 2
|
||||
AND fecha >= '2015-01-01'
|
||||
AND divisa
|
||||
AND bi
|
||||
GROUP BY t.year, t.month;
|
||||
|
||||
END$$
|
||||
DELIMITER ;
|
|
@ -1,45 +0,0 @@
|
|||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`recobro_credito`()
|
||||
BEGIN
|
||||
DECLARE EXIT HANDLER FOR SQLSTATE '45000'
|
||||
BEGIN
|
||||
ROLLBACK;
|
||||
RESIGNAL;
|
||||
END;
|
||||
|
||||
START TRANSACTION;
|
||||
UPDATE vn.`client` c
|
||||
JOIN vn.payMethod pm ON pm.id = c.payMethodFk
|
||||
SET credit = 0
|
||||
WHERE pm.`code` = 'card';
|
||||
|
||||
DROP TEMPORARY TABLE IF EXISTS clientes_credit;
|
||||
CREATE TEMPORARY TABLE clientes_credit
|
||||
SELECT Id_Cliente, if (Credito > Recobro ,Credito - Recobro,0) AS newCredit
|
||||
FROM (
|
||||
SELECT r.Id_Cliente, r.amount AS Recobro,
|
||||
timestampadd(DAY, period, UltimaFecha) AS Deadline, sub2.amount AS Credito
|
||||
FROM vn2008.recovery r
|
||||
JOIN (
|
||||
SELECT Id_Cliente, amount , odbc_date AS UltimaFecha
|
||||
FROM (
|
||||
SELECT * FROM credit
|
||||
ORDER BY odbc_date DESC
|
||||
LIMIT 10000000000000000000
|
||||
) sub
|
||||
GROUP BY Id_Cliente
|
||||
) sub2 USING(Id_Cliente)
|
||||
WHERE dend IS NULL or dend >= util.VN_CURDATE()
|
||||
GROUP BY Id_Cliente
|
||||
HAVING Deadline <= util.VN_CURDATE()
|
||||
) sub3
|
||||
WHERE Credito > 0;
|
||||
|
||||
UPDATE Clientes
|
||||
JOIN clientes_credit USING(Id_Cliente)
|
||||
SET Clientes.Credito = newCredit;
|
||||
|
||||
DROP TEMPORARY TABLE clientes_credit;
|
||||
COMMIT;
|
||||
END$$
|
||||
DELIMITER ;
|
|
@ -1,84 +0,0 @@
|
|||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`risk_vs_client_list`(maxRiskDate DATE)
|
||||
BEGIN
|
||||
/**
|
||||
* Calcula el riesgo para los clientes activos de la tabla temporal tmp.client_list
|
||||
*
|
||||
* @deprecated usar vn.client_getDebt
|
||||
* @param maxRiskDate Fecha maxima de los registros
|
||||
* @return table tmp.risk
|
||||
*/
|
||||
DECLARE startingDate DATETIME DEFAULT TIMESTAMPADD(DAY, - DAYOFMONTH(util.VN_CURDATE()) - 60, util.VN_CURDATE());
|
||||
DECLARE endingDate DATETIME;
|
||||
DECLARE MAX_RISK_ALLOWED INT DEFAULT 200;
|
||||
|
||||
SET maxRiskDate = IFNULL(maxRiskDate, util.VN_CURDATE());
|
||||
SET endingDate = TIMESTAMP(maxRiskDate, '23:59:59');
|
||||
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.client_list_2;
|
||||
CREATE TEMPORARY TABLE tmp.client_list_2
|
||||
(PRIMARY KEY (Id_Cliente))
|
||||
ENGINE = MEMORY
|
||||
SELECT *
|
||||
FROM tmp.client_list;
|
||||
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.client_list_3;
|
||||
CREATE TEMPORARY TABLE tmp.client_list_3
|
||||
(PRIMARY KEY (Id_Cliente))
|
||||
ENGINE = MEMORY
|
||||
SELECT *
|
||||
FROM tmp.client_list;
|
||||
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.tickets_sin_facturar;
|
||||
CREATE TEMPORARY TABLE tmp.tickets_sin_facturar
|
||||
(PRIMARY KEY (Id_Cliente))
|
||||
ENGINE = MEMORY
|
||||
SELECT t.Id_Cliente, floor(IF(cl.isVies, 1, 1.1) * sum(Cantidad * Preu * (100 - Descuento) / 100)) as total
|
||||
FROM Movimientos m
|
||||
JOIN Tickets t on m.Id_Ticket = t.Id_Ticket
|
||||
JOIN tmp.client_list c on c.Id_Cliente = t.Id_Cliente
|
||||
JOIN vn.client cl ON cl.id = t.Id_Cliente
|
||||
WHERE Factura IS NULL
|
||||
AND Fecha BETWEEN startingDate AND endingDate
|
||||
GROUP BY t.Id_Cliente;
|
||||
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.risk;
|
||||
CREATE TEMPORARY TABLE tmp.risk
|
||||
(PRIMARY KEY (Id_Cliente))
|
||||
ENGINE = MEMORY
|
||||
SELECT Id_Cliente, SUM(amount) risk, sum(saldo) saldo
|
||||
FROM Clientes c
|
||||
JOIN (
|
||||
SELECT clientFk, SUM(amount) amount,SUM(amount) saldo
|
||||
FROM vn.clientRisk
|
||||
JOIN tmp.client_list on Id_Cliente = clientFk
|
||||
GROUP BY clientFk
|
||||
UNION ALL
|
||||
SELECT Id_Cliente, SUM(Entregado),SUM(Entregado)
|
||||
FROM Recibos
|
||||
JOIN tmp.client_list_2 using(Id_Cliente)
|
||||
WHERE Fechacobro > endingDate
|
||||
GROUP BY Id_Cliente
|
||||
UNION ALL
|
||||
SELECT Id_Cliente, total,0
|
||||
FROM tmp.tickets_sin_facturar
|
||||
UNION ALL
|
||||
SELECT t.clientFk, CAST(-SUM(t.amount) / 100 AS DECIMAL(10,2)), CAST(-SUM(t.amount) / 100 AS DECIMAL(10,2))
|
||||
FROM hedera.tpvTransaction t
|
||||
JOIN tmp.client_list_3 on Id_Cliente = t.clientFk
|
||||
WHERE t.receiptFk IS NULL
|
||||
AND t.status = 'ok'
|
||||
GROUP BY t.clientFk
|
||||
) t ON c.Id_Cliente = t.clientFk
|
||||
WHERE c.activo != FALSE
|
||||
GROUP BY c.Id_Cliente;
|
||||
|
||||
DELETE r.*
|
||||
FROM tmp.risk r
|
||||
JOIN vn2008.Clientes c on c.Id_Cliente = r.Id_Cliente
|
||||
JOIN vn2008.pay_met pm on pm.id = c.pay_met_id
|
||||
WHERE IFNULL(r.saldo,0) < 10
|
||||
AND r.risk <= MAX_RISK_ALLOWED
|
||||
AND pm.`name` = 'TARJETA';
|
||||
END$$
|
||||
DELIMITER ;
|
Loading…
Reference in New Issue