DROP VIEW VW_CIERREZZ
GO
CREATE VIEW VW_CIERREZZ AS
SELECT DISTINCT 
    OP_SALES.DOCTYPEID, OP_SALES.NBRDOCUMENT, 
    OP_SALES.SITEID, OP_SALES.CURYID, 
    OP_SALES.DOCSTATUSID, OP_SALES.SALESPERID, 
    OP_SALES.USERID, OP_SALES.DATEDOC, 
    OP_SALES.DATEPROCESALES, OP_SALES.PERPOST, 
    OP_SALES.TOTALPEN, OP_SALES.TOTALUSD, 
    OP_SALES.SUBTOTALPEN, OP_SALES.SUBTOTALUSD, 
    OP_SALES.TAXIGVPEN, OP_SALES.TAXIGVUSD, 
    OP_SALES.TAXISCPEN, OP_SALES.TAXISCUSD, 
    OP_SALES.TOTALNOTAFFECTPEN, 
    OP_SALES.TOTALNOTAFFECTUSD, OP_SALES.TOTCASHPEN, 
    OP_SALES.TOTRETURNEDPEN, 
    OP_SALES.TOTRETURNEDUSD, OP_SALES.TOTCASHUSD, 
    OP_SALES.CUSTIDSS, OP_SALES.TAXREGNBR, 
    OP_SALES.CUSTNAME, OP_SALES.PLACA, 
    OP_SALES.NBRBONUS, OP_SALES.CURYTYPEID, 
    OP_SALES.STKCLOSEDX, OP_SALES.CURYRATE, 
    OP_SALES.SALESPOINT, OP_SALESDET.INVTIDSKU, 
    OP_SALESDET.CURYID AS CURYIDDET, 
    OP_SALESDET.SEQUENCE, 
    OP_SALESDET.DOCSTATUSID AS DOCSTATUSIDDET, 
    OP_SALESDET.DATEDOC AS DATEDOCDET, 
    OP_SALESDET.DATEPROCESALES AS DATEPROCESALESDET, 
    OP_SALESDET.PERPOST AS PERPOSTDET, 
    OP_SALESDET.NBRTURN, OP_SALESDET.NBRSIDE, 
    OP_SALESDET.NBRTRANSACFUEL, 
    OP_SALESDET.PORCENTTAXIGV, 
    OP_SALESDET.PORCENTTAXISC, 
    OP_SALESDET.TOTALPEN AS TOTALPENDET, 
    OP_SALESDET.TOTALUSD AS TOTALUSDDET, 
    OP_SALESDET.TAXPEN, OP_SALESDET.TAXUSD, 
    OP_SALESDET.STKITEM, OP_SALESDET.STKFISI, 
    OP_SALESDET.SLS_PRICE, OP_SALESDET.SLSPRICESALE, 
    OP_SALESDET.STDCOSTPEN, OP_SALESDET.STDCOSTUSD, 
    OP_SALESDET.INVTIDALTER, 
    OP_SALESDET.DESCRINVENTORY, OP_SALESDET.QTY, 
    OP_SALESDET.USERID AS USERIDDET, OP_SALESDET.KIT, 
    OP_SALES.TYPEPAYMENTID, IN_CATEGORY.CLASSID, 
    IN_CATEGORY.DESCR AS DESCATEGORIA, 
    IN_INVENTORY.DESCR AS DESCRPRODUCTO, 
    OP_SALESPERSON.SALESPERNAME, 
    OP_SALESCARD.TOTCARDPEN, 
    OP_SALESCARD.TOTCARDUSD
FROM OP_SALES INNER JOIN
    OP_SALESDET ON 
    OP_SALES.DOCTYPEID = OP_SALESDET.DOCTYPEID AND 
    OP_SALES.NBRDOCUMENT = OP_SALESDET.NBRDOCUMENT AND
     OP_SALES.SITEID = OP_SALESDET.SITEID INNER JOIN
    IN_INVENTORY ON 
    OP_SALESDET.INVTIDSKU = IN_INVENTORY.INVTIDSKU INNER JOIN
    IN_CATEGORY ON 
    IN_INVENTORY.CLASSID = IN_CATEGORY.CLASSID INNER JOIN
    OP_SALESPERSON ON 
    OP_SALES.SALESPERID = OP_SALESPERSON.SALESPERID INNER
     JOIN
    OP_SETUP ON 
    OP_SALES.DATEPROCESALES = OP_SETUP.DATEPROCESALES
     AND 
    OP_SALES.DOCTYPEID = OP_SETUP.DOCTYPEIDTICKET LEFT 
    JOIN
    OP_SALESCARD ON 
    OP_SALES.NBRDOCUMENT = OP_SALESCARD.NBRDOCUMENT
     AND OP_SALES.SITEID = OP_SALESCARD.SITEID AND 
    OP_SALES.DOCTYPEID = OP_SALESCARD.DOCTYPEID
WHERE (OP_SALES.STKCLOSEDZ = 0)
