IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'SP_Rpt_Ventas_Por_Vend_Tipo') 
                   and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
    DROP PROCEDURE [SP_Rpt_Ventas_Por_Vend_Tipo]
END
GO 
CREATE PROCEDURE [dbo].[SP_Rpt_Ventas_Por_Vend_Tipo] @Fecha_Desde DATETIME,@Fecha_Hasta DATETIME 
AS

DECLARE @ML_Cant					INT
DECLARE @ML_Importe					DECIMAL(14,2)
DECLARE @ML_Cant_Anul				INT
DECLARE @ML_Importe_Anul			DECIMAL(14,2)
DECLARE @ML_Cant_Dev				INT
DECLARE @ML_Importe_Dev				DECIMAL(14,2)
DECLARE @ML_Cant_Hoy				INT
DECLARE @ML_Importe_Hoy				DECIMAL(14,2)
DECLARE @ML_Cant_Mes				INT
DECLARE @ML_Importe_Mes				DECIMAL(14,2)
									
DECLARE @VD_Cant					INT
DECLARE @VD_Importe					DECIMAL(14,2)
DECLARE @VD_Cant_Anul				INT
DECLARE @VD_Importe_Anul			DECIMAL(14,2)
DECLARE @VD_Cant_Dev				INT
DECLARE @VD_Importe_Dev				DECIMAL(14,2)
DECLARE @VD_Cant_Hoy				INT
DECLARE @VD_Importe_Hoy				DECIMAL(14,2)
DECLARE @VD_Cant_Mes				INT
DECLARE @VD_Importe_Mes				DECIMAL(14,2)
									
DECLARE @VS_Cant					INT
DECLARE @VS_Importe					DECIMAL(14,2)
DECLARE @VS_Cant_Anul				INT
DECLARE @VS_Importe_Anul			DECIMAL(14,2)
DECLARE @VS_Cant_Dev				INT
DECLARE @VS_Importe_Dev				DECIMAL(14,2)
DECLARE @VS_Cant_Hoy				INT
DECLARE @VS_Importe_Hoy				DECIMAL(14,2)
DECLARE @VS_Cant_Mes				INT
DECLARE @VS_Importe_Mes				DECIMAL(14,2)
									
DECLARE @VG_Cant					INT
DECLARE @VG_Importe					DECIMAL(14,2)
DECLARE @VG_Cant_Anul				INT
DECLARE @VG_Importe_Anul			DECIMAL(14,2)
DECLARE @VG_Cant_Dev				INT
DECLARE @VG_Importe_Dev				DECIMAL(14,2)
DECLARE @VG_Cant_Hoy				INT
DECLARE @VG_Importe_Hoy				DECIMAL(14,2)
DECLARE @VG_Cant_Mes				INT
DECLARE @VG_Importe_Mes				DECIMAL(14,2)
									
DECLARE @MY_Cant					INT
DECLARE @MY_Importe					DECIMAL(14,2)
DECLARE @MY_Cant_Anul				INT
DECLARE @MY_Importe_Anul			DECIMAL(14,2)
DECLARE @MY_Cant_Dev				INT
DECLARE @MY_Importe_Dev				DECIMAL(14,2)
DECLARE @MY_Cant_Hoy				INT
DECLARE @MY_Importe_Hoy				DECIMAL(14,2)
DECLARE @MY_Cant_Mes				INT
DECLARE @MY_Importe_Mes				DECIMAL(14,2)
									
DECLARE @Total_Cant_Hoy				INT
DECLARE @Total_Importe_Hoy			DECIMAL(14,2)
DECLARE @Total_Cant_Mes				INT
DECLARE @Total_Importe_Mes			DECIMAL(14,2)
DECLARE @Total_Cant					INT
DECLARE @Total_Importe				DECIMAL(14,2)
DECLARE @Total_Cant_Anul			INT
DECLARE @Total_Importe_Anul			DECIMAL(14,2)
DECLARE @Total_Cant_Dev				INT
DECLARE @Total_Importe_Dev			DECIMAL(14,2)

