﻿DROP VIEW IF EXISTS `v_notas_validadas`;
CREATE VIEW `v_notas_validadas` AS
SELECT a.id_nota, a.id_cliente, DATE_FORMAT(a.fechar,'%d/%m/%y')fecha, b.empresa, a.estatus, b.contacto,
fn_total(a.id_nota)importe, IFNULL((SELECT SUM(z.importe)FROM ingresos AS z WHERE a.id_nota=z.id_nota),0)pagos,
(fn_total(a.id_nota)-IFNULL((SELECT SUM(z.importe)FROM ingresos AS z WHERE a.id_nota=z.id_nota),0))debe, a.id_sucursal
 FROM nota AS a, cliente AS b
 WHERE a.id_cliente=b.id_cliente
 AND a.estatus='Validada'
 ORDER BY a.id_nota DESC