feat: refs #6500 procRefactor8 #2131

Merged
robert merged 28 commits from 6500-procRefactor8 into dev 2024-04-17 11:32:54 +00:00
9 changed files with 131 additions and 114 deletions

View File

@ -1,5 +1,5 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `vn2008`.`raidUpdate`
CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `vn`.`raidUpdate`
ON SCHEDULE EVERY 1 DAY
STARTS '2017-12-29 00:05:00.000'
ON COMPLETION PRESERVE

View File

@ -17,15 +17,15 @@ BEGIN
SET vEnded = util.dayEnd(IFNULL(vDate, util.VN_CURDATE()));
CREATE OR REPLACE TEMPORARY TABLE tClientRisk
CREATE OR REPLACE TEMPORARY TABLE tClientRisk
ENGINE = MEMORY
SELECT cr.clientFk, SUM(cr.amount) amount
SELECT cr.clientFk, SUM(cr.amount) amount
FROM clientRisk cr
JOIN tmp.clientGetDebt c ON c.clientFk = cr.clientFk
GROUP BY cr.clientFk;
INSERT INTO tClientRisk
SELECT c.clientFk, SUM(r.amountPaid)
SELECT c.clientFk, SUM(r.amountPaid)
FROM receipt r
JOIN tmp.clientGetDebt c ON c.clientFk = r.clientFk
WHERE r.payed > vEnded

View File

@ -0,0 +1,54 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`creditRecovery`()
BEGIN
robert marked this conversation as resolved
Review

Falta descripción del procedimiento

Falta descripción del procedimiento
/**
* Actualiza el crédito de los clientes
*/
DECLARE EXIT HANDLER FOR SQLSTATE '45000'
BEGIN
ROLLBACK;
RESIGNAL;
END;
START TRANSACTION;
UPDATE `client` c
JOIN payMethod pm ON pm.id = c.payMethodFk
SET c.credit = 0
WHERE pm.`code` = 'card';
robert marked this conversation as resolved Outdated

Espacios: SELECT clientFk, IF (credit > recovery, credit - recovery, 0) newCredit

Espacios: SELECT clientFk, IF (credit > recovery, credit - recovery, 0) newCredit
robert marked this conversation as resolved Outdated

tabulación FROM

tabulación FROM
DROP TEMPORARY TABLE IF EXISTS tCreditClients;
CREATE TEMPORARY TABLE tCreditClients
SELECT clientFk, IF(credit > recovery, credit - recovery, 0) newCredit
robert marked this conversation as resolved Outdated

INTERVAL

INTERVAL
FROM (
SELECT r.clientFk,
r.amount recovery,
(sub2.created + INTERVAL r.period DAY) deadLine,
sub2.amount credit
FROM recovery r
robert marked this conversation as resolved Outdated

Especificar campos, eliminar *

Especificar campos, eliminar *
JOIN (
SELECT clientFk, amount, created
FROM (
SELECT clientFk, amount, created
FROM clientCredit
ORDER BY created DESC
LIMIT 10000000000000000000
) sub
robert marked this conversation as resolved Outdated

tabulación HAVING

tabulación HAVING
GROUP BY clientFk
) sub2 ON sub2.clientFk = r.clientFk
WHERE r.finished IS NULL OR r.finished >= util.VN_CURDATE()
GROUP BY r.clientFk
HAVING deadLine <= util.VN_CURDATE()
robert marked this conversation as resolved Outdated

tabulación JOIN y SET

tabulación JOIN y SET
) sub3
WHERE credit > 0;
UPDATE client c
JOIN tCreditClients cc ON cc.clientFk = c.id
SET c.credit = newCredit;
DROP TEMPORARY TABLE tCreditClients;
COMMIT;
END$$
DELIMITER ;

View File

