ALTER VIEW VW_REGISTRO_VENTAS_PEN AS    
SELECT  S.DOCTYPEID,D.DESCR,S.NBRDOCUMENT,S.SITEID,A.NAME,S.DATEDOC,S.DATEPROCESALES,    
 CASE (S.DOCSTATUSID) WHEN 'AN' THEN '' ELSE S.TAXREGNBR END AS TAXREGNBR,    
 CASE (S.DOCSTATUSID) WHEN 'AN' THEN 'ANULADO' ELSE S.CUSTNAME END AS CUSTNAME,    
 CASE (S.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 (S.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 (S.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 (S.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,ISNULL(AR.DUEDATE,S.DATEDOC) AS DUEDATE
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
LEFT JOIN AR_CCDOC AR (NOLOCK) ON (S.SITEID = AR.SITEID AND S.DOCTYPEID = AR.DOCTYPEID AND S.NBRDOCUMENT = AR.NBRDOCUMENT )