48 lines
1.3 KiB
SQL
48 lines
1.3 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))
|
|
SELECT t.id
|
|
FROM vn.ticket t
|
|
JOIN vn.invoiceOut io ON io.id = t.refFk
|
|
WHERE year(io.issued) = vYear
|
|
AND month(io.issued) = vMonth;
|
|
|
|
SELECT vYear Año,
|
|
vMonth Mes,
|
|
t.clientFk 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.companyFk empresa
|
|
FROM vn2008.Movimientos m
|
|
JOIN vn.ticket t ON t.id = m.Id_Ticket
|
|
JOIN vn2008.Consignatarios cs ON cs.Id_Consigna = t.addressFk
|
|
JOIN vn2008.Clientes c ON c.Id_Cliente = cs.Id_Cliente
|
|
JOIN tmp.ticket_list tt ON tt.id = t.id
|
|
JOIN vn2008.Articles a ON m.Id_Article = a.Id_Article
|
|
JOIN vn2008.empresa e ON e.id = t.companyFk
|
|
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.clientFk, grupo,t.companyFk;
|
|
|
|
DROP TEMPORARY TABLE tmp.ticket_list;
|
|
|
|
END$$
|
|
DELIMITER ;
|