Buen dia, estoy tratando de realizar una consulta donde genere las Facturas, Notas Credito y Facturas Canceladas por item, pero ya casi lo tengo pero tengo un problemita en las notas credito ya que en algunas no me muestra el valor de la nota, este es el codigo que tengo.
SELECT 'Factura de Venta' as Tipo, T2.[Memo], T2.[SlpName], T0.[DocNum], T0.[DocDate], T1.[ItemCode], T1.[Dscription],
((T1.[Price]-(T1.[Price]*(T0.[DiscPrcnt]/100))) * T1.[Quantity]) as 'Valor neto',
T1.[Quantity],
T0.[CardCode], T0.[CardName], T0.[ShipToCode], T3.[Address2S], T3.[StreetS], T3.[CityS], T3.[CountyS],
(((T1.[Price]-(T1.[Price]*(T0.[DiscPrcnt]/100))) * T1.[Quantity])) - ((T1.[GrossBuyPr] *T1.[Quantity])) as 'Margen',
(T1.[GrossBuyPr] *T1.[Quantity]) as 'Costo de Ventas'
FROM [dbo].[OINV] T0
INNER JOIN [dbo].[INV1] T1 ON T0.[DocEntry] = T1.[DocEntry]
INNER JOIN [dbo].[OSLP] T2 ON T0.[SlpCode] = T2.[SlpCode]
INNER JOIN [dbo].[INV12] T3 ON T0.[DocEntry] = T3.[DocEntry]
WHERE T0.[DocDate] between [%0] and [%1] and t0.CANCELED <> 'C'
GROUP BY T2.[Memo], T2.[SlpName], T0.[DocNum], T0.[DocDate], T1.[ItemCode], T1.[Dscription], T1.[Price], T1.[Quantity],
T0.[DiscPrcnt], T0.[CardCode], T0.[CardName], T0.[ShipToCode],T3.[Address2S], T3.[StreetS], T1.[LineTotal], T1.[GrossBuyPr],
T3.[CityS], T3.[CountyS]
Union All
SELECT 'Notas Credito' as Tipo, T2.[Memo], T2.[SlpName], T0.[DocNum], T0.[DocDate], T1.[ItemCode], T1.[Dscription],
((((T1.[Price]-(T1.[Price]*(T0.[DiscPrcnt]/100))) * T1.[Quantity])) * (-1)) as 'Valor neto',
((T1.[Quantity]) * (-1)) as [Quantity],
T0.[CardCode], T0.[CardName], T0.[ShipToCode], T3.[Address2S], T3.[StreetS],T3.[CityS], T3.[CountyS],
(((((T1.[Price]-(T1.[Price]*(T0.[DiscPrcnt]/100))) * T1.[Quantity])) - ((T1.[GrossBuyPr] *T1.[Quantity]))) * (-1)) as 'Margen',
(((T1.[GrossBuyPr] *T1.[Quantity])) * (-1)) as 'Costo de Ventas'
FROM [dbo].[ORIN] T0
INNER JOIN [dbo].[RIN1] T1 ON T0.[DocEntry] = T1.[DocEntry]
INNER JOIN [dbo].[OSLP] T2 ON T0.[SlpCode] = T2.[SlpCode]
INNER JOIN [dbo].[INV12] T3 ON T0.[DocEntry] = T3.[DocEntry]
WHERE T0.[DocDate] between [%0] and [%1] and t0.CANCELED = 'N'
GROUP BY T2.[Memo], T2.[SlpName], T0.[DocNum], T0.[DocDate], T1.[ItemCode], T1.[Dscription], T1.[Price], T1.[Quantity],
T0.[DiscPrcnt], T0.[CardCode], T0.[CardName], T0.[ShipToCode],T3.[Address2S], T3.[StreetS], T1.[LineTotal], T1.[GrossBuyPr], T3.[CityS], T3.[CountyS]
Union All
SELECT 'Facturas Canceladas' as Tipo, T2.[Memo], T2.[SlpName], T0.[DocNum], T0.[DocDate], T1.[ItemCode], T1.[Dscription],
((((T1.[Price]-(T1.[Price]*(T0.[DiscPrcnt]/100))) * T1.[Quantity])) * (-1)) as 'Valor neto',
((T1.[Quantity]) * (-1)) as [Quantity],
T0.[CardCode], T0.[CardName], T0.[ShipToCode], T3.[Address2S], T3.[StreetS], T3.[CityS], T3.[CountyS],
(((((T1.[Price]-(T1.[Price]*(T0.[DiscPrcnt]/100))) * T1.[Quantity])) - ((T1.[GrossBuyPr] *T1.[Quantity]))) * (-1)) as 'Margen',
(((T1.[GrossBuyPr] *T1.[Quantity])) * (-1)) as 'Costo de Ventas'
FROM [dbo].[OINV] T0
INNER JOIN [dbo].[INV1] T1 ON T0.[DocEntry] = T1.[DocEntry]
INNER JOIN [dbo].[OSLP] T2 ON T0.[SlpCode] = T2.[SlpCode]
INNER JOIN [dbo].[INV12] T3 ON T0.[DocEntry] = T3.[DocEntry]
WHERE T0.[DocDate] between [%0] and [%1] and t0.CANCELED = 'C'
GROUP BY T2.[Memo], T2.[SlpName], T0.[DocNum], T0.[DocDate], T1.[ItemCode], T1.[Dscription], T1.[Price], T1.[Quantity],
T0.[DiscPrcnt], T0.[CardCode], T0.[CardName], T0.[ShipToCode],T3.[Address2S], T3.[StreetS], T1.[LineTotal], T1.[GrossBuyPr], T3.[CityS], T3.[CountyS]
Creo que el problema esta es que algunas facturas o notas credito los descuetos se aplican de diferente forma, unos van directamente al articulo y otros se aplica a toda la factura, si tienen alguna sugerencia se los agradesco.
Anexo dos notas credito, en una el descuento esta aplicado a cada referencia por que en ocaciones el descuento es diferente para cada referencia. pero el otro se aplica a toda la factura lo que quiere decir que se aplicaria el descuento para cada referencia.
Lo que necesito es generar el reporte de facturas y notas credito por referencia x factura
GRACIAS