ALTER     PROCEDURE  [dbo].[sp_conDetalleCentralizacionPagos]
(
@Tipo varchar = '',
@fecini date,
@fecFIN date,
@sucursal int = 0,
@uninegocio int = 0,
@periodo  int=2017,
@idCaja INT = 0
)

AS

DECLARE @ctaclientesBoleta INT =0 
DECLARE @ctaclientes INT =0 
DECLARE @ctefectivo INT =0 
DECLARE @ctadiferenciacaja INT = 0

DECLARE @tipfol INT
DECLARE @valordiferenciacaja DECIMAL(18,4) = 0

SELECT @ctaclientesBoleta= ctaclientesBoleta ,@ctaclientes = ctaclientes 
FROM         venParametros

SELECT @ctefectivo= COALESCE(ctacontable,0)
FROM        cobFormasPago where codpag=1

SELECT @ctadiferenciacaja = ctadiferenciacaja 
FROM pvtParametros

SET @periodo = YEAR(@fecini)
	
--select @ctaclientesBoleta
--select @ctaclientes



------PAGOS


--SELECT    cobFormasPago.ctacontable,
-- pvtPagos.tipdoc,
--  pvtPagos.numdoc, venEncabezado.rutcte, pvtMovCajaEnc.tipo
--,pvtPagos.MONTO,pvtPagos.tipopago,fecdoc,0 as tipomovimiento,pvtMovCajaEnc.correlativo,tipref1,docref1, venEncabezado.codcen as uni_negocio, pvtPagos.sucursal as sucursal, pvtPagos.caja
--INTO #TODO
--FROM            pvtPagos INNER JOIN
--                         venDocumentos ON pvtPagos.tipdoc = venDocumentos.codsii INNER JOIN
--                         venEncabezado ON venDocumentos.grudoc = venEncabezado.grudoc AND venDocumentos.coddoc = venEncabezado.coddoc AND 
--                         pvtPagos.numdoc = venEncabezado.numinv INNER JOIN
--                         cobFormasPago ON pvtPagos.tipopago = cobFormasPago.codpag INNER JOIN
--                         pvtMovCajaEnc ON pvtPagos.idcierre = pvtMovCajaEnc.correlativo LEFT OUTER JOIN
--						 priSucursales ON priSucursales.codsuc = pvtPagos.sucursal --LEFT OUTER JOIN --NUEVO ->
--						 --conUnidadNegocio on conUnidadNegocio.idUniNeg = venEncabezado.codcen 
--						 --AND venEncabezado.codsii = pvtPagos.tipdoc 
--						 --AND venEncabezado.numinv = pvtPagos.numdoc --

--WHERE        (venDocumentos.grudoc = 10) AND (pvtMovCajaEnc.tipo = 1) AND (pvtMovCajaEnc.estado = 1)
--AND    pvtMovCajaEnc.tipocomprobante = 0 AND pvtMovCajaEnc.numerocomprobante  =0
--AND cobFormasPago.tippag <> 2
----nuevo
--AND (priSucursales.codsuc=@sucursal OR @sucursal=0) 
--AND (venEncabezado.codcen=@uninegocio OR @uninegocio=0) 
----
--AND CONVERT(DATE,pvtMovCajaEnc.fechainicio) BETWEEN @fecini AND @fecfin
--AND (pvtPagos.idcierre = @idCaja OR @idCaja = 0)


SELECT    cobFormasPago.ctacontable,
 pvtPagos.tipdoc,
  pvtPagos.numdoc, venEncabezado.rutcte, conCtacte.digcte, conCtacte.nomcte, pvtMovCajaEnc.tipo