DECLARE @VS_ucp_Importe_Mes DECIMAL(14,2)
,@MY_ucp_Importe_Mes DECIMAL(14,2)
,@VD_ucp_Importe_Mes DECIMAL(14,2)
,@ml_ucp_Importe_Mes DECIMAL(14,2)
,@Total_UCP_Importe_Mes DECIMAL(14,2)

DECLARE @Objetivo					DECIMAL(14,2)
DECLARE @Primer_Dia					DATE
DECLARE @Ultimo_Dia					DATE

DECLARE @FECHA_PROCESO               DATE
SET @FECHA_PROCESO = CONVERT(DATE, GETDATE()) 

--SET @Primer_Dia =(SELECT DATEADD(MONTH, -1, DATEADD(day , 1 , EOMONTH(GETDATE()))))
--SET @Ultimo_Dia =(SELECT EOMONTH(GETDATE()))
--SET @Ultimo_Dia = DATEADD(D,1,@Ultimo_Dia)

SET @Primer_Dia = @Fecha_Desde
SET @Ultimo_Dia = Dateadd(dd,1,@Fecha_Hasta)

CREATE TABLE #RESUMEN (
        Id_Vendedor VARCHAR(10)
		,Descripcion VARCHAR(60)
	    ,ML_Cant_Hoy INT
	    ,ML_Importe_Hoy DECIMAL(14,2)
	    ,ML_Cant_Mes INT 
	    ,ML_Importe_Mes DECIMAL(14,2)
        ,ML_UCR DECIMAL(14,2)
	    ,VD_Cant_Hoy INT
	    ,VD_Importe_Hoy DECIMAL(14,2)
	    ,VD_Cant_Mes INT 
	    ,VD_Importe_Mes DECIMAL(14,2)
	    ,VD_UCR DECIMAL(14,2)
		,VS_Cant_Hoy INT
		,VS_Importe_Hoy DECIMAL(14,2)
		,VS_Cant_Mes INT 
		,VS_Importe_Mes DECIMAL(14,2)
		,VS_UCR DECIMAL(14,2)
		,MY_Cant_Hoy INT
		,MY_Importe_Hoy DECIMAL(14,2)
		,MY_Cant_Mes INT 
		,MY_Importe_Mes DECIMAL(14,2)
	    ,MY_UCR DECIMAL(14,2)
		,Total_Cant_Hoy INT 
		,Total_Importe_Hoy DECIMAL(14,2)
		,Total_Cant_Mes INT 
		,Total_Importe_Mes DECIMAL(14,2)
		,TOTAL_UCR DECIMAL(14,2)
		)

		INSERT INTO #RESUMEN  
		select  Id_Vendedor,Descripcion
		,0 as ML_Cant_Hoy  
	    ,0 as ML_Importe_Hoy 
	    ,0 as ML_Cant_Mes  
	    ,0 as ML_Importe_Mes 
        ,0 as ML_UCR  
	    ,0 as VD_Cant_Hoy  
	    ,0 as VD_Importe_Hoy  
	    ,0 as VD_Cant_Mes  
	    ,0 as VD_Importe_Mes 
	    ,0 as VD_UCR 
		,0 as VS_Cant_Hoy 
		,0 as VS_Importe_Hoy  
		,0 as VS_Cant_Mes 
		,0 as VS_Importe_Mes 
		,0 as VS_UCR  
		,0 as MY_Cant_Hoy  
		,0 as MY_Importe_Hoy 
		,0 as MY_Cant_Mes  
		,0 as MY_Importe_Mes  
	    ,0 as MY_UCR  
		,0 as Total_Cant_Hoy  
		,0 as Total_Importe_Hoy  
		,0 as Total_Cant_Mes  
		,0 as Total_Importe_Mes  
		,0 as TOTAL_UCR  
		from Vendedores where Activo = 1 or isnull(Id_vendedor,'') = ''

		Update #RESUMEN SET DESCRIPCION = 'ML' WHERE Id_vendedor = ''

		

