48 lines
1.4 KiB
SQL
48 lines
1.4 KiB
SQL
DELIMITER $$
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`ventas_contables_por_cliente`(IN vYear INT, IN vMonth INT)
|
|
BEGIN
|
|
|
|
/**
|
|
* Muestra las ventas (€) de cada cliente
|
|
* dependiendo del año
|
|
*/
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.ticket_list;
|
|
|
|
CREATE TEMPORARY TABLE tmp.ticket_list
|
|
(PRIMARY KEY (Id_Ticket))
|
|
SELECT Id_Ticket
|
|
FROM vn2008.Tickets t
|
|
JOIN vn.invoiceOut io ON io.id = t.Factura
|
|
WHERE year(io.issued) = vYear
|
|
AND month(io.issued) = vMonth;
|
|
|
|
SELECT vYear Año,
|
|
vMonth Mes,
|
|
t.Id_Cliente,
|
|
round(sum(Cantidad * Preu * (100 - m.Descuento)/100)) Venta,
|
|
IF(e.empresa_grupo = e2.empresa_grupo,
|
|
1,
|
|
IF(e2.empresa_grupo,2,0))
|
|
AS grupo,
|
|
t.empresa_id empresa
|
|
FROM vn2008.Movimientos m
|
|
JOIN vn2008.Tickets t ON t.Id_Ticket = m.Id_Ticket
|
|
JOIN vn2008.Consignatarios cs ON cs.Id_Consigna = t.Id_Consigna
|
|
JOIN vn2008.Clientes c ON c.Id_Cliente = cs.Id_Cliente
|
|
JOIN tmp.ticket_list tt ON tt.Id_Ticket = t.Id_Ticket
|
|
JOIN vn2008.Articles a ON m.Id_Article = a.Id_Article
|
|
JOIN vn2008.empresa e ON e.id = t.empresa_id
|
|
LEFT JOIN vn2008.empresa e2 ON e2.Id_Cliente = c.Id_Cliente
|
|
JOIN vn2008.Tipos tp ON tp.tipo_id = a.tipo_id
|
|
WHERE Cantidad <> 0
|
|
AND Preu <> 0
|
|
AND m.Descuento <> 100
|
|
AND a.tipo_id != 188
|
|
GROUP BY t.Id_Cliente, grupo,t.empresa_id;
|
|
|
|
DROP TEMPORARY TABLE tmp.ticket_list;
|
|
|
|
END$$
|
|
DELIMITER ;
|