DROP VIEW VW_REGISTRO_VENTAS_PEN	
GO
CREATE VIEW VW_REGISTRO_VENTAS_PEN	
AS
SELECT  S.DOCTYPEID,D.DESCR,S.NBRDOCUMENT,S.SITEID,A.NAME,S.DATEDOC,S.DATEPROCESALES,
	CASE (DOCSTATUSID) WHEN 'AN' THEN '' ELSE S.TAXREGNBR END AS TAXREGNBR,
	CASE (DOCSTATUSID) WHEN 'AN' THEN 'ANULADO' ELSE S.CUSTNAME END AS CUSTNAME,
	CASE (DOCSTATUSID) WHEN 'AN' THEN 0 ELSE CASE(S.DOCTYPEID) WHEN (SELECT DOCTYPEIDNC FROM OP_SETUP) THEN (-1)*S.SUBTOTALPEN ELSE S.SUBTOTALPEN END END AS VAL_AFEC,
	CASE (DOCSTATUSID) WHEN 'AN' THEN 0 ELSE CASE(S.DOCTYPEID) WHEN (SELECT DOCTYPEIDNC FROM OP_SETUP) THEN (-1)*S.TOTALNOTAFFECTPEN ELSE S.TOTALNOTAFFECTPEN END END AS VAL_NAFEC,  
	CASE (DOCSTATUSID) WHEN 'AN' THEN 0 ELSE CASE(S.DOCTYPEID) WHEN (SELECT DOCTYPEIDNC FROM OP_SETUP) THEN (-1)*S.TAXIGVPEN ELSE S.TAXIGVPEN END END AS VAL_IGV,
	CASE (DOCSTATUSID) WHEN 'AN' THEN 0 ELSE CASE(S.DOCTYPEID) WHEN (SELECT DOCTYPEIDNC FROM OP_SETUP) THEN (-1)*S.TOTALPEN ELSE S.TOTALPEN END END AS TOTAL,
	S.SALESPOINT,C.SERIALNUMBER,S.CURYID,S.DOCSTATUSID
FROM 	((OP_SALES AS S JOIN DOCTYPE AS D ON S.DOCTYPEID=D.DOCTYPEID) JOIN SITE AS A
	ON S.SITEID=A.SITEID) JOIN OP_SALESPOINTSETTING AS C
	ON S.SALESPOINT=C.SALESPOINT