,pvtPagos.MONTO,pvtPagos.tipopago,fecdoc,0 as tipomovimiento,pvtMovCajaEnc.correlativo,tipref1,docref1, venEncabezado.codcen as uni_negocio, pvtPagos.sucursal as sucursal, pvtPagos.caja
INTO #TODO
FROM            pvtPagos INNER JOIN
                         venDocumentos ON pvtPagos.tipdoc = venDocumentos.codsii INNER JOIN
                         venEncabezado ON venDocumentos.grudoc = venEncabezado.grudoc AND venDocumentos.coddoc = venEncabezado.coddoc AND 
                         pvtPagos.numdoc = venEncabezado.numinv INNER JOIN
                         cobFormasPago ON pvtPagos.tipopago = cobFormasPago.codpag INNER JOIN
                         pvtMovCajaEnc ON pvtPagos.idcierre = pvtMovCajaEnc.correlativo-- LEFT OUTER JOIN
						 LEFT OUTER JOIN conCtacte ON venEncabezado.rutcte = conCtacte.rutcte
						 --priSucursales ON priSucursales.codsuc = pvtPagos.sucursal --LEFT OUTER JOIN --NUEVO ->
						 --conUnidadNegocio on conUnidadNegocio.idUniNeg = venEncabezado.codcen 
						 --AND venEncabezado.codsii = pvtPagos.tipdoc 
						 --AND venEncabezado.numinv = pvtPagos.numdoc --

WHERE        (venDocumentos.grudoc = 10) AND (pvtMovCajaEnc.tipo = 1) AND (pvtMovCajaEnc.estado = 1)
--AND    pvtMovCajaEnc.tipocomprobante = 0 AND pvtMovCajaEnc.numerocomprobante  =0
AND pvtPagos.tipocomprobante = 0 AND pvtPagos.numerocomprobante = 0
AND cobFormasPago.tippag <> 2
--nuevo
AND (pvtMovCajaEnc.sucursal=@sucursal OR @sucursal=0) 
AND (pvtMovCajaEnc.unineg=@uninegocio OR @uninegocio=0) 
--
AND CONVERT(DATE,venEncabezado.fecdoc) BETWEEN @fecini AND @fecfin
AND (pvtPagos.idcierre = @idCaja OR @idCaja = 0)





--SELECT * FROM  #TODO 

--SELECT * FROM #TODO2

SELECT correlativo 
INTO #correlativos
FROM #TODO
group by correlativo


