SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


ALTER PROCEDURE OP_ListarParteDiarioSimple
(@SITEID         UD_IDSITE = '76  ',
 @FECHAPROCESO   CHAR(8) = '20080505',
 @MONEDABASE     UD_CURYID = 'PEN',
 @MONEDAEXTR     UD_CURYID = 'USD',
 @USERID         UD_USERID = 'SYSADMIN')
as
--Begin
DECLARE @TOT_COMBUSTIBLE MONEY
DECLARE @TOT_GLP MONEY
DECLARE @TOT_LUBAD MONEY
DECLARE @TOT_OTROS MONEY
DECLARE @TOT_CRED_COMBUSTIBLE MONEY
DECLARE @TOT_CRED_GLP MONEY
DECLARE @TOT_CRED_LUBAD MONEY
DECLARE @TOT_CRED_OTROS MONEY
DECLARE @TOT_TARJ_COMBUSTIBLE MONEY
DECLARE @TOT_TARJ_GLP MONEY
DECLARE @TOT_TARJ_LUBAD MONEY
DECLARE @TOT_TARJ_OTROS MONEY
DECLARE @TOT_EFEC_COMBUSTIBLE MONEY
DECLARE @TOT_EFEC_GLP MONEY
DECLARE @TOT_EFEC_LUBAD MONEY
DECLARE @TOT_EFEC_OTROS MONEY
DECLARE @SERVICIOS MONEY
DECLARE @GAS MONEY
DECLARE @MOBIL MONEY
DECLARE @TELEFONO MONEY
DECLARE @OTROS MONEY
DECLARE @PAG_DIFERENCIA MONEY
DECLARE @PAG_ADELANTADO MONEY
DECLARE @TOT_FALTANTE MONEY
DECLARE @TOT_SOBRANTE MONEY
DECLARE @TOT_COBRANZA MONEY
DECLARE @DEPOSITOS MONEY
DECLARE @AFERICION MONEY
DECLARE @DEPOSITO_TIENDA MONEY