/*#################################### MERCADO LIBRE #################################################*/

/* Cantidad de facturas MERCADO LIBRE HOY */
--SELECT @ML_Cant_Hoy=COUNT(*) , @ML_Importe_Hoy=ISNULL(SUM(ISNULL(CBF.Imp_Total*T.compromete_factura,0)),0)
/*SELECT CBP.Id_Vendedor, COUNT(*) AS  ML_Cant_Hoy, ISNULL(SUM(ISNULL(CBF.Imp_Total*T.compromete_factura,0)),0) AS ML_Importe_Hoy
INTO #DETALLE
FROM Cbtes CBF, Cbtes CBP, CBTES_TIPOS T
WHERE CBF.Id_Tipo_Cbte = T.ID_TIPO_CBTE
AND (T.COMPROMETE_FACTURA IN (1,-1) or CBF.Id_Tipo_Cbte IN ('FP25'))
AND T.ID_SUCURSAL = 25
AND CBP.Id_Tipo_Cbte IN ('PF25', 'PA25', 'PD25', 'PX25')
AND CBF.Id_Cliente = CBP.Id_Cliente
AND CBF.Id_Estado_Cbte NOT IN (0,3)
--AND CBP.Id_Estado_Proceso not in (54,49)
AND CBP.Cbte_Nro_Externo IS NOT NULL AND CBP.Cbte_Nro_Externo <> ''
AND CBP.Cbte_Nro_Externo = CBF.Cbte_Nro_Externo 
AND CBF.Fecha = @FECHA_PROCESO
GROUP BY  CBP.Id_Vendedor

UPDATE #RESUMEN SET ML_Cant_Hoy  = D.ML_Cant_Hoy,  ML_Importe_Hoy  = D.ML_Importe_Hoy
FROM #RESUMEN R, #DETALLE D WHERE R.ID_VENDEDOR = D.ID_VENDEDOR

DROP TABLE #DETALLE


/* Cantidad de facturas MERCADO LIBRE MES */

SELECT CBP.Id_Vendedor, COUNT(*) AS  ML_Cant_Mes, ISNULL(SUM(ISNULL(CBF.Imp_Total*T.compromete_factura,0)),0) AS ML_Importe_Mes
INTO #DETALLE1
FROM Cbtes CBF, Cbtes CBP, CBTES_TIPOS T
WHERE CBF.Id_Tipo_Cbte = T.ID_TIPO_CBTE
AND (T.COMPROMETE_FACTURA IN (1,-1) or CBF.Id_Tipo_Cbte IN ('FP25'))
AND T.ID_SUCURSAL = 25
AND CBP.Id_Tipo_Cbte IN ('PF25', 'PA25', 'PD25', 'PX25')
AND CBF.Id_Cliente = CBP.Id_Cliente
AND CBF.Id_Estado_Cbte NOT IN (0,3)
AND CBP.Cbte_Nro_Externo IS NOT NULL AND CBP.Cbte_Nro_Externo <> ''
AND CBP.Cbte_Nro_Externo =  CBF.Cbte_Nro_Externo  
AND CBF.Fecha>=@Primer_Dia
AND CBF.Fecha<@Ultimo_Dia
GROUP BY  CBP.Id_Vendedor

UPDATE #RESUMEN SET ML_Cant_Mes  = D.ML_Cant_Mes,  ML_Importe_Mes  = D.ML_Importe_Mes
FROM #RESUMEN R, #DETALLE1 D WHERE R.ID_VENDEDOR = D.ID_VENDEDOR

DROP TABLE #DETALLE1
*/
/*################################# VENTA DIRECTA ####################################################*/
/* Cantidad de facturas VENTA DIRECTA HOY */

