From 27d59f4a8f7ceb8eb17284483d2110fb83eef0fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Andr=C3=A9s?= Date: Thu, 18 Apr 2024 19:12:24 +0200 Subject: [PATCH] fix: descuentos en bs.ventas refs #6974 --- db/routines/bi/procedures/comparativa_add.sql | 32 ----------- .../bi/procedures/comparativa_add_manual.sql | 40 ------------- db/routines/bs/procedures/ventas_add.sql | 52 +++++++++-------- .../11001-blackPalmetto/00-firstScript.sql | 56 +++++++++++++++++++ 4 files changed, 84 insertions(+), 96 deletions(-) delete mode 100644 db/routines/bi/procedures/comparativa_add.sql delete mode 100644 db/routines/bi/procedures/comparativa_add_manual.sql create mode 100644 db/versions/11001-blackPalmetto/00-firstScript.sql diff --git a/db/routines/bi/procedures/comparativa_add.sql b/db/routines/bi/procedures/comparativa_add.sql deleted file mode 100644 index 4297c8aff..000000000 --- a/db/routines/bi/procedures/comparativa_add.sql +++ /dev/null @@ -1,32 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bi`.`comparativa_add`() -BEGIN - DECLARE lastCOMP INT; # Se trata de una variable para almacenar el ultimo valor del Periodo - DECLARE vMaxPeriod INT; - DECLARE vMaxWeek INT; - - SELECT t.period, t.`week` INTO vMaxPeriod, vMaxWeek - FROM vn.`time` t - WHERE t.dated = util.VN_CURDATE(); - - SELECT MAX(Periodo) INTO lastCOMP FROM vn2008.Comparativa; - -- Fijaremos las ventas con más de un mes de antiguedad en la tabla Comparativa - - IF lastCOMP < vMaxPeriod - 3 AND vMaxWeek > 3 THEN - - REPLACE vn2008.Comparativa(Periodo, Id_Article, warehouse_id, Cantidad,price) - SELECT tm.period as Periodo, m.Id_Article, t.warehouse_id, sum(m.Cantidad), sum(v.importe) - FROM bs.ventas v - JOIN vn2008.time tm ON tm.date = v.fecha - JOIN vn2008.Movimientos m ON m.Id_Movimiento = v.Id_Movimiento - JOIN vn2008.Tipos tp ON tp.tipo_id = v.tipo_id - JOIN vn2008.reinos r ON r.id = tp.reino_id - JOIN vn2008.Tickets t ON t.Id_Ticket = m.Id_Ticket - WHERE tm.period BETWEEN lastCOMP AND vMaxPeriod - 3 - AND t.Id_Cliente NOT IN(400,200) - AND t.warehouse_id NOT IN (0,13) - GROUP BY m.Id_Article, Periodo, t.warehouse_id; - - END IF; -END$$ -DELIMITER ; diff --git a/db/routines/bi/procedures/comparativa_add_manual.sql b/db/routines/bi/procedures/comparativa_add_manual.sql deleted file mode 100644 index 281e15b23..000000000 --- a/db/routines/bi/procedures/comparativa_add_manual.sql +++ /dev/null @@ -1,40 +0,0 @@ -DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bi`.`comparativa_add_manual`(IN vStarted DATE, IN vEnded DATE) -BEGIN -/** - * Recalcula la tabla Comparativa para dos valores dados - * - * @param vStarted fecha desde - * @param vEnded fecha hasta - */ - - DECLARE periodStart INT; - DECLARE periodEnd INT; - - -- Seleccionamos la fecha minima/maxima del periodo que vamos a consultar - - SELECT t.period INTO periodStart - FROM vn.`time` t - WHERE t.dated = vStarted; - - SELECT t.period INTO periodEnd - FROM vn.`time` t - WHERE t.dated = vEnded; - - DELETE FROM vn2008.Comparativa - WHERE Periodo BETWEEN periodStart AND periodEnd; - - INSERT INTO vn2008.Comparativa(Periodo, Id_Article, warehouse_id, Cantidad,price) - SELECT tm.period as Periodo, m.Id_Article, t.warehouse_id, sum(m.Cantidad), sum(v.importe) - FROM bs.ventas v - JOIN vn2008.time tm ON tm.date = v.fecha - JOIN vn2008.Movimientos m ON m.Id_Movimiento = v.Id_Movimiento - JOIN vn2008.Tipos tp ON tp.tipo_id = v.tipo_id - JOIN vn2008.reinos r ON r.id = tp.reino_id - JOIN vn2008.Tickets t ON t.Id_Ticket = m.Id_Ticket - WHERE tm.period BETWEEN periodStart AND periodEnd - AND t.Id_Cliente NOT IN(400,200) - AND t.warehouse_id NOT IN (0,13) - GROUP BY m.Id_Article, Periodo, t.warehouse_id; -END$$ -DELIMITER ; diff --git a/db/routines/bs/procedures/ventas_add.sql b/db/routines/bs/procedures/ventas_add.sql index fcb00e092..0c8f636e3 100644 --- a/db/routines/bs/procedures/ventas_add.sql +++ b/db/routines/bs/procedures/ventas_add.sql @@ -29,30 +29,34 @@ BEGIN WHILE vEndingDate <= vEnded DO - REPLACE ventas(Id_Movimiento, importe, recargo, fecha, tipo_id, Id_Cliente, empresa_id) - SELECT saleFk, - SUM(IF(ct.isBase, s.quantity * sc.value, 0)) importe, - SUM(IF(ct.isBase, 0, s.quantity * sc.value)) recargo, - vStartingDate, - i.typeFk, - a.clientFk, - t.companyFk - FROM vn.saleComponent sc - JOIN vn.component c ON c.id = sc.componentFk - JOIN vn.componentType ct ON ct.id = c.typeFk - JOIN vn.sale s ON s.id = sc.saleFk - JOIN vn.item i ON i.id = s.itemFk - JOIN vn.itemType it ON it.id = i.typeFk - JOIN vn.itemCategory ic ON ic.id = it.categoryFk - JOIN vn.ticket t ON t.id = s.ticketFk - JOIN vn.address a ON a.id = t.addressFk - JOIN vn.client cl ON cl.id = a.clientFk - WHERE t.shipped BETWEEN vStartingDate AND vEndingDate - AND s.quantity <> 0 - AND s.discount <> 100 - AND ic.merchandise - GROUP BY sc.saleFk - HAVING IFNULL(importe,0) <> 0 OR IFNULL(recargo,0) <> 0; + REPLACE ventas(Id_Movimiento, + importe, + recargo, + fecha, + tipo_id, + Id_Cliente, + empresa_id + )SELECT saleFk, + IFNULL(SUM(IF(ct.isBase, s.quantity * sc.value, 0)), 0) importe, + IFNULL(SUM(IF(ct.isBase, 0, s.quantity * sc.value)), 0) recargo, + vStartingDate, + i.typeFk, + a.clientFk, + t.companyFk + FROM vn.saleComponent sc + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + JOIN vn.sale s ON s.id = sc.saleFk + JOIN vn.item i ON i.id = s.itemFk + JOIN vn.itemType it ON it.id = i.typeFk + JOIN vn.itemCategory ic ON ic.id = it.categoryFk + JOIN vn.ticket t ON t.id = s.ticketFk + JOIN vn.address a ON a.id = t.addressFk + JOIN vn.client cl ON cl.id = a.clientFk + WHERE t.shipped BETWEEN vStartingDate AND vEndingDate + AND s.quantity <> 0 + AND ic.merchandise + GROUP BY sc.saleFk; UPDATE sale s JOIN ( diff --git a/db/versions/11001-blackPalmetto/00-firstScript.sql b/db/versions/11001-blackPalmetto/00-firstScript.sql new file mode 100644 index 000000000..b2a032265 --- /dev/null +++ b/db/versions/11001-blackPalmetto/00-firstScript.sql @@ -0,0 +1,56 @@ + UPDATE vn.componentType + SET isBase = 1 + WHERE code = 'MANA'; + + CREATE OR REPLACE TEMPORARY TABLE tmp.discountSale + (PRIMARY KEY (saleFk)) + SELECT bs.saleFk, + bs.amount, + s.discount, + SUM(IF(ct.isBase, s.quantity * sc.value, 0)) importe, + SUM(IF(ct.isBase, 0, s.quantity * sc.value)) recargo + FROM bs.sale bs + JOIN vn.sale s ON s.id = bs.saleFk + JOIN vn.saleComponent sc ON sc.saleFk = s.id + JOIN vn.component c ON c.id = sc.componentFk + JOIN vn.componentType ct ON ct.id = c.typeFk + WHERE s.discount + GROUP BY bs.saleFk; + UPDATE bs.sale bs + JOIN tmp.discountSale ts ON ts.saleFk = bs.saleFk + SET bs.amount = ts.importe + bs.surcharge = ts.recargo; + + DROP TEMPORARY TABLE tmp.discountSale; + + DELETE FROM comparative + WHERE timePeriod BETWEEN '202101' AND '202409'; + + INSERT INTO comparative( + timePeriod, + itemFk, + warehouseFk, + quantity, + price, + countryFk + ) + SELECT tm.period, + s.itemFk, + t.warehouseFk, + sum(s.quantity), + sum(v.importe), + p.countryFk + FROM bs.ventas v + JOIN time tm ON tm.dated = v.fecha + JOIN sale s ON s.id = v.Id_Movimiento + JOIN itemType tp ON tp.id = v.tipo_id + JOIN itemCategory ic ON ic.id = tp.categoryFk + JOIN ticket t ON t.id = s.ticketFk + JOIN client c ON c.id = t.clientFk + JOIN warehouse w ON w.id = t.warehouseFk + JOIN address ad ON ad.id = t.addressFk + LEFT JOIN province p ON p.id = ad.provinceFk + WHERE tm.period BETWEEN '202101' AND '202409' + AND c.typeFk <> 'loses' + AND NOT w.code = 'inv' + GROUP BY p.countryFk, s.itemFk, tm.period, t.warehouseFk;