-- Ventas de clientes de una sola ciudad (Palmira, Valle) de Odoo

SET NAMES 'utf8';

SELECT
	cs.id_cliente AS NIT,
	cs.nombre_cliente AS Cliente,
	cs.telefono AS Telefono,
	cs.direccion AS Direccion,
	YEAR(dv.fecha) AS Ano,
	SUM(dvi.valor) AS valor
FROM
	documento_ventas dv
	LEFT JOIN documento_ventas_has_item dvi
	ON ( dv.id_documento=dvi.id_documento AND dv.id_sucursal=dvi.id_sucursal )
	LEFT JOIN item i
	ON ( dvi.id_item=i.id_item )
	JOIN cliente_has_sucursal cs
	ON ( dv.id_sucursal=cs.id_sucursal AND dv.id_cliente=cs.id_cliente )
	JOIN zona z
	ON ( z.id_zona=LEFT(cs.id_zona,8) )
WHERE
	(LEFT(dv.id_documento,2) = 'DV' OR
	 LEFT(dv.id_documento,2) = 'EF' OR
	 LEFT(dv.id_documento,1) = 'F')
	AND z.id_zona = '77076520'
GROUP BY
	Ano,NIT
ORDER BY
	Ano,NIT