SELECT CBP.Id_Vendedor, COUNT(*) AS  VD_Cant_Hoy, ISNULL(SUM(ISNULL(CBF.Imp_Total*T.compromete_factura,0)),0) AS VD_Importe_Hoy
INTO #DETALLE2
FROM Cbtes CBF, Cbtes CBP, CBTES_TIPOS T
WHERE CBF.Id_Tipo_Cbte = T.ID_TIPO_CBTE
AND (T.COMPROMETE_FACTURA IN (1,-1) AND T.COMPROMETE_IMPUESTOS IN (1,-1))
AND T.ID_SUCURSAL = 25
AND CBP.Id_Tipo_Cbte IN ('PI25')
AND CBF.Pedido_Nro = CONVERT(VARCHAR(10), CBP.Comprobante_Nro)
AND CBF.Id_Cliente = CBP.Id_Cliente
AND CBF.Id_Estado_Cbte NOT IN (0,3)
AND CBF.Fecha= @FECHA_PROCESO
GROUP BY  CBP.Id_Vendedor

UPDATE #RESUMEN SET VD_Cant_Hoy  = D.VD_Cant_Hoy,  VD_Importe_Hoy  = D.VD_Importe_Hoy
FROM #RESUMEN R, #DETALLE2 D WHERE R.ID_VENDEDOR = D.ID_VENDEDOR

DROP TABLE #DETALLE2

/* Cantidad de facturas VENTA DIRECTA MES */

SELECT CBP.Id_Vendedor, COUNT(*) AS  VD_Cant_Mes, ISNULL(SUM(ISNULL(CBF.Imp_Total*T.compromete_factura,0)),0) AS VD_Importe_Mes
INTO #DETALLE3
FROM Cbtes CBF, Cbtes CBP, CBTES_TIPOS T
WHERE CBF.Id_Tipo_Cbte = T.ID_TIPO_CBTE
AND (T.COMPROMETE_FACTURA IN (1,-1) AND T.COMPROMETE_IMPUESTOS IN (1,-1))
AND T.ID_SUCURSAL = 25
AND CBP.Id_Tipo_Cbte IN ('PI25')
AND CBF.Pedido_Nro = CONVERT(VARCHAR(10), CBP.Comprobante_Nro)
AND CBF.Id_Cliente = CBP.Id_Cliente
AND CBF.Id_Estado_Cbte NOT IN (0,3)
AND CBF.Fecha >= @Primer_Dia
AND CBF.Fecha < @Ultimo_Dia
GROUP BY  CBP.Id_Vendedor

UPDATE #RESUMEN SET VD_Cant_Mes  = D.VD_Cant_Mes,  VD_Importe_Mes  = D.VD_Importe_Mes
FROM #RESUMEN R, #DETALLE3 D WHERE R.ID_VENDEDOR = D.ID_VENDEDOR

DROP TABLE #DETALLE3
/*##################################### VENTA SALON ################################################*/

/* Cantidad de facturas VENTA SALON HOY */

SELECT CBP.Id_Vendedor, COUNT(*) AS  VS_Cant_Hoy, ISNULL(SUM(ISNULL(CBF.Imp_Total*T.compromete_factura,0)),0) AS VS_Importe_Hoy
INTO #DETALLE4
FROM Cbtes CBF, Cbtes CBP, CBTES_TIPOS T
WHERE CBF.Id_Tipo_Cbte = T.ID_TIPO_CBTE
AND (T.COMPROMETE_FACTURA IN (1,-1) AND T.COMPROMETE_IMPUESTOS IN (1,-1))
AND T.ID_SUCURSAL = 25
AND CBP.Id_Tipo_Cbte IN ('PS25')
AND CBF.Pedido_Nro = CONVERT(VARCHAR(10), CBP.Comprobante_Nro)
AND CBF.Id_Cliente = CBP.Id_Cliente
AND CBF.Id_Estado_Cbte NOT IN (0,3)
AND CBF.Fecha= @FECHA_PROCESO
GROUP BY  CBP.Id_Vendedor

UPDATE #RESUMEN SET VS_Cant_Hoy  = D.VS_Cant_Hoy,  VS_Importe_Hoy  = D.VS_Importe_Hoy
FROM #RESUMEN R, #DETALLE4 D WHERE R.ID_VENDEDOR = D.ID_VENDEDOR

