diff --git a/db/routines/vn2008/events/raidUpdate.sql b/db/routines/vn/events/raidUpdate.sql similarity index 65% rename from db/routines/vn2008/events/raidUpdate.sql rename to db/routines/vn/events/raidUpdate.sql index aacfd6dcd..619dadb48 100644 --- a/db/routines/vn2008/events/raidUpdate.sql +++ b/db/routines/vn/events/raidUpdate.sql @@ -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 diff --git a/db/routines/vn/procedures/client_getDebt.sql b/db/routines/vn/procedures/client_getDebt.sql index ad7b5b7d2..3eaace4e9 100644 --- a/db/routines/vn/procedures/client_getDebt.sql +++ b/db/routines/vn/procedures/client_getDebt.sql @@ -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 diff --git a/db/routines/vn/procedures/creditRecovery.sql b/db/routines/vn/procedures/creditRecovery.sql new file mode 100644 index 000000000..687d652dd --- /dev/null +++ b/db/routines/vn/procedures/creditRecovery.sql @@ -0,0 +1,54 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`creditRecovery`() +BEGIN +/** + * 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'; + + DROP TEMPORARY TABLE IF EXISTS tCreditClients; + CREATE TEMPORARY TABLE tCreditClients + SELECT clientFk, IF(credit > recovery, credit - recovery, 0) newCredit + FROM ( + SELECT r.clientFk, + r.amount recovery, + (sub2.created + INTERVAL r.period DAY) deadLine, + sub2.amount credit + FROM recovery r + JOIN ( + SELECT clientFk, amount, created + FROM ( + SELECT clientFk, amount, created + FROM clientCredit + ORDER BY created DESC + LIMIT 10000000000000000000 + ) sub + 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() + ) 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 ; diff --git a/db/routines/vn/procedures/raidUpdate.sql b/db/routines/vn/procedures/raidUpdate.sql new file mode 100644 index 000000000..703c14e5c --- /dev/null +++ b/db/routines/vn/procedures/raidUpdate.sql @@ -0,0 +1,31 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`raidUpdate`() +BEGIN +/** + * 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 + 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 + 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 ; diff --git a/db/routines/vn/procedures/rateView.sql b/db/routines/vn/procedures/rateView.sql new file mode 100644 index 000000000..d840aa9d5 --- /dev/null +++ b/db/routines/vn/procedures/rateView.sql @@ -0,0 +1,38 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`rateView`() +BEGIN +/** + * 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, + 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 + 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 ; diff --git a/db/routines/vn2008/procedures/raidUpdate.sql b/db/routines/vn2008/procedures/raidUpdate.sql deleted file mode 100644 index 9746f3cf9..000000000 --- a/db/routines/vn2008/procedures/raidUpdate.sql +++ /dev/null @@ -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 ; diff --git a/db/routines/vn2008/procedures/rateView.sql b/db/routines/vn2008/procedures/rateView.sql deleted file mode 100644 index 91e317be6..000000000 --- a/db/routines/vn2008/procedures/rateView.sql +++ /dev/null @@ -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 ; diff --git a/db/routines/vn2008/procedures/recobro_credito.sql b/db/routines/vn2008/procedures/recobro_credito.sql deleted file mode 100644 index 3657f2b9b..000000000 --- a/db/routines/vn2008/procedures/recobro_credito.sql +++ /dev/null @@ -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 ; diff --git a/db/versions/10984-navyDendro/00-firstScript.sql b/db/versions/10984-navyDendro/00-firstScript.sql new file mode 100644 index 000000000..c08462d75 --- /dev/null +++ b/db/versions/10984-navyDendro/00-firstScript.sql @@ -0,0 +1,4 @@ +-- Place your SQL code here +UPDATE bs.nightTask SET `schema`='vn' WHERE `procedure`='raidUpdate'; + +UPDATE bs.nightTask SET `schema`='vn',`procedure`='creditRecovery' WHERE `procedure` ='recobro_credito'; \ No newline at end of file