----------CONTOMETROS-----------------------
SELECT @TOT_COMBUSTIBLE =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
--INNER JOIN OP_SETUP 	SETP	ON (S.DATEPROCESALES = SETP.DATEPROCESALES)
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.INVTIDSKU IN ('1001','1002','1003','1004','1005') --FILTRO DE ARTICULOS
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
--AND S.USERID = 'VENDPLAYA'
-------------------------------------------------SELECT * FROM OP_SALES WHERE USERID <> 'VENDPLAYA'
----------GLP-----------------------
SELECT @TOT_GLP =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.INVTIDSKU IN ('1006','06') --FILTRO DE ARTICULOS
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
--AND S.USERID = 'VENDPLAYA'
-------------------------------------------------
----------LUBRICANTES/ADITIVOS-----------------------
SELECT @TOT_LUBAD =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.CLASSID IN ('02','04') --FILTRO DE = LUBRICANTES + ADITIVOS
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA	
-------------------------------------------------
----------OTROS-----------------------
SELECT @TOT_OTROS =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.CLASSID NOT IN ('02','04','19','1006') --FILTRO DE OTROS = NO : LUBRICANTES, ADITIVOS, COMPUSTILE, GAS LICUADO
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
-------------------------------------------------
--*************************************************
----------CREDITO COMBUSTIBLE-----------------------
SELECT @TOT_CRED_COMBUSTIBLE =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
--INNER JOIN OP_SETUP 	SETP	ON (S.DATEPROCESALES = SETP.DATEPROCESALES)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.INVTIDSKU IN ('1001','1002','1003','1004','1005') --FILTRO DE ARTICULOS
AND S.TYPEPAYMENTID = '06' --FILTRO DE FORMA DE PAGO (CREDITO)
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
-------------------------------------------------
----------CREDITO GLP-----------------------
SELECT @TOT_CRED_GLP =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.INVTIDSKU IN ('1006','06') --FILTRO DE ARTICULOS
AND S.TYPEPAYMENTID = '06' --FILTRO DE FORMA DE PAGO (CREDITO)
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
-------------------------------------------------
----------CREDITO LUBRICANTES/ADITIVOS-----------------------
SELECT @TOT_CRED_LUBAD =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.CLASSID IN ('02','04') --FILTRO DE = LUBRICANTES + ADITIVOS
AND S.TYPEPAYMENTID = '06' --FILTRO DE FORMA DE PAGO (CREDITO)
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
-------------------------------------------------
----------CREDITO OTROS-----------------------
SELECT @TOT_CRED_OTROS =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.CLASSID NOT IN ('02','04','19','1006') --FILTRO DE OTROS = NO : LUBRICANTES, ADITIVOS, COMPUSTILE, GAS LICUADO
AND S.TYPEPAYMENTID = '06' --FILTRO DE FORMA DE PAGO (CREDITO)
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
-------------------------------------------------
--*************************************************
----------TARJETA CREDITO COMBUSTIBLE-----------------------
SELECT @TOT_TARJ_COMBUSTIBLE =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
--INNER JOIN OP_SETUP 	SETP	ON (S.DATEPROCESALES = SETP.DATEPROCESALES)
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.INVTIDSKU IN ('1001','1002','1003','1004','1005') --FILTRO DE ARTICULOS
AND S.TYPEPAYMENTID = '02' --FILTRO DE FORMA DE PAGO (TARJETA CREDITO)
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
-------------------------------------------------
----------TARJETA CREDITO GLP-----------------------
SELECT @TOT_TARJ_GLP =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.INVTIDSKU IN ('1006','06') --FILTRO DE ARTICULOS
AND S.TYPEPAYMENTID = '02' --FILTRO DE FORMA DE PAGO (TARJETA CREDITO)
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
-------------------------------------------------
----------TARJETA CREDITO LUBRICANTES/ADITIVOS-----------------------
SELECT @TOT_TARJ_LUBAD =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.CLASSID IN ('02','04') --FILTRO DE = LUBRICANTES + ADITIVOS
AND S.TYPEPAYMENTID = '02' --FILTRO DE FORMA DE PAGO (TARJETA CREDITO)
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
-------------------------------------------------
----------TARJETA CREDITO OTROS-----------------------
SELECT @TOT_TARJ_OTROS =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.CLASSID NOT IN ('02','04','19','1006') --FILTRO DE OTROS = NO : LUBRICANTES, ADITIVOS, COMPUSTILE, GAS LICUADO
AND S.TYPEPAYMENTID = '02' --FILTRO DE FORMA DE PAGO (TARJETA CREDITO)
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
-------------------------------------------------
--*************************************************
----------EFECTIVO COMBUSTIBLE-----------------------
SELECT @TOT_EFEC_COMBUSTIBLE =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
--INNER JOIN OP_SETUP 	SETP	ON (S.DATEPROCESALES = SETP.DATEPROCESALES)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND S.TYPEPAYMENTID = '01' --FILTRO DE FORMA DE PAGO (EFECTIVO)
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
-------------------------------------------------
----------EFECTIVO GLP-----------------------
SELECT @TOT_EFEC_GLP =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.INVTIDSKU IN ('1006','06') --FILTRO DE ARTICULOS
AND S.TYPEPAYMENTID = '01' --FILTRO DE FORMA DE PAGO (EFECTIVO)
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
-------------------------------------------------
----------EFECTIVO LUBRICANTES/ADITIVOS-----------------------
SELECT @TOT_EFEC_LUBAD =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.CLASSID IN ('02','04') --FILTRO DE = LUBRICANTES + ADITIVOS
AND S.TYPEPAYMENTID = '01' --FILTRO DE FORMA DE PAGO (EFECTIVO)
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
-------------------------------------------------
----------EFECTIVO OTROS-----------------------
SELECT @TOT_EFEC_OTROS =
CASE @MONEDABASE WHEN 'PEN' THEN SUM(SD.TOTALPEN) ELSE SUM(SD.TOTALUSD) END
FROM OP_SALES 		S
INNER JOIN OP_SALESDET 	SD 	ON (S.DOCTYPEID = SD.DOCTYPEID AND S.NBRDOCUMENT = SD.NBRDOCUMENT AND S.SITEID = SD.SITEID)
INNER JOIN IN_INVENTORY I 	ON (SD.INVTIDSKU = I.INVTIDSKU) 
INNER JOIN IN_CATEGORY  C 	ON (I.CLASSID = C.CLASSID)
INNER JOIN OP_SALESPERSON SP 	ON (S.SALESPERID = SP.SALESPERID) 
INNER JOIN OP_SALESPOINTSETTING SPS ON (S.SALESPOINT = SPS.SALESPOINT)
LEFT JOIN OP_SALESCARD 	SC	ON (S.NBRDOCUMENT = SC.NBRDOCUMENT AND S.SITEID = SC.SITEID AND S.DOCTYPEID = SC.DOCTYPEID)
WHERE S.SITEID = @SITEID AND S.DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO
AND I.CLASSID NOT IN ('02','04','19','1006') --FILTRO DE OTROS = NO : LUBRICANTES, ADITIVOS, COMPUSTILE, GAS LICUADO
AND S.TYPEPAYMENTID = '01' --FILTRO DE FORMA DE PAGO (EFECTIVO)
AND SPS.BUSINESSTYPE = 1 -- SOLO LOS TIPO DE NEGOCIO PLAYA
-------------------------------------------------
--*************************************************
--CONCEPTOS MANUALES
SELECT 
@SERVICIOS = SERVICIOS,
@GAS = GAS,
@MOBIL = MOBIL,
@TELEFONO = TELEFONO,
@OTROS = OTROS,
@PAG_DIFERENCIA = PAG_DIFERENCIA,
@PAG_ADELANTADO = PAG_ADELANTADO,
@AFERICION = AFERICION,
@TOT_COBRANZA = COBRANZAS,
@DEPOSITO_TIENDA = DEPOSITO_TIENDA	--POR MIENTRAS--**********************************************************<<--------------------------
FROM OP_PD_MANUAL (NOLOCK)--SELECT * FROM OP_PD_MANUAL
WHERE SITEID = @SITEID AND DATEPROCESALES = @FECHAPROCESO--FECHA DE PROCESO