DROP TABLE #DETALLE4

/* Cantidad de facturas VENTA SALON MES */

SELECT CBP.Id_Vendedor, COUNT(*) AS  VS_Cant_Mes, ISNULL(SUM(ISNULL(CBF.Imp_Total*T.compromete_factura,0)),0) AS VS_Importe_Mes
INTO #DETALLE5
FROM Cbtes CBF, Cbtes CBP, CBTES_TIPOS T
WHERE CBF.Id_Tipo_Cbte = T.ID_TIPO_CBTE
AND (T.COMPROMETE_FACTURA IN (1,-1) AND T.COMPROMETE_IMPUESTOS IN (1,-1))
AND T.ID_SUCURSAL = 25
AND CBP.Id_Tipo_Cbte IN ('PS25')
AND CBF.Pedido_Nro = CONVERT(VARCHAR(10), CBP.Comprobante_Nro)
AND CBF.Id_Cliente = CBP.Id_Cliente
AND CBF.Id_Estado_Cbte NOT IN (0,3)
AND CBF.Fecha>=@Primer_Dia
AND CBF.Fecha<@Ultimo_Dia
GROUP BY  CBP.Id_Vendedor

UPDATE #RESUMEN SET VS_Cant_Mes  = D.VS_Cant_Mes,  VS_Importe_Mes  = D.VS_Importe_Mes
FROM #RESUMEN R, #DETALLE5 D WHERE R.ID_VENDEDOR = D.ID_VENDEDOR

DROP TABLE #DETALLE5

/* Cantidad de facturas PRESUPUESTOS / MAYORISTA HOY */

SELECT CBF.Id_Vendedor, COUNT(*) AS  MY_Cant_Hoy, ISNULL(SUM(ISNULL(CBF.Imp_Total*CT.compromete_factura,0)),0) AS MY_Importe_Hoy
INTO #DETALLE6
FROM Cbtes CBF, Cbtes_Tipos CT
WHERE  CT.Id_Tipo_Cbte=CBF.Id_Tipo_Cbte
AND CBF.Id_Tipo_Cbte NOT IN ('FP25') 
AND CT.ID_SUCURSAL = 25
AND CT.Compromete_Impuestos NOT IN (1,-1)
AND CT.Compromete_factura IN (1,-1)
AND CBF.Id_Estado_Cbte NOT IN (0,3)
AND CBF.Fecha= @FECHA_PROCESO
GROUP BY  CBF.Id_Vendedor

UPDATE #RESUMEN SET MY_Cant_Hoy  = D.MY_Cant_Hoy,  MY_Importe_Hoy  = D.MY_Importe_Hoy
FROM #RESUMEN R, #DETALLE6 D WHERE R.ID_VENDEDOR = D.ID_VENDEDOR

DROP TABLE #DETALLE6
/* Cantidad de facturas PRESUPUESTOS / MAYORISTA MES */

SELECT CBF.Id_Vendedor, COUNT(*) AS  MY_Cant_Mes, ISNULL(SUM(ISNULL(CBF.Imp_Total*CT.compromete_factura,0)),0) AS MY_Importe_Mes
INTO #DETALLE7
FROM Cbtes CBF, Cbtes_Tipos CT
WHERE  CT.Id_Tipo_Cbte=CBF.Id_Tipo_Cbte
AND  CBF.Id_Tipo_Cbte NOT IN ('FP25') 
AND CT.ID_SUCURSAL = 25
AND CT.Compromete_Impuestos NOT IN (1,-1)
AND CT.Compromete_factura IN (1,-1)
AND CBF.Id_Estado_Cbte NOT IN (0,3)
AND CBF.Fecha >= @Primer_Dia
AND CBF.Fecha < @Ultimo_Dia
GROUP BY  CBF.Id_Vendedor

UPDATE #RESUMEN SET MY_Cant_Mes  = D.MY_Cant_Mes,  MY_Importe_Mes  = D.MY_Importe_Mes
FROM #RESUMEN R, #DETALLE7 D WHERE R.ID_VENDEDOR = D.ID_VENDEDOR