SELECT @valordiferenciacaja = SUM(diferencia) 
FROM pvtMovCajaEnc
WHERE tipo = 1
AND EXISTS(SELECT correlativo FROM #correlativos WHERE #correlativos.correlativo = pvtMovCajaEnc.correlativocierre)

---PAGOS FACTURAS

SELECT 'PAGOS AL DEBE AGRUPADO POR CUENTA ' AS itemtipo,1 as tcentra ,ctacontable as cuenta, 
  0 as linea,
  p.descripcion as descripcion,
  SUM(MONTO) AS debe,
  0 as haber, 
 '' as glosa_linea, 
  0 as  ctacte,
  0 as item, 
  0 as tipo_docto, 
  0 as num_docto,
 '1900-01-01' as vencimiento,
	0 as analisis, 
    uni_negocio, 
	0 as moneda,
	0 as tasa_cambio, 
	0 as periodo,
	'' as loging,
	'' as fecing,
	0  as flujo,
P.marcaCtacte,P.marcaDocumento,P.marcaItem,P.marcaMoneda, P.marcaNegocio, P.marcaAnalisis, P.marcaFlujo, sucursal, caja
,1 as tipodetalle,0 AS tipo,0 AS tipomovimiento,0 AS tipopago
INTO #FULLPAGOS
FROM            conPlanCuentas as P INNER JOIN
                     #TODO ON 
		P.cuenta = ctacontable
WHERE  P.periodo=@periodo
AND (tipdoc<>60 AND tipdoc<>61 AND tipdoc<>112)
GROUP BY ctacontable,descripcion,marcaCtacte,marcaDocumento,marcaItem, P.marcaAnalisis, P.marcaFlujo,marcaMoneda,marcaNegocio, uni_negocio, sucursal, caja


UNION ALL

SELECT 'HABER DETALLE PAGOS' AS itemtipo, 2 as tcentra ,
CASE WHEN (CASE WHEN tipdoc IN(55,56) THEN tipref1 ELSE tipdoc END) IN(35,39,48) THEN @ctaclientesBoleta ELSE @ctaclientes END AS cuenta ,
  0 as linea,
   p.descripcion as descripcion,
  0 AS debe,
  MONTO  as haber, 
 '' as glosa_linea, 
  rutcte as  ctacte,
  digcte,
  nomcte,
  0 as item, 
  CASE WHEN tipdoc IN(111,55,56) THEN tipref1 ELSE tipdoc END AS  tipo_docto, 
  CASE WHEN tipdoc IN(111,55,56) THEN docref1 ELSE numdoc END AS  num_docto,
   fecdoc as vencimiento,
	0 as analisis, 
    uni_negocio, 
	0 as moneda,
	0 as tasa_cambio, 
	0 as periodo,
	'' as loging,
	'' as fecing,
	0  as flujo,
marcaCtacte,P.marcaDocumento,P.marcaItem,P.marcaMoneda, P.marcaNegocio, P.marcaAnalisis, P.marcaFlujo, sucursal, caja
,2 as tipodetalle,tipo,tipomovimiento,tipopago
FROM            conPlanCuentas as P INNER JOIN
                     #TODO  ON 
		P.cuenta = CASE WHEN (CASE WHEN tipdoc IN(55,56,111) THEN tipref1 ELSE tipdoc END) IN(35,39,48) THEN @ctaclientesBoleta ELSE @ctaclientes END 
WHERE  P.periodo=@periodo
AND (tipdoc<>60 AND tipdoc<>61 AND tipdoc<>112)


---PAGOS NOTA CREDITOS

SELECT 'PAGOS AL HABER AGRUPADO POR CUENTA NC ' AS itemtipo,2 as tcentra ,
 ctacontable as cuenta, 
  0 as linea,
  p.descripcion as descripcion,
  0 AS debe,
 SUM(MONTO) as haber, 
 '' as glosa_linea, 
  0 as  ctacte,
  0 as digcte,
  '' as nomcte,
  0 as item, 
  0 as tipo_docto, 
  0 as num_docto,
 '1900-01-01' as vencimiento,
	0 as analisis, 
    uni_negocio, 
	0 as moneda,
	0 as tasa_cambio, 
	0 as periodo,
	'' as loging,
	'' as fecing,
	0  as flujo,
P.marcaCtacte,P.marcaDocumento,P.marcaItem,P.marcaMoneda, P.marcaNegocio, P.marcaAnalisis, P.marcaFlujo, sucursal, caja
,3 as tipodetalle,0 AS tipo,0 AS tipomovimiento,0 AS tipopago
INTO #FULLPAGOSNC
FROM            conPlanCuentas as P INNER JOIN
                     #TODO ON 
		P.cuenta = ctacontable
WHERE  P.periodo=@periodo
AND (tipdoc=60 OR tipdoc=61 OR tipdoc=112)
GROUP BY ctacontable,descripcion,marcaCtacte,marcaDocumento,marcaItem, P.marcaAnalisis, P.marcaFlujo,marcaMoneda,marcaNegocio, uni_negocio, sucursal, caja


UNION ALL

SELECT 'DEBE DETALLE PAGOS NC' AS itemtipo, 1 as tcentra ,
CASE WHEN tipref1 IN(35,39,48) THEN @ctaclientesBoleta ELSE @ctaclientes END AS cuenta ,
  0 as linea,
   p.descripcion as descripcion,
  MONTO AS debe,
  0  as haber, 
 '' as glosa_linea, 
  rutcte as  ctacte,
  digcte,
  nomcte,
  0 as item, 
  tipref1 as tipo_docto, 
  docref1 as num_docto,
   fecdoc as vencimiento,
	0 as analisis, 
    uni_negocio, 
	0 as moneda,
	0 as tasa_cambio, 
	0 as periodo,
	'' as loging,
	'' as fecing,
	0  as flujo,
marcaCtacte,P.marcaDocumento,P.marcaItem,P.marcaMoneda, P.marcaNegocio, P.marcaAnalisis, P.marcaFlujo, sucursal, caja
,4 as tipodetalle,tipo,tipomovimiento,tipopago
FROM            conPlanCuentas as P INNER JOIN
                     #TODO  ON 
		P.cuenta = CASE WHEN tipdoc IN(35,39,48) THEN @ctaclientesBoleta ELSE @ctaclientes END
WHERE  P.periodo=@periodo
AND (tipdoc=60 OR tipdoc=61 OR tipdoc=112)
ORDER BY tipopago,tipomovimiento,tipodetalle

---FIN PAGOS NOTA CREDITOS



--SELECT * FROM  #FULLPAGOS ORDER BY tipodetalle
--SELECT * FROM  #FULLPAGOSNC ORDER BY tipodetalle













------MOVIMIENTOS CAJAS

SELECT   pvtMovCajaEnc.tipo,pvtMovCajaEnc.tipomovimiento, pvtMovimientosCajas.ctacontable,
monto,
monto as haber, ---SALIDA al haber
 0  as debe ---ENTRADA al debe
 ,  pvtMovCajaEnc.unineg
 ,  pvtMovCajaEnc.tipdoc
 ,  pvtMovCajaEnc.numdoc
 ,sucursal
 , caja
INTO #TODOMOVIMIENTOSSALIDAS
FROM            pvtMovCajaEnc INNER JOIN
                         pvtMovimientosCajas ON pvtMovCajaEnc.tipo = pvtMovimientosCajas.tipo AND pvtMovCajaEnc.tipomovimiento = pvtMovimientosCajas.codigo
WHERE   pvtMovCajaEnc.tipo = 3
AND EXISTS(SELECT correlativo FROM #correlativos WHERE #correlativos.correlativo = pvtMovCajaEnc.correlativocierre)
AND MONTO > 0


SELECT   pvtMovCajaEnc.tipo,pvtMovCajaEnc.tipomovimiento, pvtMovimientosCajas.ctacontable,
monto,
0 as haber, ---SALIDA al haber
monto as debe ---ENTRADA al debe
 ,  pvtMovCajaEnc.unineg
 ,  pvtMovCajaEnc.tipdoc
 ,  pvtMovCajaEnc.numdoc
  ,sucursal
 , caja
INTO #TODOMOVIMIENTOSENTRADAS
FROM            pvtMovCajaEnc INNER JOIN
                         pvtMovimientosCajas ON pvtMovCajaEnc.tipo = pvtMovimientosCajas.tipo AND pvtMovCajaEnc.tipomovimiento = pvtMovimientosCajas.codigo
WHERE   pvtMovCajaEnc.tipo = 2
AND EXISTS(SELECT correlativo FROM #correlativos WHERE #correlativos.correlativo = pvtMovCajaEnc.correlativocierre)
AND MONTO > 0



SELECT 'SALIDAS' AS itemtipo, 2 as tcentra, @ctefectivo as cuenta, 
  0 as linea,
  p.descripcion as descripcion,
  0 AS debe,
  haber as haber, 
 '' as glosa_linea, 
  0 as  ctacte,
  0 as digcte,
  '' as nomcte,
  0 as item, 
  conComprobantes.tipdoc as tipo_docto, 
  conComprobantes.numdoc as num_docto,
  '1900-01-01' as vencimiento,
	0 as analisis, 
    conComprobantes.unineg as uni_negocio, 
	0 as moneda,
	0 as tasa_cambio, 
	0 as periodo,
	'' as loging,
	'' as fecing,
	0  as flujo,
	P.marcaCtacte,P.marcaDocumento,P.marcaItem,P.marcaMoneda, P.marcaNegocio, P.marcaAnalisis, P.marcaFlujo
,1 as tipodetalle,conComprobantes.tipo,tipomovimiento,0 as tipopago,  sucursal
 , caja
INTO #FULLMOVIMIENTOS
FROM            conPlanCuentas as P INNER JOIN
                    #TODOMOVIMIENTOSSALIDAS AS   conComprobantes ON 
		P.cuenta =  @ctefectivo
WHERE  P.periodo=@periodo

UNION ALL

SELECT 'SUMA MOVIMIENTO SALIDA POR TIPO' AS itemtipo,1 as tcentra ,   ctacontable as cuenta, 
  0 as linea,
   p.descripcion as descripcion,
 --sum(monto) as debe,
 monto as debe,
 0 haber,
  'ENTRADA EFECTIVO' as glosa_linea, 
  0 as  ctacte,
  0 as digcte,
  '' as nomcte,
  0 as item, 
  #TODOMOVIMIENTOSSALIDAS.tipdoc as tipo_docto, 
  #TODOMOVIMIENTOSSALIDAS.numdoc as num_docto,
'1900-01-01' as vencimiento,
	0 as analisis, 
  #TODOMOVIMIENTOSSALIDAS.unineg as uni_negocio, 
	0 as moneda,
	0 as tasa_cambio, 
	0 as periodo,
	'' as loging,
	'' as fecing,
	0  as flujo,
	marcaCtacte,P.marcaDocumento,P.marcaItem,P.marcaMoneda, P.marcaNegocio, P.marcaAnalisis, P.marcaFlujo
,2 as tipodetalle, #TODOMOVIMIENTOSSALIDAS .tipo,tipomovimiento,0 as tipopago, sucursal
 , caja
FROM            conPlanCuentas as P INNER JOIN
                     #TODOMOVIMIENTOSSALIDAS   ON 
		P.cuenta = ctacontable
WHERE  P.periodo=@periodo
--GROUP BY ctacontable , monto, marcaCtacte,P.marcaDocumento,P.marcaItem,P.marcaMoneda, P.marcaNegocio, P.marcaAnalisis, P.marcaFlujo,
--#TODOMOVIMIENTOSSALIDAS .tipo,tipomovimiento,  p.descripcion


UNION ALL


SELECT 'ENTRADAS' AS itemtipo, 2 as tcentra, @ctefectivo as cuenta, 
  0 as linea,
  p.descripcion as descripcion,
  debe AS debe,
  0 as haber, 
 '' as glosa_linea, 
  0 as  ctacte,
  0 as digcte,
  '' as nomcte,
  0 as item, 
  conComprobantes.tipdoc as tipo_docto, 
  conComprobantes.numdoc as num_docto,
  '1900-01-01' as vencimiento,
	0 as analisis, 
   conComprobantes.unineg as uni_negocio,  
	0 as moneda,
	0 as tasa_cambio, 
	0 as periodo,
	'' as loging,
	'' as fecing,
	0  as flujo,
	P.marcaCtacte,P.marcaDocumento,P.marcaItem,P.marcaMoneda, P.marcaNegocio, P.marcaAnalisis, P.marcaFlujo
,1 as tipodetalle,conComprobantes.tipo,tipomovimiento,0 as tipopago  ,sucursal
 , caja
FROM            conPlanCuentas as P INNER JOIN
                    #TODOMOVIMIENTOSENTRADAS AS     conComprobantes ON 
		P.cuenta =  @ctefectivo
WHERE  P.periodo=@periodo

UNION ALL

SELECT 'SUMA MOVIMIENTO ENTRADA POR TIPO' AS itemtipo,1 tcentra ,   ctacontable as cuenta, 
  0 as linea,
   p.descripcion as descripcion,
   0 AS debe ,
  --sum(monto)  as haber,
  monto  as haber,

 'ENTRADA EFECTIVO' as glosa_linea, 
  0 as  ctacte,
  0 as digcte,
  '' as nomcte,
  0 as item, 
  #TODOMOVIMIENTOSENTRADAS.tipdoc as tipo_docto, 
  #TODOMOVIMIENTOSENTRADAS.numdoc as num_docto,
'1900-01-01' as vencimiento,
	0 as analisis, 
  #TODOMOVIMIENTOSENTRADAS.unineg as uni_negocio,   
	0 as moneda,
	0 as tasa_cambio, 
	0 as periodo,
	'' as loging,
	'' as fecing,
	0  as flujo,
	marcaCtacte,P.marcaDocumento,P.marcaItem,P.marcaMoneda, P.marcaNegocio, P.marcaAnalisis, P.marcaFlujo
,2 as tipodetalle, #TODOMOVIMIENTOSENTRADAS .tipo,tipomovimiento,0 as tipopago  ,#TODOMOVIMIENTOSENTRADAS.sucursal
 , #TODOMOVIMIENTOSENTRADAS.caja
FROM            conPlanCuentas as P INNER JOIN
                     #TODOMOVIMIENTOSENTRADAS   ON 
		P.cuenta = ctacontable
WHERE  P.periodo=@periodo
--GROUP BY ctacontable ,  marcaCtacte,P.marcaDocumento,P.marcaItem,P.marcaMoneda, P.marcaNegocio, P.marcaAnalisis, P.marcaFlujo,
--#TODOMOVIMIENTOSENTRADAS .tipo,tipomovimiento,  p.descripcion



--SELECT * FROM #FULLPAGOS

--select * from #FULLMOVIMIENTOS



SELECT #FULLPAGOS.* ,
COALESCE(conCtacte.nomcte,'') AS nom_cte,
'' AS des_analisis,
'' AS des_unineg, 
'' AS des_item,0 as orden
INTO #COMPROBANTEFINAL
FROM #FULLPAGOS LEFT OUTER JOIN
                         conCtacte ON #FULLPAGOS.ctacte = conCtacte.rutcte

UNION ALL


SELECT #FULLPAGOSNC.* ,
COALESCE(conCtacte.nomcte,'') AS nom_cte,
'' AS des_analisis,
'' AS des_unineg, 
'' AS des_item,0 as orden
FROM #FULLPAGOSNC LEFT OUTER JOIN
                         conCtacte ON #FULLPAGOSNC.ctacte = conCtacte.rutcte

/*UNION ALL

SELECT *,
'' AS nom_cte,
'' AS des_analisis,
'' AS des_unineg, 
'' AS des_item, 1 as orden,
0 as sucursal,
0 as caja
FROM #FULLMOVIMIENTOS */   --> Se une abajo para evitar agrupacion



SELECT cuenta, 
   0 as linea,
  descripcion,
 debe as debe,
 haber as haber,
 '' as glosa_linea, 
 CASE marcaCtacte WHEN 1 THEN  ctacte ELSE 0 END as  ctacte,
 CASE marcaCtacte WHEN 1 THEN  digcte ELSE 0 END as  digcte,
 CASE marcaCtacte WHEN 1 THEN  nomcte ELSE 0 END as  nomcte,
 CASE marcaitem WHEN 1 THEN item ELSE 0 END as item,
 CASE marcadOCUMENTO WHEN 1 THEN  tipo_docto ELSE 0 END as  tipo_docto,
 CASE marcadOCUMENTO WHEN 1 THEN  num_docto ELSE 0 END as  num_docto,
 CASE marcadOCUMENTO WHEN 1 THEN  vencimiento ELSE '1900-01-01' END as vencimiento,
 CASE marcaAnalisis WHEN 1 THEN  analisis ELSE 0 END as  analisis,
 CASE marcaNegocio WHEN 1 THEN  uni_negocio ELSE 0 END as uni_negocio,
 CASE marcaMoneda WHEN 1 THEN moneda ELSE 0 end as moneda ,
 CASE tasa_cambio WHEN 1 THEN tasa_cambio ELSE 0 end as tasa_cambio ,
 0 as periodo,
	'' as loging,
	'' as fecing,
	0  as flujo,
		'' AS nom_cte,
		'' AS des_analisis,
		'' AS des_unineg, 
		'' AS des_item,
		marcaCtacte,
		marcaDocumento,
		marcaItem,
		marcaMoneda, 
		marcaNegocio,
		marcaAnalisis,
		 marcaFlujo,orden,tcentra, caja, sucursal
 into #COMPROBANTEFINALAGRUPADO
 FROM #COMPROBANTEFINAL
 

IF @valordiferenciacaja <> 0
BEGIN
SELECT cuenta, 
   0 as linea,
  descripcion,
 sum(debe) as debe,
 sum(haber) as haber,
 '' as glosa_linea, 
ctacte,
digcte,
nomcte
  item,
 tipo_docto,
 num_docto,
 vencimiento,
analisis,
uni_negocio,
 moneda ,
tasa_cambio ,
@periodo as periodo,
	'' as loging,
	'' as fecing,
	0  as flujo,
		'' AS nom_cte,
		'' AS des_analisis,
		'' AS des_unineg, 
		'' AS des_item,
		marcaCtacte,
		marcaDocumento,
		marcaItem,
		marcaMoneda, 
		marcaNegocio,
		marcaAnalisis,
		 marcaFlujo, 
		 #COMPROBANTEFINALAGRUPADO.caja,
		 #COMPROBANTEFINALAGRUPADO.sucursal,
         orden
   FROM #COMPROBANTEFINALAGRUPADO LEFT OUTER JOIN
   priSucursales ON #COMPROBANTEFINALAGRUPADO.sucursal = priSucursales.codsuc
  GROUP BY cuenta,descripcion,orden ,
 	marcaCtacte,
		marcaDocumento,
		marcaItem,
		marcaMoneda, 
		marcaNegocio,
		marcaAnalisis,
		 marcaFlujo,ctacte,item,uni_negocio,tipo_docto,num_docto,vencimiento,analisis,moneda,tasa_cambio,tcentra, #COMPROBANTEFINALAGRUPADO.caja, #COMPROBANTEFINALAGRUPADO.sucursal


UNION ALL

SELECT cuenta,
        linea,
        descripcion,
        debe,
        haber,
        glosa_linea,
        ctacte,
		digcte,
		nomcte
        item,
        tipo_docto,
        num_docto,
        vencimiento,
        analisis,
        uni_negocio,
        moneda,
        tasa_cambio,
        @periodo as periodo,
        '' as loging,
	    '' as fecing,
	    0  as flujo,
		'' AS nom_cte,
		'' AS des_analisis,
		'' AS des_unineg, 
		'' AS des_item,
		marcaCtacte,
		marcaDocumento,
		marcaItem,
		marcaMoneda, 
		marcaNegocio,
		marcaAnalisis,
		marcaFlujo, 
		#FULLMOVIMIENTOS.caja,
		 #FULLMOVIMIENTOS.sucursal,
        1 as orden

FROM #FULLMOVIMIENTOS


UNION ALL

--PARA CUENTA DIFERENCIA DE CAJA

SELECT CASE 
		WHEN @valordiferenciacaja < 0 THEN @ctadiferenciacaja
		ELSE @ctefectivo
	   END cuenta,
	   0 as linea,
       conPlanCuentas.descripcion as descripcion,
       CASE 
	     WHEN @valordiferenciacaja < 0 THEN @valordiferenciacaja * -1
		 ELSE @valordiferenciacaja
	   END  as debe,
       0 as haber,
       '' as glosa_linea,
       0 as ctacte,
	   0 as digcte,
	   '' as nomcte,
       0 as  item,
       0 as tipo_docto,
       0 as num_docto,
      '1900-01-01' as vencimiento,
       0 as analisis,
       0 as uni_negocio,
       0 as moneda,
       0 as tasa_cambio,
       @periodo as periodo,
       '' as loging,
	   '' as fecing,
	   0  as flujo,
	   '' AS nom_cte,
	   '' AS des_analisis,
	   '' AS des_unineg, 
	   '' AS des_item,
	    marcaCtacte,
		marcaDocumento,
		marcaItem,
		marcaMoneda, 
		marcaNegocio,
		marcaAnalisis,
		marcaFlujo, 
		0 as caja,
		0 as sucursal,
        1 as orden
FROM conPlanCuentas
WHERE periodo = @periodo
AND	  cuenta = (SELECT CASE 
		WHEN @valordiferenciacaja < 0 THEN @ctadiferenciacaja
		ELSE @ctefectivo
	   END)

UNION ALL

SELECT CASE 
		WHEN @valordiferenciacaja < 0 THEN @ctefectivo
		ELSE @ctadiferenciacaja
	   END cuenta,		
       0 as linea,
       conPlanCuentas.descripcion as descripcion,
       0 as debe,
      CASE 
		 WHEN  @valordiferenciacaja < 0 THEN @valordiferenciacaja * -1
		 ELSE @valordiferenciacaja	  
	  END as haber,
       '' as glosa_linea,
       0 as ctacte,
	   0 as digcte,
	   '' as nomcte,
       0 as  item,
       0 as tipo_docto,
       0 as num_docto,
      '1900-01-01' as vencimiento,
       0 as analisis,
       0 as uni_negocio,
       0 as moneda,
       0 as tasa_cambio,
       @periodo as periodo,
       '' as loging,
	   '' as fecing,
	   0  as flujo,
	   '' AS nom_cte,
	   '' AS des_analisis,
	   '' AS des_unineg, 
	   '' AS des_item,
	    marcaCtacte,
		marcaDocumento,
		marcaItem,
		marcaMoneda, 
		marcaNegocio,
		marcaAnalisis,
		marcaFlujo, 
	   0 as caja,
		0 as sucursal,
        1 as orden
FROM conPlanCuentas
WHERE periodo = @periodo
AND	  cuenta = (SELECT CASE 
		WHEN @valordiferenciacaja < 0 THEN @ctefectivo
		ELSE @ctadiferenciacaja
	   END )


order by orden

END

IF @valordiferenciacaja = 0
BEGIN
SELECT cuenta, 
   0 as linea,
  descripcion,
 sum(debe) as debe,
 sum(haber) as haber,
 '' as glosa_linea, 
ctacte,
digcte,
nomcte
  item,
 tipo_docto,
 num_docto,
 vencimiento,
analisis,
uni_negocio,
 moneda ,
tasa_cambio ,
@periodo as periodo,
	'' as loging,
	'' as fecing,
	0  as flujo,
		'' AS nom_cte,
		'' AS des_analisis,
		'' AS des_unineg, 
		'' AS des_item,
		marcaCtacte,
		marcaDocumento,
		marcaItem,
		marcaMoneda, 
		marcaNegocio,
		marcaAnalisis,
		 marcaFlujo, 
		 caja,
		 sucursal,
         orden
   FROM #COMPROBANTEFINALAGRUPADO LEFT OUTER JOIN
   priSucursales ON #COMPROBANTEFINALAGRUPADO.sucursal = priSucursales.codsuc
  GROUP BY cuenta,descripcion,orden ,
 	marcaCtacte,
		marcaDocumento,
		marcaItem,
		marcaMoneda, 
		marcaNegocio,
		marcaAnalisis,
		 marcaFlujo,ctacte,item,uni_negocio,tipo_docto,num_docto,vencimiento,analisis,moneda,tasa_cambio,tcentra, caja, sucursal


UNION ALL

SELECT cuenta,
        linea,
        descripcion,
        debe,
        haber,
        glosa_linea,
        ctacte,
		digcte,
		nomcte
        item,
        tipo_docto,
        num_docto,
        vencimiento,
        analisis,
        uni_negocio,
        moneda,
        tasa_cambio,
        @periodo as periodo,
        '' as loging,
	    '' as fecing,
	    0  as flujo,
		'' AS nom_cte,
		'' AS des_analisis,
		'' AS des_unineg, 
		'' AS des_item,
		marcaCtacte,
		marcaDocumento,
		marcaItem,
		marcaMoneda, 
		marcaNegocio,
		marcaAnalisis,
		marcaFlujo, 
		caja,
		sucursal,
        1 as orden

FROM #FULLMOVIMIENTOS

order by orden


END