DECLARE @pFechaDel DATE = '2017-03-22'
DECLARE @pFechaAl DATE = '2017-03-22'
DECLARE @IdGuids nvarchar(MAX) = '7078a725-ca59-4b71-bc56-21b8f456d63b'
SELECT isnull(SUM(CH.Total),0) AS Facturas,
isnull(COUNT(CH.Total),0) AS CountFacturas,
(SELECT isnull(SUM(P1.Monto),0) FROM Finanzas.PagosrecibidosCheque P1 INNER JOIN
Finanzas.PagosRecibidosHeader PR ON PR.IdGuid=P1.IdGuidHeader WHERE CONVERT(DATE, P1.Fecha , 103) BETWEEN @pFechaDel AND @pFechaAl AND
PR.IdGuidCliente IN (@IdGuids) AND IsNull(PR.PagoCancelado,0)=0)AS Cheques,
(SELECT isnull(COUNT(P1.Monto),0) FROM Finanzas.PagosrecibidosCheque P1 INNER JOIN
Finanzas.PagosRecibidosHeader PR ON PR.IdGuid=P1.IdGuidHeader WHERE CONVERT(DATE, P1.Fecha , 103) BETWEEN @pFechaDel AND @pFechaAl AND
PR.IdGuidCliente IN (@IdGuids) AND IsNull(PR.PagoCancelado,0)=0)AS CountCheques,
(SELECT isnull(SUM(P1.Monto),0) FROM Finanzas.PagosRecibidosTransf P1 INNER JOIN
Finanzas.PagosRecibidosHeader PR ON PR.IdGuid=P1.IdGuidHeader WHERE CONVERT(DATE, P1.Fecha , 103) BETWEEN @pFechaDel AND @pFechaAl AND
PR.IdGuidCliente IN (@IdGuids) AND IsNull(PR.PagoCancelado,0)=0)AS Transferencias,
(SELECT isnull(COUNT(P1.Monto),0) FROM Finanzas.PagosRecibidosTransf P1 INNER JOIN
Finanzas.PagosRecibidosHeader PR ON PR.IdGuid=P1.IdGuidHeader WHERE CONVERT(DATE, P1.Fecha , 103) BETWEEN @pFechaDel AND @pFechaAl AND
PR.IdGuidCliente IN (@IdGuids) AND IsNull(PR.PagoCancelado,0)=0)AS CountTransferencias,
(SELECT isnull(SUM(P1.Monto),0) FROM Finanzas.PagosRecibidosEfect P1 INNER JOIN
Finanzas.PagosRecibidosHeader PR ON PR.IdGuid=P1.IdGuidHeader WHERE CONVERT(DATE, P1.Fecha , 103) BETWEEN @pFechaDel AND @pFechaAl AND
PR.IdGuidCliente IN (@IdGuids) AND IsNull(PR.PagoCancelado,0)=0)AS Efectivos,
(SELECT isnull(COUNT(P1.Monto),0) FROM Finanzas.PagosRecibidosEfect P1 INNER JOIN
Finanzas.PagosRecibidosHeader PR ON PR.IdGuid=P1.IdGuidHeader WHERE CONVERT(DATE, P1.Fecha , 103) BETWEEN @pFechaDel AND @pFechaAl AND
PR.IdGuidCliente IN (@IdGuids) AND IsNull(PR.PagoCancelado,0)=0)AS CountEfectivos,
(SELECT isnull(SUM(P1.Monto),0) FROM Finanzas.PagosRecibidosTarjeta P1 INNER JOIN
Finanzas.PagosRecibidosHeader PR ON PR.IdGuid=P1.IdGuidHeader WHERE CONVERT(DATE, P1.Fecha , 103) BETWEEN @pFechaDel AND @pFechaAl AND
PR.IdGuidCliente IN (@IdGuids) AND IsNull(PR.PagoCancelado,0)=0)AS Tarjetas,
(SELECT isnull(COUNT(P1.Monto),0) FROM Finanzas.PagosRecibidosTarjeta P1 INNER JOIN
Finanzas.PagosRecibidosHeader PR ON PR.IdGuid=P1.IdGuidHeader WHERE CONVERT(DATE, P1.Fecha , 103) BETWEEN @pFechaDel AND @pFechaAl AND
PR.IdGuidCliente IN (@IdGuids) AND IsNull(PR.PagoCancelado,0)=0)AS CountTarjetas,
(SELECT isnull(SUM(CH1.Total),0) FROM Ventas.vCalculosHeader CH1 WHERE CH1.TipoDocto='V4' AND CH1.Cancelada=0 AND CONVERT(DATE, CH1.Fecha , 103) BETWEEN @pFechaDel AND @pFechaAl
AND CH1.ClaveCliente IN (SELECT C.Clave FROM Clientes C WHERE C.IdGuid IN (@IdGuids))) AS NotasCredito,
(SELECT isnull(COUNT(CH1.Total),0) FROM Ventas.vCalculosHeader CH1 WHERE CH1.TipoDocto='V4' AND CH1.Cancelada=0 AND CONVERT(DATE, CH1.Fecha , 103) BETWEEN @pFechaDel AND @pFechaAl
AND CH1.ClaveCliente IN (SELECT C.Clave FROM Clientes C WHERE C.IdGuid IN (@IdGuids))) AS CountNotasCredito,
(SELECT isnull(SUM(CH1.Total),0) FROM Ventas.vCalculosHeader CH1 WHERE CH1.TipoDocto='V6' AND CH1.Cancelada=0 AND CONVERT(DATE, CH1.Fecha , 103) BETWEEN @pFechaDel AND @pFechaAl
AND CH1.ClaveCliente IN (SELECT C.Clave FROM Clientes C WHERE C.IdGuid IN (@IdGuids))) AS DevolucionMcia,
(SELECT isnull(COUNT(CH1.Total),0) FROM Ventas.vCalculosHeader CH1 WHERE CH1.TipoDocto='V6' AND CH1.Cancelada=0 AND CONVERT(DATE, CH1.Fecha , 103) BETWEEN @pFechaDel AND @pFechaAl
AND CH1.ClaveCliente IN (SELECT C.Clave FROM Clientes C WHERE C.IdGuid IN (@IdGuids))) AS CountDevolucionMcia,
(SELECT IsNull(SUM(b.Cargo)-SUM(b.Abono),0)
FROM Ventas.vDetalleClientes b
WHERE CONVERT(DATE, b.Fecha , 103) < @pFechaDel AND b.IdGuid IN (SELECT IdGuid FROM Ventas.vCalculosHeader WHERE Cancelada=0)
AND b.ClaveCliente IN (SELECT C.Clave FROM Clientes C WHERE C.IdGuid IN (@IdGuids))
) AS SaldoIniFijo,
(SELECT IsNull(SUM(b.Cargo)-SUM(b.Abono),0)
FROM Ventas.vDetalleClientes b
WHERE CONVERT(DATE, b.Fecha , 103) <= @pFechaAl AND b.IdGuid IN (SELECT IdGuid FROM Ventas.vCalculosHeader WHERE Cancelada=0)
AND b.ClaveCliente IN (SELECT C.Clave FROM Clientes C WHERE C.IdGuid IN (@IdGuids))
) AS SaldoFinFijo,
datediff(d,@pFechaDel,@pFechaAl)+1 AS DifFechas
FROM Ventas.vCalculosHeader CH WITH (INDEX = idx_ClaveCliente)
WHERE CH.TipoDocto='V3' AND CONVERT(DATE, CH.Fecha , 103) BETWEEN @pFechaDel AND @pFechaAl AND Cancelada =0
AND CH.ClaveCliente IN (SELECT C.Clave FROM Clientes C WHERE C.IdGuid IN (@IdGuids))