DROP TABLE #DETALLE7

/*-------ucp-------------------------------------------------------------------------------*/ 
/*
SELECT CBP.Id_Vendedor, SUM(ISNULL(P.Costo_Rentabilidad,0)*(1+(TI.Porc1/100)) * CI.CANTIDAD * t.compromete_factura) AS ML_UCR
INTO #DETALLE8
FROM Cbtes CBF, Cbtes CBP, Cbtes_Items CI, Productos P,CBTES_TIPOS T,Productos_TasaIva TI
WHERE CBF.Id_Tipo_Cbte = T.ID_TIPO_CBTE
AND (T.COMPROMETE_FACTURA IN (1,-1) or CBF.Id_Tipo_Cbte IN ('FP25'))
AND T.ID_SUCURSAL = 25
AND CBP.Id_Tipo_Cbte IN ('PF25', 'PA25', 'PD25', 'PX25')
AND CBF.Id_Cliente = CBP.Id_Cliente
AND CBF.Id_Estado_Cbte NOT IN (0,3)
AND CBP.Cbte_Nro_Externo IS NOT NULL AND CBP.Cbte_Nro_Externo <> ''
AND CBP.Cbte_Nro_Externo =  CBF.Cbte_Nro_Externo  
AND CBF.Id_Trans = CI.Id_Trans
AND P.Id_Producto= CI.Id_Producto
AND P.Id_Iva = TI.Id_Iva
AND P.Id_Familia not in (93,1000,1020)
AND CBF.Fecha >= @Primer_Dia
AND CBF.Fecha < @Ultimo_Dia
GROUP BY  CBP.Id_Vendedor

UPDATE #RESUMEN SET ML_UCR  = D.ML_UCR 
FROM #RESUMEN R, #DETALLE8 D WHERE R.ID_VENDEDOR = D.ID_VENDEDOR

DROP TABLE #DETALLE8
*/
SELECT  CBP.Id_Vendedor, SUM(ISNULL(P.Costo_Rentabilidad,0) * (1+(TI.Porc1/100)) * CI.CANTIDAD * t.compromete_factura ) AS VD_UCR
INTO #DETALLE9
FROM Cbtes CBF, Cbtes CBP, Cbtes_Items CI, Productos P,CBTES_TIPOS T,Productos_TasaIva TI
WHERE CBF.Id_Tipo_Cbte = T.ID_TIPO_CBTE
AND (T.COMPROMETE_FACTURA IN (1,-1) AND T.COMPROMETE_IMPUESTOS IN (1,-1))
AND T.ID_SUCURSAL = 25
AND CBP.Id_Tipo_Cbte IN ('PI25')
AND CBF.Pedido_Nro = CONVERT(VARCHAR(10), CBP.Comprobante_Nro)
AND CBF.Id_Cliente = CBP.Id_Cliente
AND CBF.Id_Estado_Cbte NOT IN (0,3)
AND CBF.Id_Trans = CI.Id_Trans
AND P.Id_Producto= CI.Id_Producto
AND P.Id_Iva = TI.Id_Iva
AND P.Id_Familia not in (93,1000,1020)
AND CBF.Fecha >= @Primer_Dia
AND CBF.Fecha < @Ultimo_Dia
GROUP BY  CBP.Id_Vendedor

UPDATE #RESUMEN SET  VD_UCR  = D.VD_UCR
FROM #RESUMEN R, #DETALLE9 D WHERE R.ID_VENDEDOR = D.ID_VENDEDOR

DROP TABLE #DETALLE9

