
DROP VIEW VW_REPORTE_VALES_ADELANTO_PEN
GO
CREATE VIEW VW_REPORTE_VALES_ADELANTO_PEN
AS
SELECT 	S.DOCTYPEID,D.MINDES,S.NBRDOCUMENT,S.SITEID,A.NAME,
	S.CUSTIDSS,CL.CUSTNAME,CL.TAXREGNBR AS RUC,B.NBRBONUS,S.PLACA,SD.INVTIDSKU AS Codigo,SD.DESCRINVENTORY,SD.QTY AS Cantidad,SD.SLS_PRICE AS Precio,SD.TOTALPEN AS Total,CAST(CONVERT(CHAR(20),S.DATEDOC,103) AS SMALLDATETIME) AS DATEDOC,S.DATEPROCESALES
FROM ((((OP_SALES AS S JOIN OP_SALESDET AS SD
      ON S.DOCTYPEID=SD.DOCTYPEID AND S.NBRDOCUMENT=SD.NBRDOCUMENT AND S.SITEID=SD.SITEID) JOIN OP_BONUS AS B
      ON S.DOCTYPEID=B.DOCTYPEID AND S.NBRDOCUMENT=B.NBRDOCUMENT AND S.SITEID=B.SITEID AND S.CUSTIDSS=B.CUSTIDSS) JOIN DOCTYPE AS D
      ON B.DOCTYPEID=D.DOCTYPEID) JOIN SITE AS A
      ON B.SITEID=A.SITEID) JOIN CUSTOMER AS CL
      ON B.CUSTIDSS=CL.CUSTIDSS 
WHERE S.TYPEPAYMENTID IN ('14') AND S.DOCSTATUSID<>'AN'





