DROP VIEW dbo.vw_ingresoscompraproveedor
GO
CREATE VIEW dbo.vw_ingresoscompraproveedor
AS
SELECT     dbo.IN_INTRAHEAD.SITEID, dbo.IN_INTRAHEAD.BATNBR, dbo.IN_INTRAHEAD.TYPEDOCID, dbo.IN_INTRAHEAD.VENDIDSS, 
                      dbo.IN_INTRAHEAD.DOCTYPEID, dbo.IN_INTRAHEAD.DOCSTATUSID, dbo.IN_INTRAHEAD.REFNBR, dbo.IN_INTRAHEAD.CURYRATE, 
                      dbo.IN_INTRAHEAD.SITEPROCES, dbo.IN_INTRAHEAD.NBRORDER, dbo.IN_INTRAHEAD.DATENBRORDER, dbo.IN_INTRAHEAD.USERIDORDER, 
                      dbo.IN_INTRAHEAD.INTRDATE, dbo.IN_INTRAHEAD.INTRDATEPROCE, dbo.IN_INTRAHEAD.SITEDEST,
 		      CASE (dbo.IN_INTRAHEAD.TYPEDOCID) WHEN (SELECT INTYPECREDMEM FROM IN_SETUP) THEN (-1)* dbo.IN_INTRAHEAD.INTRTOTPEN ELSE dbo.IN_INTRAHEAD.INTRTOTPEN END AS INTRTOTPEN, 
                      CASE (dbo.IN_INTRAHEAD.TYPEDOCID) WHEN (SELECT INTYPECREDMEM FROM IN_SETUP) THEN (-1)* dbo.IN_INTRAHEAD.INTRTAXPEN ELSE dbo.IN_INTRAHEAD.INTRTAXPEN END AS INTRTAXPEN,
		      CASE (dbo.IN_INTRAHEAD.TYPEDOCID) WHEN (SELECT INTYPECREDMEM FROM IN_SETUP) THEN (-1)* dbo.IN_INTRAHEAD.INTRTOTUSD ELSE dbo.IN_INTRAHEAD.INTRTOTUSD END AS INTRTOTUSD,
                      CASE (dbo.IN_INTRAHEAD.TYPEDOCID) WHEN (SELECT INTYPECREDMEM FROM IN_SETUP) THEN (-1)* dbo.IN_INTRAHEAD.INTRTAXUSD ELSE dbo.IN_INTRAHEAD.INTRTAXUSD END AS INTRTAXUSD,
		      dbo.IN_INTRAHEAD.PERPOST, 
                      dbo.IN_TYPEDOC.STKBUY, dbo.IN_TYPEDOC.DESCR, dbo.IN_VENDOR.DESCR AS NPROVEEDOR, dbo.IN_VENDOR.TAXREGNBR AS RUC, 
                      dbo.SITE.NAME, dbo.DOCTYPE.MINDES AS tdocumento, dbo.CURRENCY.DESCRMONEY, dbo.IN_INTRAHEAD.FROMCURYID
FROM         dbo.IN_INTRAHEAD INNER JOIN
                      dbo.SITE ON dbo.IN_INTRAHEAD.SITEID = dbo.SITE.SITEID INNER JOIN
                      dbo.IN_VENDOR ON dbo.IN_INTRAHEAD.VENDIDSS = dbo.IN_VENDOR.VENDIDSS INNER JOIN
                      dbo.IN_TYPEDOC ON dbo.IN_INTRAHEAD.TYPEDOCID = dbo.IN_TYPEDOC.TYPEDOCID INNER JOIN
                      dbo.DOCTYPE ON dbo.IN_INTRAHEAD.DOCTYPEID = dbo.DOCTYPE.DOCTYPEID INNER JOIN
                      dbo.CURRENCY ON dbo.IN_INTRAHEAD.FROMCURYID = dbo.CURRENCY.CURYID
WHERE     (dbo.IN_TYPEDOC.STKBUY = 1  AND dbo.IN_INTRAHEAD.STKCLOSED=1) -- SOLO LOS MOVIMIENTOS POR COMPRAS Y QUE ESTEN CERRADOS