@ -0,0 +1,31 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`raidUpdate`()
BEGIN
/**
robert marked this conversation as resolved Outdated

falta descripción

falta descripción
* Actualiza el travel de las entradas de redadas
*/
UPDATE entry e
JOIN entryVirtual ev ON ev.entryFk = e.id
JOIN travel t ON t.id = e.travelFk
JOIN (
SELECT *
FROM (
SELECT t.id, t.landed, tt.warehouseInFk, tt.warehouseOutFk
robert marked this conversation as resolved Outdated

faltan alias en los campos

faltan alias en los campos
FROM travel t
JOIN (
SELECT t.warehouseInFk, t.warehouseOutFk
FROM entryVirtual ev
JOIN entry e ON e.id = ev.entryFk
JOIN travel t ON t.id = e.travelFk
GROUP BY t.warehouseInFk, t.warehouseOutFk
) tt ON t.warehouseInFk = tt.warehouseInFk AND t.warehouseOutFk = tt.warehouseOutFk
WHERE shipped > util.VN_CURDATE() AND NOT isDelivered
robert marked this conversation as resolved Outdated

isDelivered = FALSE

NOT isDelivered

Faltan alias

isDelivered = FALSE NOT isDelivered Faltan alias
ORDER BY t.landed
LIMIT 10000000000000000000
) t
GROUP BY t.warehouseInFk, t.warehouseOutFk
) tt ON t.warehouseInFk = tt.warehouseInFk AND t.warehouseOutFk = tt.warehouseOutFk
SET e.travelFk = t.id;
END$$
DELIMITER ;

View File

@ -0,0 +1,38 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`rateView`()
BEGIN
/**
robert marked this conversation as resolved Outdated

falta descripción

falta descripción
* Muestra información sobre tasas de cambio de Dolares
*/
SELECT
t.year año,
t.month mes,
pay.dollars dolares,
pay.changePractical cambioPractico,
CAST(SUM(iit.foreignValue ) / SUM(iit.taxableBase) AS DECIMAL(5,4))cambioTeorico,
pay.changeOfficial cambioOficial
FROM invoiceIn ii
JOIN time t ON t.dated = ii.issued
JOIN invoiceInTax iit ON ii.id = iit.invoiceInFk
JOIN
( SELECT
t.year,
t.month,
CAST(SUM(p.divisa) AS DECIMAL(10,2)) dollars,
robert marked this conversation as resolved Outdated

faltan alias, no se sabe de que tablas vienen los campos

faltan alias, no se sabe de que tablas vienen los campos
CAST(SUM(p.divisa) / SUM(p.amount) AS DECIMAL(5,4)) changePractical,
CAST(rr.value * 0.998 AS DECIMAL(5,4)) changeOfficial
FROM payment p
JOIN time t ON t.dated = p.received
JOIN referenceRate rr ON rr.dated = p.received
JOIN currency c ON c.id = rr.currencyFk
WHERE p.divisa
AND c.code = 'USD'
GROUP BY t.year, t.month
) pay ON t.year = pay.year AND t.month = pay.month
robert marked this conversation as resolved Outdated

En la medida de lo posible no se debe de escribir en español, en este caso el nombre de la subconsulta es totalmente evitable que este en español, traducir

En la medida de lo posible no se debe de escribir en español, en este caso el nombre de la subconsulta es totalmente evitable que este en español, traducir
JOIN currency c ON c.id = ii.currencyFk
WHERE c.code = 'USD'
AND iit.foreignValue
AND iit.taxableBase
GROUP BY t.year, t.month;
END$$
DELIMITER ;

View File

@ -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 ;

View File

@ -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 ;

View File

@ -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 ;

View File

@ -0,0 +1,4 @@
-- Place your SQL code here
UPDATE bs.nightTask SET `schema`='vn' WHERE `procedure`='raidUpdate';
robert marked this conversation as resolved Outdated

Utilizar el nombre del procedure y no la posición

Utilizar el nombre del procedure y no la posición
UPDATE bs.nightTask SET `schema`='vn',`procedure`='creditRecovery' WHERE `procedure` ='recobro_credito';
robert marked this conversation as resolved Outdated

Utilizar el nombre del procedure y no la posición

Utilizar el nombre del procedure y no la posición