USE [SAINT]
GO
SET IDENTITY_INSERT [dbo].[_REPORTE_ENC] ON 

INSERT [dbo].[_REPORTE_ENC] ([ID], [REPORTE], [SQL1], [SQL2], [SQL3], [RPT]) VALUES (1, N'CLIENTES', N'INSERT INTO _REPORTE ( REPORTE,DESCRIP01, DESCRIP02, DESCRIP03, DESCRIP04, DESCRIP05, DESCRIP06, NUMERO01)
SELECT  ''CLIENTES'', CODCLIE, ID3, DESCRIP, DIREC1, DIREC2, TELEF, SALDO FROM SACLIE
WHERE CODCLIE+DESCRIP+ISNULL(DIREC1,'''')+ISNULL(TELEF,'''') LIKE ''%[DESCRIP01]%'' ORDER BY DESCRIP;', NULL, NULL, [encoded data, 17410 characters])
INSERT [dbo].[_REPORTE_ENC] ([ID], [REPORTE], [SQL1], [SQL2], [SQL3], [RPT]) VALUES (2, N'PROVEEDOR', N'INSERT INTO _REPORTE ( REPORTE,DESCRIP01, DESCRIP02, DESCRIP03, DESCRIP04, DESCRIP05, DESCRIP06, NUMERO01)
SELECT ''PROVEEDOR'', CODPROV, ID3, DESCRIP, DIREC1, DIREC2, TELEF, SALDO FROM SAPROV
WHERE DESCRIP LIKE ''%[DESCRIP01]%'';', NULL, NULL, [encoded data, 16386 characters])
INSERT [dbo].[_REPORTE_ENC] ([ID], [REPORTE], [SQL1], [SQL2], [SQL3], [RPT]) VALUES (4, N'VENDEDOR', N'INSERT INTO _REPORTE ( REPORTE,DESCRIP01, DESCRIP02, DESCRIP03, DESCRIP04, DESCRIP05, DESCRIP06, NUMERO01)



SELECT   ''VENDEDOR'',  SAVEND.CodVend, SAVEND.ID3, SAVEND.Descrip, SAVEND.Direc1, SAVEND.Direc2, SAVEND.Telef, SACVEN.Monto
FROM         SAVEND INNER JOIN
                      SACVEN ON SAVEND.CodVend = SACVEN.CodVend
WHERE     (SACVEN.TipoRgo = 5) AND (SACVEN.NroUnico = 1)  AND SAVEND.DESCRIP LIKE ''%[DESCRIP01]%''', NULL, NULL, [encoded data, 16386 characters])
INSERT [dbo].[_REPORTE_ENC] ([ID], [REPORTE], [SQL1], [SQL2], [SQL3], [RPT]) VALUES (5, N'FACTURAS PENDIENTES', N'INSERT INTO _REPORTE ( REPORTE,DESCRIP01, DESCRIP02, FECHA01, FECHA02, DESCRIP03, DESCRIP04, DESCRIP05, DESCRIP06, DESCRIP07, DESCRIP08, NUMERO01, NUMERO02, NUMERO03, NUMERO04, NUMERO05)

SELECT  ''FACTURAS PENDIENTES'',   SAACXC.CodClie, SACLIE.Descrip, SAACXC.FechaE, SAACXC.FechaV, SAACXC.CodEsta, SAACXC.CodUsua, SAACXC.CodOper, SAACXC.CodVend, SAVEND.Descrip AS NomVend, SAACXC.NumeroD, 
                      SAACXC.Factor, SAACXC.Monto, SAACXC.MontoNeto, SAACXC.MtoTax, SAACXC.Saldo
FROM         SAACXC INNER JOIN
                      SACLIE ON SAACXC.CodClie = SACLIE.CodClie INNER JOIN
                      SAVEND ON SAACXC.CodVend = SAVEND.CodVend
WHERE     (SAACXC.TipoCxc = ''10'') AND (SAACXC.Saldo > 0) AND (SACLIE.Descrip LIKE ''%[DESCRIP01]%'') AND (CONVERT(VARCHAR, SAACXC.FechaV, 112) >= [FECHA01]) AND (CONVERT(VARCHAR, 
                      SAACXC.FechaV, 112) <= [FECHA02])', NULL, NULL, [encoded data, 16386 characters])
INSERT [dbo].[_REPORTE_ENC] ([ID], [REPORTE], [SQL1], [SQL2], [SQL3], [RPT]) VALUES (6, N'TIPO DE PAGO', N'INSERT INTO _REPORTE ( REPORTE,DESCRIP01, DESCRIP02, DESCRIP03, NUMERO01)
SELECT ''TIPO DE PAGO'', CODTARJ, DESCRIP,CLASE,IMPUESTO FROM SATARJ
WHERE CODTARJ+DESCRIP+CLASE LIKE ''%[DESCRIP01]%'';', NULL, NULL, [encoded data, 15362 characters])
INSERT [dbo].[_REPORTE_ENC] ([ID], [REPORTE], [SQL1], [SQL2], [SQL3], [RPT]) VALUES (7, N'PRODUCTO MAS VENDIDO', N'INSERT INTO _REPORTE ( REPORTE,DESCRIP01, DESCRIP02, DESCRIP03, NUMERO01, NUMERO02, NUMERO03)

SELECT    ''PRODUCTO MAS VENDIDO'',    ISNULL(SAINSTA.Descrip, ''SERVICIO'') AS INSTANCIA, SAITEMFAC.CodItem, SAITEMFAC.Descrip1, SUM(SAFACT.Signo * SAITEMFAC.Cantidad) AS CANTIDAD, SUM(SAFACT.Signo * SAITEMFAC.TotalItem) AS VENTA, 
                         SUM(SAFACT.Signo * SAITEMFAC.Cantidad * SAITEMFAC.Costo) AS UTILIDAD
FROM            SAINSTA RIGHT OUTER JOIN
                         SAPROD ON SAINSTA.CodInst = SAPROD.CodInst RIGHT OUTER JOIN
                         SAFACT INNER JOIN
                         SAITEMFAC ON SAFACT.TipoFac = SAITEMFAC.TipoFac AND SAFACT.NumeroD = SAITEMFAC.NumeroD ON SAPROD.CodProd = SAITEMFAC.CodItem
WHERE        (SAFACT.TipoFac IN (''A'', ''B'')) AND (CONVERT(VARCHAR, SAFACT.FechaE, 112) >= [FECHA01]) AND (CONVERT(VARCHAR, SAFACT.FechaE, 112) <= [FECHA02]) AND SAITEMFAC.DESCRIP1+SAINSTA.DESCRIP+SAITEMFAC.CODITEM LIKE ''%[DESCRIP01]%''
GROUP BY SAINSTA.Descrip, SAITEMFAC.CodItem, SAITEMFAC.Descrip1, SAPROD.CodInst
ORDER BY UTILIDAD DESC', NULL, NULL, [encoded data, 16386 characters])
INSERT [dbo].[_REPORTE_ENC] ([ID], [REPORTE], [SQL1], [SQL2], [SQL3], [RPT]) VALUES (8, N'CXC', N'INSERT INTO _REPORTE ( REPORTE,DESCRIP01, DESCRIP02, FECHA01, FECHA02, DESCRIP03, DESCRIP04, DESCRIP05, DESCRIP06, DESCRIP07, DESCRIP08, NUMERO01, NUMERO02, NUMERO03, NUMERO04, NUMERO05, NUMERO06)

SELECT  ''CXC'',   SAACXC.CodClie, SACLIE.Descrip, SAACXC.FechaE, SAACXC.FechaV, SAACXC.CodEsta, SAACXC.CodUsua, SAACXC.CodOper, SAACXC.CodVend, SAVEND.Descrip AS NomVend, SAACXC.NumeroD, 
                      SAACXC.Factor, SAACXC.Monto, SAACXC.MontoNeto, SAACXC.MtoTax, SAACXC.Saldo, (SAACXC.Monto/SAACXC.Factor) As MontoD
FROM         SAACXC INNER JOIN
                      SACLIE ON SAACXC.CodClie = SACLIE.CodClie INNER JOIN
                      SAVEND ON SAACXC.CodVend = SAVEND.CodVend
WHERE     (SAACXC.TipoCxc = ''10'') AND (SAACXC.Saldo > 0) AND (SACLIE.Descrip LIKE ''%[DESCRIP01]%'') AND (CONVERT(VARCHAR, SAACXC.FechaV, 112) >= [FECHA01]) AND (CONVERT(VARCHAR, 
                      SAACXC.FechaV, 112) <= [FECHA02])', NULL, NULL, [encoded data, 18434 characters])
INSERT [dbo].[_REPORTE_ENC] ([ID], [REPORTE], [SQL1], [SQL2], [SQL3], [RPT]) VALUES (1008, N'MOVIMIENTO EN UNIDADES', N'INSERT INTO _REPORTE ( REPORTE,DESCRIP01, DESCRIP02, FECHA01, FECHA02,DESCRIP03, DESCRIP04, DESCRIP05, DESCRIP06,  DESCRIP07, NUMERO01, NUMERO02, NUMERO03, NUMERO04, NUMERO05)

select ''MOVIMIENTO EN UNIDADES'', ''INICIAL'' AS TIPO, SAINITI.CODUBIC , CONVERT(DATETIME,PERIODO+''01'') AS FechaE, CONVERT(DATETIME,PERIODO+''01'') AS FechaV, PERIODO AS NumeroD, 
'''' AS CodProv, '''' AS Descrip, SAINITI.CodPROD, SAPROD.Descrip, SAINITI.EXINICIAL AS Cantidad, SAINITI.Costo, SAINITI.EXINICIAL*SAINITI.COSTO  TotalItem, 0 AS Precio3, 
0 as TotalPrecio 
 from SAINITI  inner join 	SAPROD ON 
	SAINITI.CODPROD=SAPROD.CODPROD 

	WHERE  (SAPROD.Descrip LIKE ''%[DESCRIP01]%'') AND  PERIODO=LEFT([FECHA01],6)  

UNION

select ''MOVIMIENTO EN UNIDADES'', ''COMPRA'' AS TIPO , SAITEMCOM.CODUBIC, SACOMP.FechaE, SACOMP.FechaV, SACOMP.NumeroD, SACOMP.CodProv, SACOMP.Descrip,
 SAITEMCOM.CodItem, SAITEMCOM.Descrip1, SAITEMCOM.Cantidad, SAITEMCOM.Costo,  SAITEMCOM.TotalItem, SAITEMCOM.Precio3, SAITEMCOM.Precio3*SAITEMCOM.Cantidad as TotalPrecio from SACOMP 
 inner join 
	SAITEMCOM ON 
	SACOMP.TipoCom=SAITEMCOM.TipoCom AND SACOMP.NumeroD=SAITEMCOM.NumeroD AND SACOMP.CodProv=SAITEMCOM.CodProv

	WHERE SACOMP.TipoCom=''H'' AND (SAITEMCOM.Descrip1 LIKE ''%[DESCRIP01]%'') AND  CONVERT (VARCHAR,SACOMP.FECHAE,112)>=[FECHA01]   AND CONVERT (VARCHAR,SACOMP.FECHAE,112)<=[FECHA02] 



	UNION

select ''MOVIMIENTO EN UNIDADES'', ''DEVCOMPRA'' AS TIPO , SAITEMCOM.CODUBIC,SACOMP.FechaE, SACOMP.FechaV, SACOMP.NumeroD, SACOMP.CodProv, SACOMP.Descrip,
 SAITEMCOM.CodItem, SAITEMCOM.Descrip1, SAITEMCOM.Cantidad*-1, SAITEMCOM.Costo*-1,  SAITEMCOM.TotalItem*-1, SAITEMCOM.Precio3, SAITEMCOM.Precio3*SAITEMCOM.Cantidad*-1 as TotalPrecio from SACOMP 
 inner join 
	SAITEMCOM ON 
	SACOMP.TipoCom=SAITEMCOM.TipoCom AND SACOMP.NumeroD=SAITEMCOM.NumeroD AND SACOMP.CodProv=SAITEMCOM.CodProv

	WHERE SACOMP.TipoCom=''I'' AND (SAITEMCOM.Descrip1 LIKE ''%[DESCRIP01]%'')  AND  CONVERT (VARCHAR,SACOMP.FECHAE,112)>=[FECHA01]   AND CONVERT (VARCHAR,SACOMP.FECHAE,112)<=[FECHA02] 

	UNION

	SELECT ''MOVIMIENTO EN UNIDADES'', ''FACTURA'' AS TIPO,SAITEMFAC.CODUBIC, SAFACT.FechaE, SAFACT.FechaV, SAFACT.NumeroD, SAFACT.CodClie, SAFACT.Descrip, 
SAITEMFAC.CodItem, SAITEMFAC.Descrip1, SAITEMFAC.Cantidad*-1 AS CANTIDAD, SAITEMFAC.Costo*-1, 
SAITEMFAC.Cantidad*Costo*-1 AS TotalItem, SAITEMFAC.Precio*-1,  SAITEMFAC.Precio*SAITEMFAC.Cantidad*-1 as TotalPrecio 

from SAFACT INNER JOIN SAITEMFAC ON SAFACT.TipoFac=SAITEMFAC.TipoFac AND SAFACT.NumeroD=SAITEMFAC.NumeroD

WHERE SAFACT.TipoFac=''A'' AND (SAITEMFAC.Descrip1 LIKE ''%[DESCRIP01]%'') AND  CONVERT (VARCHAR,SAFACT.FECHAE,112)>=[FECHA01]   AND CONVERT (VARCHAR,SAFACT.FECHAE,112)<=[FECHA02] 

	UNION

	SELECT ''MOVIMIENTO EN UNIDADES'', ''DEVOLUCION'' AS TIPO,SAITEMFAC.CODUBIC, SAFACT.FechaE, SAFACT.FechaV, SAFACT.NumeroD, SAFACT.CodClie, SAFACT.Descrip, 
SAITEMFAC.CodItem, SAITEMFAC.Descrip1, SAITEMFAC.Cantidad AS CANTIDAD, SAITEMFAC.Costo, 
SAITEMFAC.Cantidad*Costo AS TotalItem, SAITEMFAC.Precio,  SAITEMFAC.Precio*SAITEMFAC.Cantidad as TotalPrecio 

from SAFACT INNER JOIN SAITEMFAC ON SAFACT.TipoFac=SAITEMFAC.TipoFac AND SAFACT.NumeroD=SAITEMFAC.NumeroD

WHERE SAFACT.TipoFac=''B'' AND (SAITEMFAC.Descrip1 LIKE ''%[DESCRIP01]%'') AND  CONVERT (VARCHAR,SAFACT.FECHAE,112)>=[FECHA01]   AND CONVERT (VARCHAR,SAFACT.FECHAE,112)<=[FECHA02] 

	UNION 

	SELECT ''MOVIMIENTO EN UNIDADES'', ''CARGO'' AS TIPO, SAITEMOPI.CODUBIC, SAOPEI.FechaE, '''' as fechaV, SAOPEI.NumeroD, SAOPEI.Autori, SAOPEI.Respon, SAITEMOPI.CodItem, SAITEMOPI.Descrip1, SAITEMOPI.Cantidad, 0 AS COSTO,
    0 AS PRECIO, 0 AS Totcosto, 0 as TotPrecio from SAOPEI
   INNER JOIN SAITEMOPI ON SAOPEI.TipoOpI=SAITEMOPI.TipoOpI and SAOPEI.NumeroD=SAITEMOPI.NumeroD
 
   where SAOPEI.TipoOpI=''O'' AND (SAITEMOPI.Descrip1 LIKE ''%[DESCRIP01]%'') AND  CONVERT (VARCHAR,SAOPEI.FECHAE,112)>=[FECHA01]   AND CONVERT (VARCHAR,SAOPEI.FECHAE,112)<=[FECHA02] 

   UNION

SELECT ''MOVIMIENTO EN UNIDADES'', ''DESCARGO'' AS TIPO, SAITEMOPI.CODUBIC, SAOPEI.FechaE,'''' as fechaV, SAOPEI.NumeroD, SAOPEI.Autori, SAOPEI.Respon, SAITEMOPI.CodItem, SAITEMOPI.Descrip1, SAITEMOPI.Cantidad*-1, 0 AS COSTO,
   0 AS PRECIO, 0 AS Totcosto, 0 as TotPrecio from SAOPEI
   INNER JOIN SAITEMOPI ON SAOPEI.TipoOpI=SAITEMOPI.TipoOpI and SAOPEI.NumeroD=SAITEMOPI.NumeroD

   where SAOPEI.TipoOpI=''P'' AND (SAITEMOPI.Descrip1 LIKE ''%[DESCRIP01]%'') AND  CONVERT (VARCHAR,SAOPEI.FECHAE,112)>=[FECHA01]   AND CONVERT (VARCHAR,SAOPEI.FECHAE,112)<=[FECHA02] 

   ORDER BY FechaE DESC
', NULL, NULL, [encoded data, 20482 characters])
SET IDENTITY_INSERT [dbo].[_REPORTE_ENC] OFF
