ALTER VIEW VW_REGISTRO_VENTAS_USD 
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.SUBTOTALUSD ELSE S.SUBTOTALUSD 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.TOTALNOTAFFECTUSD ELSE S.TOTALNOTAFFECTUSD 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.TAXIGVUSD ELSE S.TAXIGVUSD 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.TOTALUSD ELSE S.TOTALUSD 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 )
