54 lines
1.7 KiB
SQL
54 lines
1.7 KiB
SQL
DELIMITER $$
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bi`.`analisis_ventas_update`()
|
|
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,
|
|
dm.description AS Vista,
|
|
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 vn.ticket t ON t.id = m.Id_Ticket
|
|
JOIN vn2008.Agencias a ON a.Id_Agencia = t.agencyModeFk
|
|
LEFT JOIN vn.deliveryMethod dm ON dm.id = a.Vista
|
|
LEFT JOIN vn2008.Consignatarios cs ON cs.Id_Consigna = t.addressFk
|
|
LEFT JOIN vn2008.province p ON p.province_id = cs.province_id
|
|
LEFT JOIN vn.warehouse w ON w.id = t.warehouseFk
|
|
WHERE bt.fecha >= vLastMonth AND r.mercancia;
|
|
END$$
|
|
DELIMITER ;
|