2024-01-31 11:14:24 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bi`.`analisis_ventas_update`()
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
DECLARE vLastMonth DATE;
|
|
|
|
|
|
|
|
SET vLastMonth = util.firstDayOfMonth(TIMESTAMPADD(MONTH, -1, util.VN_CURDATE()));
|
|
|
|
|
|
|
|
DELETE FROM analisis_ventas
|
|
|
|
WHERE Año > YEAR(vLastMonth)
|
|
|
|
OR (Año = YEAR(vLastMonth) AND Mes >= MONTH(vLastMonth));
|
|
|
|
|
|
|
|
INSERT INTO analisis_ventas (
|
|
|
|
Familia,
|
|
|
|
Reino,
|
|
|
|
Comercial,
|
|
|
|
Comprador,
|
|
|
|
Provincia,
|
|
|
|
almacen,
|
|
|
|
Año,
|
|
|
|
Mes,
|
|
|
|
Semana,
|
|
|
|
Vista,
|
|
|
|
Importe
|
|
|
|
)
|
|
|
|
SELECT
|
|
|
|
tp.Tipo AS Familia,
|
|
|
|
r.reino AS Reino,
|
|
|
|
tr.CodigoTrabajador AS Comercial,
|
|
|
|
tr2.CodigoTrabajador AS Comprador,
|
|
|
|
p.name AS Provincia,
|
|
|
|
w.name AS almacen,
|
|
|
|
tm.year AS Año,
|
|
|
|
tm.month AS Mes,
|
|
|
|
tm.week AS Semana,
|
2024-01-31 11:14:24 +00:00
|
|
|
dm.description AS Vista,
|
2024-01-15 11:31:03 +00:00
|
|
|
bt.importe AS Importe
|
|
|
|
FROM bs.ventas bt
|
|
|
|
LEFT JOIN vn2008.Tipos tp ON tp.tipo_id = bt.tipo_id
|
|
|
|
LEFT JOIN vn2008.reinos r ON r.id = tp.reino_id
|
|
|
|
LEFT JOIN vn2008.Clientes c on c.Id_Cliente = bt.Id_Cliente
|
|
|
|
LEFT JOIN vn2008.Trabajadores tr ON tr.Id_Trabajador = c.Id_Trabajador
|
|
|
|
LEFT JOIN vn2008.Trabajadores tr2 ON tr2.Id_Trabajador = tp.Id_Trabajador
|
|
|
|
JOIN vn2008.time tm ON tm.date = bt.fecha
|
|
|
|
JOIN vn2008.Movimientos m ON m.Id_Movimiento = bt.Id_Movimiento
|
|
|
|
LEFT JOIN vn2008.Tickets t ON t.Id_Ticket = m.Id_Ticket
|
|
|
|
JOIN vn2008.Agencias a ON a.Id_Agencia = t.Id_Agencia
|
2024-01-31 11:14:24 +00:00
|
|
|
LEFT JOIN vn.deliveryMethod dm ON dm.id = a.Vista
|
2024-01-15 11:31:03 +00:00
|
|
|
LEFT JOIN vn2008.Consignatarios cs ON cs.Id_Consigna = t.Id_Consigna
|
|
|
|
LEFT JOIN vn2008.province p ON p.province_id = cs.province_id
|
|
|
|
LEFT JOIN vn2008.warehouse w ON w.id = t.warehouse_id
|
|
|
|
WHERE bt.fecha >= vLastMonth AND r.mercancia;
|
2024-01-31 11:14:24 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|