SELECT  CBF.Id_Vendedor, isnull(SUM(ISNULL(P.Costo_Rentabilidad,0) * (1+(TI.Porc1/100)) * CI.CANTIDAD * Ct.compromete_factura ),0)AS MY_UCR
INTO #DETALLE10
FROM Cbtes CBF, Cbtes_Items CI, Productos P,CBTES_TIPOS CT,Productos_TasaIva TI
WHERE CBF.Id_Tipo_Cbte = CT.ID_TIPO_CBTE
AND CBF.Id_Tipo_Cbte NOT IN ('FP25') 
AND CT.ID_SUCURSAL = 25
AND CT.Compromete_Impuestos NOT IN (1,-1)
AND CT.Compromete_factura IN (1,-1)
AND CBF.Id_Estado_Cbte NOT IN (0,3)
AND CBF.Id_Trans = CI.Id_Trans
AND P.Id_Producto= CI.Id_Producto
AND P.Id_Iva = TI.Id_Iva
AND P.Id_Familia not in (93,1000,1020)
AND CBF.Fecha >= @Primer_Dia
AND CBF.Fecha < @Ultimo_Dia
GROUP BY  CBF.Id_Vendedor

UPDATE #RESUMEN SET MY_UCR  = D.MY_UCR
FROM #RESUMEN R, #DETALLE10 D WHERE R.ID_VENDEDOR = D.ID_VENDEDOR

DROP TABLE #DETALLE10


SELECT  CBP.Id_Vendedor, SUM(ISNULL(P.Costo_Rentabilidad,0) * (1+(TI.Porc1/100)) * CI.CANTIDAD * t.compromete_factura ) AS VS_UCR
INTO #DETALLE11
FROM Cbtes CBF, Cbtes CBP, Cbtes_Items CI, Productos P,CBTES_TIPOS T,Productos_TasaIva TI
WHERE CBF.Id_Tipo_Cbte = T.ID_TIPO_CBTE
AND (T.COMPROMETE_FACTURA IN (1,-1) AND T.COMPROMETE_IMPUESTOS IN (1,-1))
AND T.ID_SUCURSAL = 25
AND CBP.Id_Tipo_Cbte IN ('PS25')
AND CBF.Pedido_Nro = CONVERT(VARCHAR(10), CBP.Comprobante_Nro)
AND CBF.Id_Cliente = CBP.Id_Cliente
AND CBF.Id_Estado_Cbte NOT IN (0,3)
AND CBF.Id_Trans = CI.Id_Trans
AND P.Id_Producto= CI.Id_Producto
AND P.Id_Iva = TI.Id_Iva
AND P.Id_Familia not in (93,1000,1020)
AND CBF.Fecha >= @Primer_Dia
AND CBF.Fecha < @Ultimo_Dia
GROUP BY  CBP.Id_Vendedor

UPDATE #RESUMEN SET VS_UCR  = D.VS_UCR
FROM #RESUMEN R, #DETALLE11 D WHERE R.ID_VENDEDOR = D.ID_VENDEDOR

DROP TABLE #DETALLE11

/*
SET @Objetivo= ISNULL((	SELECT SUM(MONTO_VENTAS) 
					FROM OBJETIVOS 
					WHERE Periodo >= @Fecha_Desde and Periodo < dateadd(dd,1,@Fecha_Hasta)
					AND Id_Sucursal=25),0)
*/
/*#####################################################################################*/

UPDATE #RESUMEN SET 
		  Total_Cant_Hoy    = ML_Cant_Hoy + VD_Cant_Hoy + VS_Cant_Hoy + MY_Cant_Hoy 
		 ,Total_Importe_Hoy = ML_Importe_Hoy + VD_Importe_Hoy+ VS_Importe_Hoy + MY_Importe_Hoy
		 ,Total_Cant_Mes    = ML_Cant_Mes + VD_Cant_Mes + VS_Cant_Mes + MY_Cant_Mes
		 ,Total_Importe_Mes = ML_Importe_Mes + VD_Importe_Mes + VS_Importe_Mes + MY_Importe_Mes
		 ,TOTAL_UCR         = ML_UCR + VD_UCR + VS_UCR + MY_UCR


SELECT * FROM #RESUMEN where Total_Importe_Mes <> 0  and Id_Vendedor not in ('99','291','42','')
 

/*

EXEC [SP_Rpt_Ventas_Por_Vend_Tipo] '20220501','20220522'

*/
