54 lines
1.9 KiB
MySQL
54 lines
1.9 KiB
MySQL
|
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,
|
||
|
v.vista 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 vn2008.Tickets t ON t.Id_Ticket = m.Id_Ticket
|
||
|
JOIN vn2008.Agencias a ON a.Id_Agencia = t.Id_Agencia
|
||
|
LEFT JOIN vn2008.Vistas v ON v.vista_id = a.Vista
|
||
|
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;
|
||
|
END$$
|
||
|
DELIMITER ;
|