Hola Oscar, feliz año …
corrigiendo algunos detalles sobre la marcha , la consulta ya esta funcionando bastante bien, hay un monton de cambios y hasta se hizo mas larga . Aprendi algunas cosas en el camino ya que soy autodidacta , asi que cualquier correccion o sugerencia para este novato es bienvenida.
SELECT
datepart(yyyy,case when T7.DTE>= CASE WHEN T7.DTEX IS NULL THEN T8.FECHAPAGO ELSE T7.DTEX END then T7.DTE else CASE WHEN T7.DTEX IS NULL THEN T8.FECHAPAGO ELSE T7.DTEX END end ) as 'AÑO',
datepart(mm,case when T7.DTE>= CASE WHEN T7.DTEX IS NULL THEN T8.FECHAPAGO ELSE T7.DTEX END then T7.DTE else CASE WHEN T7.DTEX IS NULL THEN T8.FECHAPAGO ELSE T7.DTEX END end ) AS 'MES',
T6.[SlpName]AS 'VENDEDOR',
T0.[DocStatus],
T0.[CardCode] AS 'RUT',
T0.[CardName]AS 'CLIENTE',
T0.[DocNum] AS 'N°DOC',
T0.[FolioNum] AS 'N°FOLIO',
T0.[DocDate]AS 'FECHA DOC',
T0.[DocCur]AS 'MONEDA',
CASE WHEN T0.[VatSum]=0 AND T0.[DocCur]<>'CLP' THEN CASE WHEN T0.[TotalExpFC] >'0' THEN T0.[DocTotalFC]-T0.[TotalExpFC] ELSE T0.[DocTotalFC] END ELSE (T0.[DocTotal]-T0.[TotalExpns]) - T0.[VatSum] END 'NETO FACT' ,
T0.[VatSum] AS 'IVA FACT',
CASE WHEN T0.[VatSum]=0 AND T0.[DocCur]<>'CLP' THEN CASE WHEN T0.[TotalExpFC] >'0' THEN T0.[DocTotalFC]-T0.[TotalExpFC] ELSE T0.[DocTotalFC] END ELSE T0.[DocTotal]-T0.[TotalExpns] END 'TOTAL FACT' ,
CASE T0.[Indicator]
WHEN 'XR' THEN 'NC_EX'
WHEN 'XP' THEN 'F_EX'
WHEN 'XN' THEN 'NC_NA'
WHEN 'XF' THEN 'F_NA'
WHEN 'XD' THEN 'ND_NA'
WHEN 'FN' THEN 'FV_NA'
WHEN 'FE' THEN 'FV_EX'
WHEN 'XT' THEN 'ND_EX'
ELSE T0.[Indicator] END 'IND',
T8.PAGO,
case when T7.DTE>= CASE WHEN T7.DTEX IS NULL THEN T8.FECHAPAGO ELSE T7.DTEX END then T7.DTE else CASE WHEN T7.DTEX IS NULL THEN T8.FECHAPAGO ELSE T7.DTEX END end 'FECHA PAGO',
T8.NETOPAGO,
T8.IVA,
T8.TOTALPAGO,
T0.[U_CV_Extras] AS 'EXTRA'
FROM
OINV T0
INNER JOIN INV1 T1 ON T0.[DocEntry] = T1.[DocEntry]
INNER JOIN (SELECT T0.[DocEntry] AS 'DC',MIN( T0.[LineNum]) AS 'LINE' FROM INV1 T0 GROUP BY T0.[DocEntry])LINE ON DC=T1.[DocEntry] AND LINE=T1.[LineNum]
LEFT JOIN OSLP T6 ON T0.[SlpCode] = T6.[SlpCode]
LEFT JOIN
( SELECT
T0.[DocNum] AS 'DOC',
MAX(T2.[DocNum]) AS 'MAX' ,
MAX(T3.[DocDueDate])AS 'DTE'
,MAX(T3.[DocDate])AS 'DTEX'
FROM OINV T0 INNER JOIN INV1 T1 ON T0.[DocEntry] = T1.[DocEntry]
LEFT JOIN RCT2 T2 ON T2.[DocEntry]=T0.[DocEntry] AND T2.[DocTransId]= T0.[TransId]
LEFT JOIN ORCT T3 ON T3.[DocEntry] = T2.[DocNum]
WHERE T0.[CANCELED] = 'N' AND T0.[DocDate] >= '01/01/2015' and T0.[DocStatus]='c' GROUP BY T0.[DocNum] ) T7 ON DOC=T0.[DocNum]
LEFT JOIN
( SELECT T0.[DocNum] AS 'DC',
T2.[DocNum] AS 'PAGO',
T2.[DocDate] AS 'FECHAPAGO',
(T1.[AppliedSys]- T1.[ExpAppld])- T1.[vatAppldSy] AS 'NETOPAGO',
T1.[vatAppldSy] AS 'IVA',
T1.[AppliedSys]- T1.[ExpAppld] AS 'TOTALPAGO'
FROM OINV T0
LEFT JOIN RCT2 T1 ON T1.[DocEntry]=T0.[DocEntry] AND T1.[DocTransId]= T0.[TransId]
INNER JOIN ORCT T2 ON T1.[DocNum] = T2.[DocEntry]
where T0.[CANCELED] = 'N' AND T0.[DocDate] >= '01/01/2015' and T0.[DocStatus]='c' AND T0.[U_EstadoCV] IS NULL and (T2.[CANCELED] = 'N' OR T2.[CANCELED] IS NULL)
UNION ALL
SELECT T0.[DocNum] AS 'DC',
MAX(T3.[DocNum] )AS 'PAGO',
T3.[DocDate] AS 'FECHAPAGO',
(T3.[DocTotal] -T3.[TotalExpns] ) -T3.[VatSum] AS 'NETOPAGO',
T3.[VatSum] AS 'IVA',
T3.[DocTotal] -T3.[TotalExpns] AS 'TOTALPAGO'
FROM OINV T0 INNER JOIN INV1 T1 ON T0.[DocEntry] = T1.[DocEntry]
LEFT JOIN RIN1 T2 ON T2.BaseEntry = T1.DocEntry
INNER JOIN ORIN T3 ON T2.[DocEntry] = T3.[DocEntry]
WHERE T0.[CANCELED] = 'N' AND T0.[DocDate] >= '01/01/2015' AND T0.[Indicator]<>'AA'
GROUP BY T0.[DocNum],T3.[DocDate] ,T3.[DocTotal] ,T3.[TotalExpns] ,T3.[VatSum] ) T8 ON T8.DC = T0.[DocNum] AND T1.LineNum =T1.LineNum
WHERE T0.[CANCELED] = 'N' AND T0.[DocDate] >= '01/01/2015' and T0.[DocStatus]='c' AND T0.[U_EstadoCV] IS NULL AND T0.[Indicator]<>'AA'
UNION ALL
SELECT
datepart(yyyy,case when T7.DTE>= CASE WHEN T7.DTEX IS NULL THEN T8.FECHAPAGO ELSE T7.DTEX END then T7.DTE else CASE WHEN T7.DTEX IS NULL THEN T8.FECHAPAGO ELSE T7.DTEX END end ) as 'AÑO',
datepart(mm,case when T7.DTE>= CASE WHEN T7.DTEX IS NULL THEN T8.FECHAPAGO ELSE T7.DTEX END then T7.DTE else CASE WHEN T7.DTEX IS NULL THEN T8.FECHAPAGO ELSE T7.DTEX END end ) AS 'MES',
T6.[SlpName]AS 'VENDEDOR',
T0.[DocStatus],
T0.[CardCode] AS 'RUT',
T0.[CardName]AS 'CLIENTE',
T0.[DocNum] AS 'N°DOC',
T0.[FolioNum] AS 'N°FOLIO',
T0.[DocDate]AS 'FECHA DOC',
T0.[DocCur]AS 'MONEDA',
CASE WHEN T0.[VatSum]=0 AND T0.[DocCur]<>'CLP' THEN CASE WHEN T0.[TotalExpFC] >'0' THEN T0.[DocTotalFC]-T0.[TotalExpFC] ELSE T0.[DocTotalFC] END ELSE (T0.[DocTotal]-T0.[TotalExpns]) - T0.[VatSum] END 'NETO FACT' ,
T0.[VatSum] AS 'IVA FACT',
CASE WHEN T0.[VatSum]=0 AND T0.[DocCur]<>'CLP' THEN CASE WHEN T0.[TotalExpFC] >'0' THEN T0.[DocTotalFC]-T0.[TotalExpFC] ELSE T0.[DocTotalFC] END ELSE T0.[DocTotal]-T0.[TotalExpns] END 'TOTAL FACT' ,
CASE T0.[Indicator]
WHEN 'XR' THEN 'NC_EX'
WHEN 'XP' THEN 'F_EX'
WHEN 'XN' THEN 'NC_NA'
WHEN 'XF' THEN 'F_NA'
WHEN 'XD' THEN 'ND_NA'
WHEN 'FN' THEN 'FV_NA'
WHEN 'FE' THEN 'FV_EX'
WHEN 'XT' THEN 'ND_EX'
ELSE T0.[Indicator] END 'IND',
T8.PAGO,
case when T7.DTE>= CASE WHEN T7.DTEX IS NULL THEN T8.FECHAPAGO ELSE T7.DTEX END then T7.DTE else CASE WHEN T7.DTEX IS NULL THEN T8.FECHAPAGO ELSE T7.DTEX END end 'FECHA PAGO',
T8.NETOPAGO,
T8.IVA,
T8.TOTALPAGO,
T0.[U_CV_Extras] AS 'EXTRA'
FROM
ODPI T0
INNER JOIN DPI1 T1 ON T0.[DocEntry] = T1.[DocEntry]
INNER JOIN (SELECT T0.[DocEntry] AS 'DC',MIN( T0.[LineNum]) AS 'LINE' FROM INV1 T0 GROUP BY T0.[DocEntry])LINE ON DC=T1.[DocEntry] AND LINE=T1.[LineNum]
LEFT JOIN OSLP T6 ON T0.[SlpCode] = T6.[SlpCode]
LEFT JOIN
( SELECT
T0.[DocNum] AS 'DOC',
MAX(T2.[DocNum]) AS 'MAX' ,
MAX(T3.[DocDueDate])AS 'DTE'
,MAX(T3.[DocDate])AS 'DTEX'
FROM ODPI T0 INNER JOIN DPI1 T1 ON T0.[DocEntry] = T1.[DocEntry]
LEFT JOIN RCT2 T2 ON T2.[DocEntry]=T0.[DocEntry] AND T2.[DocTransId]= T0.[TransId]
LEFT JOIN ORCT T3 ON T3.[DocEntry] = T2.[DocNum]
WHERE T0.[CANCELED] = 'N' AND T0.[DocDate] >= '01/01/2015' and T0.[DocStatus]='c' GROUP BY T0.[DocNum] ) T7 ON DOC=T0.[DocNum]
LEFT JOIN
( SELECT T0.[DocNum] AS 'DC',
T2.[DocNum] AS 'PAGO',
T2.[DocDate] AS 'FECHAPAGO',
(T1.[AppliedSys]- T1.[ExpAppld])- T1.[vatAppldSy] AS 'NETOPAGO',
T1.[vatAppldSy] AS 'IVA',
T1.[AppliedSys]- T1.[ExpAppld] AS 'TOTALPAGO'
FROM ODPI T0
LEFT JOIN RCT2 T1 ON T1.[DocEntry]=T0.[DocEntry] AND T1.[DocTransId]= T0.[TransId]
INNER JOIN ORCT T2 ON T1.[DocNum] = T2.[DocEntry]
where T0.[CANCELED] = 'N' AND T0.[DocDate] >= '01/01/2015' and T0.[DocStatus]='c' AND T0.[U_EstadoCV] IS NULL and (T2.[CANCELED] = 'N' OR T2.[CANCELED] IS NULL)
UNION ALL
SELECT T0.[DocNum] AS 'DC',
MAX(T3.[DocNum] )AS 'PAGO',
T3.[DocDate] AS 'FECHAPAGO',
(T3.[DocTotal] -T3.[TotalExpns] ) -T3.[VatSum] AS 'NETOPAGO',
T3.[VatSum] AS 'IVA',
T3.[DocTotal] -T3.[TotalExpns] AS 'TOTALPAGO'
FROM ODPI T0 INNER JOIN DPI1 T1 ON T0.[DocEntry] = T1.[DocEntry]
LEFT JOIN RIN1 T2 ON T2.BaseEntry = T1.DocEntry
INNER JOIN ORIN T3 ON T2.[DocEntry] = T3.[DocEntry]
WHERE T0.[CANCELED] = 'N' AND T0.[DocDate] >= '01/01/2015' AND T0.[Indicator]<>'AA'
GROUP BY T0.[DocNum],T3.[DocDate] ,T3.[DocTotal] ,T3.[TotalExpns] ,T3.[VatSum] ) T8 ON T8.DC = T0.[DocNum] AND T1.LineNum =T1.LineNum
WHERE T0.[CANCELED] = 'N' AND T0.[DocDate] >= '01/01/2015' and T0.[DocStatus]='c' AND T0.[U_EstadoCV] IS NULL AND T0.[Indicator]<>'AA'
UNION ALL
SELECT
datepart(yyyy,t0.DocDate) AS 'AÑO',
DATEPART(MM,t0.DocDate)AS 'MES' ,
T1.[SlpName],
T0.[DocStatus],
T0.[CardCode],
T0.[CardName],
T0.[DocNum],
T0.[FolioNum],
T0.[DocDate],
T0.[DocCur],
CASE WHEN T0.[VatSum]=0 THEN CASE WHEN T0.[TotalExpns]>'0' THEN (T0.[DocTotal]-T0.[TotalExpns])*-1 ELSE T0.[DocTotalFC]*-1 END ELSE (T0.[DocTotal] - T0.[VatSum])*-1 END 'NETO DOC',
T0.[VatSum]*-1 AS 'IVA',
CASE WHEN T0.[VatSum]=0 THEN CASE WHEN T0.[TotalExpns]>'0' THEN (T0.[DocTotal]-T0.[TotalExpns])*-1 ELSE T0.[DocTotalFC]*-1 END ELSE T0.[DocTotal]*-1 END 'TOTAL DOC' ,
CASE T0.[Indicator]
WHEN 'XR' THEN 'NC_EX'
WHEN 'XP' THEN 'F_EX'
WHEN 'XN' THEN 'NC_NA'
WHEN 'XF' THEN 'F_NA'
WHEN 'XD' THEN 'ND_NA'
WHEN 'FN' THEN 'FV_NA'
WHEN 'FE' THEN 'FV_EX'
WHEN 'XT' THEN 'ND_EX'
ELSE T0.[Indicator] END 'IND',
T0.[ReceiptNum],
T0.[DocDate],
CASE WHEN T0.[VatSum]=0 THEN (T0.[DocTotal]-T0.[TotalExpns])*-1 ELSE (T0.[DocTotal] - T0.[VatSum])*-1 END 'NETO PAGO',
T0.[VatSum]*-1 AS 'IVA',
CASE WHEN T0.[VatSum]=0 THEN (T0.[DocTotal]-T0.[TotalExpns])*-1 ELSE T0.[DocTotal]*-1 END 'TOTAL PAGO',
T0.[U_CV_Extras]
FROM
ORIN T0
LEFT JOIN OSLP T1 ON T0.[SlpCode] = T1.[SlpCode]
WHERE
T0.[CANCELED] = 'N' AND T0.[DocDate] >= '01/01/2015' AND T0.[Indicator]<>'AA'
ORDER BY T0.[CardName] ASC , T0.[DocNum] ASC
saludos!!