--DEPOSITO PLAYA
SELECT @DEPOSITOS=CASE @MONEDABASE WHEN 'PEN' THEN SUM(TOTALPEN) ELSE SUM(TOTALUSD) END
FROM OP_DEPOSITSITEDET (NOLOCK) 
WHERE SITEID=@SITEID AND DATEPROCE=@FECHAPROCESO 

--SOBRANTE/FALTANTE
SELECT @TOT_FALTANTE=CASE @MONEDABASE WHEN 'PEN' THEN 
					SUM(CASE CCSALESPERSONTYPEID WHEN 'F' THEN TOTALPEN ELSE 0 END) 
				ELSE SUM(CASE CCSALESPERSONTYPEID WHEN 'F' THEN TOTALUSD ELSE 0 END) END,
       @TOT_SOBRANTE=CASE @MONEDABASE WHEN 'PEN' THEN 
					SUM(CASE CCSALESPERSONTYPEID WHEN 'S' THEN TOTALPEN ELSE 0 END)
				ELSE SUM(CASE CCSALESPERSONTYPEID WHEN 'S' THEN TOTALPEN ELSE 0 END) END
       FROM OP_CCSALESPERSONDET (NOLOCK)
       WHERE SITEID=@SITEID AND DATEPROCE=@FECHAPROCESO 
--COBRANZAS
/*
SELECT @TOT_COBRANZA=CASE @MONEDABASE WHEN 'PEN' THEN SUM(A.PAIDMENTPEN) ELSE SUM(A.PAIDMENTUSD) END
	FROM AR_CCDOC A  
	LEFT JOIN AR_CCDOCDET CD (NOLOCK) ON (A.SITEID = CD.SITEID AND A.DOCTYPEID = CD.DOCTYPEID AND A.NBRDOCUMENT = CD.NBRDOCUMENT) 
	INNER JOIN DOCTYPE D (NOLOCK) ON (A.DOCTYPEID=D.DOCTYPEID) 
	INNER JOIN CUSTOMER C (NOLOCK) ON (C.CUSTIDSS = A.CUSTIDSS) 
	WHERE A.DOCSTATUSID NOT IN ('AN') 
	AND A.DOCTYPEID = '01' AND LEFT(A.NBRDOCUMENT,2) <> '17' 
	AND A.SITEID = @SITEID AND --A.DATEPROCESALES=@FECHAPROCESO 
	CD.DATEPROCEPAIDMENT=@FECHAPROCESO
*/

SELECT ISNULL(@TOT_COMBUSTIBLE,0) AS TOT_COMBUSTIBLE,
ISNULL(@TOT_GLP,0) AS TOT_GLP,
ISNULL(@TOT_LUBAD,0) AS TOT_LUBAD,
ISNULL(@TOT_OTROS,0) AS TOT_OTROS,
ISNULL(@TOT_CRED_COMBUSTIBLE,0) AS TOT_CRED_COMBUSTIBLE,
ISNULL(@TOT_CRED_GLP,0) AS TOT_CRED_GLP,
ISNULL(@TOT_CRED_LUBAD,0) AS TOT_CRED_LUBAD,
ISNULL(@TOT_CRED_OTROS,0) AS TOT_CRED_OTROS,
ISNULL(@TOT_TARJ_COMBUSTIBLE,0) AS TOT_TARJ_COMBUSTIBLE,
ISNULL(@TOT_TARJ_GLP,0) AS TOT_TARJ_GLP,
ISNULL(@TOT_TARJ_LUBAD,0) AS TOT_TARJ_LUBAD,
ISNULL(@TOT_TARJ_OTROS,0) AS TOT_TARJ_OTROS,
ISNULL(@TOT_EFEC_COMBUSTIBLE,0) AS TOT_EFEC_COMBUSTIBLE,
ISNULL(@TOT_EFEC_GLP,0) AS TOT_EFEC_GLP,
ISNULL(@TOT_EFEC_LUBAD,0) AS TOT_EFEC_LUBAD,
ISNULL(@TOT_EFEC_OTROS,0) AS TOT_EFEC_OTROS,
ISNULL(@SERVICIOS,0) AS SERVICIOS,
ISNULL(@GAS,0) AS GAS,
ISNULL(@MOBIL,0) AS MOBIL,
ISNULL(@TELEFONO,0) AS TELEFONO,
ISNULL(@OTROS,0) AS OTROS,
ISNULL(@PAG_DIFERENCIA,0) AS PAG_DIFERENCIA,
ISNULL(@PAG_ADELANTADO,0) AS PAG_ADELANTADO,
ISNULL(@TOT_FALTANTE,0) AS TOT_FALTANTE,
ISNULL(@TOT_SOBRANTE,0) AS TOT_SOBRANTE,
ISNULL(@TOT_COBRANZA,0) AS TOT_COBRANZA,
ISNULL(@DEPOSITOS,0) AS DEPOSITOS,
ISNULL(@AFERICION,0) AS AFERICION,
ISNULL(@DEPOSITO_TIENDA,0) AS DEPOSITO_TIENDA




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

