﻿DROP VIEW IF EXISTS `v_prestamos`;
CREATE VIEW `v_prestamos` AS
SELECT a.id_gasto, DATE_FORMAT(a.fecha,'%d/%m/%Y')fecha, a.beneficiario, a.concepto,  b.banco, a.importe,
		(SELECT IFNULL(SUM(x.importe),0)abono FROM ingresos AS x WHERE x.estatus=a.id_gasto)abonado,
		(a.importe-(SELECT IFNULL(SUM(x.importe),0)abono FROM ingresos AS x WHERE x.estatus=a.id_gasto))pendiente, a.id_sucursal
		FROM gastos AS a, cuenta_bancaria AS b
		WHERE a.id_banco=b.id_banco
		AND a.id_cuenta=13
