From 1bcb61a22458e684fa8dd97de6b8de8e37f4b070 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 12 Mar 2024 08:25:58 +0100 Subject: [PATCH 1/3] refactor: refs #7018 Mirated procs to vn --- .../vn/procedures/invoiceInTaxMakeByDua.sql | 9 ++- .../vn/procedures/invoiceInTax_insert.sql | 56 +++++++++++++++++++ .../procedures/ListaTicketsEncajados.sql | 25 --------- .../procedures/customerDebtEvolution.sql | 49 ---------------- .../vn2008/procedures/preOrdenarRuta.sql | 22 -------- .../vn2008/procedures/prepare_ticket_list.sql | 22 -------- .../vn2008/procedures/recibidaIvaInsert.sql | 39 ------------- 7 files changed, 62 insertions(+), 160 deletions(-) create mode 100644 db/routines/vn/procedures/invoiceInTax_insert.sql delete mode 100644 db/routines/vn2008/procedures/ListaTicketsEncajados.sql delete mode 100644 db/routines/vn2008/procedures/customerDebtEvolution.sql delete mode 100644 db/routines/vn2008/procedures/preOrdenarRuta.sql delete mode 100644 db/routines/vn2008/procedures/prepare_ticket_list.sql delete mode 100644 db/routines/vn2008/procedures/recibidaIvaInsert.sql diff --git a/db/routines/vn/procedures/invoiceInTaxMakeByDua.sql b/db/routines/vn/procedures/invoiceInTaxMakeByDua.sql index 2ff478d6b..6707486d0 100644 --- a/db/routines/vn/procedures/invoiceInTaxMakeByDua.sql +++ b/db/routines/vn/procedures/invoiceInTaxMakeByDua.sql @@ -1,8 +1,11 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`invoiceInTaxMakeByDua`(vDuaFk INT) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`invoiceInTaxMakeByDua`( + vDuaFk INT +) BEGIN /** - * Borra los valores de duaTax y sus vctos. y los vuelve a crear en base a la tabla duaEntry + * Borra los valores de duaTax y sus vctos. y los vuelve a + * crear en base a la tabla duaEntry. * * @param vDuaFk Id del dua a recalcular */ @@ -26,7 +29,7 @@ BEGIN LEAVE l; END IF; - CALL vn2008.recibidaIvaInsert(vInvoiceInFk); + CALL invoiceInTax_insert(vInvoiceInFk); CALL invoiceInDueDay_recalc(vInvoiceInFk); END LOOP; diff --git a/db/routines/vn/procedures/invoiceInTax_insert.sql b/db/routines/vn/procedures/invoiceInTax_insert.sql new file mode 100644 index 000000000..e43a7763d --- /dev/null +++ b/db/routines/vn/procedures/invoiceInTax_insert.sql @@ -0,0 +1,56 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`invoiceInTax_insert`( + IN vSelf INT +) +BEGIN + DECLARE vRate DOUBLE DEFAULT 1; + DECLARE vDated DATE; + DECLARE vExpenseFk INT; + + SELECT MAX(rr.dated) INTO vDated + FROM referenceRate rr + JOIN invoiceIn ii ON ii.id = vSelf + WHERE rr.dated <= ii.issued + AND rr.currencyFk = ii.currencyFk; + + IF vDated THEN + SELECT `value` INTO vRate + FROM referenceRate + WHERE dated = vDated; + END IF; + + DELETE FROM invoiceInTax WHERE invoiceInFk = vSelf; + + SELECT id INTO vExpenseFk + FROM expense + WHERE code = 'extraCommGoodsAcquisition'; + + IF vExpenseFk IS NULL THEN + CALL util.throw('Expense extraCommGoodsAcquisition not exists'); + END IF; + + INSERT INTO invoiceInTax( + invoiceInFk, + taxableBase, + expenseFk, + foreignValue, + taxTypeSageFk, + transactionTypeSageFk + ) + SELECT ii.id, + SUM(b.buyingValue * b.quantity) / vRate bi, + vExpenseFk, + IF(c.code = 'EUR', NULL, SUM(b.buyingValue * b.quantity)), + s.taxTypeSageFk, + s.transactionTypeSageFk + FROM invoiceIn ii + JOIN currency c ON c.id = ii.currencyFk + JOIN `entry` e ON e.invoiceInFk = ii.id + JOIN supplier s ON s.id = e.supplierFk + JOIN buy b ON b.entryFk = e.id + LEFT JOIN referenceRate rr ON rr.currencyFk = ii.currencyFk + AND rr.dated = ii.issued + WHERE ii.id = vSelf + HAVING bi IS NOT NULL; +END$$ +DELIMITER ; diff --git a/db/routines/vn2008/procedures/ListaTicketsEncajados.sql b/db/routines/vn2008/procedures/ListaTicketsEncajados.sql deleted file mode 100644 index ce99916ee..000000000 --- a/db/routines/vn2008/procedures/ListaTicketsEncajados.sql +++ /dev/null @@ -1,25 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`ListaTicketsEncajados`(IN intId_Trabajador int) -BEGIN - -SELECT Agencia, - Consignatario, - ti.Id_Ticket, - ts.userFk Id_Trabajador, - IFNULL(ncajas,0) AS ncajas, - IFNULL(nbultos,0) AS nbultos, - IFNULL(notros,0) AS notros, - ts.code AS Estado - FROM Tickets ti - INNER JOIN Consignatarios ON ti.Id_Consigna = Consignatarios.Id_consigna - INNER JOIN Agencias ON ti.Id_Agencia = Agencias.Id_Agencia - LEFT JOIN (SELECT ticketFk,count(*) AS ncajas FROM vn.expedition WHERE packagingFk=94 GROUP BY ticketFk) sub1 ON ti.Id_Ticket=sub1.ticketFk - LEFT JOIN (SELECT ticketFk,count(*) AS nbultos FROM vn.expedition WHERE packagingFk IS NULL GROUP BY ticketFk) sub2 ON ti.Id_Ticket=sub2.ticketFk - LEFT JOIN (SELECT ticketFk,count(*) AS notros FROM vn.expedition WHERE packagingFk >0 GROUP BY ticketFk) sub3 ON ti.Id_Ticket=sub3.ticketFk - INNER JOIN vn.ticketState ts ON ti.Id_ticket = ts.ticketFk - WHERE ti.Fecha=util.VN_CURDATE() AND - ts.userFk=intId_Trabajador - GROUP BY ti.Id_Ticket; - -END$$ -DELIMITER ; diff --git a/db/routines/vn2008/procedures/customerDebtEvolution.sql b/db/routines/vn2008/procedures/customerDebtEvolution.sql deleted file mode 100644 index b9763b985..000000000 --- a/db/routines/vn2008/procedures/customerDebtEvolution.sql +++ /dev/null @@ -1,49 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`customerDebtEvolution`(IN vCustomer INT) -BEGIN - -SELECT * FROM -( - SELECT day, date, @s:= round(IFNULL(Euros,0) + @s,2) as Saldo, Euros, Credito, 0 as Cero - FROM - ( - SELECT day, date, IFNULL(Euros,0) as Euros, Credito - FROM time - JOIN (SELECT @s:= 0, - Credito as Credito FROM Clientes WHERE Id_Cliente = vCustomer) c - LEFT JOIN - (SELECT Euros, date(Fecha) as Fecha FROM - ( - SELECT Fechacobro as Fecha, Entregado as Euros - FROM Recibos - WHERE Id_Cliente = vCustomer - AND Fechacobro >= '2017-01-01' - UNION ALL - SELECT vn.getDueDate(io.issued,c.Vencimiento), - io.amount - FROM vn.invoiceOut io - JOIN Clientes c ON io.clientFk = c.Id_Cliente - WHERE io.clientFk = vCustomer - AND io.issued >= '2017-01-01' - UNION ALL - SELECT '2016-12-31', Debt - FROM bi.customerDebtInventory - WHERE Id_Cliente = vCustomer - UNION ALL - SELECT Fecha, - SUM(Cantidad * Preu * (100 - Descuento ) * 1.10 / 100) - FROM Tickets t - JOIN Movimientos m on m.Id_Ticket = t.Id_Ticket - WHERE Id_Cliente = vCustomer - AND Factura IS NULL - AND Fecha >= '2017-01-01' - GROUP BY Fecha - ) sub2 - ORDER BY Fecha - )sub ON time.date = sub.Fecha - WHERE time.date BETWEEN '2016-12-31' AND util.VN_CURDATE() - ORDER BY date - ) sub3 -)sub4 -; - - -END$$ -DELIMITER ; diff --git a/db/routines/vn2008/procedures/preOrdenarRuta.sql b/db/routines/vn2008/procedures/preOrdenarRuta.sql deleted file mode 100644 index b5fd2b24b..000000000 --- a/db/routines/vn2008/procedures/preOrdenarRuta.sql +++ /dev/null @@ -1,22 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`preOrdenarRuta`(IN vRutaId INT) -BEGIN -/* Usa los valores del ultimo año para adivinar el orden de los tickets en la ruta - * vRutaId id ruta - * DEPRECATED use vn.routeGressPriority -*/ - -UPDATE Tickets mt -JOIN ( - SELECT tt.Id_Consigna, round(ifnull(avg(t.Prioridad),0),0) as Prioridad - from Tickets t - JOIN Tickets tt on tt.Id_Consigna = t.Id_Consigna - where t.Fecha > TIMESTAMPADD(YEAR,-1,util.VN_CURDATE()) - AND tt.Id_Ruta = vRutaId - GROUP BY Id_Consigna - ) sub ON sub.Id_Consigna = mt.Id_Consigna - SET mt.Prioridad = sub.Prioridad - WHERE mt.Id_Ruta = vRutaId; - -END$$ -DELIMITER ; diff --git a/db/routines/vn2008/procedures/prepare_ticket_list.sql b/db/routines/vn2008/procedures/prepare_ticket_list.sql deleted file mode 100644 index e407a91b7..000000000 --- a/db/routines/vn2008/procedures/prepare_ticket_list.sql +++ /dev/null @@ -1,22 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`prepare_ticket_list`(vStartingDate DATETIME, vEndingDate DATETIME) -BEGIN - DROP TEMPORARY TABLE IF EXISTS tmp.ticket_list; - CREATE TEMPORARY TABLE tmp.ticket_list - (PRIMARY KEY (Id_Ticket)) - ENGINE = MEMORY - SELECT t.Id_Ticket, c.Id_Cliente - FROM Tickets t - LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.Id_Ticket - JOIN Clientes c ON c.Id_Cliente = t.Id_Cliente - WHERE c.typeFk IN ('normal','handMaking','internalUse') - AND ( - Fecha BETWEEN util.today() AND vEndingDate - OR ( - ts.alertLevel < 3 - AND t.Fecha >= vStartingDate - AND t.Fecha < util.today() - ) - ); -END$$ -DELIMITER ; diff --git a/db/routines/vn2008/procedures/recibidaIvaInsert.sql b/db/routines/vn2008/procedures/recibidaIvaInsert.sql deleted file mode 100644 index e2aba0a35..000000000 --- a/db/routines/vn2008/procedures/recibidaIvaInsert.sql +++ /dev/null @@ -1,39 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`recibidaIvaInsert`(IN vId INT) -BEGIN - - DECLARE vRate DOUBLE DEFAULT 1; - DECLARE vDated DATE; - - SELECT MAX(rr.date) INTO vDated - FROM reference_rate rr - JOIN recibida r ON r.id = vId - WHERE rr.date <= r.fecha - AND rr.moneda_id = r.moneda_id ; - - IF vDated THEN - - SELECT rate INTO vRate - FROM reference_rate - WHERE `date` = vDated; - END IF; - - DELETE FROM recibida_iva WHERE recibida_id = vId; - - INSERT INTO recibida_iva(recibida_id, bi, gastos_id, divisa, taxTypeSageFk, transactionTypeSageFk) - SELECT r.id, - SUM(Costefijo * Cantidad) / IFNULL(vRate,1) bi, - 6003000000, - IF(r.moneda_id = 1,NULL,SUM(Costefijo * Cantidad )) divisa, - taxTypeSageFk, - transactionTypeSageFk - FROM recibida r - JOIN Entradas e ON e.recibida_id = r.id - JOIN Proveedores p ON p.Id_Proveedor = e.Id_Proveedor - JOIN Compres c ON c.Id_Entrada = e.Id_Entrada - LEFT JOIN reference_rate rr ON rr.moneda_id = r.moneda_id AND rr.date = r.fecha - WHERE r.id = vId - HAVING bi IS NOT NULL; - -END$$ -DELIMITER ; From e2eddbaef7d7ca8ccd297ac4265aef3290899507 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 12 Mar 2024 08:35:13 +0100 Subject: [PATCH 2/3] refactor: refs #7018 Added comment --- db/routines/vn/procedures/invoiceInTax_insert.sql | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/db/routines/vn/procedures/invoiceInTax_insert.sql b/db/routines/vn/procedures/invoiceInTax_insert.sql index e43a7763d..761d8b0c5 100644 --- a/db/routines/vn/procedures/invoiceInTax_insert.sql +++ b/db/routines/vn/procedures/invoiceInTax_insert.sql @@ -1,8 +1,14 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`invoiceInTax_insert`( - IN vSelf INT + vSelf INT ) BEGIN +/** + * Inserta los impuestos de una factura, ajustando por + * tasa de cambio y detalles de compra. + * + * @param vSelf Id de invoiceInTax + */ DECLARE vRate DOUBLE DEFAULT 1; DECLARE vDated DATE; DECLARE vExpenseFk INT; From 1d398a0acf6b71ccf35b97a8f34bb5b8559d8f31 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 12 Mar 2024 08:40:11 +0100 Subject: [PATCH 3/3] refactor: refs #7018 Added comment and minor changes --- .../vn/procedures/invoiceInTaxMakeByDua.sql | 2 +- ...eInTax_insert.sql => invoiceInTax_recalc.sql} | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) rename db/routines/vn/procedures/{invoiceInTax_insert.sql => invoiceInTax_recalc.sql} (79%) diff --git a/db/routines/vn/procedures/invoiceInTaxMakeByDua.sql b/db/routines/vn/procedures/invoiceInTaxMakeByDua.sql index 6707486d0..3453516cc 100644 --- a/db/routines/vn/procedures/invoiceInTaxMakeByDua.sql +++ b/db/routines/vn/procedures/invoiceInTaxMakeByDua.sql @@ -29,7 +29,7 @@ BEGIN LEAVE l; END IF; - CALL invoiceInTax_insert(vInvoiceInFk); + CALL invoiceInTax_recalc(vInvoiceInFk); CALL invoiceInDueDay_recalc(vInvoiceInFk); END LOOP; diff --git a/db/routines/vn/procedures/invoiceInTax_insert.sql b/db/routines/vn/procedures/invoiceInTax_recalc.sql similarity index 79% rename from db/routines/vn/procedures/invoiceInTax_insert.sql rename to db/routines/vn/procedures/invoiceInTax_recalc.sql index 761d8b0c5..3b5ce5247 100644 --- a/db/routines/vn/procedures/invoiceInTax_insert.sql +++ b/db/routines/vn/procedures/invoiceInTax_recalc.sql @@ -1,13 +1,13 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`invoiceInTax_insert`( - vSelf INT +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`invoiceInTax_recalc`( + vInvoiceInFk INT ) BEGIN /** - * Inserta los impuestos de una factura, ajustando por - * tasa de cambio y detalles de compra. + * Recalcula y actualiza los impuestos de la factura + * usando la última tasa de cambio y detalles de compra. * - * @param vSelf Id de invoiceInTax + * @param vInvoiceInFk Id de factura recibida */ DECLARE vRate DOUBLE DEFAULT 1; DECLARE vDated DATE; @@ -15,7 +15,7 @@ BEGIN SELECT MAX(rr.dated) INTO vDated FROM referenceRate rr - JOIN invoiceIn ii ON ii.id = vSelf + JOIN invoiceIn ii ON ii.id = vInvoiceInFk WHERE rr.dated <= ii.issued AND rr.currencyFk = ii.currencyFk; @@ -25,7 +25,7 @@ BEGIN WHERE dated = vDated; END IF; - DELETE FROM invoiceInTax WHERE invoiceInFk = vSelf; + DELETE FROM invoiceInTax WHERE invoiceInFk = vInvoiceInFk; SELECT id INTO vExpenseFk FROM expense @@ -56,7 +56,7 @@ BEGIN JOIN buy b ON b.entryFk = e.id LEFT JOIN referenceRate rr ON rr.currencyFk = ii.currencyFk AND rr.dated = ii.issued - WHERE ii.id = vSelf + WHERE ii.id = vInvoiceInFk HAVING bi IS NOT NULL; END$$ DELIMITER ;